From 7de3b8158cdb9a78f5079d13d237f5a837dca62f Mon Sep 17 00:00:00 2001 From: taoria <445625470@qq.com> Date: Mon, 27 Jun 2022 16:55:23 +0800 Subject: [PATCH] 1.create search panel when right click --- TNode/Attribute/NodeAttribute.cs | 13 +++++++++++ TNode/Attribute/NodeAttribute.cs.meta | 3 +++ TNode/Editor/BaseViews/DataGraphView.cs | 23 +++++++++++++++---- TNode/Editor/BaseViews/NodeView.cs | 2 +- TNode/Editor/GraphEditor.cs | 21 +++++++++++++++++ TNode/Editor/GraphEditorData.cs | 2 +- TNode/Editor/SearchWindowProvider.cs | 18 +++++++++++++++ TNode/Editor/SearchWindowProvider.cs.meta | 3 +++ .../GraphEditorCreator/GraphEditorCreator.cs | 2 +- TNode/Models/NodeLink.cs | 2 +- 10 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 TNode/Attribute/NodeAttribute.cs create mode 100644 TNode/Attribute/NodeAttribute.cs.meta create mode 100644 TNode/Editor/SearchWindowProvider.cs create mode 100644 TNode/Editor/SearchWindowProvider.cs.meta diff --git a/TNode/Attribute/NodeAttribute.cs b/TNode/Attribute/NodeAttribute.cs new file mode 100644 index 0000000..f27663f --- /dev/null +++ b/TNode/Attribute/NodeAttribute.cs @@ -0,0 +1,13 @@ +using JetBrains.Annotations; +using TNode.Models; + +namespace TNode.Attribute{ + [MeansImplicitUse] + [BaseTypeRequired(typeof(NodeData))] + + public class NodeAttribute:System.Attribute{ + public NodeAttribute(GraphData graphData){ + + } + } +} \ No newline at end of file diff --git a/TNode/Attribute/NodeAttribute.cs.meta b/TNode/Attribute/NodeAttribute.cs.meta new file mode 100644 index 0000000..2950fdf --- /dev/null +++ b/TNode/Attribute/NodeAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2311dd22c61449b7aa44b37085623fc4 +timeCreated: 1656298063 \ No newline at end of file diff --git a/TNode/Editor/BaseViews/DataGraphView.cs b/TNode/Editor/BaseViews/DataGraphView.cs index a0ce812..b0e3493 100644 --- a/TNode/Editor/BaseViews/DataGraphView.cs +++ b/TNode/Editor/BaseViews/DataGraphView.cs @@ -1,7 +1,9 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using TNode.BaseViews; using TNode.Cache; using TNode.Models; +using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEngine; using UnityEngine.UIElements; @@ -28,10 +30,10 @@ namespace TNode.Editor.BaseViews{ RegisterCallback(evt => { var pos = evt.mousePosition; - evt.menu.AppendAction("Add Node", (dropMenuAction) => { + evt.menu.AppendAction("Add NodeAttribute", (dropMenuAction) => { DialogueNodeView nodeView = new DialogueNodeView{ GUID = Guid.NewGuid().ToString(), - title = "New Node" + title = "New NodeAttribute" }; // make it a 200x100 box nodeView.SetPosition(new Rect(pos.x - 100, pos.y - 50, 200, 100)); @@ -102,7 +104,7 @@ namespace TNode.Editor.BaseViews{ */ public abstract class DataGraphView:GraphView where T:GraphData{ private T _data; - + private SearchWindowProvider _searchWindowProvider; public T Data{ get{ return _data; } set{ @@ -128,7 +130,7 @@ namespace TNode.Editor.BaseViews{ //Get the node type var nodeType = dataNode.GetType(); - //Get the derived type of Node View from the node type + //Get the derived type of NodeAttribute View from the node type var nodeViewType = typeof(NodeView<>).MakeGenericType(nodeType); //Fetch the node view from the node view type @@ -156,7 +158,18 @@ namespace TNode.Editor.BaseViews{ public event DataChangedEventHandler OnDataChanged; public delegate void DataChangedEventHandler(object sender, DataChangedEventArgs e); + + private void ConstructDefaultBehaviour(){ + //Register a right click context menu + //ConstructContextualMenuOption(); + } + + public void ConstructViewContextualMenu(EventCallback callback){ + RegisterCallback(callback); + } + private void OnInit(){ + ConstructDefaultBehaviour(); OnGraphViewCreate(); } diff --git a/TNode/Editor/BaseViews/NodeView.cs b/TNode/Editor/BaseViews/NodeView.cs index a2596f8..60777c4 100644 --- a/TNode/Editor/BaseViews/NodeView.cs +++ b/TNode/Editor/BaseViews/NodeView.cs @@ -4,7 +4,7 @@ using UnityEditor.Experimental.GraphView; namespace TNode.BaseViews{ - //A Node monitor some type of node in the graph + //A NodeAttribute monitor some type of node in the graph public abstract class NodeView : Node where T:NodeData,new(){ protected T _data; diff --git a/TNode/Editor/GraphEditor.cs b/TNode/Editor/GraphEditor.cs index 1aa89e8..24b0f55 100644 --- a/TNode/Editor/GraphEditor.cs +++ b/TNode/Editor/GraphEditor.cs @@ -5,6 +5,7 @@ using TNode.Editor.BaseViews; using TNode.Editor.Model; using TNode.Models; using UnityEditor; +using UnityEditor.Experimental.GraphView; using UnityEngine; using UnityEngine.Serialization; using UnityEngine.UIElements; @@ -38,6 +39,26 @@ namespace TNode.Editor{ _graphView = NodeEditorExtensions.CreateInstance>(); rootVisualElement.Add(_graphView); _graphView.StretchToParentSize(); + + + _graphView.ConstructViewContextualMenu(evt => { + + //Current issue is that the search window don't show up at the exact position of the mouse click by dma.eventInfo.mousePosition + //So I have to manually set the position of the search window to fit the mouse click position by add an offset driven by Editor's position + //Maybe a better way exists to fix this issue + Vector2 editorPosition = this.position.position; + + + evt.menu.AppendAction("Create Node", dma => { + var dmaPos = dma.eventInfo.mousePosition+editorPosition; + SearchWindowContext searchWindowContext = new SearchWindowContext(dmaPos,200,200); + SearchWindow.Open(searchWindowContext, ScriptableObject.CreateInstance()); + }); + }); + } + private void ConstructSearchWindow(){ + //Register a search window + } private void DefineGraphEditorActions(){ diff --git a/TNode/Editor/GraphEditorData.cs b/TNode/Editor/GraphEditorData.cs index 8d17f52..20f999c 100644 --- a/TNode/Editor/GraphEditorData.cs +++ b/TNode/Editor/GraphEditorData.cs @@ -4,7 +4,7 @@ using UnityEngine; namespace TNode.Editor{ - [CreateAssetMenu(fileName = "Node Editor Config", menuName = "TNode/Node Editor Config")] + [CreateAssetMenu(fileName = "NodeAttribute Editor Config", menuName = "TNode/NodeAttribute Editor Config")] public class GraphEditorData:ScriptableObject{ public List nodesData; diff --git a/TNode/Editor/SearchWindowProvider.cs b/TNode/Editor/SearchWindowProvider.cs new file mode 100644 index 0000000..3073585 --- /dev/null +++ b/TNode/Editor/SearchWindowProvider.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using UnityEditor.Experimental.GraphView; +using UnityEngine; + +namespace TNode.Editor{ + public class SearchWindowProvider:ScriptableObject,ISearchWindowProvider{ + public List CreateSearchTree(SearchWindowContext context){ + var list = new List(); + list.Add(new SearchTreeGroupEntry(new GUIContent("Add New Node"), 0)); + list.Add(new SearchTreeGroupEntry(new GUIContent("Add Placemat"), 0)); + return list; + } + + public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context){ + return false; + } + } +} \ No newline at end of file diff --git a/TNode/Editor/SearchWindowProvider.cs.meta b/TNode/Editor/SearchWindowProvider.cs.meta new file mode 100644 index 0000000..d5f817d --- /dev/null +++ b/TNode/Editor/SearchWindowProvider.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1daac5c2c1794f36933cdbc922840769 +timeCreated: 1656314210 \ No newline at end of file diff --git a/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs b/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs index 5387b68..f245737 100644 --- a/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs +++ b/TNode/Editor/Tools/GraphEditorCreator/GraphEditorCreator.cs @@ -138,7 +138,7 @@ namespace TNode.Editor.Tools.GraphEditorCreator{ EditorApplication.update(); } - //Create an Node Editor Data Instance for the new graph editor + //Create an NodeAttribute Editor Data Instance for the new graph editor NodeEditorData nodeEditorData = ScriptableObject.CreateInstance(); nodeEditorData.name = editorName; EditorUtility.SetDirty(nodeEditorData); diff --git a/TNode/Models/NodeLink.cs b/TNode/Models/NodeLink.cs index 55266e8..810f3d9 100644 --- a/TNode/Models/NodeLink.cs +++ b/TNode/Models/NodeLink.cs @@ -2,7 +2,7 @@ using Dialogue; namespace TNode.Models{ - //Node links are stored in output side of the two node port. + //NodeAttribute links are stored in output side of the two node port. [Serializable] public class NodeLink{ // public DialogueNodePortData From{ get; }