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

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

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

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

@ -135,7 +135,8 @@ namespace Fungus.EditorUtils
protected Block dragBlock;
protected static FungusState fungusState;
static FlowchartEditor flowchartEditor;
static protected VariableListAdaptor variableListAdaptor;
[MenuItem("Tools/Fungus/Flowchart Window")]
static void Init()
@ -145,6 +146,7 @@ namespace Fungus.EditorUtils
protected virtual void OnEnable()
{
// All block nodes use the same GUIStyle, but with a different background
nodeStyle.border = new RectOffset(20, 20, 5, 5);
nodeStyle.padding = nodeStyle.border;
@ -224,16 +226,20 @@ 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;
}
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;
}
@ -503,8 +509,15 @@ namespace Fungus.EditorUtils
flowchart.VariablesScrollPos = GUILayout.BeginScrollView(flowchart.VariablesScrollPos);
{
GUILayout.Space(8);
flowchartEditor.DrawVariablesGUI(true, 0);
if (variableListAdaptor != null)
{
if (variableListAdaptor.TargetFlowchart == null)
variableListAdaptor = null;
else
variableListAdaptor.DrawVarList(0);
}
Rect variableWindowRect = GUILayoutUtility.GetLastRect();
if (flowchart.VariablesExpanded && flowchart.Variables.Count > 0)

44
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).
// 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;
@ -15,7 +12,6 @@ namespace Fungus.EditorUtils
{
public class VariableListAdaptor
{
protected class AddVariableInfo
{
public Flowchart flowchart;
@ -32,28 +28,36 @@ namespace Fungus.EditorUtils
public int widthOfList;
private ReorderableList list;
private Flowchart targetFlowchart;
public Flowchart TargetFlowchart { get; private set; }
public SerializedProperty this[int 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)
throw new ArgumentNullException("Array property was null.");
if (!arrayProperty.isArray)
throw new InvalidOperationException("Specified serialized propery is not an array.");
this.targetFlowchart = _targetFlowchart;
this.TargetFlowchart = _targetFlowchart;
this.fixedItemHeight = 0;
this._arrayProperty = arrayProperty;
this.fixedItemHeight = fixedItemHeight;
this.widthOfList = widthOfList - ScrollSpacer;
list = new ReorderableList(arrayProperty.serializedObject, arrayProperty, true, false, true, true);
list.drawElementCallback = DrawItem;
@ -67,7 +71,7 @@ namespace Fungus.EditorUtils
{
int index = list.index;
// Remove the Fungus Variable component
Variable variable = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Variable;
Variable variable = this[index].objectReferenceValue as Variable;
Undo.DestroyObjectImmediate(variable);
}
@ -87,7 +91,7 @@ namespace Fungus.EditorUtils
}
AddVariableInfo addVariableInfo = new AddVariableInfo();
addVariableInfo.flowchart = targetFlowchart;
addVariableInfo.flowchart = TargetFlowchart;
addVariableInfo.variableType = type;
GUIContent typeName = new GUIContent(variableInfo.VariableType);
@ -106,7 +110,7 @@ namespace Fungus.EditorUtils
}
AddVariableInfo info = new AddVariableInfo();
info.flowchart = targetFlowchart;
info.flowchart = TargetFlowchart;
info.variableType = type;
GUIContent typeName = new GUIContent(variableInfo.Category + "/" + variableInfo.VariableType);
@ -144,22 +148,24 @@ namespace Fungus.EditorUtils
public void DrawVarList(int w)
{
_arrayProperty.serializedObject.Update();
this.widthOfList = (w == 0 ? VariableListAdaptor.DefaultWidth : w) - ScrollSpacer;
if(GUILayout.Button("Variables"))
{
arrayProperty.isExpanded = !arrayProperty.isExpanded;
_arrayProperty.isExpanded = !_arrayProperty.isExpanded;
}
if (arrayProperty.isExpanded)
if (_arrayProperty.isExpanded)
{
list.DoLayoutList();
}
_arrayProperty.serializedObject.ApplyModifiedProperties();
}
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)
{
@ -193,7 +199,7 @@ namespace Fungus.EditorUtils
return;
}
var flowchart = targetFlowchart;
var flowchart = TargetFlowchart;
if (flowchart == null)
{
return;
@ -236,7 +242,7 @@ namespace Fungus.EditorUtils
// 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
SerializedObject variableObject = new SerializedObject(this[index].objectReferenceValue);
SerializedObject variableObject = new SerializedObject(variable);
variableObject.Update();

Loading…
Cancel
Save