Browse Source

CommandListAdapter now uses Unity ReorderableList not Rotorz

master
desktop-maesty/steve 7 years ago
parent
commit
bbf3c06d3c
  1. 25
      Assets/Fungus/Scripts/Editor/BlockEditor.cs
  2. 146
      Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs
  3. 12
      Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs

25
Assets/Fungus/Scripts/Editor/BlockEditor.cs

@ -48,6 +48,9 @@ namespace Fungus.EditorUtils
static List<System.Type> commandTypes; static List<System.Type> commandTypes;
static List<System.Type> eventHandlerTypes; static List<System.Type> eventHandlerTypes;
private CommandListAdaptor commandListAdaptor;
private SerializedProperty commandListProperty;
static void CacheEventHandlerTypes() static void CacheEventHandlerTypes()
{ {
eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).ToList(); eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).ToList();
@ -62,12 +65,27 @@ namespace Fungus.EditorUtils
protected virtual void OnEnable() protected virtual void OnEnable()
{ {
//this appears to happen when leaving playmode
try
{
if (serializedObject == null)
return;
}
catch (Exception)
{
return;
}
upIcon = FungusEditorResources.Up; upIcon = FungusEditorResources.Up;
downIcon = FungusEditorResources.Down; downIcon = FungusEditorResources.Down;
addIcon = FungusEditorResources.Add; addIcon = FungusEditorResources.Add;
duplicateIcon = FungusEditorResources.Duplicate; duplicateIcon = FungusEditorResources.Duplicate;
deleteIcon = FungusEditorResources.Delete; deleteIcon = FungusEditorResources.Delete;
commandListProperty = serializedObject.FindProperty("commandList");
commandListAdaptor = new CommandListAdaptor(target as Block, commandListProperty);
CacheEventHandlerTypes(); CacheEventHandlerTypes();
} }
@ -96,6 +114,8 @@ namespace Fungus.EditorUtils
{ {
serializedObject.Update(); serializedObject.Update();
var block = target as Block;
// Execute any queued cut, copy, paste, etc. operations from the prevous GUI update // 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 // 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 // the ReorderableList control emits GUI errors if you clear the list in the same frame
@ -112,9 +132,6 @@ namespace Fungus.EditorUtils
actionList.Clear(); actionList.Clear();
} }
var block = target as Block;
SerializedProperty commandListProperty = serializedObject.FindProperty("commandList");
if (block == flowchart.SelectedBlock) if (block == flowchart.SelectedBlock)
{ {
@ -149,7 +166,7 @@ namespace Fungus.EditorUtils
command.ParentBlock = block; command.ParentBlock = block;
} }
CommandListAdaptor.DrawCommandList(block, commandListProperty); commandListAdaptor.DrawCommandList();
// EventType.contextClick doesn't register since we moved the Block Editor to be inside // 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. // a GUI Area, no idea why. As a workaround we just check for right click instead.

146
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). // 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) // 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 UnityEngine;
using UnityEditor; using UnityEditor;
using System; using System;
using Rotorz.ReorderableList; using UnityEditorInternal;
namespace Fungus.EditorUtils 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) if (block.CommandList.Count == 0)
{ {
EditorGUILayout.HelpBox("Press the + button below to add a command to the list.", MessageType.Info); EditorGUILayout.HelpBox("Press the + button below to add a command to the list.", MessageType.Info);
} }
else else
{ {
ReorderableListControl.DrawControlFromState(adaptor, null, flags); EditorGUI.indentLevel++;
list.DoLayoutList();
EditorGUI.indentLevel--;
} }
} }
protected SerializedProperty _arrayProperty; protected SerializedProperty _arrayProperty;
public float fixedItemHeight; protected ReorderableList list;
public Rect nodeRect = new Rect(); protected Block block;
public float fixedItemHeight;
public SerializedProperty this[int index] { public SerializedProperty this[int index] {
get { return _arrayProperty.GetArrayElementAtIndex(index); } get { return _arrayProperty.GetArrayElementAtIndex(index); }
@ -46,7 +40,7 @@ namespace Fungus.EditorUtils
get { return _arrayProperty; } get { return _arrayProperty; }
} }
public CommandListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight) { public CommandListAdaptor(Block _block, SerializedProperty arrayProperty, float fixedItemHeight = 0) {
if (arrayProperty == null) if (arrayProperty == null)
throw new ArgumentNullException("Array property was null."); throw new ArgumentNullException("Array property was null.");
if (!arrayProperty.isArray) if (!arrayProperty.isArray)
@ -54,123 +48,19 @@ namespace Fungus.EditorUtils
this._arrayProperty = arrayProperty; this._arrayProperty = arrayProperty;
this.fixedItemHeight = fixedItemHeight; this.fixedItemHeight = fixedItemHeight;
} this.block = _block;
public CommandListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f) { list = new ReorderableList(arrayProperty.serializedObject, arrayProperty, true, true, false, false);
list.drawHeaderCallback = DrawHeader;
list.drawElementCallback = DrawItem;
} }
public int Count { private void DrawHeader(Rect rect)
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; EditorGUI.PrefixLabel(rect, new GUIContent("Commands"));
}
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<Comment>(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<Block>();
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);
}
}
public void BeginGUI()
{}
public void EndGUI()
{}
public void DrawItemBackground(Rect position, int index) {
} }
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; Command command = this[index].objectReferenceValue as Command;

12
Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs

@ -43,11 +43,6 @@ namespace Fungus.EditorUtils
return this[index].objectReferenceValue as Variable; return this[index].objectReferenceValue as Variable;
} }
//public SerializedProperty arrayProperty
//{
// get { return _arrayProperty; }
//}
public VariableListAdaptor(SerializedProperty arrayProperty, Flowchart _targetFlowchart) public VariableListAdaptor(SerializedProperty arrayProperty, Flowchart _targetFlowchart)
{ {
if (arrayProperty == null) if (arrayProperty == null)
@ -59,10 +54,10 @@ namespace Fungus.EditorUtils
this.fixedItemHeight = 0; this.fixedItemHeight = 0;
this._arrayProperty = arrayProperty; this._arrayProperty = arrayProperty;
this.widthOfList = widthOfList - ScrollSpacer; this.widthOfList = widthOfList - ScrollSpacer;
list = new ReorderableList(arrayProperty.serializedObject, arrayProperty, true, false, true, true); list = new ReorderableList(arrayProperty.serializedObject, arrayProperty, true, false, true, true);
list.drawElementCallback = DrawItem; list.drawElementCallback = DrawItem;
list.onRemoveCallback = RemoveItem; list.onRemoveCallback = RemoveItem;
//list.drawHeaderCallback = DrawHeader;
list.onAddCallback = AddButton; list.onAddCallback = AddButton;
list.onRemoveCallback = RemoveItem; list.onRemoveCallback = RemoveItem;
} }
@ -141,11 +136,6 @@ namespace Fungus.EditorUtils
PrefabUtility.RecordPrefabInstancePropertyModifications(flowchart); PrefabUtility.RecordPrefabInstancePropertyModifications(flowchart);
} }
private void DrawHeader(Rect rect)
{
EditorGUI.PrefixLabel(rect, new GUIContent("Variables"));
}
public void DrawVarList(int w) public void DrawVarList(int w)
{ {
_arrayProperty.serializedObject.Update(); _arrayProperty.serializedObject.Update();

Loading…
Cancel
Save