1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 09:16:01 +08:00

Fixed #128, #127, #64 - Added NodeEditorBase.OnCreate, OnGraphEditor.OnOpen, and NodeEditorBase.window

This commit is contained in:
Thor Brigsted 2019-04-06 13:27:44 +02:00
parent f6e0e3bc4d
commit 8adc4fd459
5 changed files with 26 additions and 9 deletions

View File

@ -269,7 +269,7 @@ namespace XNodeEditor {
} else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
if (!Selection.Contains(hoveredNode)) SelectNode(hoveredNode, false);
GenericMenu menu = new GenericMenu();
NodeEditor.GetEditor(hoveredNode).AddContextMenuItems(menu);
NodeEditor.GetEditor(hoveredNode, this).AddContextMenuItems(menu);
menu.DropDown(new Rect(Event.current.mousePosition, Vector2.zero));
e.Use(); // Fixes copy/paste context menu appearing in Unity 5.6.6f2 - doesn't occur in 2018.3.2f1 Probably needs to be used in other places.
} else if (!IsHoveringNode) {

View File

@ -14,10 +14,11 @@ namespace XNodeEditor.Internal {
/// <summary> Custom editors defined with [CustomNodeEditor] </summary>
private static Dictionary<Type, Type> editorTypes;
private static Dictionary<K, T> editors = new Dictionary<K, T>();
public NodeEditorWindow window;
public K target;
public SerializedObject serializedObject;
public static T GetEditor(K target) {
public static T GetEditor(K target, NodeEditorWindow window) {
if (target == null) return null;
T editor;
if (!editors.TryGetValue(target, out editor)) {
@ -26,9 +27,12 @@ namespace XNodeEditor.Internal {
editor = Activator.CreateInstance(editorType) as T;
editor.target = target;
editor.serializedObject = new SerializedObject(target);
editor.window = window;
editor.OnCreate();
editors.Add(target, editor);
}
if (editor.target == null) editor.target = target;
if (editor.window != window) editor.window = window;
if (editor.serializedObject == null) editor.serializedObject = new SerializedObject(target);
return editor;
}
@ -56,6 +60,9 @@ namespace XNodeEditor.Internal {
}
}
/// <summary> Called on creation, after references have been set </summary>
public virtual void OnCreate() { }
public interface INodeEditorAttrib {
Type GetInspectedType();
}

View File

@ -18,9 +18,7 @@ namespace XNodeEditor {
Event e = Event.current;
Matrix4x4 m = GUI.matrix;
if (graph == null) return;
graphEditor = NodeGraphEditor.GetEditor(graph);
graphEditor.position = position;
ValidateGraphEditor();
Controls();
DrawGrid(position, zoom, panOffset);
@ -288,7 +286,7 @@ namespace XNodeEditor {
_portConnectionPoints = _portConnectionPoints.Where(x => x.Key.node != node).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
NodeEditor nodeEditor = NodeEditor.GetEditor(node);
NodeEditor nodeEditor = NodeEditor.GetEditor(node, this);
NodeEditor.portPositions = new Dictionary<XNode.NodePort, Vector2>();

View File

@ -66,7 +66,7 @@ namespace XNodeEditor {
void OnFocus() {
current = this;
graphEditor = NodeGraphEditor.GetEditor(graph);
ValidateGraphEditor();
if (graphEditor != null && NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
}
@ -84,6 +84,15 @@ namespace XNodeEditor {
}
}
/// <summary> Make sure the graph editor is assigned and to the right object </summary>
private void ValidateGraphEditor() {
NodeGraphEditor graphEditor = NodeGraphEditor.GetEditor(graph, this);
if (this.graphEditor != graphEditor) {
this.graphEditor = graphEditor;
graphEditor.OnOpen();
}
}
/// <summary> Create editor window </summary>
public static NodeEditorWindow Init() {
NodeEditorWindow w = CreateInstance<NodeEditorWindow>();

View File

@ -8,13 +8,16 @@ namespace XNodeEditor {
/// <summary> Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. </summary>
[CustomNodeGraphEditor(typeof(XNode.NodeGraph))]
public class NodeGraphEditor : XNodeEditor.Internal.NodeEditorBase<NodeGraphEditor, NodeGraphEditor.CustomNodeGraphEditorAttribute, XNode.NodeGraph> {
/// <summary> The position of the window in screen space. </summary>
public Rect position;
[Obsolete("Use window.position instead")]
public Rect position { get { return window.position; } set { window.position = value; } }
/// <summary> Are we currently renaming a node? </summary>
protected bool isRenaming;
public virtual void OnGUI() { }
/// <summary> Called when opened by NodeEditorWindow </summary>
public virtual void OnOpen() { }
public virtual Texture2D GetGridTexture() {
return NodeEditorPreferences.GetSettings().gridTexture;
}