Browse Source

Added ability to show variable list in the Flowchart Inspector

-Refactored DrawVariablesGUI
-Added bool to FungusEditorPreferences, that hides the variable list in the flowchart inspector
master
desktop-maesty/steve 8 years ago
parent
commit
0b81c75e2e
  1. 3
      Assets/Fungus/Scripts/Components/Flowchart.cs
  2. 30
      Assets/Fungus/Scripts/Editor/FlowchartEditor.cs
  3. 2
      Assets/Fungus/Scripts/Editor/FlowchartWindow.cs
  4. 27
      Assets/Fungus/Scripts/Editor/FungusEditorPreferences.cs
  5. 22
      Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs

3
Assets/Fungus/Scripts/Components/Flowchart.cs

@ -80,6 +80,9 @@ namespace Fungus
[Tooltip("The ExecuteLua command adds a global Lua variable with this name bound to the flowchart prior to executing.")] [Tooltip("The ExecuteLua command adds a global Lua variable with this name bound to the flowchart prior to executing.")]
[SerializeField] protected string luaBindingName = "flowchart"; [SerializeField] protected string luaBindingName = "flowchart";
[Tooltip("Draw the variable gui in the inspector.")]
[SerializeField] protected bool showVariablesInInspector = true;
protected static List<Flowchart> cachedFlowcharts = new List<Flowchart>(); protected static List<Flowchart> cachedFlowcharts = new List<Flowchart>();
protected static bool eventSystemPresent; protected static bool eventSystemPresent;

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

