Browse Source

FlowchartEditor keeps a VariableListAdapter rather than an entire FlowchartEditor

master
desktop-maesty/steve 7 years ago
parent
commit
5038f8baae
  1. 10
      Assets/Fungus/Scripts/Editor/FlowchartEditor.cs
  2. 31
      Assets/Fungus/Scripts/Editor/FlowchartWindow.cs
  3. 42
      Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs

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

@ -47,7 +47,7 @@ namespace Fungus.EditorUtils
addTexture = FungusEditorResources.AddSmall; addTexture = FungusEditorResources.AddSmall;
variableListAdaptor = new VariableListAdaptor(variablesProp, 0, 0, target as Flowchart); variableListAdaptor = new VariableListAdaptor(variablesProp, target as Flowchart);
} }
public override void OnInspectorGUI() public override void OnInspectorGUI()
@ -95,9 +95,15 @@ namespace Fungus.EditorUtils
public virtual void DrawVariablesGUI(bool showVariableToggleButton, int w) public virtual void DrawVariablesGUI(bool showVariableToggleButton, int w)
{ {
var t = target as Flowchart;
if(t == null)
{
return;
}
serializedObject.Update(); serializedObject.Update();
var t = target as Flowchart;
if (t.Variables.Count == 0) if (t.Variables.Count == 0)
{ {

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

@ -135,7 +135,8 @@ namespace Fungus.EditorUtils
protected Block dragBlock; protected Block dragBlock;
protected static FungusState fungusState; protected static FungusState fungusState;
static FlowchartEditor flowchartEditor; static protected VariableListAdaptor variableListAdaptor;
[MenuItem("Tools/Fungus/Flowchart Window")] [MenuItem("Tools/Fungus/Flowchart Window")]
static void Init() static void Init()
@ -145,6 +146,7 @@ namespace Fungus.EditorUtils
protected virtual void OnEnable() protected virtual void OnEnable()
{ {
// All block nodes use the same GUIStyle, but with a different background // All block nodes use the same GUIStyle, but with a different background
nodeStyle.border = new RectOffset(20, 20, 5, 5); nodeStyle.border = new RectOffset(20, 20, 5, 5);
nodeStyle.padding = nodeStyle.border; nodeStyle.padding = nodeStyle.border;
@ -224,16 +226,20 @@ namespace Fungus.EditorUtils
var fs = Selection.activeGameObject.GetComponent<Flowchart>(); var fs = Selection.activeGameObject.GetComponent<Flowchart>();
if (fs != null) if (fs != null)
{ {
//make sure we have a valid editor for this flowchart fungusState.SelectedFlowchart = fs;
if (fungusState.SelectedFlowchart != fs || fungusState.SelectedFlowchart == null || flowchartEditor == null)
{
DestroyImmediate(flowchartEditor);
flowchartEditor = Editor.CreateEditor(fs) as FlowchartEditor;
fungusState.SelectedFlowchart = fs;
}
} }
} }
if (fungusState.SelectedFlowchart == null)
{
variableListAdaptor = null;
}
else if (variableListAdaptor == null || variableListAdaptor.TargetFlowchart != fungusState.SelectedFlowchart)
{
var fsSO = new SerializedObject(fungusState.SelectedFlowchart);
variableListAdaptor = new VariableListAdaptor(fsSO.FindProperty("variables"), fungusState.SelectedFlowchart);
}
return fungusState.SelectedFlowchart; return fungusState.SelectedFlowchart;
} }
@ -504,7 +510,14 @@ namespace Fungus.EditorUtils
{ {
GUILayout.Space(8); GUILayout.Space(8);
flowchartEditor.DrawVariablesGUI(true, 0);
if (variableListAdaptor != null)
{
if (variableListAdaptor.TargetFlowchart == null)
variableListAdaptor = null;
else
variableListAdaptor.DrawVarList(0);
}
Rect variableWindowRect = GUILayoutUtility.GetLastRect(); Rect variableWindowRect = GUILayoutUtility.GetLastRect();
if (flowchart.VariablesExpanded && flowchart.Variables.Count > 0) if (flowchart.VariablesExpanded && flowchart.Variables.Count > 0)

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

@ -1,9 +1,6 @@
// 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;
@ -15,7 +12,6 @@ namespace Fungus.EditorUtils
{ {
public class VariableListAdaptor public class VariableListAdaptor
{ {
protected class AddVariableInfo protected class AddVariableInfo
{ {
public Flowchart flowchart; public Flowchart flowchart;
@ -32,28 +28,36 @@ namespace Fungus.EditorUtils
public int widthOfList; public int widthOfList;
private ReorderableList list; private ReorderableList list;
private Flowchart targetFlowchart; public Flowchart TargetFlowchart { get; private set; }
public SerializedProperty this[int index] public SerializedProperty this[int index]
{ {
get { return _arrayProperty.GetArrayElementAtIndex(index); } get { return _arrayProperty.GetArrayElementAtIndex(index); }
} }
public SerializedProperty arrayProperty public Variable GetVarAt(int index)
{ {
get { return _arrayProperty; } if (list.list != null)
return list.list[index] as Variable;
else
return this[index].objectReferenceValue as Variable;
} }
public VariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight, int widthOfList, Flowchart _targetFlowchart) //public SerializedProperty arrayProperty
//{
// get { return _arrayProperty; }
//}
public VariableListAdaptor(SerializedProperty arrayProperty, 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.TargetFlowchart = _targetFlowchart;
this.fixedItemHeight = 0;
this._arrayProperty = arrayProperty; this._arrayProperty = arrayProperty;
this.fixedItemHeight = fixedItemHeight;
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;
@ -67,7 +71,7 @@ namespace Fungus.EditorUtils
{ {
int index = list.index; int index = list.index;
// Remove the Fungus Variable component // Remove the Fungus Variable component
Variable variable = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Variable; Variable variable = this[index].objectReferenceValue as Variable;
Undo.DestroyObjectImmediate(variable); Undo.DestroyObjectImmediate(variable);
} }
@ -87,7 +91,7 @@ namespace Fungus.EditorUtils
} }
AddVariableInfo addVariableInfo = new AddVariableInfo(); AddVariableInfo addVariableInfo = new AddVariableInfo();
addVariableInfo.flowchart = targetFlowchart; addVariableInfo.flowchart = TargetFlowchart;
addVariableInfo.variableType = type; addVariableInfo.variableType = type;
GUIContent typeName = new GUIContent(variableInfo.VariableType); GUIContent typeName = new GUIContent(variableInfo.VariableType);
@ -106,7 +110,7 @@ namespace Fungus.EditorUtils
} }
AddVariableInfo info = new AddVariableInfo(); AddVariableInfo info = new AddVariableInfo();
info.flowchart = targetFlowchart; info.flowchart = TargetFlowchart;
info.variableType = type; info.variableType = type;
GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType); GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType);
@ -144,22 +148,24 @@ namespace Fungus.EditorUtils
public void DrawVarList(int w) public void DrawVarList(int w)
{ {
_arrayProperty.serializedObject.Update();
this.widthOfList = (w == 0 ? VariableListAdaptor.DefaultWidth : w) - ScrollSpacer; this.widthOfList = (w == 0 ? VariableListAdaptor.DefaultWidth : w) - ScrollSpacer;
if(GUILayout.Button("Variables")) if(GUILayout.Button("Variables"))
{ {
arrayProperty.isExpanded = !arrayProperty.isExpanded; _arrayProperty.isExpanded = !_arrayProperty.isExpanded;
} }
if (arrayProperty.isExpanded) if (_arrayProperty.isExpanded)
{ {
list.DoLayoutList(); list.DoLayoutList();
} }
_arrayProperty.serializedObject.ApplyModifiedProperties();
} }
public void DrawItem(Rect position, int index, bool selected, bool focused) public void DrawItem(Rect position, int index, bool selected, bool focused)
{ {
Variable variable = this[index].objectReferenceValue as Variable; Variable variable = GetVarAt(index);// this[index].objectReferenceValue as Variable;
if (variable == null) if (variable == null)
{ {
@ -193,7 +199,7 @@ namespace Fungus.EditorUtils
return; return;
} }
var flowchart = targetFlowchart; var flowchart = TargetFlowchart;
if (flowchart == null) if (flowchart == null)
{ {
return; return;
@ -236,7 +242,7 @@ namespace Fungus.EditorUtils
// To access properties in a monobehavior, you have to new a SerializedObject // To access properties in a monobehavior, you have to new a SerializedObject
// http://answers.unity3d.com/questions/629803/findrelativeproperty-never-worked-for-me-how-does.html // http://answers.unity3d.com/questions/629803/findrelativeproperty-never-worked-for-me-how-does.html
SerializedObject variableObject = new SerializedObject(this[index].objectReferenceValue); SerializedObject variableObject = new SerializedObject(variable);
variableObject.Update(); variableObject.Update();

Loading…
Cancel
Save