Browse Source

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
master
desktop-maesty/steve 7 years ago
parent
commit
a518dc284f
  1. 116
      Assets/Fungus/Scripts/Editor/FlowchartEditor.cs
  2. 10
      Assets/Fungus/Scripts/Editor/FlowchartWindow.cs
  3. 151
      Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs

116
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;
@ -32,6 +26,8 @@ namespace Fungus.EditorUtils
protected Texture2D addTexture;
protected VariableListAdaptor variableListAdaptor;
protected virtual void OnEnable()
{
if (NullTargetCheck()) // Check for an orphaned editor instance
@ -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<System.Type> types = FindAllDerivedTypes<Variable>();
// 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<System.Type> FindAllDerivedTypes<T>()
{
return FindAllDerivedTypes<T>(Assembly.GetAssembly(typeof(T)));

10
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,9 +224,15 @@ namespace Fungus.EditorUtils
var fs = Selection.activeGameObject.GetComponent<Flowchart>();
if (fs != null)
{
//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;
}
}
}
return fungusState.SelectedFlowchart;
}
@ -496,9 +504,7 @@ namespace Fungus.EditorUtils
{
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)

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

@ -8,32 +8,31 @@
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]
{
@ -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<System.Type> types = FlowchartEditor.FindAllDerivedTypes<Variable>();
public virtual bool CanDrag(int index)
// Add variable types without a category
foreach (var type in types)
{
VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type);
if (variableInfo == null ||
variableInfo.Category != "")
{
return true;
continue;
}
public virtual bool CanRemove(int index)
{
return true;
AddVariableInfo addVariableInfo = new AddVariableInfo();
addVariableInfo.flowchart = targetFlowchart;
addVariableInfo.variableType = type;
GUIContent typeName = new GUIContent(variableInfo.VariableType);
menu.AddItem(typeName, false, AddVariable, addVariableInfo);
}
public void Add()
// Add types with a category
foreach (var type in types)
{
VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type);
if (variableInfo == null ||
variableInfo.Category == "")
{
int newIndex = _arrayProperty.arraySize;
++_arrayProperty.arraySize;
_arrayProperty.GetArrayElementAtIndex(newIndex).ResetValue();
continue;
}
public void Insert(int index)
{
_arrayProperty.InsertArrayElementAtIndex(index);
_arrayProperty.GetArrayElementAtIndex(index).ResetValue();
AddVariableInfo info = new AddVariableInfo();
info.flowchart = targetFlowchart;
info.variableType = type;
GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType);
menu.AddItem(typeName, false, AddVariable, info);
}
public void Duplicate(int index)
{
_arrayProperty.InsertArrayElementAtIndex(index);
menu.ShowAsContext();
}
public void Remove(int index)
protected virtual void AddVariable(object obj)
{
// Remove the Fungus Variable component
Variable variable = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Variable;
Undo.DestroyObjectImmediate(variable);
_arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = null;
_arrayProperty.DeleteArrayElementAtIndex(index);
AddVariableInfo addVariableInfo = obj as AddVariableInfo;
if (addVariableInfo == null)
{
return;
}
public void Move(int sourceIndex, int destIndex)
{
if (destIndex > sourceIndex)
--destIndex;
_arrayProperty.MoveArrayElement(sourceIndex, destIndex);
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 BeginGUI()
{ }
public void DrawVarList(int w)
{
this.widthOfList = (w == 0 ? VariableListAdaptor.DefaultWidth : w) - ScrollSpacer;
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)
;
}
}
}

Loading…
Cancel
Save