@ -30,6 +30,7 @@ namespace Fungus.EditorUtils
protected SerializedProperty hideCommandsProp; protected SerializedProperty hideCommandsProp;
protected SerializedProperty luaEnvironmentProp; protected SerializedProperty luaEnvironmentProp;
protected SerializedProperty luaBindingNameProp; protected SerializedProperty luaBindingNameProp;
protected SerializedProperty varInspectorProp;
protected Texture2D addTexture; protected Texture2D addTexture;
@ -49,6 +50,7 @@ namespace Fungus.EditorUtils
hideCommandsProp = serializedObject.FindProperty("hideCommands"); hideCommandsProp = serializedObject.FindProperty("hideCommands");
luaEnvironmentProp = serializedObject.FindProperty("luaEnvironment"); luaEnvironmentProp = serializedObject.FindProperty("luaEnvironment");
luaBindingNameProp = serializedObject.FindProperty("luaBindingName"); luaBindingNameProp = serializedObject.FindProperty("luaBindingName");
varInspectorProp = serializedObject.FindProperty("showVariablesInInspector");
addTexture = FungusEditorResources.AddSmall; addTexture = FungusEditorResources.AddSmall;
} }
@ -71,6 +73,11 @@ namespace Fungus.EditorUtils
EditorGUILayout.PropertyField(luaEnvironmentProp); EditorGUILayout.PropertyField(luaEnvironmentProp);
EditorGUILayout.PropertyField(luaBindingNameProp); EditorGUILayout.PropertyField(luaBindingNameProp);
if (!FungusEditorPreferences.hideVariableInFlowchartInspector)
{
EditorGUILayout.PropertyField(varInspectorProp);
}
// Show list of commands to hide in Add Command menu // Show list of commands to hide in Add Command menu
ReorderableListGUI.Title(new GUIContent(hideCommandsProp.displayName, hideCommandsProp.tooltip)); ReorderableListGUI.Title(new GUIContent(hideCommandsProp.displayName, hideCommandsProp.tooltip));
ReorderableListGUI.ListField(hideCommandsProp); ReorderableListGUI.ListField(hideCommandsProp);
@ -82,13 +89,22 @@ namespace Fungus.EditorUtils
EditorWindow.GetWindow(typeof(FlowchartWindow), false, "Flowchart"); EditorWindow.GetWindow(typeof(FlowchartWindow), false, "Flowchart");
} }
GUILayout.FlexibleSpace(); GUILayout.FlexibleSpace();
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
}
public virtual void DrawVariablesGUI() if (varInspectorProp.boolValue && !FungusEditorPreferences.hideVariableInFlowchartInspector)
{
GUILayout.Space(20);
DrawVariablesGUI(false, Screen.width - 70);
}
}
public virtual void DrawVariablesGUI(bool showVariableToggleButton, int w)
{ {
serializedObject.Update(); serializedObject.Update();
@ -99,7 +115,7 @@ namespace Fungus.EditorUtils
t.VariablesExpanded = true; t.VariablesExpanded = true;
} }
if (!t.VariablesExpanded) if (showVariableToggleButton && !t.VariablesExpanded)
{ {
if (GUILayout.Button ("Variables (" + t.Variables.Count + ")", GUILayout.Height(24))) if (GUILayout.Button ("Variables (" + t.Variables.Count + ")", GUILayout.Height(24)))
{ {
@ -129,11 +145,12 @@ namespace Fungus.EditorUtils
} }
ReorderableListGUI.Title("Variables"); ReorderableListGUI.Title("Variables");
VariableListAdaptor adaptor = new VariableListAdaptor(variablesProp, 0); VariableListAdaptor adaptor = new VariableListAdaptor(variablesProp, 0, w == 0 ? VariableListAdaptor.DefaultWidth : w);
ReorderableListFlags flags = ReorderableListFlags.DisableContextMenu | ReorderableListFlags.HideAddButton; ReorderableListFlags flags = ReorderableListFlags.DisableContextMenu | ReorderableListFlags.HideAddButton;
ReorderableListControl.DrawControlFromState(adaptor, null, flags); ReorderableListControl.DrawControlFromState(adaptor, null, flags);
listRect = GUILayoutUtility.GetLastRect(); listRect = GUILayoutUtility.GetLastRect();
} }
else else
@ -156,7 +173,7 @@ namespace Fungus.EditorUtils
buttonRect.width -= 30; buttonRect.width -= 30;
} }
if (GUI.Button (buttonRect, "Variables")) if (showVariableToggleButton && GUI.Button(buttonRect, "Variables"))
{ {
t.VariablesExpanded = false; t.VariablesExpanded = false;
} }
@ -165,7 +182,8 @@ namespace Fungus.EditorUtils
Rect lastRect = buttonRect; Rect lastRect = buttonRect;
lastRect.x += 5; lastRect.x += 5;
lastRect.y += 5; lastRect.y += 5;
EditorGUI.Foldout(lastRect, true, ""); //this is not required, seems to be legacy that is hidden in the normal reorderable
//EditorGUI.Foldout(lastRect, true, "");
Rect plusRect = listRect; Rect plusRect = listRect;
plusRect.x += plusRect.width - plusWidth; plusRect.x += plusRect.width - plusWidth;

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

@ -497,7 +497,7 @@ namespace Fungus.EditorUtils
GUILayout.Space(8); GUILayout.Space(8);
FlowchartEditor flowchartEditor = Editor.CreateEditor (flowchart) as FlowchartEditor; FlowchartEditor flowchartEditor = Editor.CreateEditor (flowchart) as FlowchartEditor;
flowchartEditor.DrawVariablesGUI(); flowchartEditor.DrawVariablesGUI(true, 0);
DestroyImmediate(flowchartEditor); DestroyImmediate(flowchartEditor);
Rect variableWindowRect = GUILayoutUtility.GetLastRect(); Rect variableWindowRect = GUILayoutUtility.GetLastRect();

27
Assets/Fungus/Scripts/Editor/FungusEditorPreferences.cs

