NodeEditorBase.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEngine;
  7. #if ODIN_INSPECTOR
  8. using Sirenix.OdinInspector.Editor;
  9. #endif
  10. namespace XNodeEditor.Internal {
  11. /// <summary> Handles caching of custom editor classes and their target types. Accessible with GetEditor(Type type) </summary>
  12. /// <typeparam name="T">Editor Type. Should be the type of the deriving script itself (eg. NodeEditor) </typeparam>
  13. /// <typeparam name="A">Attribute Type. The attribute used to connect with the runtime type (eg. CustomNodeEditorAttribute) </typeparam>
  14. /// <typeparam name="K">Runtime Type. The ScriptableObject this can be an editor for (eg. Node) </typeparam>
  15. public abstract class NodeEditorBase<T, A, K> where A : Attribute, NodeEditorBase<T, A, K>.INodeEditorAttrib where T : NodeEditorBase<T, A, K> where K : ScriptableObject {
  16. /// <summary> Custom editors defined with [CustomNodeEditor] </summary>
  17. private static Dictionary<Type, Type> editorTypes;
  18. private static Dictionary<K, T> editors = new Dictionary<K, T>();
  19. public NodeEditorWindow window;
  20. public K target;
  21. public SerializedObject serializedObject;
  22. #if ODIN_INSPECTOR
  23. private PropertyTree _objectTree;
  24. public PropertyTree objectTree {
  25. get {
  26. if (this._objectTree == null) {
  27. try {
  28. bool wasInEditor = NodeEditor.inNodeEditor;
  29. NodeEditor.inNodeEditor = true;
  30. this._objectTree = PropertyTree.Create(this.serializedObject);
  31. NodeEditor.inNodeEditor = wasInEditor;
  32. } catch (ArgumentException ex) {
  33. Debug.Log(ex);
  34. }
  35. }
  36. return this._objectTree;
  37. }
  38. }
  39. #endif
  40. public static T GetEditor(K target, NodeEditorWindow window) {
  41. if (target == null) return null;
  42. T editor;
  43. if (!editors.TryGetValue(target, out editor)) {
  44. Type type = target.GetType();
  45. Type editorType = GetEditorType(type);
  46. editor = Activator.CreateInstance(editorType) as T;
  47. editor.target = target;
  48. editor.serializedObject = new SerializedObject(target);
  49. editor.window = window;
  50. editor.OnCreate();
  51. editors.Add(target, editor);
  52. }
  53. if (editor.target == null) editor.target = target;
  54. if (editor.window != window) editor.window = window;
  55. if (editor.serializedObject == null) editor.serializedObject = new SerializedObject(target);
  56. return editor;
  57. }
  58. private static Type GetEditorType(Type type) {
  59. if (type == null) return null;
  60. if (editorTypes == null) CacheCustomEditors();
  61. Type result;
  62. if (editorTypes.TryGetValue(type, out result)) return result;
  63. //If type isn't found, try base type
  64. return GetEditorType(type.BaseType);
  65. }
  66. private static void CacheCustomEditors() {
  67. editorTypes = new Dictionary<Type, Type>();
  68. //Get all classes deriving from NodeEditor via reflection
  69. Type[] nodeEditors = typeof(T).GetDerivedTypes();
  70. for (int i = 0; i < nodeEditors.Length; i++) {
  71. if (nodeEditors[i].IsAbstract) continue;
  72. var attribs = nodeEditors[i].GetCustomAttributes(typeof(A), false);
  73. if (attribs == null || attribs.Length == 0) continue;
  74. A attrib = attribs[0] as A;
  75. editorTypes.Add(attrib.GetInspectedType(), nodeEditors[i]);
  76. }
  77. }
  78. /// <summary> Called on creation, after references have been set </summary>
  79. public virtual void OnCreate() { }
  80. public interface INodeEditorAttrib {
  81. Type GetInspectedType();
  82. }
  83. }
  84. }