From a518dc284f43b5fb736ecd056aa2425a71a43cbe Mon Sep 17 00:00:00 2001 From: desktop-maesty/steve Date: Wed, 16 May 2018 20:54:36 +1000 Subject: [PATCH] VariableListAdapter no longer Rotorz FlowchartEditor uses an instance of one to handle using UnityEditorInternal ReorderableList FlowchartWindow caches a FlowchartEditor for the current flowchart so it can correctly use the new ReorderableList methods --- .../Fungus/Scripts/Editor/FlowchartEditor.cs | 116 +----------- .../Fungus/Scripts/Editor/FlowchartWindow.cs | 14 +- .../Scripts/Editor/VariableListAdaptor.cs | 169 ++++++++++-------- 3 files changed, 111 insertions(+), 188 deletions(-) diff --git a/Assets/Fungus/Scripts/Editor/FlowchartEditor.cs b/Assets/Fungus/Scripts/Editor/FlowchartEditor.cs index fe5a97dd..af903eb2 100644 --- a/Assets/Fungus/Scripts/Editor/FlowchartEditor.cs +++ b/Assets/Fungus/Scripts/Editor/FlowchartEditor.cs @@ -12,12 +12,6 @@ namespace Fungus.EditorUtils [CustomEditor (typeof(Flowchart))] public class FlowchartEditor : Editor { - protected class AddVariableInfo - { - public Flowchart flowchart; - public System.Type variableType; - } - protected SerializedProperty descriptionProp; protected SerializedProperty colorCommandsProp; protected SerializedProperty hideComponentsProp; @@ -31,6 +25,8 @@ namespace Fungus.EditorUtils protected SerializedProperty luaBindingNameProp; protected Texture2D addTexture; + + protected VariableListAdaptor variableListAdaptor; protected virtual void OnEnable() { @@ -50,6 +46,8 @@ namespace Fungus.EditorUtils luaBindingNameProp = serializedObject.FindProperty("luaBindingName"); addTexture = FungusEditorResources.AddSmall; + + variableListAdaptor = new VariableListAdaptor(variablesProp, 0, 0, target as Flowchart); } public override void OnInspectorGUI() @@ -122,8 +120,6 @@ namespace Fungus.EditorUtils } else { - Rect listRect = new Rect(); - // Remove any null variables from the list // Can sometimes happen when upgrading to a new version of Fungus (if .meta GUID changes for a variable class) for (int i = t.Variables.Count - 1; i >= 0; i--) @@ -134,114 +130,12 @@ namespace Fungus.EditorUtils } } - VariableListAdaptor.DrawVarList(w, variablesProp); - - listRect = GUILayoutUtility.GetLastRect(); - - float plusWidth = 32; - float plusHeight = 24; - - Rect buttonRect = listRect; - float buttonHeight = 24; - buttonRect.x = 4; - buttonRect.y -= buttonHeight - 1; - buttonRect.height = buttonHeight; - if (!Application.isPlaying) - { - buttonRect.width -= 30; - } - - if (showVariableToggleButton && GUI.Button(buttonRect, "Variables")) - { - t.VariablesExpanded = false; - } - - // Draw disclosure triangle - Rect lastRect = buttonRect; - lastRect.x += 5; - lastRect.y += 5; - - //this is not required, seems to be legacy that is hidden in the normal reorderable - if(showVariableToggleButton) - EditorGUI.Foldout(lastRect, true, ""); - - Rect plusRect = listRect; - plusRect.x += plusRect.width - plusWidth; - plusRect.y -= plusHeight - 1; - plusRect.width = plusWidth; - plusRect.height = plusHeight; - - if (!Application.isPlaying && - GUI.Button(plusRect, addTexture)) - { - GenericMenu menu = new GenericMenu (); - List types = FindAllDerivedTypes(); - - // Add variable types without a category - foreach (var type in types) - { - VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); - if (variableInfo == null || - variableInfo.Category != "") - { - continue; - } - - AddVariableInfo addVariableInfo = new AddVariableInfo(); - addVariableInfo.flowchart = t; - addVariableInfo.variableType = type; - - GUIContent typeName = new GUIContent(variableInfo.VariableType); - - menu.AddItem(typeName, false, AddVariable, addVariableInfo); - } - - // Add types with a category - foreach (var type in types) - { - VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); - if (variableInfo == null || - variableInfo.Category == "") - { - continue; - } - - AddVariableInfo info = new AddVariableInfo(); - info.flowchart = t; - info.variableType = type; - - GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType); - - menu.AddItem(typeName, false, AddVariable, info); - } - - menu.ShowAsContext (); - } + variableListAdaptor.DrawVarList(w); } serializedObject.ApplyModifiedProperties(); } - protected virtual void AddVariable(object obj) - { - AddVariableInfo addVariableInfo = obj as AddVariableInfo; - if (addVariableInfo == null) - { - return; - } - - var flowchart = addVariableInfo.flowchart; - System.Type variableType = addVariableInfo.variableType; - - Undo.RecordObject(flowchart, "Add Variable"); - Variable newVariable = flowchart.gameObject.AddComponent(variableType) as Variable; - newVariable.Key = flowchart.GetUniqueVariableKey(""); - flowchart.Variables.Add(newVariable); - - // Because this is an async call, we need to force prefab instances to record changes - PrefabUtility.RecordPrefabInstancePropertyModifications(flowchart); - } - public static List FindAllDerivedTypes() { return FindAllDerivedTypes(Assembly.GetAssembly(typeof(T))); diff --git a/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs b/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs index c21e042f..10ad7b85 100644 --- a/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs +++ b/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs @@ -135,6 +135,8 @@ namespace Fungus.EditorUtils protected Block dragBlock; protected static FungusState fungusState; + static FlowchartEditor flowchartEditor; + [MenuItem("Tools/Fungus/Flowchart Window")] static void Init() { @@ -222,7 +224,13 @@ namespace Fungus.EditorUtils var fs = Selection.activeGameObject.GetComponent(); if (fs != null) { - fungusState.SelectedFlowchart = fs; + //make sure we have a valid editor for this flowchart + if (fungusState.SelectedFlowchart != fs || fungusState.SelectedFlowchart == null || flowchartEditor == null) + { + DestroyImmediate(flowchartEditor); + flowchartEditor = Editor.CreateEditor(fs) as FlowchartEditor; + fungusState.SelectedFlowchart = fs; + } } } @@ -495,10 +503,8 @@ namespace Fungus.EditorUtils flowchart.VariablesScrollPos = GUILayout.BeginScrollView(flowchart.VariablesScrollPos); { GUILayout.Space(8); - - FlowchartEditor flowchartEditor = Editor.CreateEditor (flowchart) as FlowchartEditor; + flowchartEditor.DrawVariablesGUI(true, 0); - DestroyImmediate(flowchartEditor); Rect variableWindowRect = GUILayoutUtility.GetLastRect(); if (flowchart.VariablesExpanded && flowchart.Variables.Count > 0) diff --git a/Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs b/Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs index 0ec5bb07..a711adce 100644 --- a/Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs +++ b/Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs @@ -8,33 +8,32 @@ using UnityEngine; using UnityEditor; using System; -using Rotorz.ReorderableList; +using UnityEditorInternal; +using System.Collections.Generic; namespace Fungus.EditorUtils { - public class VariableListAdaptor : IReorderableListAdaptor + public class VariableListAdaptor { + protected class AddVariableInfo + { + public Flowchart flowchart; + public System.Type variableType; + } + public static readonly int DefaultWidth = 80 + 100 + 140 + 60; - public static readonly int ScrollSpacer = 8; - public static readonly int ReorderListSkirts = 70; + public static readonly int ScrollSpacer = 0; + public static readonly int ReorderListSkirts = 50; protected SerializedProperty _arrayProperty; public float fixedItemHeight; public int widthOfList; - - static public void DrawVarList(int w, SerializedProperty variablesProp) - { - ReorderableListGUI.Title("Variables"); - VariableListAdaptor adaptor = new VariableListAdaptor(variablesProp, 0, w == 0 ? VariableListAdaptor.DefaultWidth : w); - - ReorderableListFlags flags = ReorderableListFlags.DisableContextMenu | ReorderableListFlags.HideAddButton; - - ReorderableListControl.DrawControlFromState(adaptor, null, flags); - } - + private ReorderableList list; + private Flowchart targetFlowchart; + public SerializedProperty this[int index] { get { return _arrayProperty.GetArrayElementAtIndex(index); } @@ -45,88 +44,120 @@ namespace Fungus.EditorUtils get { return _arrayProperty; } } - public VariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight, int widthOfList) + public VariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight, int widthOfList, Flowchart _targetFlowchart) { if (arrayProperty == null) throw new ArgumentNullException("Array property was null."); if (!arrayProperty.isArray) throw new InvalidOperationException("Specified serialized propery is not an array."); + this.targetFlowchart = _targetFlowchart; this._arrayProperty = arrayProperty; this.fixedItemHeight = fixedItemHeight; this.widthOfList = widthOfList - ScrollSpacer; + list = new ReorderableList(arrayProperty.serializedObject, arrayProperty, true, false, true, true); + list.drawElementCallback = DrawItem; + list.onRemoveCallback = RemoveItem; + //list.drawHeaderCallback = DrawHeader; + list.onAddCallback = AddButton; + list.onRemoveCallback = RemoveItem; } - public VariableListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f, DefaultWidth) + private void RemoveItem(ReorderableList list) { + int index = list.index; + // Remove the Fungus Variable component + Variable variable = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Variable; + Undo.DestroyObjectImmediate(variable); } - public int Count + private void AddButton(ReorderableList list) { - get { return _arrayProperty.arraySize; } - } + GenericMenu menu = new GenericMenu(); + List types = FlowchartEditor.FindAllDerivedTypes(); - public virtual bool CanDrag(int index) - { - return true; - } + // Add variable types without a category + foreach (var type in types) + { + VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); + if (variableInfo == null || + variableInfo.Category != "") + { + continue; + } - public virtual bool CanRemove(int index) - { - return true; - } + AddVariableInfo addVariableInfo = new AddVariableInfo(); + addVariableInfo.flowchart = targetFlowchart; + addVariableInfo.variableType = type; - public void Add() - { - int newIndex = _arrayProperty.arraySize; - ++_arrayProperty.arraySize; - _arrayProperty.GetArrayElementAtIndex(newIndex).ResetValue(); - } + GUIContent typeName = new GUIContent(variableInfo.VariableType); - public void Insert(int index) - { - _arrayProperty.InsertArrayElementAtIndex(index); - _arrayProperty.GetArrayElementAtIndex(index).ResetValue(); - } + menu.AddItem(typeName, false, AddVariable, addVariableInfo); + } - public void Duplicate(int index) - { - _arrayProperty.InsertArrayElementAtIndex(index); - } + // Add types with a category + foreach (var type in types) + { + VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type); + if (variableInfo == null || + variableInfo.Category == "") + { + continue; + } - public void Remove(int index) - { - // Remove the Fungus Variable component - Variable variable = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Variable; - Undo.DestroyObjectImmediate(variable); + AddVariableInfo info = new AddVariableInfo(); + info.flowchart = targetFlowchart; + info.variableType = type; - _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = null; - _arrayProperty.DeleteArrayElementAtIndex(index); + GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType); + + menu.AddItem(typeName, false, AddVariable, info); + } + + menu.ShowAsContext(); } - public void Move(int sourceIndex, int destIndex) + protected virtual void AddVariable(object obj) { - if (destIndex > sourceIndex) - --destIndex; - _arrayProperty.MoveArrayElement(sourceIndex, destIndex); + AddVariableInfo addVariableInfo = obj as AddVariableInfo; + if (addVariableInfo == null) + { + return; + } + + var flowchart = addVariableInfo.flowchart; + System.Type variableType = addVariableInfo.variableType; + + Undo.RecordObject(flowchart, "Add Variable"); + Variable newVariable = flowchart.gameObject.AddComponent(variableType) as Variable; + newVariable.Key = flowchart.GetUniqueVariableKey(""); + flowchart.Variables.Add(newVariable); + + // Because this is an async call, we need to force prefab instances to record changes + PrefabUtility.RecordPrefabInstancePropertyModifications(flowchart); } - public void Clear() + private void DrawHeader(Rect rect) { - _arrayProperty.ClearArray(); + EditorGUI.PrefixLabel(rect, new GUIContent("Variables")); } + + public void DrawVarList(int w) + { + this.widthOfList = (w == 0 ? VariableListAdaptor.DefaultWidth : w) - ScrollSpacer; - public void BeginGUI() - { } - - public void EndGUI() - { } + if(GUILayout.Button("Variables")) + { + arrayProperty.isExpanded = !arrayProperty.isExpanded; + } - public virtual void DrawItemBackground(Rect position, int index) - { + if (arrayProperty.isExpanded) + { + list.DoLayoutList(); + } } - public void DrawItem(Rect position, int index) + public void DrawItem(Rect position, int index, bool selected, bool focused) { Variable variable = this[index].objectReferenceValue as Variable; @@ -162,7 +193,7 @@ namespace Fungus.EditorUtils return; } - var flowchart = FlowchartWindow.GetFlowchart(); + var flowchart = targetFlowchart; if (flowchart == null) { return; @@ -250,14 +281,6 @@ namespace Fungus.EditorUtils GUI.backgroundColor = Color.white; } - - public virtual float GetItemHeight(int index) - { - return fixedItemHeight != 0f - ? fixedItemHeight - : EditorGUI.GetPropertyHeight(this[index], GUIContent.none, false) - ; - } } }