@ -1,5 +1,6 @@
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
using UnityEditor.Callbacks;
namespace Fungus namespace Fungus
{ {
@ -10,34 +11,48 @@ namespace Fungus
/// ///
/// ref https://docs.unity3d.com/ScriptReference/PreferenceItem.html /// ref https://docs.unity3d.com/ScriptReference/PreferenceItem.html
/// </summary> /// </summary>
public class FungusEditorPreferences [InitializeOnLoad]
public static class FungusEditorPreferences
{ {
// Have we loaded the prefs yet // Have we loaded the prefs yet
private static bool prefsLoaded = false; private static bool prefsLoaded = false;
public static bool hideMushroomInHierarchy = false; public static bool hideMushroomInHierarchy,
hideVariableInFlowchartInspector ;
static FungusEditorPreferences()
{
LoadOnScriptLoad();
}
// Add preferences section named "My Preferences" to the Preferences Window // Add preferences section named "My Preferences" to the Preferences Window
[PreferenceItem("Fungus")] [PreferenceItem("Fungus")]
public static void PreferencesGUI() public static void PreferencesGUI()
{ {
// Load the preferences // Load the preferences
if (!prefsLoaded) if (!prefsLoaded)
{ {
hideMushroomInHierarchy = EditorPrefs.GetBool("hideMushroomInHierarchy", false); LoadOnScriptLoad();
prefsLoaded = true;
} }
// Preferences GUI // Preferences GUI
hideMushroomInHierarchy = EditorGUILayout.Toggle("Hide Mushroom Flowchart Icon", hideMushroomInHierarchy); hideMushroomInHierarchy = EditorGUILayout.Toggle("Hide Mushroom Flowchart Icon", hideMushroomInHierarchy);
hideVariableInFlowchartInspector = EditorGUILayout.Toggle("Hide Variables in Flowchart Inspector", hideVariableInFlowchartInspector);
// Save the preferences // Save the preferences
if (GUI.changed) if (GUI.changed)
{ {
EditorPrefs.SetBool("hideMushroomInHierarchy", hideMushroomInHierarchy); EditorPrefs.SetBool("hideMushroomInHierarchy", hideMushroomInHierarchy);
EditorPrefs.SetBool("hideVariableInFlowchartInspector", hideVariableInFlowchartInspector);
} }
} }
public static void LoadOnScriptLoad()
{
hideMushroomInHierarchy = EditorPrefs.GetBool("hideMushroomInHierarchy", false);
hideVariableInFlowchartInspector = EditorPrefs.GetBool("hideVariableInFlowchartInspector", true);
prefsLoaded = true;
}
} }
} }
} }

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

@ -13,11 +13,15 @@ using Rotorz.ReorderableList;
namespace Fungus.EditorUtils namespace Fungus.EditorUtils
{ {
public class VariableListAdaptor : IReorderableListAdaptor { public class VariableListAdaptor : IReorderableListAdaptor {
public static readonly int DefaultWidth = 80 + 100 + 140 + 60;
protected SerializedProperty _arrayProperty; protected SerializedProperty _arrayProperty;
public float fixedItemHeight; public float fixedItemHeight;
public int widthOfList;
public SerializedProperty this[int index] { public SerializedProperty this[int index] {
get { return _arrayProperty.GetArrayElementAtIndex(index); } get { return _arrayProperty.GetArrayElementAtIndex(index); }
} }
@ -26,7 +30,7 @@ namespace Fungus.EditorUtils
get { return _arrayProperty; } get { return _arrayProperty; }
} }
public VariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight) { public VariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight, int widthOfList) {
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)
@ -34,9 +38,10 @@ namespace Fungus.EditorUtils
this._arrayProperty = arrayProperty; this._arrayProperty = arrayProperty;
this.fixedItemHeight = fixedItemHeight; this.fixedItemHeight = fixedItemHeight;
this.widthOfList = widthOfList;
} }
public VariableListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f) { public VariableListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f, DefaultWidth) {
} }
public int Count { public int Count {
@ -103,7 +108,14 @@ namespace Fungus.EditorUtils
return; return;
} }
float[] widths = { 80, 100, 140, 60 }; int width = widthOfList;
int totalRatio = DefaultWidth;
float[] widths = { (80.0f/ totalRatio) * width,
(100.0f / totalRatio) * width,
(140.0f/ totalRatio) * width,
(60.0f/ totalRatio) * width };
Rect[] rects = new Rect[4]; Rect[] rects = new Rect[4];
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)

Loading…
Cancel
Save