From bbf3c06d3c25404a393283f9c2e8c65b084bdb99 Mon Sep 17 00:00:00 2001 From: desktop-maesty/steve Date: Fri, 18 May 2018 08:33:12 +1000 Subject: [PATCH] CommandListAdapter now uses Unity ReorderableList not Rotorz --- Assets/Fungus/Scripts/Editor/BlockEditor.cs | 25 ++- .../Scripts/Editor/CommandListAdaptor.cs | 150 +++--------------- .../Scripts/Editor/VariableListAdaptor.cs | 14 +- 3 files changed, 43 insertions(+), 146 deletions(-) diff --git a/Assets/Fungus/Scripts/Editor/BlockEditor.cs b/Assets/Fungus/Scripts/Editor/BlockEditor.cs index 8d404755..d18b76d0 100644 --- a/Assets/Fungus/Scripts/Editor/BlockEditor.cs +++ b/Assets/Fungus/Scripts/Editor/BlockEditor.cs @@ -48,6 +48,9 @@ namespace Fungus.EditorUtils static List commandTypes; static List eventHandlerTypes; + private CommandListAdaptor commandListAdaptor; + private SerializedProperty commandListProperty; + static void CacheEventHandlerTypes() { eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).ToList(); @@ -62,12 +65,27 @@ namespace Fungus.EditorUtils protected virtual void OnEnable() { + //this appears to happen when leaving playmode + try + { + if (serializedObject == null) + return; + } + catch (Exception) + { + return; + } + upIcon = FungusEditorResources.Up; downIcon = FungusEditorResources.Down; addIcon = FungusEditorResources.Add; duplicateIcon = FungusEditorResources.Duplicate; deleteIcon = FungusEditorResources.Delete; + commandListProperty = serializedObject.FindProperty("commandList"); + + commandListAdaptor = new CommandListAdaptor(target as Block, commandListProperty); + CacheEventHandlerTypes(); } @@ -96,6 +114,8 @@ namespace Fungus.EditorUtils { serializedObject.Update(); + var block = target as Block; + // Execute any queued cut, copy, paste, etc. operations from the prevous GUI update // We need to defer applying these operations until the following update because // the ReorderableList control emits GUI errors if you clear the list in the same frame @@ -112,9 +132,6 @@ namespace Fungus.EditorUtils actionList.Clear(); } - var block = target as Block; - - SerializedProperty commandListProperty = serializedObject.FindProperty("commandList"); if (block == flowchart.SelectedBlock) { @@ -149,7 +166,7 @@ namespace Fungus.EditorUtils command.ParentBlock = block; } - CommandListAdaptor.DrawCommandList(block, commandListProperty); + commandListAdaptor.DrawCommandList(); // EventType.contextClick doesn't register since we moved the Block Editor to be inside // a GUI Area, no idea why. As a workaround we just check for right click instead. diff --git a/Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs b/Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs index d3c6aa98..9306c4da 100644 --- a/Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs +++ b/Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs @@ -1,42 +1,36 @@ // This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) -// Copyright (c) 2012-2013 Rotorz Limited. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - using UnityEngine; using UnityEditor; using System; -using Rotorz.ReorderableList; +using UnityEditorInternal; namespace Fungus.EditorUtils { - public class CommandListAdaptor : IReorderableListAdaptor { + public class CommandListAdaptor { - public static void DrawCommandList(Block block, SerializedProperty commandListProperty) + public void DrawCommandList() { - ReorderableListGUI.Title("Commands"); - CommandListAdaptor adaptor = new CommandListAdaptor(commandListProperty, 0); - adaptor.nodeRect = block._NodeRect; - - ReorderableListFlags flags = ReorderableListFlags.HideAddButton | ReorderableListFlags.HideRemoveButtons | ReorderableListFlags.DisableContextMenu; - if (block.CommandList.Count == 0) { EditorGUILayout.HelpBox("Press the + button below to add a command to the list.", MessageType.Info); } else { - ReorderableListControl.DrawControlFromState(adaptor, null, flags); + EditorGUI.indentLevel++; + list.DoLayoutList(); + EditorGUI.indentLevel--; } } protected SerializedProperty _arrayProperty; + + protected ReorderableList list; + + protected Block block; public float fixedItemHeight; - - public Rect nodeRect = new Rect(); public SerializedProperty this[int index] { get { return _arrayProperty.GetArrayElementAtIndex(index); } @@ -46,7 +40,7 @@ namespace Fungus.EditorUtils get { return _arrayProperty; } } - public CommandListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight) { + public CommandListAdaptor(Block _block, SerializedProperty arrayProperty, float fixedItemHeight = 0) { if (arrayProperty == null) throw new ArgumentNullException("Array property was null."); if (!arrayProperty.isArray) @@ -54,123 +48,19 @@ namespace Fungus.EditorUtils this._arrayProperty = arrayProperty; this.fixedItemHeight = fixedItemHeight; - } - - public CommandListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f) { - } - - public int Count { - get { return _arrayProperty.arraySize; } - } - - public virtual bool CanDrag(int index) { - return true; - } - - public virtual bool CanRemove(int index) { - return true; - } - - public void Add() { - Command newCommand = AddNewCommand(); - if (newCommand == null) - { - return; - } - - int newIndex = _arrayProperty.arraySize; - ++_arrayProperty.arraySize; - _arrayProperty.GetArrayElementAtIndex(newIndex).objectReferenceValue = newCommand; - } - - public void Insert(int index) { - Command newCommand = AddNewCommand(); - if (newCommand == null) - { - return; - } - - _arrayProperty.InsertArrayElementAtIndex(index); - _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = newCommand; - } - - Command AddNewCommand() - { - Flowchart flowchart = FlowchartWindow.GetFlowchart(); - if (flowchart == null) - { - return null; - } - - var block = flowchart.SelectedBlock; - if (block == null) - { - return null; - } - - var newCommand = Undo.AddComponent(block.gameObject) as Command; - newCommand.ItemId = flowchart.NextItemId(); - flowchart.ClearSelectedCommands(); - flowchart.AddSelectedCommand(newCommand); - - return newCommand; - } - - public void Duplicate(int index) { - - Command command = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Command; - - // Add the command as a new component - var parentBlock = command.GetComponent(); - - System.Type type = command.GetType(); - Command newCommand = Undo.AddComponent(parentBlock.gameObject, type) as Command; - newCommand.ItemId = newCommand.GetFlowchart().NextItemId(); - System.Reflection.FieldInfo[] fields = type.GetFields(); - foreach (System.Reflection.FieldInfo field in fields) - { - field.SetValue(newCommand, field.GetValue(command)); - } - - _arrayProperty.InsertArrayElementAtIndex(index); - _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = newCommand; - } - - public void Remove(int index) { - // Remove the Fungus Command component - Command command = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Command; - if (command != null) - { - Undo.DestroyObjectImmediate(command); - } - - _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = null; - _arrayProperty.DeleteArrayElementAtIndex(index); - } - - public void Move(int sourceIndex, int destIndex) { - if (destIndex > sourceIndex) - --destIndex; - _arrayProperty.MoveArrayElement(sourceIndex, destIndex); - } - - public void Clear() { - while (Count > 0) - { - Remove(0); - } - } + this.block = _block; - public void BeginGUI() - {} - - public void EndGUI() - {} + list = new ReorderableList(arrayProperty.serializedObject, arrayProperty, true, true, false, false); + list.drawHeaderCallback = DrawHeader; + list.drawElementCallback = DrawItem; + } - public void DrawItemBackground(Rect position, int index) { + private void DrawHeader(Rect rect) + { + EditorGUI.PrefixLabel(rect, new GUIContent("Commands")); } - public void DrawItem(Rect position, int index) + public void DrawItem(Rect position, int index, bool selected, bool focused) { Command command = this[index].objectReferenceValue as Command; diff --git a/Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs b/Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs index e4b19b3a..0471fd53 100644 --- a/Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs +++ b/Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs @@ -42,12 +42,7 @@ namespace Fungus.EditorUtils else return this[index].objectReferenceValue as Variable; } - - //public SerializedProperty arrayProperty - //{ - // get { return _arrayProperty; } - //} - + public VariableListAdaptor(SerializedProperty arrayProperty, Flowchart _targetFlowchart) { if (arrayProperty == null) @@ -59,10 +54,10 @@ namespace Fungus.EditorUtils this.fixedItemHeight = 0; this._arrayProperty = arrayProperty; 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; } @@ -141,11 +136,6 @@ namespace Fungus.EditorUtils PrefabUtility.RecordPrefabInstancePropertyModifications(flowchart); } - private void DrawHeader(Rect rect) - { - EditorGUI.PrefixLabel(rect, new GUIContent("Variables")); - } - public void DrawVarList(int w) { _arrayProperty.serializedObject.Update();