chore: minor work around

1.dragging a property from blackboard now create a node with an output port

2.add a rename text field to modify node's title fast
main
taoria 3 years ago
parent de0d55d07a
commit 1e16ddda5c
  1. 76
      TNode/Editor/BaseViews/DataGraphView.cs
  2. 46
      TNode/Editor/BaseViews/NodeView.cs
  3. 3
      TNode/Editor/Cache/NodeEditorExtensions.cs
  4. 3
      TNode/Editor/Manipulators.meta
  5. 4
      TNode/Editor/Search/NodeSearchWindowProvider.cs
  6. 2
      TNode/Editor/Tools/NodeCreator/NodeCreator.cs
  7. 0
      TNode/Editor/Tools/NodeCreator/NodeCreator.cs.meta
  8. 4
      TNode/JsonSerialize/JsonSerializeTool.cs
  9. 10
      TNode/Models/BlackboardDragNodeData.cs

@ -7,7 +7,7 @@ using TNode.Cache;
using TNode.Editor.GraphBlackboard; using TNode.Editor.GraphBlackboard;
using TNode.Editor.Inspector; using TNode.Editor.Inspector;
using TNode.Editor.Model; using TNode.Editor.Model;
using TNode.Editor.Tools.NodeCreator;
using TNode.Models; using TNode.Models;
using Unity.VisualScripting; using Unity.VisualScripting;
using UnityEditor; using UnityEditor;
@ -138,8 +138,11 @@ namespace TNode.Editor.BaseViews{
} }
public event DataChangedEventHandler OnDataChanged; public event DataChangedEventHandler OnDataChanged;
#endregion #endregion
#region event declarations
public delegate void DataChangedEventHandler(object sender, DataChangedEventArgs<T> e); public delegate void DataChangedEventHandler(object sender, DataChangedEventArgs<T> e);
#endregion
//A Constructor for the DataGraphView ,never to override it //A Constructor for the DataGraphView ,never to override it
@ -176,12 +179,19 @@ namespace TNode.Editor.BaseViews{
}); });
} }
private void OnInit(){
ConstructDefaultBehaviour();
OnGraphViewCreate();
}
public void RegisterDragEvent(){ public void RegisterDragEvent(){
RegisterCallback<DragUpdatedEvent>(OnDragUpdated); RegisterCallback<DragUpdatedEvent>(OnDragUpdated);
RegisterCallback<DragPerformEvent>(OnDragPerform); RegisterCallback<DragPerformEvent>(OnDragPerform);
} }
#endregion #endregion
#region event callbakc
private void OnDragPerform(DragPerformEvent evt){ private void OnDragPerform(DragPerformEvent evt){
if (DragAndDrop.GetGenericData("DragSelection") is List<ISelectable>{Count: > 0} data){ if (DragAndDrop.GetGenericData("DragSelection") is List<ISelectable>{Count: > 0} data){
@ -191,17 +201,12 @@ namespace TNode.Editor.BaseViews{
//Make a constructor of BlackboardDragNodeData<field.PropertyType > by reflection //Make a constructor of BlackboardDragNodeData<field.PropertyType > by reflection
var specifiedType = var specifiedType =
typeof(BlackboardDragNodeData<>).MakeGenericType(field.BlackboardProperty.PropertyType); typeof(BlackboardDragNodeData<>).MakeGenericType(field.BlackboardProperty.PropertyType);
//get specific constructor of specified type with two parameters, one is a string ,another is a blackboarddata //Create a new instance of specified type
var constructor = specifiedType.GetConstructor(new[] {typeof(string),typeof(BlackboardData)}); var dragNodeData = NodeCreator.InstantiateNodeData(specifiedType);
//create a new instance of the specified type with the constructor and the two parameters this.AddTNode(dragNodeData,new Rect(evt.mousePosition,new Vector2(200,200)));
if (constructor != null){
var dragNodeData = constructor.Invoke(new object[]
{field.BlackboardProperty.PropertyName, _data.blackboardData}) as NodeData;
this.AddTNode(dragNodeData,new Rect(Owner.position.position+evt.mousePosition,new Vector2(100,100)));
}
} }
} }
} }
} }
@ -217,10 +222,11 @@ namespace TNode.Editor.BaseViews{
} }
private void OnInit(){
ConstructDefaultBehaviour(); #endregion
OnGraphViewCreate();
}
public void ResetGraphView(){ public void ResetGraphView(){
//Clear all nodes //Clear all nodes
foreach (var node in nodes){ foreach (var node in nodes){
@ -276,13 +282,13 @@ namespace TNode.Editor.BaseViews{
_isInspectorOn = true; _isInspectorOn = true;
} }
public void CreateMiniMap(Rect rect){ public virtual void CreateMiniMap(Rect rect){
var miniMap = new MiniMap(); var miniMap = new MiniMap();
this.Add(miniMap); this.Add(miniMap);
miniMap.SetPosition(rect); miniMap.SetPosition(rect);
} }
public void CreateBlackboard(){ public virtual void CreateBlackboard(){
_blackboard = new Blackboard(); _blackboard = new Blackboard();
//Blackboard add "Add Node" button //Blackboard add "Add Node" button
// blackboard.Add(new BlackboardSection(){ // blackboard.Add(new BlackboardSection(){
@ -370,11 +376,10 @@ namespace TNode.Editor.BaseViews{
} }
} }
private void SaveEdge(){ private void SaveEdge(){
var links = new List<NodeLink>();
foreach (var edge in edges){ foreach (var edge in edges){
var inputNode = edge.input.node as INodeView; var inputNode = edge.input.node as INodeView;
var outputNode = edge.output.node as INodeView; var outputNode = edge.output.node as INodeView;
var links = new List<NodeLink>();
Debug.Log($"Edge{inputNode},{outputNode}");
if (inputNode != null && outputNode != null){ if (inputNode != null && outputNode != null){
var inputNodeData = inputNode.GetNodeData(); var inputNodeData = inputNode.GetNodeData();
var outputNodeData = outputNode.GetNodeData(); var outputNodeData = outputNode.GetNodeData();
@ -389,16 +394,15 @@ namespace TNode.Editor.BaseViews{
links.Add(newNodeLink); links.Add(newNodeLink);
} }
_data.nodeLinks = links;
} }
_data.nodeLinks = links;
} }
private void SaveGraphData(){ private void SaveGraphData(){
_data.NodeDictionary.Clear(); _data.NodeDictionary.Clear();
_data.nodeLinks.Clear();
SaveNode(); SaveNode();
SaveEdge(); SaveEdge();
EditorUtility.SetDirty(_data); EditorUtility.SetDirty(_data);
} }
@ -436,7 +440,6 @@ namespace TNode.Editor.BaseViews{
} }
} }
}); });
if(nodeView is INodeView nodeViewInterface){ if(nodeView is INodeView nodeViewInterface){
nodeViewInterface.SetNodeData(nodeData); nodeViewInterface.SetNodeData(nodeData);
} }
@ -448,30 +451,27 @@ namespace TNode.Editor.BaseViews{
menu.AddItem(new GUIContent("Delete"), false, () => { menu.AddItem(new GUIContent("Delete"), false, () => {
RemoveElement(nodeView); RemoveElement(nodeView);
if (nodeView is INodeView tNodeView){ if (nodeView is INodeView tNodeView){
var nodeData1 = tNodeView.GetNodeData(); RemoveTNode(tNodeView.GetNodeData());
_data.NodeDictionary.Remove(nodeData1.id);
_nodeDict.Remove(nodeData1.id);
//Break all edges connected to this node
foreach (var edge in edges){
if (edge.input.node == nodeView || edge.output.node == nodeView){
RemoveElement(edge);
}
}
Owner.graphEditorData.graphElementsData.RemoveAll(x => x.guid == nodeData1.id);
} }
}); });
menu.ShowAsContext(); menu.ShowAsContext();
}); });
} }
} }
public void RemoveTNode(NodeData nodeData){ public void RemoveTNode(NodeData nodeData){
throw new NotImplementedException();
_data.NodeDictionary.Remove(nodeData.id);
var nodeView = _nodeDict[nodeData.id];
_nodeDict.Remove(nodeData.id);
//Break all edges connected to this node
foreach (var edge in edges){
if (edge.input.node == nodeView || edge.output.node == nodeView){
RemoveElement(edge);
}
}
Owner.graphEditorData.graphElementsData.RemoveAll(x => x.guid == nodeData.id);
} }
} }

