From f641aa816d29f0cc33d15ddbbbc2995082f8ee3e Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Sat, 16 Jul 2022 14:04:51 +0800 Subject: [PATCH] feat:runtime graph support --- .../NodeGraphView/IBaseDataGraphView.cs | 6 +- .../Editor/Resources/GraphViewBackground.uss | 9 ++ TNodeCore/Runtime/RuntimeGraph.cs | 3 +- .../DefaultGraphBlackboardView.cs | 7 +- .../GraphBlackboardPropertyField.cs | 11 +- TNodeGraphViewImpl/Editor/GraphEditor.cs | 5 +- .../Editor/NodeGraphView/DataGraphView.cs | 106 ++++++++++++++++-- .../Search/BlackboardSearchWindowProvider.cs | 1 - 8 files changed, 128 insertions(+), 20 deletions(-) diff --git a/TNodeCore/Editor/NodeGraphView/IBaseDataGraphView.cs b/TNodeCore/Editor/NodeGraphView/IBaseDataGraphView.cs index bd13be7..14e0ae3 100644 --- a/TNodeCore/Editor/NodeGraphView/IBaseDataGraphView.cs +++ b/TNodeCore/Editor/NodeGraphView/IBaseDataGraphView.cs @@ -9,8 +9,10 @@ namespace TNodeCore.Editor.NodeGraphView{ public void CreateBlackboard(); public GraphData GetGraphData(); public BlackboardData GetBlackboardData(); + + public bool IsRuntimeGraph{ get; set; } - - void SetGraphData(GraphData graph); + public void SetGraphData(GraphData graph); + } } \ No newline at end of file diff --git a/TNodeCore/Editor/Resources/GraphViewBackground.uss b/TNodeCore/Editor/Resources/GraphViewBackground.uss index 44451b6..e9d3385 100644 --- a/TNodeCore/Editor/Resources/GraphViewBackground.uss +++ b/TNodeCore/Editor/Resources/GraphViewBackground.uss @@ -4,3 +4,12 @@ GridBackground{ --thick-line-color: rgba(211, 211, 211, 0.2); --spacing:50; } + +#HintLabel{ + position: absolute; + left: 45%; + top: 45%; + + font-size: 14; + +} \ No newline at end of file diff --git a/TNodeCore/Runtime/RuntimeGraph.cs b/TNodeCore/Runtime/RuntimeGraph.cs index 1d019e4..9df5282 100644 --- a/TNodeCore/Runtime/RuntimeGraph.cs +++ b/TNodeCore/Runtime/RuntimeGraph.cs @@ -11,7 +11,8 @@ namespace TNodeCore.Runtime{ public void OnValidate(){ if(runtimeBlackboardData==null||runtimeBlackboardData.GetType()==typeof(BlackboardData)){ - runtimeBlackboardData = graphData?.blackboardData; + if(graphData!=null) + runtimeBlackboardData = RuntimeCache.RuntimeCache.Instance.GetBlackboardData(graphData); } } } diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs index 0a74e1a..129d9ea 100644 --- a/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/DefaultGraphBlackboardView.cs @@ -2,6 +2,7 @@ using System.Reflection; using TNode.Editor.Search; using TNodeCore.Attribute; +using TNodeCore.Editor.NodeGraphView; using TNodeCore.Editor.Serialization; using TNodeCore.Models; using UnityEditor; @@ -21,6 +22,8 @@ namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ protected override void UpdateBlackboard(BlackboardData data){ if (data == null) return; var serializedObject = new SerializedObject((BlackboardDataWrapper)data); + var currentGraphView = graphView as IBaseDataGraphView; + var isRuntimeGraph = currentGraphView?.IsRuntimeGraph ?? false; foreach (var field in data.GetType() .GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)){ //if the field is MonoBehaviour,add a property field for blackboard @@ -31,14 +34,12 @@ namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ var foldoutData = new Foldout{ text = field.Name }; - var drawer = new GraphBlackboardPropertyField(serializedObject.FindProperty("data").FindPropertyRelative(field.Name),field.Name); + var drawer = new GraphBlackboardPropertyField(serializedObject.FindProperty("data").FindPropertyRelative(field.Name),field.Name,isRuntimeGraph); drawer.Bind(serializedObject); foldoutData.Add(drawer); visualElement.Add(propertyField); visualElement.Add(foldoutData); - Add(visualElement); - } else{ var blackboardList = new BlackboardSection{ diff --git a/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardPropertyField.cs b/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardPropertyField.cs index e96416f..33c40e6 100644 --- a/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardPropertyField.cs +++ b/TNodeGraphViewImpl/Editor/GraphBlackboard/GraphBlackboardPropertyField.cs @@ -4,15 +4,18 @@ using UnityEngine.UIElements; namespace TNodeGraphViewImpl.Editor.GraphBlackboard{ public class GraphBlackboardPropertyField:PropertyField{ - public GraphBlackboardPropertyField(SerializedProperty findPropertyRelative, string fieldName):base(findPropertyRelative, fieldName){ - - } + private readonly bool _runtime; + public GraphBlackboardPropertyField(SerializedProperty findPropertyRelative, string fieldName,bool runtime):base(findPropertyRelative, fieldName){ + _runtime = runtime; + } + protected override void ExecuteDefaultActionAtTarget(EventBase evt) { + base.ExecuteDefaultActionAtTarget(evt); if (this.Q() != null){ - this.Q().allowSceneObjects = false; + this.Q().allowSceneObjects = _runtime; } } diff --git a/TNodeGraphViewImpl/Editor/GraphEditor.cs b/TNodeGraphViewImpl/Editor/GraphEditor.cs index 4721969..f6c2460 100644 --- a/TNodeGraphViewImpl/Editor/GraphEditor.cs +++ b/TNodeGraphViewImpl/Editor/GraphEditor.cs @@ -66,6 +66,7 @@ namespace TNodeGraphViewImpl.Editor{ BuildGraphView(); DefineGraphEditorActions(); + GraphView.Owner = this; OnCreate(); } @@ -78,9 +79,9 @@ namespace TNodeGraphViewImpl.Editor{ } - public void Setup(T graphData){ - GraphView.Owner = this; + public void SetupNonRuntime(T graphData){ GraphView.Data = graphData; + GraphView.IsRuntimeGraph = false; } private void BuildGraphView(){ GraphView = NodeEditorExtensions.CreateViewComponentFromBaseType>(); diff --git a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs index 6c17f8f..d2713d5 100644 --- a/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs +++ b/TNodeGraphViewImpl/Editor/NodeGraphView/DataGraphView.cs @@ -8,19 +8,25 @@ using TNodeCore.Editor.EditorPersistence; using TNodeCore.Editor.NodeGraphView; using TNodeCore.Editor.Tools.NodeCreator; using TNodeCore.Models; +using TNodeCore.Runtime; using TNodeGraphViewImpl.Editor.Cache; using TNodeGraphViewImpl.Editor.GraphBlackboard; using TNodeGraphViewImpl.Editor.NodeViews; + using UnityEditor; using UnityEditor.Experimental.GraphView; +using UnityEditor.VersionControl; using UnityEngine; using UnityEngine.UIElements; + using Edge = UnityEditor.Experimental.GraphView.Edge; namespace TNodeGraphViewImpl.Editor.NodeGraphView{ public abstract class BaseDataGraphView:GraphView,IDataGraphView where T:GraphData{ #region variables and properties private T _data; + private RuntimeGraph _runtimeGraph; + private bool _isInspectorOn; private NodeSearchWindowProvider _nodeSearchWindowProvider; private NodeInspector _nodeInspector; @@ -59,7 +65,91 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale); RegisterDragEvent(); OnInit(); + CheckAfterInit(); } + /// + /// Probably reusable in later GTFs version + /// + private void WaitingForAGraph(){ + Debug.Log("hello"); + VisualElement visualElement = new VisualElement(); + //Set background color to white + visualElement.style.backgroundColor = new StyleColor(new Color(0.1f, 0.1f, 0.1f, 1)); + + Debug.Log("hello2"); + visualElement.StretchToParentSize(); + visualElement.name = "WaitingForAGraph"; + Add(visualElement); + visualElement.BringToFront(); + + + //Add a label at the center of the created element + Label label = new Label("drag a graph item here"){ + style ={ + position = Position.Absolute + }, + name = "HintLabel" + }; + + visualElement.RegisterCallback((evt) => { + //check if the dragged object is a graph data or a Game Object contains a runtime graph + var res = DragAndDrop.objectReferences; + foreach (var obj in res){ + if (obj is T graphData){ + Data = graphData; + IsRuntimeGraph = false; + } + else{ + if (obj is GameObject gameObject){ + + if (gameObject.GetComponent() != null){ + if (gameObject.GetComponent().graphData != null){ + + _runtimeGraph = gameObject.GetComponent(); + IsRuntimeGraph = true; + + + Data = gameObject.GetComponent().graphData as T; + if(Data==null){ + Debug.LogError($"Dragged a wrong graph data to editor,expected {typeof(T)} but got {gameObject.GetComponent().graphData.GetType()}"); + } + + } + } + } + } + } + }); + visualElement.RegisterCallback((evt) => { + //check if the dragged object is a graph data or a Game Object contains a runtime graph + var res = DragAndDrop.objectReferences; + foreach (var obj in res){ + if (obj is GraphData graphData){ + DragAndDrop.visualMode = DragAndDropVisualMode.Link; + } + else{ + if (obj is GameObject gameObject){ + if (gameObject.GetComponent() != null){ + DragAndDrop.visualMode = DragAndDropVisualMode.Link; + } + } + } + } + + }); + visualElement.Add(label); + OnDataChanged += (sender, e) => { + if (Data != null){ + visualElement.RemoveFromHierarchy(); + } + }; + } + private void CheckAfterInit(){ + if(Data == null){ + WaitingForAGraph(); + } + } + private void ConstructDefaultBehaviour(){ //Register a right click context menu ConstructViewContextualMenu(); @@ -101,7 +191,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ if(selectable is { } field) { //Make a constructor of BlackboardDragNodeData by reflection var dragNodeData = NodeCreator.InstantiateNodeData(); - dragNodeData.blackboardData = _data.blackboardData; + dragNodeData.blackboardData = GetBlackboardData(); dragNodeData.blackDragData = field.BlackboardProperty.PropertyName; AddTNode(dragNodeData,new Rect(evt.mousePosition,new Vector2(200,200))); } @@ -182,7 +272,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ public virtual void CreateMiniMap(Rect rect){ var miniMap = new MiniMap(); - this.Add(miniMap); + Add(miniMap); miniMap.SetPosition(rect); } @@ -194,7 +284,7 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ if (_data.blackboardData == null) return; } - _blackboard.SetBlackboardData(_data.blackboardData); + _blackboard.SetBlackboardData(GetBlackboardData()); } public virtual void DestroyInspector(){ @@ -352,13 +442,10 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ _blackboard.Setup(this,Owner); var castedBlackboard = _blackboard as Blackboard; - Add(castedBlackboard); - Rect blackboardPos = new Rect(0,0,300,700); castedBlackboard?.SetPosition(blackboardPos); - OnDataChanged+= (sender, e) => { BlackboardUpdate(); }; } @@ -368,9 +455,14 @@ namespace TNodeGraphViewImpl.Editor.NodeGraphView{ public BlackboardData GetBlackboardData(){ - return this._data.blackboardData; + if (IsRuntimeGraph){ + return _runtimeGraph.runtimeBlackboardData; + } + return _data.blackboardData; } + public bool IsRuntimeGraph{ get; set; } + public void SetGraphData(GraphData graph){ Data = graph as T; } diff --git a/TNodeGraphViewImpl/Editor/Search/BlackboardSearchWindowProvider.cs b/TNodeGraphViewImpl/Editor/Search/BlackboardSearchWindowProvider.cs index 35341c4..4eedc26 100644 --- a/TNodeGraphViewImpl/Editor/Search/BlackboardSearchWindowProvider.cs +++ b/TNodeGraphViewImpl/Editor/Search/BlackboardSearchWindowProvider.cs @@ -45,7 +45,6 @@ namespace TNode.Editor.Search{ } Debug.Log($"{list.Count}"); return list; - } public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context){