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. 14
      Assets/Fungus/Scripts/Editor/FlowchartWindow.cs
  3. 169
      Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs

116
Assets/Fungus/Scripts/Editor/FlowchartEditor.cs

@ -12,12 +12,6 @@ namespace Fungus.EditorUtils
[CustomEditor (typeof(Flowchart))] [CustomEditor (typeof(Flowchart))]
public class FlowchartEditor : Editor public class FlowchartEditor : Editor
{ {
protected class AddVariableInfo
{
public Flowchart flowchart;
public System.Type variableType;
}
protected SerializedProperty descriptionProp; protected SerializedProperty descriptionProp;
protected SerializedProperty colorCommandsProp; protected SerializedProperty colorCommandsProp;
protected SerializedProperty hideComponentsProp; protected SerializedProperty hideComponentsProp;
@ -31,6 +25,8 @@ namespace Fungus.EditorUtils
protected SerializedProperty luaBindingNameProp; protected SerializedProperty luaBindingNameProp;
protected Texture2D addTexture; protected Texture2D addTexture;
protected VariableListAdaptor variableListAdaptor;
protected virtual void OnEnable() protected virtual void OnEnable()
{ {
@ -50,6 +46,8 @@ namespace Fungus.EditorUtils
luaBindingNameProp = serializedObject.FindProperty("luaBindingName"); luaBindingNameProp = serializedObject.FindProperty("luaBindingName");
addTexture = FungusEditorResources.AddSmall; addTexture = FungusEditorResources.AddSmall;
variableListAdaptor = new VariableListAdaptor(variablesProp, 0, 0, target as Flowchart);
} }
public override void OnInspectorGUI() public override void OnInspectorGUI()
@ -122,8 +120,6 @@ namespace Fungus.EditorUtils
} }
else else
{ {
Rect listRect = new Rect();
// Remove any null variables from the list // 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) // 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--) for (int i = t.Variables.Count - 1; i >= 0; i--)
@ -134,114 +130,12 @@ namespace Fungus.EditorUtils
} }
} }
VariableListAdaptor.DrawVarList(w, variablesProp); variableListAdaptor.DrawVarList(w);
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 ();
}
} }
serializedObject.ApplyModifiedProperties(); 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>() public static List<System.Type> FindAllDerivedTypes<T>()
{ {
return FindAllDerivedTypes<T>(Assembly.GetAssembly(typeof(T))); return FindAllDerivedTypes<T>(Assembly.GetAssembly(typeof(T)));

14
Assets/Fungus/Scripts/Editor/FlowchartWindow.cs

@ -135,6 +135,8 @@ namespace Fungus.EditorUtils
protected Block dragBlock; protected Block dragBlock;
protected static FungusState fungusState; protected static FungusState fungusState;
static FlowchartEditor flowchartEditor;
[MenuItem("Tools/Fungus/Flowchart Window")] [MenuItem("Tools/Fungus/Flowchart Window")]
static void Init() static void Init()
{ {
@ -222,7 +224,13 @@ namespace Fungus.EditorUtils
var fs = Selection.activeGameObject.GetComponent<Flowchart>(); var fs = Selection.activeGameObject.GetComponent<Flowchart>();
if (fs != null) 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); flowchart.VariablesScrollPos = GUILayout.BeginScrollView(flowchart.VariablesScrollPos);
{ {
GUILayout.Space(8); GUILayout.Space(8);
FlowchartEditor flowchartEditor = Editor.CreateEditor (flowchart) as FlowchartEditor;
flowchartEditor.DrawVariablesGUI(true, 0); flowchartEditor.DrawVariablesGUI(true, 0);
DestroyImmediate(flowchartEditor);
Rect variableWindowRect = GUILayoutUtility.GetLastRect(); Rect variableWindowRect = GUILayoutUtility.GetLastRect();
if (flowchart.VariablesExpanded && flowchart.Variables.Count > 0) if (flowchart.VariablesExpanded && flowchart.Variables.Count > 0)

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

@ -8,33 +8,32 @@
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
using System; using System;
using Rotorz.ReorderableList; using UnityEditorInternal;
using System.Collections.Generic;
namespace Fungus.EditorUtils 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 DefaultWidth = 80 + 100 + 140 + 60;
public static readonly int ScrollSpacer = 8; public static readonly int ScrollSpacer = 0;
public static readonly int ReorderListSkirts = 70; public static readonly int ReorderListSkirts = 50;
protected SerializedProperty _arrayProperty; protected SerializedProperty _arrayProperty;
public float fixedItemHeight; public float fixedItemHeight;
public int widthOfList; public int widthOfList;
private ReorderableList list;
static public void DrawVarList(int w, SerializedProperty variablesProp) private Flowchart targetFlowchart;
{
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);
}
public SerializedProperty this[int index] public SerializedProperty this[int index]
{ {
get { return _arrayProperty.GetArrayElementAtIndex(index); } get { return _arrayProperty.GetArrayElementAtIndex(index); }
@ -45,88 +44,120 @@ namespace Fungus.EditorUtils
get { return _arrayProperty; } get { return _arrayProperty; }
} }
public VariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight, int widthOfList) public VariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight, int widthOfList, Flowchart _targetFlowchart)
{ {
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)
throw new InvalidOperationException("Specified serialized propery is not an array."); throw new InvalidOperationException("Specified serialized propery is not an array.");
this.targetFlowchart = _targetFlowchart;
this._arrayProperty = arrayProperty; this._arrayProperty = arrayProperty;
this.fixedItemHeight = fixedItemHeight; this.fixedItemHeight = fixedItemHeight;
this.widthOfList = widthOfList - ScrollSpacer; 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)
return true; {
} VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type);
if (variableInfo == null ||
variableInfo.Category != "")
{
continue;
}
public virtual bool CanRemove(int index) AddVariableInfo addVariableInfo = new AddVariableInfo();
{ addVariableInfo.flowchart = targetFlowchart;
return true; addVariableInfo.variableType = type;
}
public void Add() GUIContent typeName = new GUIContent(variableInfo.VariableType);
{
int newIndex = _arrayProperty.arraySize;
++_arrayProperty.arraySize;
_arrayProperty.GetArrayElementAtIndex(newIndex).ResetValue();
}
public void Insert(int index) menu.AddItem(typeName, false, AddVariable, addVariableInfo);
{ }
_arrayProperty.InsertArrayElementAtIndex(index);
_arrayProperty.GetArrayElementAtIndex(index).ResetValue();
}
public void Duplicate(int index) // Add types with a category
{ foreach (var type in types)
_arrayProperty.InsertArrayElementAtIndex(index); {
} VariableInfoAttribute variableInfo = VariableEditor.GetVariableInfo(type);
if (variableInfo == null ||
variableInfo.Category == "")
{
continue;
}
public void Remove(int index) AddVariableInfo info = new AddVariableInfo();
{ info.flowchart = targetFlowchart;
// Remove the Fungus Variable component info.variableType = type;
Variable variable = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Variable;
Undo.DestroyObjectImmediate(variable);
_arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = null; GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType);
_arrayProperty.DeleteArrayElementAtIndex(index);
menu.AddItem(typeName, false, AddVariable, info);
}
menu.ShowAsContext();
} }
public void Move(int sourceIndex, int destIndex) protected virtual void AddVariable(object obj)
{ {
if (destIndex > sourceIndex) AddVariableInfo addVariableInfo = obj as AddVariableInfo;
--destIndex; if (addVariableInfo == null)
_arrayProperty.MoveArrayElement(sourceIndex, destIndex); {
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() if(GUILayout.Button("Variables"))
{ } {
arrayProperty.isExpanded = !arrayProperty.isExpanded;
public void EndGUI() }
{ }
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; Variable variable = this[index].objectReferenceValue as Variable;
@ -162,7 +193,7 @@ namespace Fungus.EditorUtils
return; return;
} }
var flowchart = FlowchartWindow.GetFlowchart(); var flowchart = targetFlowchart;
if (flowchart == null) if (flowchart == null)
{ {
return; return;
@ -250,14 +281,6 @@ namespace Fungus.EditorUtils
GUI.backgroundColor = Color.white; GUI.backgroundColor = Color.white;
} }
public virtual float GetItemHeight(int index)
{
return fixedItemHeight != 0f
? fixedItemHeight
: EditorGUI.GetPropertyHeight(this[index], GUIContent.none, false)
;
}
} }
} }

Loading…
Cancel
Save