diff --git a/TNode/Editor/BaseViews/DataGraphView.cs b/TNode/Editor/BaseViews/DataGraphView.cs index baa2227..456cb21 100644 --- a/TNode/Editor/BaseViews/DataGraphView.cs +++ b/TNode/Editor/BaseViews/DataGraphView.cs @@ -7,7 +7,7 @@ using TNode.Cache; using TNode.Editor.GraphBlackboard; using TNode.Editor.Inspector; using TNode.Editor.Model; - +using TNode.Editor.Tools.NodeCreator; using TNode.Models; using Unity.VisualScripting; using UnityEditor; @@ -111,7 +111,7 @@ namespace TNode.Editor.BaseViews{ } */ public abstract class DataGraphView:GraphView,IDataGraphView where T:GraphData{ - #region variablesandproperties + #region variables and properties @@ -138,7 +138,10 @@ namespace TNode.Editor.BaseViews{ } public event DataChangedEventHandler OnDataChanged; #endregion + #region event declarations public delegate void DataChangedEventHandler(object sender, DataChangedEventArgs e); + + #endregion //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(){ RegisterCallback(OnDragUpdated); RegisterCallback(OnDragPerform); } + #endregion + #region event callbakc + private void OnDragPerform(DragPerformEvent evt){ if (DragAndDrop.GetGenericData("DragSelection") is List{Count: > 0} data){ @@ -191,17 +201,12 @@ namespace TNode.Editor.BaseViews{ //Make a constructor of BlackboardDragNodeData by reflection var specifiedType = typeof(BlackboardDragNodeData<>).MakeGenericType(field.BlackboardProperty.PropertyType); - //get specific constructor of specified type with two parameters, one is a string ,another is a blackboarddata - var constructor = specifiedType.GetConstructor(new[] {typeof(string),typeof(BlackboardData)}); - //create a new instance of the specified type with the constructor and the two parameters - 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))); - } - + //Create a new instance of specified type + var dragNodeData = NodeCreator.InstantiateNodeData(specifiedType); + this.AddTNode(dragNodeData,new Rect(evt.mousePosition,new Vector2(200,200))); } } + } } @@ -217,10 +222,11 @@ namespace TNode.Editor.BaseViews{ } - private void OnInit(){ - ConstructDefaultBehaviour(); - OnGraphViewCreate(); - } + + #endregion + + + public void ResetGraphView(){ //Clear all nodes foreach (var node in nodes){ @@ -268,7 +274,7 @@ namespace TNode.Editor.BaseViews{ - + public virtual void CreateInspector(){ NodeInspector nodeInspector = new NodeInspector(); this.Add(nodeInspector); @@ -276,13 +282,13 @@ namespace TNode.Editor.BaseViews{ _isInspectorOn = true; } - public void CreateMiniMap(Rect rect){ + public virtual void CreateMiniMap(Rect rect){ var miniMap = new MiniMap(); this.Add(miniMap); miniMap.SetPosition(rect); } - public void CreateBlackboard(){ + public virtual void CreateBlackboard(){ _blackboard = new Blackboard(); //Blackboard add "Add Node" button // blackboard.Add(new BlackboardSection(){ @@ -370,11 +376,10 @@ namespace TNode.Editor.BaseViews{ } } private void SaveEdge(){ + var links = new List(); foreach (var edge in edges){ var inputNode = edge.input.node as INodeView; var outputNode = edge.output.node as INodeView; - var links = new List(); - Debug.Log($"Edge{inputNode},{outputNode}"); if (inputNode != null && outputNode != null){ var inputNodeData = inputNode.GetNodeData(); var outputNodeData = outputNode.GetNodeData(); @@ -388,17 +393,16 @@ namespace TNode.Editor.BaseViews{ }); links.Add(newNodeLink); } - - _data.nodeLinks = links; } + + _data.nodeLinks = links; } private void SaveGraphData(){ _data.NodeDictionary.Clear(); - + _data.nodeLinks.Clear(); SaveNode(); SaveEdge(); - EditorUtility.SetDirty(_data); } @@ -436,7 +440,6 @@ namespace TNode.Editor.BaseViews{ } } }); - if(nodeView is INodeView nodeViewInterface){ nodeViewInterface.SetNodeData(nodeData); } @@ -448,30 +451,27 @@ namespace TNode.Editor.BaseViews{ menu.AddItem(new GUIContent("Delete"), false, () => { RemoveElement(nodeView); if (nodeView is INodeView tNodeView){ - var nodeData1 = 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); + RemoveTNode(tNodeView.GetNodeData()); } }); menu.ShowAsContext(); }); - - - - } } 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); } } diff --git a/TNode/Editor/BaseViews/NodeView.cs b/TNode/Editor/BaseViews/NodeView.cs index 15c6636..b7caa5d 100644 --- a/TNode/Editor/BaseViews/NodeView.cs +++ b/TNode/Editor/BaseViews/NodeView.cs @@ -7,6 +7,7 @@ using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEditor.UIElements; using UnityEngine; +using UnityEngine.UIElements; namespace TNode.Editor.BaseViews{ @@ -41,10 +42,13 @@ namespace TNode.Editor.BaseViews{ protected NodeView(){ OnDataChanged+=OnDataChangedHandler; + _nodeInspectorInNode = new NodeInspectorInNode(){ name = "nodeInspectorInNode" }; this.extensionContainer.Add(_nodeInspectorInNode); + + BuildDoubleClickRename(); } private void OnDataChangedHandler(T obj){ 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(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(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(evt => { + if (evt.clickCount == 2){ + StartARenameTitleTextField(); + } + }); + } + public void SetNodeData(NodeData nodeData){ Data = (T)nodeData; diff --git a/TNode/Editor/Cache/NodeEditorExtensions.cs b/TNode/Editor/Cache/NodeEditorExtensions.cs index 6338d97..80cf405 100644 --- a/TNode/Editor/Cache/NodeEditorExtensions.cs +++ b/TNode/Editor/Cache/NodeEditorExtensions.cs @@ -145,6 +145,9 @@ namespace TNode.Cache{ } public static object CreateNodeViewFromNodeType(Type 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); if (NodeEditorSingleton.Instance.FromGenericToSpecific.ContainsKey(type)){ var implementedType = NodeEditorSingleton.Instance.FromGenericToSpecific[type]; diff --git a/TNode/Editor/Manipulators.meta b/TNode/Editor/Manipulators.meta new file mode 100644 index 0000000..f9c9614 --- /dev/null +++ b/TNode/Editor/Manipulators.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 400542f3cec140e2b55e2bcf637b2d9b +timeCreated: 1657009018 \ No newline at end of file diff --git a/TNode/Editor/Search/NodeSearchWindowProvider.cs b/TNode/Editor/Search/NodeSearchWindowProvider.cs index 58a3c1d..e694f4a 100644 --- a/TNode/Editor/Search/NodeSearchWindowProvider.cs +++ b/TNode/Editor/Search/NodeSearchWindowProvider.cs @@ -48,8 +48,8 @@ namespace TNode.Editor{ //Check if type is derived from NodeData if (typeof(NodeData).IsAssignableFrom(type)){ //Make an instance of the type - if (NodeHelper.InstantiateNodeData(type) is { } nodeData){ - nodeData.nodeName = "New Node"; + if (NodeCreator.InstantiateNodeData(type) is { } nodeData){ + nodeData.nodeName = $"New {type.Name}"; ((IDataGraphView) _graphView).AddTNode(nodeData, new Rect(localPos.x, localPos.y, 100, 100)); } } diff --git a/TNode/Editor/Tools/NodeCreator/NodeHelper.cs b/TNode/Editor/Tools/NodeCreator/NodeCreator.cs similarity index 94% rename from TNode/Editor/Tools/NodeCreator/NodeHelper.cs rename to TNode/Editor/Tools/NodeCreator/NodeCreator.cs index adf3b15..416e8a1 100644 --- a/TNode/Editor/Tools/NodeCreator/NodeHelper.cs +++ b/TNode/Editor/Tools/NodeCreator/NodeCreator.cs @@ -2,7 +2,7 @@ using TNode.Models; namespace TNode.Editor.Tools.NodeCreator{ - public static class NodeHelper{ + public static class NodeCreator{ /// /// always use this to create a new node. diff --git a/TNode/Editor/Tools/NodeCreator/NodeHelper.cs.meta b/TNode/Editor/Tools/NodeCreator/NodeCreator.cs.meta similarity index 100% rename from TNode/Editor/Tools/NodeCreator/NodeHelper.cs.meta rename to TNode/Editor/Tools/NodeCreator/NodeCreator.cs.meta diff --git a/TNode/JsonSerialize/JsonSerializeTool.cs b/TNode/JsonSerialize/JsonSerializeTool.cs index e886ccf..58888ba 100644 --- a/TNode/JsonSerialize/JsonSerializeTool.cs +++ b/TNode/JsonSerialize/JsonSerializeTool.cs @@ -21,7 +21,9 @@ namespace TNode.JsonSerialize{ DateFormatString = "yyyy-MM-dd HH:mm:ss", Converters = new List { new Vector3Converter() }, TypeNameHandling = TypeNameHandling.Auto, - ContractResolver = new WritablePropertiesOnlyResolver() + ContractResolver = new WritablePropertiesOnlyResolver(), + Formatting = Formatting.Indented + }; diff --git a/TNode/Models/BlackboardDragNodeData.cs b/TNode/Models/BlackboardDragNodeData.cs index 3b75c22..5119283 100644 --- a/TNode/Models/BlackboardDragNodeData.cs +++ b/TNode/Models/BlackboardDragNodeData.cs @@ -4,17 +4,17 @@ using TNode.Attribute.Ports; using TNode.RuntimeCache; namespace TNode.Models{ - public class BlackboardDragNodeData:NodeData where T:BlackboardData{ + public class BlackboardDragNodeData:NodeData{ [JsonIgnore] private string _blackDragData; [JsonIgnore] - private T _blackboardData; + private BlackboardData _blackboardData; [Output] public T Value => _blackboardData.GetValue(_blackDragData); - public BlackboardDragNodeData(string blackDragData,T blackboardData){ - _blackDragData = blackDragData; - _blackboardData = blackboardData; + + public BlackboardDragNodeData(){ + } }