@ -7,6 +7,7 @@ using UnityEditor;
using UnityEditor.Experimental.GraphView; using UnityEditor.Experimental.GraphView;
using UnityEditor.UIElements; using UnityEditor.UIElements;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements;
namespace TNode.Editor.BaseViews{ namespace TNode.Editor.BaseViews{
@ -41,10 +42,13 @@ namespace TNode.Editor.BaseViews{
protected NodeView(){ protected NodeView(){
OnDataChanged+=OnDataChangedHandler; OnDataChanged+=OnDataChangedHandler;
_nodeInspectorInNode = new NodeInspectorInNode(){ _nodeInspectorInNode = new NodeInspectorInNode(){
name = "nodeInspectorInNode" name = "nodeInspectorInNode"
}; };
this.extensionContainer.Add(_nodeInspectorInNode); this.extensionContainer.Add(_nodeInspectorInNode);
BuildDoubleClickRename();
} }
private void OnDataChangedHandler(T obj){ private void OnDataChangedHandler(T obj){
this.title = _data.nodeName; this.title = _data.nodeName;
@ -83,6 +87,48 @@ namespace TNode.Editor.BaseViews{
} }
} }
public void StartARenameTitleTextField(){
var textField = new TextField{
value = title,
style ={
//Make the text filed overlap the title container
position = Position.Absolute,
left = 0,
top = 0,
width = titleContainer.layout.width,
height = titleContainer.layout.height
}
};
textField.StretchToParentSize();
textField.RegisterValueChangedCallback(evt2 => {
title = evt2.newValue;
});
textField.RegisterCallback<FocusOutEvent>(evt2 => {
title = textField.text;
((NodeDataWrapper)_data).SetValue("nodeName",textField.text);
textField.RemoveFromHierarchy();
});
//if enter is pressed ,set the title and remove the text field
textField.RegisterCallback<KeyDownEvent>(evt2 => {
if (evt2.keyCode == KeyCode.Return){
title = textField.text;
((NodeDataWrapper)_data).SetValue("nodeName",textField.text);
textField.RemoveFromHierarchy();
}
});
titleContainer.Add(textField);
textField.Focus();
}
private void BuildDoubleClickRename(){
//when double click titleContainer ,create a textfield to rename the node
titleContainer.RegisterCallback<MouseDownEvent>(evt => {
if (evt.clickCount == 2){
StartARenameTitleTextField();
}
});
}
public void SetNodeData(NodeData nodeData){ public void SetNodeData(NodeData nodeData){
Data = (T)nodeData; Data = (T)nodeData;

@ -145,6 +145,9 @@ namespace TNode.Cache{
} }
public static object CreateNodeViewFromNodeType(Type t){ public static object CreateNodeViewFromNodeType(Type t){
//Check the generic type of NodeView by t //Check the generic type of NodeView by t
Debug.Log(t);
Debug.Log(t.ToString());
Debug.Log(typeof(NodeData).IsAssignableFrom(t));
var type = typeof(NodeView<>).MakeGenericType(t); var type = typeof(NodeView<>).MakeGenericType(t);
if (NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(type)){ if (NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(type)){
var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[type]; var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[type];

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 400542f3cec140e2b55e2bcf637b2d9b
timeCreated: 1657009018

@ -48,8 +48,8 @@ namespace TNode.Editor{
//Check if type is derived from NodeData //Check if type is derived from NodeData
if (typeof(NodeData).IsAssignableFrom(type)){ if (typeof(NodeData).IsAssignableFrom(type)){
//Make an instance of the type //Make an instance of the type
if (NodeHelper.InstantiateNodeData(type) is { } nodeData){ if (NodeCreator.InstantiateNodeData(type) is { } nodeData){
nodeData.nodeName = "New Node"; nodeData.nodeName = $"New {type.Name}";
((IDataGraphView) _graphView).AddTNode(nodeData, new Rect(localPos.x, localPos.y, 100, 100)); ((IDataGraphView) _graphView).AddTNode(nodeData, new Rect(localPos.x, localPos.y, 100, 100));
} }
} }

@ -2,7 +2,7 @@
using TNode.Models; using TNode.Models;
namespace TNode.Editor.Tools.NodeCreator{ namespace TNode.Editor.Tools.NodeCreator{
public static class NodeHelper{ public static class NodeCreator{
/// <summary> /// <summary>
/// always use this to create a new node. /// always use this to create a new node.

@ -21,7 +21,9 @@ namespace TNode.JsonSerialize{
DateFormatString = "yyyy-MM-dd HH:mm:ss", DateFormatString = "yyyy-MM-dd HH:mm:ss",
Converters = new List<JsonConverter> { new Vector3Converter() }, Converters = new List<JsonConverter> { new Vector3Converter() },
TypeNameHandling = TypeNameHandling.Auto, TypeNameHandling = TypeNameHandling.Auto,
ContractResolver = new WritablePropertiesOnlyResolver() ContractResolver = new WritablePropertiesOnlyResolver(),
Formatting = Formatting.Indented
}; };

@ -4,17 +4,17 @@ using TNode.Attribute.Ports;
using TNode.RuntimeCache; using TNode.RuntimeCache;
namespace TNode.Models{ namespace TNode.Models{
public class BlackboardDragNodeData<T>:NodeData where T:BlackboardData{ public class BlackboardDragNodeData<T>:NodeData{
[JsonIgnore] [JsonIgnore]
private string _blackDragData; private string _blackDragData;
[JsonIgnore] [JsonIgnore]
private T _blackboardData; private BlackboardData _blackboardData;
[Output] [Output]
public T Value => _blackboardData.GetValue<T>(_blackDragData); public T Value => _blackboardData.GetValue<T>(_blackDragData);
public BlackboardDragNodeData(string blackDragData,T blackboardData){
_blackDragData = blackDragData; public BlackboardDragNodeData(){
_blackboardData = blackboardData;
} }
} }

Loading…
Cancel
Save