diff --git a/Scripts/Attributes.meta b/Scripts/Attributes.meta new file mode 100644 index 0000000..c0be849 --- /dev/null +++ b/Scripts/Attributes.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 5644dfc7eed151045af664a9d4fd1906 +folderAsset: yes +timeCreated: 1541633926 +licenseType: Free +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Attributes/NodeEnum.cs b/Scripts/Attributes/NodeEnum.cs new file mode 100644 index 0000000..9a5c12a --- /dev/null +++ b/Scripts/Attributes/NodeEnum.cs @@ -0,0 +1,4 @@ +using UnityEngine; + +/// Draw enums correctly within nodes. Without it, enums show up at the wrong positions. +public class NodeEnumAttribute : PropertyAttribute { } \ No newline at end of file diff --git a/Scripts/Attributes/NodeEnum.cs.meta b/Scripts/Attributes/NodeEnum.cs.meta new file mode 100644 index 0000000..813a80b --- /dev/null +++ b/Scripts/Attributes/NodeEnum.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 10a8338f6c985854697b35459181af0a +timeCreated: 1541633942 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Editor/Drawers.meta b/Scripts/Editor/Drawers.meta new file mode 100644 index 0000000..b69e0ac --- /dev/null +++ b/Scripts/Editor/Drawers.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: 7adf21edfb51f514fa991d7556ecd0ef +folderAsset: yes +timeCreated: 1541971984 +licenseType: Free +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Editor/Drawers/NodeEnumDrawer.cs b/Scripts/Editor/Drawers/NodeEnumDrawer.cs new file mode 100644 index 0000000..cdb6511 --- /dev/null +++ b/Scripts/Editor/Drawers/NodeEnumDrawer.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; +using XNode; +using XNodeEditor; + +namespace XNodeEditor { + [CustomPropertyDrawer(typeof(NodeEnumAttribute))] + public class NodeEnumDrawer : PropertyDrawer { + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { + EditorGUI.BeginProperty(position, label, property); + + // Throw error on wrong type + if (property.propertyType != SerializedPropertyType.Enum) { + throw new ArgumentException("Parameter selected must be of type System.Enum"); + } + + // Add label + position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); + + // Get current enum name + string enumName = ""; + if (property.enumValueIndex >= 0 && property.enumValueIndex < property.enumDisplayNames.Length) enumName = property.enumDisplayNames[property.enumValueIndex]; + + // Display dropdown + if (EditorGUI.DropdownButton(position, new GUIContent(enumName), FocusType.Passive)) { + // Position is all wrong if we show the dropdown during the node draw phase. + // Instead, add it to onLateGUI to display it later. + NodeEditorWindow.current.onLateGUI += () => ShowContextMenuAtMouse(property); + } + EditorGUI.EndProperty(); + } + + private void ShowContextMenuAtMouse(SerializedProperty property) { + // Initialize menu + GenericMenu menu = new GenericMenu(); + + // Add all enum display names to menu + for (int i = 0; i < property.enumDisplayNames.Length; i++) { + int index = i; + menu.AddItem(new GUIContent(property.enumDisplayNames[i]), false, () => SetEnum(property, index)); + } + + // Display at cursor position + Rect r = new Rect(Event.current.mousePosition, new Vector2(0, 0)); + menu.DropDown(r); + } + + private void SetEnum(SerializedProperty property, int index) { + property.enumValueIndex = index; + property.serializedObject.ApplyModifiedProperties(); + } + } +} \ No newline at end of file diff --git a/Scripts/Editor/Drawers/NodeEnumDrawer.cs.meta b/Scripts/Editor/Drawers/NodeEnumDrawer.cs.meta new file mode 100644 index 0000000..beacf6b --- /dev/null +++ b/Scripts/Editor/Drawers/NodeEnumDrawer.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 83db81f92abadca439507e25d517cabe +timeCreated: 1541633798 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index dd74a01..34fa6a7 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -11,6 +11,8 @@ namespace XNodeEditor { private List selectionCache; private List culledNodes; private int topPadding { get { return isDocked() ? 19 : 22; } } + /// Executed after all other window GUI. Useful if Zoom is ruining your day. Automatically resets after being run. + public event Action onLateGUI; private void OnGUI() { Event e = Event.current; @@ -29,6 +31,12 @@ namespace XNodeEditor { DrawTooltip(); graphEditor.OnGUI(); + // Run and reset onLateGUI + if (onLateGUI != null) { + onLateGUI(); + onLateGUI = null; + } + GUI.matrix = m; }