From 01189efbc0f7dd957927ec40862967fb99395189 Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Mon, 8 Aug 2022 21:34:50 +0800 Subject: [PATCH] fix:fix placemat bug and node create in correct position.further more,graph view scale is now persisted as data --- .../Runtime/Nodes/GraphMetaNode.cs | 3 +- TNode/Samples/AddNode.cs | 1 + TNode/Samples/New HelloGraph.asset | 181 +++++++++++------- .../Editor/Blackboard/IBlackboardView.cs | 8 +- .../IGraphViewPersistence.cs | 8 +- .../Editor/Inspector/INodeDataBinding.cs | 2 +- .../Editor/Inspector/INodeDataBindingBase.cs | 2 +- TNode/TNodeCore/Editor/Models/EditorModel.cs | 4 +- .../TNodeCore/Editor/Models/GraphViewData.cs | 13 ++ .../Editor/Models/GraphViewData.cs.meta | 3 + .../Models/{Placemat.cs => PlacematData.cs} | 5 +- ...{Placemat.cs.meta => PlacematData.cs.meta} | 0 TNode/TNodeCore/Runtime/Logger/NodeLogger.cs | 1 + TNode/TNodeCore/Runtime/Models/GraphData.cs | 25 ++- .../Editor/Cache/NodeEditorExtensions.cs | 2 - .../DefaultGraphBlackboardView.cs | 4 +- .../Editor/NodeGraphView/DataGraphView.cs | 151 ++++++++++----- .../Editor/NodeViews/NodeView.cs | 6 +- .../Editor/Placemats/PlacematView.cs | 39 +++- .../Editor/Search/NodeSearchWindowProvider.cs | 17 +- 20 files changed, 330 insertions(+), 145 deletions(-) create mode 100644 TNode/TNodeCore/Editor/Models/GraphViewData.cs create mode 100644 TNode/TNodeCore/Editor/Models/GraphViewData.cs.meta rename TNode/TNodeCore/Editor/Models/{Placemat.cs => PlacematData.cs} (71%) rename TNode/TNodeCore/Editor/Models/{Placemat.cs.meta => PlacematData.cs.meta} (100%) diff --git a/TNode/GraphCreator/Runtime/Nodes/GraphMetaNode.cs b/TNode/GraphCreator/Runtime/Nodes/GraphMetaNode.cs index 4b4241a..64d6dd1 100644 --- a/TNode/GraphCreator/Runtime/Nodes/GraphMetaNode.cs +++ b/TNode/GraphCreator/Runtime/Nodes/GraphMetaNode.cs @@ -1,8 +1,9 @@ using System; using TNodeCore.Runtime.Attributes; +using TNodeCore.Runtime.Models; namespace TNode.GraphCreator.Runtime.Nodes{ - using TNodeCore.Runtime.Models; + namespace TNode.GraphCreator.Runtime{ [GraphUsage(typeof(MetaGraph))] diff --git a/TNode/Samples/AddNode.cs b/TNode/Samples/AddNode.cs index 2c22e58..bd269dd 100644 --- a/TNode/Samples/AddNode.cs +++ b/TNode/Samples/AddNode.cs @@ -17,6 +17,7 @@ namespace Samples{ public override void Process(){ Res = A + (Vector3)B; + Debug.Log(Res); this.Log(Res.ToString()); } } diff --git a/TNode/Samples/New HelloGraph.asset b/TNode/Samples/New HelloGraph.asset index 984c78f..bf972ef 100644 --- a/TNode/Samples/New HelloGraph.asset +++ b/TNode/Samples/New HelloGraph.asset @@ -13,119 +13,166 @@ MonoBehaviour: m_Name: New HelloGraph m_EditorClassIdentifier: nodeList: - - rid: 4804121563801583862 - - rid: 4804121563801583947 - - rid: 4804121563801583866 - - rid: 4804121563801583898 - - rid: 4804121563801583870 - nodeLinks: - - inPort: - portEntryName: A - nodeDataId: 6ceba867-fe0d-40c3-9d30-2d5d12803b52 - outPort: - portEntryName: Value - nodeDataId: ac84573e-638d-45fa-b4e2-1a81c31fa9e7 - - inPort: - portEntryName: A - nodeDataId: 6ceba867-fe0d-40c3-9d30-2d5d12803b52 - outPort: - portEntryName: Value - nodeDataId: b2ab4a52-e65d-4104-8891-dc316af217d9 + - id: 0 + - id: 1 + - id: 2 + - id: 3 + - id: 4 + - id: 5 + - id: 6 + - id: 7 + nodeLinks: [] blackboardData: - rid: 4804121563801583854 + id: 8 sceneReference: + editorModels: + - id: 9 + graphViewData: + id: 10 references: - version: 2 - RefIds: - - rid: 4804121563801583854 - type: {class: HelloBlackboard, ns: TNode.Samples, asm: Assembly-CSharp} + version: 1 + 00000000: + type: {class: AddNode, ns: Samples, asm: Assembly-CSharp} data: positionInView: serializedVersion: 2 - x: 0 - y: 0 + x: 1944 + y: 205 width: 0 height: 0 - HelloString: Hello World - HelloGameObject: {fileID: 0} - V3S: - - {x: 0, y: 0, z: 0} - - {x: 0, y: 0, z: 0} - - {x: 0, y: 0, z: 0} - V2S: - - {x: 0, y: 0} - - {x: 0, y: 0} - - {x: 0, y: 0} - - rid: 4804121563801583862 - type: {class: BlackboardDragNodeData, ns: TNodeCore.Runtime.Models, asm: NewAssembly} + id: 74136d91-e634-401b-8c99-3c2bb351cab2 + nodeName: AddNode + entryPoint: 0 + isTest: 0 + 00000001: + type: {class: AddNode, ns: Samples, asm: Assembly-CSharp} data: positionInView: serializedVersion: 2 - x: 336 - y: 355 + x: 2045 + y: 205 width: 0 height: 0 - id: bae506a7-58ec-4b79-9c21-747fa2b6a7ba - nodeName: + id: 8bee0814-52ef-4068-bffb-58a786d73ec2 + nodeName: AddNode entryPoint: 0 isTest: 0 - blackDragData: HelloString - isListElement: 0 - - rid: 4804121563801583866 + 00000002: type: {class: AddNode, ns: Samples, asm: Assembly-CSharp} data: positionInView: serializedVersion: 2 - x: 973.3707 - y: 189.23111 + x: 2146 + y: 205 width: 0 height: 0 - id: 6ceba867-fe0d-40c3-9d30-2d5d12803b52 + id: 819d1689-d64d-41ca-a9ac-0e00d25806c0 nodeName: AddNode entryPoint: 0 isTest: 0 - - rid: 4804121563801583870 - type: {class: BlackboardDragNodeData, ns: TNodeCore.Runtime.Models, asm: NewAssembly} + 00000003: + type: {class: AddNode, ns: Samples, asm: Assembly-CSharp} data: positionInView: serializedVersion: 2 - x: 748.0001 - y: 291 + x: 2247 + y: 205 width: 0 height: 0 - id: b2ab4a52-e65d-4104-8891-dc316af217d9 - nodeName: + id: b8495e41-8ec2-4015-8784-7a99c1003c6b + nodeName: AddNode entryPoint: 0 isTest: 0 - blackDragData: V2S.0 - isListElement: 1 - - rid: 4804121563801583898 - type: {class: BlackboardDragNodeData, ns: TNodeCore.Runtime.Models, asm: NewAssembly} + 00000004: + type: {class: AddNode, ns: Samples, asm: Assembly-CSharp} data: positionInView: serializedVersion: 2 - x: 747 - y: 179 + x: 2014 + y: 340 width: 0 height: 0 - id: ac84573e-638d-45fa-b4e2-1a81c31fa9e7 - nodeName: + id: 3f27f6fe-b2db-41f2-9b3f-5e5fc468e536 + nodeName: AddNode + entryPoint: 0 + isTest: 0 + 00000005: + type: {class: AddNode, ns: Samples, asm: Assembly-CSharp} + data: + positionInView: + serializedVersion: 2 + x: 2130 + y: 340 + width: 0 + height: 0 + id: 6500c6c2-3551-4930-8338-afd02a49105b + nodeName: AddNode entryPoint: 0 isTest: 0 - blackDragData: V3S.0 - isListElement: 1 - - rid: 4804121563801583947 + 00000006: + type: {class: AddNode, ns: Samples, asm: Assembly-CSharp} + data: + positionInView: + serializedVersion: 2 + x: 2247 + y: 340 + width: 0 + height: 0 + id: 002de40a-5ef5-4e99-8373-2628d189d6c9 + nodeName: AddNode + entryPoint: 0 + isTest: 0 + 00000007: type: {class: BlackboardDragNodeData, ns: TNodeCore.Runtime.Models, asm: NewAssembly} data: positionInView: serializedVersion: 2 - x: 299 - y: 313 + x: 1944.9998 + y: 475.8858 width: 0 height: 0 - id: b8c1e42e-c6a3-491e-aba2-c2b6ebd1c419 + id: 2f9adf6b-b40b-4fbd-9bd6-0d1ebb10992f nodeName: entryPoint: 0 isTest: 0 blackDragData: HelloGameObject isListElement: 0 + 00000008: + type: {class: HelloBlackboard, ns: TNode.Samples, asm: Assembly-CSharp} + data: + positionInView: + serializedVersion: 2 + x: 0 + y: 0 + width: 0 + height: 0 + HelloString: hello + HelloGameObject: {fileID: 3675195758153116951, guid: 35a9f7ccd0ed7d744b85580016a0aab5, type: 3} + V3S: + - {x: 0, y: 0, z: 0} + V2S: + - {x: 0, y: 0} + 00000009: + type: {class: PlacematData, ns: TNodeCore.Editor.Models, asm: NewAssembly} + data: + positionInView: + serializedVersion: 2 + x: 1903.9056 + y: 75.76817 + width: 500 + height: 500 + hostModels: [] + zOrder: 0 + title: Title + 0000000A: + type: {class: GraphViewData, ns: TNode.TNodeCore.Editor.Models, asm: NewAssembly} + data: + positionInView: + serializedVersion: 2 + x: 0 + y: 0 + width: 0 + height: 0 + persistScale: 0.8695652 + persistOffset: {x: -878, y: -29} + isBlackboardOn: 1 diff --git a/TNode/TNodeCore/Editor/Blackboard/IBlackboardView.cs b/TNode/TNodeCore/Editor/Blackboard/IBlackboardView.cs index bf6360a..8da60e4 100644 --- a/TNode/TNodeCore/Editor/Blackboard/IBlackboardView.cs +++ b/TNode/TNodeCore/Editor/Blackboard/IBlackboardView.cs @@ -4,16 +4,16 @@ using UnityEditor; namespace TNodeCore.Editor.Blackboard{ public interface IBlackboardView{ - public BlackboardData GetBlackboardData(); - public void SetBlackboardData(BlackboardData data); + BlackboardData GetBlackboardData(); + void SetBlackboardData(BlackboardData data); - public void AddItem(); + void AddItem(); void Setup(IBaseDataGraphView graphView,EditorWindow ownerWindow); void NotifyUpdate(); } public interface IBlackboardView : IBlackboardView where T : BlackboardData{ - public T Data{ get; set; } + T Data{ get; set; } } } \ No newline at end of file diff --git a/TNode/TNodeCore/Editor/EditorPersistence/IGraphViewPersistence.cs b/TNode/TNodeCore/Editor/EditorPersistence/IGraphViewPersistence.cs index 9c22f1a..672b2ed 100644 --- a/TNode/TNodeCore/Editor/EditorPersistence/IGraphViewPersistence.cs +++ b/TNode/TNodeCore/Editor/EditorPersistence/IGraphViewPersistence.cs @@ -1,9 +1,9 @@ namespace TNodeCore.Editor.EditorPersistence{ public interface IGraphViewPersistence{ - public string GetPersistenceId(); - public void ResetPos(GraphEditorData editorData); - public void SavePos(GraphEditorData editorData); + string GetPersistenceId(); + void ResetPos(GraphEditorData editorData); + void SavePos(GraphEditorData editorData); - public void OnRemoveFromGraph(GraphEditorData editorData); + void OnRemoveFromGraph(GraphEditorData editorData); } } \ No newline at end of file diff --git a/TNode/TNodeCore/Editor/Inspector/INodeDataBinding.cs b/TNode/TNodeCore/Editor/Inspector/INodeDataBinding.cs index e964260..a502fb5 100644 --- a/TNode/TNodeCore/Editor/Inspector/INodeDataBinding.cs +++ b/TNode/TNodeCore/Editor/Inspector/INodeDataBinding.cs @@ -3,6 +3,6 @@ - public void OnBindingDataUpdate(); + void OnBindingDataUpdate(); } } \ No newline at end of file diff --git a/TNode/TNodeCore/Editor/Inspector/INodeDataBindingBase.cs b/TNode/TNodeCore/Editor/Inspector/INodeDataBindingBase.cs index 28f6c40..1b2367e 100644 --- a/TNode/TNodeCore/Editor/Inspector/INodeDataBindingBase.cs +++ b/TNode/TNodeCore/Editor/Inspector/INodeDataBindingBase.cs @@ -2,7 +2,7 @@ namespace TNodeCore.Editor.Inspector{ public interface INodeDataBindingBase{ - public string BindingPath{ get; set; } + string BindingPath{ get; set; } public NodeData BindingNodeData{ get; set; } } } \ No newline at end of file diff --git a/TNode/TNodeCore/Editor/Models/EditorModel.cs b/TNode/TNodeCore/Editor/Models/EditorModel.cs index 42eddae..4923434 100644 --- a/TNode/TNodeCore/Editor/Models/EditorModel.cs +++ b/TNode/TNodeCore/Editor/Models/EditorModel.cs @@ -1,8 +1,10 @@ -using TNodeCore.Runtime.Models; +using System; +using TNodeCore.Runtime.Models; using UnityEngine; namespace TNodeCore.Editor.Models{ //This models are editor only + [Serializable] public abstract class EditorModel:Model{ } diff --git a/TNode/TNodeCore/Editor/Models/GraphViewData.cs b/TNode/TNodeCore/Editor/Models/GraphViewData.cs new file mode 100644 index 0000000..d631d45 --- /dev/null +++ b/TNode/TNodeCore/Editor/Models/GraphViewData.cs @@ -0,0 +1,13 @@ +using System; +using TNodeCore.Editor.Models; +using UnityEngine; +using UnityEngine.Serialization; + +namespace TNode.TNodeCore.Editor.Models{ + [Serializable] + public class GraphViewData:EditorModel{ + public float persistScale = 1f; + public Vector2 persistOffset = Vector2.zero; + public bool isBlackboardOn; + } +} \ No newline at end of file diff --git a/TNode/TNodeCore/Editor/Models/GraphViewData.cs.meta b/TNode/TNodeCore/Editor/Models/GraphViewData.cs.meta new file mode 100644 index 0000000..a0aa741 --- /dev/null +++ b/TNode/TNodeCore/Editor/Models/GraphViewData.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1df79f038838423c914db0e2f5d4b2b0 +timeCreated: 1659964614 \ No newline at end of file diff --git a/TNode/TNodeCore/Editor/Models/Placemat.cs b/TNode/TNodeCore/Editor/Models/PlacematData.cs similarity index 71% rename from TNode/TNodeCore/Editor/Models/Placemat.cs rename to TNode/TNodeCore/Editor/Models/PlacematData.cs index 5666da5..8ab8729 100644 --- a/TNode/TNodeCore/Editor/Models/Placemat.cs +++ b/TNode/TNodeCore/Editor/Models/PlacematData.cs @@ -5,8 +5,11 @@ using UnityEngine; namespace TNodeCore.Editor.Models{ [Serializable] - public class Placemat:EditorModel{ + public class PlacematData:EditorModel{ [SerializeReference] public List hostModels = new List(); + + public int zOrder; + public string title; } } \ No newline at end of file diff --git a/TNode/TNodeCore/Editor/Models/Placemat.cs.meta b/TNode/TNodeCore/Editor/Models/PlacematData.cs.meta similarity index 100% rename from TNode/TNodeCore/Editor/Models/Placemat.cs.meta rename to TNode/TNodeCore/Editor/Models/PlacematData.cs.meta diff --git a/TNode/TNodeCore/Runtime/Logger/NodeLogger.cs b/TNode/TNodeCore/Runtime/Logger/NodeLogger.cs index 077db9b..5b43dd8 100644 --- a/TNode/TNodeCore/Runtime/Logger/NodeLogger.cs +++ b/TNode/TNodeCore/Runtime/Logger/NodeLogger.cs @@ -17,5 +17,6 @@ namespace TNodeCore.Runtime{ public interface INodeLoggerImpl{ public void Log(string message); + void LogTexture(Texture2D texture2D); } } \ No newline at end of file diff --git a/TNode/TNodeCore/Runtime/Models/GraphData.cs b/TNode/TNodeCore/Runtime/Models/GraphData.cs index dffd551..691712c 100644 --- a/TNode/TNodeCore/Runtime/Models/GraphData.cs +++ b/TNode/TNodeCore/Runtime/Models/GraphData.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using TNode.TNodeCore.Editor.Models; using TNodeCore.Editor.Models; using UnityEditor; using UnityEngine; @@ -44,9 +45,27 @@ namespace TNodeCore.Runtime.Models{ } #if UNITY_EDITOR - [SerializeReference] public List EditorModels = new List(); - -#endif + [SerializeReference] + protected List editorModels ; + [SerializeReference] + protected GraphViewData graphViewData; + + + public List EditorModels{ + get{ + return editorModels ??= new List(); + + } + set => editorModels = value; + } + public GraphViewData GraphViewData{ + get{ + return graphViewData ??= new GraphViewData(); + + } + set => graphViewData = value; + } + #endif } diff --git a/TNode/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs b/TNode/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs index 8466485..8e8061f 100644 --- a/TNode/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs +++ b/TNode/TNodeGraphViewImpl/Editor/Cache/NodeEditorExtensions.cs @@ -10,7 +10,6 @@ using TNodeCore.Editor.EditorPersistence; using TNodeCore.Editor.NodeGraphView; using TNodeCore.Runtime.Attributes; using TNodeCore.Runtime.Models; -using Unity.VisualScripting; using UnityEditor; using UnityEngine; @@ -278,7 +277,6 @@ namespace TNode.TNodeGraphViewImpl.Editor.Cache{ public class Launcher{ static Launcher(){ - Debug.Log("NES Launched"); NodeEditorSingleton.Instance.Initialize(); } } diff --git a/TNode/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs b/TNode/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs index 649d74d..6fde335 100644 --- a/TNode/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs +++ b/TNode/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs @@ -1,12 +1,12 @@ using System; using System.Collections; +using System.Linq; using System.Reflection; using TNode.TNodeGraphViewImpl.Editor.Search; using TNodeCore.Editor.NodeGraphView; using TNodeCore.Editor.Serialization; using TNodeCore.Runtime.Attributes; using TNodeCore.Runtime.Models; -using Unity.VisualScripting; using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEditor.UIElements; @@ -34,7 +34,7 @@ namespace TNode.TNodeGraphViewImpl.Editor.GraphBlackboard{ Add(blackboardGlobalSection); foreach (var field in data.GetType() .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)){ - if(field.HasAttribute(typeof(HideInBlackboard))) continue; + if(field.GetCustomAttributes(typeof(HideInBlackboard)).Count()!=0) continue; //if the field is MonoBehaviour,add a property field for blackboard //skip if the field is a list or Ilist if (!typeof(IList).IsAssignableFrom(field.FieldType)&&!field.FieldType.IsArray){ diff --git a/TNode/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs b/TNode/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs index eb56a7c..1c89460 100644 --- a/TNode/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs +++ b/TNode/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs @@ -3,14 +3,17 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; +using TNode.TNodeCore.Editor.Models; using TNode.TNodeGraphViewImpl.Editor.Cache; using TNode.TNodeGraphViewImpl.Editor.GraphBlackboard; using TNode.TNodeGraphViewImpl.Editor.Inspector; using TNode.TNodeGraphViewImpl.Editor.NodeViews; +using TNode.TNodeGraphViewImpl.Editor.Placemats; using TNode.TNodeGraphViewImpl.Editor.Search; using TNodeCore.Editor; using TNodeCore.Editor.Blackboard; using TNodeCore.Editor.EditorPersistence; +using TNodeCore.Editor.Models; using TNodeCore.Editor.NodeGraphView; using TNodeCore.Editor.Tools.NodeCreator; @@ -39,6 +42,7 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ private Dictionary _nodeDict = new Dictionary(); private IBlackboardView _blackboard; private bool _loaded; + private GraphViewData _graphViewData; public T Data{ get{ return _data; } set{ @@ -107,7 +111,6 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ } else{ if (obj is GameObject gameObject){ - if (gameObject.GetComponent() != null){ if (gameObject.GetComponent().graphData != null){ _runtimeGraph = gameObject.GetComponent(); @@ -167,20 +170,7 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ } } } - // private void UpdateRuntimeGraphBehaviour(){ - // if(_runtimeGraph != null){ - // if (_runtimeGraphUpdate){ - // _runtimeGraphUpdate = false; - // _runtimeGraph.TraverseAll(); - // - // AfterGraphResolved?.Invoke(); - // } - // } - // else{ - // EditorApplication.update -= UpdateRuntimeGraphBehaviour; - // } - // } - + private void CheckDataAfterInit(){ if(Data == null){ WaitingForAGraph(); @@ -201,24 +191,43 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ var dmaPos = dma.eventInfo.mousePosition+editorPosition; SearchWindowContext searchWindowContext = new SearchWindowContext(dmaPos,200,200); var searchWindow = ScriptableObject.CreateInstance(); - searchWindow.Setup(typeof(T),this,Owner); + var targetPos = this.viewTransform.matrix.inverse.MultiplyPoint(dma.eventInfo.localMousePosition); + searchWindow.Setup(typeof(T),this,Owner,targetPos); + + Debug.Log(targetPos); SearchWindow.Open(searchWindowContext, searchWindow); }); - evt.menu.AppendAction("Create Placemat",dma=> { + evt.menu.AppendAction("Create PlacematData",dma=> { //find placemat container - PlacematContainer placematContainer = this.Q(); - if (placematContainer == null){ - placematContainer = new PlacematContainer(this); - this.Add(placematContainer); - } - var dmaPos = dma.eventInfo.mousePosition+editorPosition; - var dmaPosRect = new Rect(dmaPos,new Vector2(500,500)); - placematContainer.CreatePlacemat(dmaPosRect,1,"Title"); - + var placematContainer = GetPlacematContainer(); + var targetPos = this.viewTransform.matrix.inverse.MultiplyPoint(dma.eventInfo.localMousePosition); + var dmaPosRect = new Rect(targetPos,new Vector2(500,500)); + var placemat = placematContainer.CreatePlacemat(dmaPosRect,1,"Title"); + var placematData = new PlacematData{ + title = "Title", + positionInView = dmaPosRect + }; + placemat.PlacematData = placematData; + AddPlacemat(placematData); + }); }); } + private void AddPlacemat(PlacematData data){ + _data.EditorModels.Add(data); + } + + private PlacematContainer GetPlacematContainer(){ + if (this.placematContainer == null){ + PlacematContainer container = new PlacematContainer(this){ + name = "PlacematContainer" + }; + this.Add(container); + } + return this.placematContainer; + } + private void OnInit(){ ConstructDefaultBehaviour(); @@ -251,7 +260,7 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ public virtual void AfterEditorLoadGraphView(){ } - + protected void CreateMenu(){ if (this.Q("TopMenu") != null) return; var visualElement = new VisualElement{ @@ -313,6 +322,7 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ #region event callbakc private void OnDragPerform(DragPerformEvent evt){ + var realPos = viewTransform.matrix.inverse.MultiplyPoint(evt.localMousePosition); if (DragAndDrop.GetGenericData("DragSelection") is List data){ if (data.Count == 0){ @@ -325,7 +335,7 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ var dragNodeData = NodeCreator.InstantiateNodeData(); dragNodeData.BlackboardData = GetBlackboardData(); dragNodeData.BlackDragData = field.BlackboardProperty.PropertyName; - AddTNode(dragNodeData,new Rect(evt.mousePosition,new Vector2(200,200))); + AddTNode(dragNodeData,new Rect(realPos,new Vector2(200,200))); } } @@ -336,10 +346,9 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ var dragNodeData = NodeCreator.InstantiateNodeData(); dragNodeData.BlackboardData = GetBlackboardData(); dragNodeData.BlackDragData = entry.propertyPath; - AddTNode(dragNodeData,new Rect(evt.mousePosition,new Vector2(200,200))); + AddTNode(dragNodeData,new Rect(realPos,new Vector2(200,200))); } } - } } @@ -354,18 +363,26 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ } #endregion - - public void ResetGraphView(){ - //Clear all nodes + public void ClearAll(){ foreach (var node in nodes.ToList()){ RemoveElement(node); } foreach (var edge in edges.ToList()){ RemoveElement(edge); } + foreach (var placemat in placematContainer.Placemats.ToList()){ + RemoveElement(placemat); + } + } + public void ResetGraphView(){ + ClearAll(); + + LoadPersistentGraphViewData(); + + if (_nodeDict == null) throw new ArgumentNullException(nameof(_nodeDict)); - + foreach (var dataNode in _data.NodeDictionary.Values){ if(dataNode==null) continue; @@ -400,9 +417,30 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ newEdge.output?.Connect(newEdge); AddElement(newEdge); } + var placemats = _data.EditorModels.OfType(); + foreach (var placemat in placemats){ + var container = GetPlacematContainer(); + var res = container.CreatePlacemat(placemat.positionInView, 0, placemat.title); + res.PlacematData = placemat; + + } _nodeDict.Clear(); } + private void LoadPersistentGraphViewData(){ + var r= _data.GraphViewData; + this.viewTransformChanged-=OnViewTransformChanged; + this.UpdateViewTransform(r.persistOffset,new Vector3(r.persistScale,r.persistScale,1)); + this.viewTransformChanged+=OnViewTransformChanged; + + } + + private void OnViewTransformChanged(GraphView graphview){ + if (_data == null) return; + _data.GraphViewData.persistOffset = graphview.viewTransform.position; + _data.GraphViewData.persistScale = graphview.viewTransform.scale.x; + } + private void AddPersistentNode(NodeData dataNode){ var nodePos = dataNode.positionInView; AddTNode(dataNode, nodePos); @@ -455,21 +493,7 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ } public void SaveEditorData(GraphEditorData graphEditorData){ - // graphEditorData.graphElementsData?.Clear(); - // //iterator nodes - // if (graphEditorData.graphElementsData == null){ - // graphEditorData.graphElementsData = new List(); - // } - // foreach (var node in this.nodes){ - // var nodeEditorData = new GraphElementEditorData{ - // pos = node.GetPosition(), - // }; - // if (node is IBaseNodeView nodeView){ - // nodeEditorData.guid = nodeView.GetNodeData().id; - // } - // graphEditorData.graphElementsData.Add(nodeEditorData); - // EditorUtility.SetDirty(graphEditorData); - // } + //To do something later } public void SaveWithEditorData(GraphEditorData graphEditorData){ @@ -514,12 +538,24 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ private void SaveGraphData(){ _data.NodeDictionary.Clear(); _data.NodeLinks.Clear(); + _data.EditorModels.Clear(); SaveNode(); SaveEdge(); SaveBlackboard(); + SaveEditorModels(); EditorUtility.SetDirty(_data); } + private void SaveEditorModels(){ + var placemats = placematContainer.Placemats.ToList(); + Debug.Log(placemats.Count); + foreach (var placemat in placemats){ + if (placemat is PlacematView placematView){ + _data.EditorModels.Add(placematView.PlacematData); + } + } + } + private void SaveBlackboard(){ if (_data.blackboardData == null){ _data.blackboardData = NodeEditorExtensions.GetAppropriateBlackboardData(_data.GetType()); @@ -591,12 +627,16 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ } #region implement interfaces + /// + /// Simultaneously add the node to the graph data and add the node to the graph view + /// + /// + /// The give rect is the actual position in the graph view public void AddTNode(NodeData nodeData, Rect rect){ if (NodeEditorExtensions.CreateNodeViewFromNodeType(nodeData.GetType()) is Node nodeView){ //convert rect at graph space - - var resPos = this.viewTransform.matrix.inverse.MultiplyPoint3x4(rect.position); - rect.position = resPos; + + rect.position = rect.position; if(nodeView is IBaseNodeView nodeViewInterface){ nodeViewInterface.SetNodeData(nodeData); } @@ -637,6 +677,10 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ } } + /// + /// Remove node from + /// + /// public void RemoveTNode(NodeData nodeData){ Undo.RegisterCompleteObjectUndo(_data,"Node deletion"); @@ -685,6 +729,9 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeGraphView{ castedBlackboard?.SetPosition(blackboardPos); UpdateBlackboardData(); OnDataChanged+= (sender, e) => { UpdateBlackboardData(); }; + if(_data.blackboardData!=null){ + _data.GraphViewData.isBlackboardOn = true; + } } public GraphData GetGraphData(){ diff --git a/TNode/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs b/TNode/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs index ccca056..b7ed2a8 100644 --- a/TNode/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs +++ b/TNode/TNodeGraphViewImpl/Editor/NodeViews/NodeView.cs @@ -37,7 +37,11 @@ namespace TNode.TNodeGraphViewImpl.Editor.NodeViews{ } loggerArea.multiline = true; - loggerArea.value = message; + loggerArea.value += message; + } + + public void LogTexture(Texture2D texture2D){ + } } diff --git a/TNode/TNodeGraphViewImpl/Editor/Placemats/PlacematView.cs b/TNode/TNodeGraphViewImpl/Editor/Placemats/PlacematView.cs index baa95fc..882cfd1 100644 --- a/TNode/TNodeGraphViewImpl/Editor/Placemats/PlacematView.cs +++ b/TNode/TNodeGraphViewImpl/Editor/Placemats/PlacematView.cs @@ -1,7 +1,44 @@ -using UnityEditor.Experimental.GraphView; + + + +using TNodeCore.Editor.Models; +using UnityEditor.Experimental.GraphView; +using UnityEngine; +using UnityEngine.UIElements; namespace TNode.TNodeGraphViewImpl.Editor.Placemats{ public class PlacematView:Placemat{ + public PlacematData PlacematData{ + get => _placematData; + set{ + _placematData = value; + UpdatePlacematData(); + } + } + private PlacematData _placematData; + public int zOrder {get;set;} + public PlacematView(){ + var title = this.Q(); + title.RegisterValueChangedCallback(evt => { + PlacematData.title = title.value; + }); + } + public virtual void UpdatePlacematData(){ + SetPosition(_placematData.positionInView); + this.Color = new Color(43/255.0f, 72/255.0f, 101/255.0f); + } + public sealed override void SetPosition(Rect newPos){ + base.SetPosition(newPos); + if (_placematData == null){ + return; + } + _placematData.positionInView = newPos; + } + public virtual void Collapse(){ + this.Collapsed = true; + } + + } } \ No newline at end of file diff --git a/TNode/TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs b/TNode/TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs index 642aeef..c661713 100644 --- a/TNode/TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs +++ b/TNode/TNodeGraphViewImpl/Editor/Search/NodeSearchWindowProvider.cs @@ -16,10 +16,13 @@ namespace TNode.TNodeGraphViewImpl.Editor.Search{ private Type _graphType; private GraphView _graphView; private EditorWindow _editor; - public void Setup(Type graph,GraphView graphView,EditorWindow editor){ + private Vector2 _createLocation; + + public void Setup(Type graph,GraphView graphView,EditorWindow editor,Vector2 createLocation=default){ _graphType = graph; _graphView = graphView; _editor = editor; + _createLocation = createLocation; } public List CreateSearchTree(SearchWindowContext context){ var nodeDataTypes = NodeEditorExtensions.GetGraphDataUsage(_graphType); @@ -46,17 +49,23 @@ namespace TNode.TNodeGraphViewImpl.Editor.Search{ } return list; } + + public Vector2 FromScreenToViewPos(VisualElement visualElement,Vector2 screenPos){ + var vector = visualElement.worldTransform.inverse.MultiplyVector(screenPos); + return vector; + } public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context){ var userData = SearchTreeEntry.userData; - var relativePos = context.screenMousePosition - _editor.position.position; - var localPos = _graphView.WorldToLocal(relativePos); + + + if (userData is Type type){ //Check if type is derived from NodeData if (typeof(NodeData).IsAssignableFrom(type)){ //Make an instance of the type if (NodeCreator.InstantiateNodeData(type) is { } nodeData){ nodeData.nodeName = $"{type.Name}"; - ((IBaseDataGraphView) _graphView).AddTNode(nodeData, new Rect(localPos.x, localPos.y, 100, 100)); + ((IBaseDataGraphView) _graphView).AddTNode(nodeData, new Rect(_createLocation.x, _createLocation.y, 100, 100)); } } return true;