From d4705b257e831873d4844857a7e89000b21145c2 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Mon, 11 Aug 2014 13:20:03 +0100 Subject: [PATCH] Added custom variable list control --- .../Editor/FungusScript/FungusScriptEditor.cs | 4 +- .../FungusScript/FungusVariableEditor.cs | 8 +- .../Editor/FungusScript/VariableList.cs | 148 ++++++++++++++++++ .../Editor/FungusScript/VariableList.cs.meta | 8 + Assets/Shuttle/ShuttleGame.unity | Bin 80644 -> 82132 bytes 5 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 Assets/Fungus/Editor/FungusScript/VariableList.cs create mode 100644 Assets/Fungus/Editor/FungusScript/VariableList.cs.meta diff --git a/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs b/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs index 560bdf8b..9bac8a57 100644 --- a/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs @@ -63,7 +63,9 @@ namespace Fungus.Script } ReorderableListGUI.Title("Variables"); - ReorderableListGUI.ListField(variablesProperty, ReorderableListFlags.DisableContextMenu | ReorderableListFlags.HideAddButton ); + + VariableListAdaptor adaptor = new VariableListAdaptor(variablesProperty, 0); + ReorderableListControl.DrawControlFromState(adaptor, null, ReorderableListFlags.DisableContextMenu | ReorderableListFlags.HideAddButton); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); diff --git a/Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs b/Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs index 0c799d27..c39a830a 100644 --- a/Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs @@ -8,7 +8,6 @@ using System.Linq; namespace Fungus.Script { - [CustomPropertyDrawer (typeof(Variable))] public class VariableDrawer : PropertyDrawer { @@ -77,6 +76,13 @@ namespace Fungus.Script [CustomEditor (typeof(FungusVariable), true)] public class FungusVariableEditor : FungusCommandEditor { + void OnEnable() + { + // Uncomment to hide variable components in inspector + //FungusVariable t = target as FungusVariable; + //t.hideFlags = HideFlags.HideInInspector; + } + public override void OnInspectorGUI() { FungusVariable t = target as FungusVariable; diff --git a/Assets/Fungus/Editor/FungusScript/VariableList.cs b/Assets/Fungus/Editor/FungusScript/VariableList.cs new file mode 100644 index 00000000..970b39f8 --- /dev/null +++ b/Assets/Fungus/Editor/FungusScript/VariableList.cs @@ -0,0 +1,148 @@ +// 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; + +using System; + +using Rotorz.ReorderableList; + +namespace Fungus.Script +{ + + public class VariableListAdaptor : IReorderableListAdaptor + { + + private SerializedProperty _arrayProperty; + + public float fixedItemHeight; + + public SerializedProperty this[int index] + { + get { return _arrayProperty.GetArrayElementAtIndex(index); } + } + + public SerializedProperty arrayProperty + { + get { return _arrayProperty; } + } + + public VariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight) + { + if (arrayProperty == null) + throw new ArgumentNullException("Array property was null."); + if (!arrayProperty.isArray) + throw new InvalidOperationException("Specified serialized propery is not an array."); + + this._arrayProperty = arrayProperty; + this.fixedItemHeight = fixedItemHeight; + } + + public VariableListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f) + { + } + + public int Count + { + get { return _arrayProperty.arraySize; } + } + + public virtual bool CanDrag(int index) + { + return true; + } + + public virtual bool CanRemove(int index) + { + return true; + } + + public void Add() + { + int newIndex = _arrayProperty.arraySize; + ++_arrayProperty.arraySize; + ResetValue(_arrayProperty.GetArrayElementAtIndex(newIndex)); + } + + public void Insert(int index) + { + _arrayProperty.InsertArrayElementAtIndex(index); + ResetValue(_arrayProperty.GetArrayElementAtIndex(index)); + } + + public void Duplicate(int index) + { + _arrayProperty.InsertArrayElementAtIndex(index); + } + + public void Remove(int index) { + _arrayProperty.DeleteArrayElementAtIndex(index); + } + + + public void Move(int sourceIndex, int destIndex) + { + if (destIndex > sourceIndex) + --destIndex; + _arrayProperty.MoveArrayElement(sourceIndex, destIndex); + } + + public void Clear() { + _arrayProperty.ClearArray(); + } + + public virtual void DrawItem(Rect position, int index) + { + EditorGUI.PropertyField(position, this[index], GUIContent.none, false); + } + + public virtual float GetItemHeight(int index) + { + return fixedItemHeight != 0f + ? fixedItemHeight + : EditorGUI.GetPropertyHeight(this[index], GUIContent.none, false) + ; + } + + private void ResetValue(SerializedProperty element) + { + switch (element.type) { + case "string": + element.stringValue = ""; + break; + case "Vector2f": + element.vector2Value = Vector2.zero; + break; + case "Vector3f": + element.vector3Value = Vector3.zero; + break; + case "Rectf": + element.rectValue = new Rect(); + break; + case "Quaternionf": + element.quaternionValue = Quaternion.identity; + break; + case "int": + element.intValue = 0; + break; + case "float": + element.floatValue = 0f; + break; + case "UInt8": + element.boolValue = false; + break; + case "ColorRGBA": + element.colorValue = Color.black; + break; + + default: + if (element.type.StartsWith("PPtr")) + element.objectReferenceValue = null; + break; + } + } + } + +} diff --git a/Assets/Fungus/Editor/FungusScript/VariableList.cs.meta b/Assets/Fungus/Editor/FungusScript/VariableList.cs.meta new file mode 100644 index 00000000..04ad3af7 --- /dev/null +++ b/Assets/Fungus/Editor/FungusScript/VariableList.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d73a4523e32ec485db5ced2e5d7fa9b6 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Shuttle/ShuttleGame.unity b/Assets/Shuttle/ShuttleGame.unity index 1769ed8c6a2ac9aaf59f35b0abf44386025b50da..66535a2c848d9e41d6696c6430e7c865a85f6855 100644 GIT binary patch delta 1198 zcmZ9MT}TvB6vxla^((bTG-uYnTzB1FKek;p*N+IdGO->?Lqw#=O|~*OKet#RSOueA z3gZU7g+2AsgCdbHLG~m?kYePcndMsGL#atv_P?{UH0!{fx%bEaf9^TwU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