Browse Source

Fixed delete variable errors

master
chrisgregan 11 years ago
parent
commit
6115eaa8c7
  1. 56
      Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs
  2. 121
      Assets/Fungus/Editor/FungusScript/FungusVariableListAdaptor.cs

56
Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs

@ -91,10 +91,10 @@ namespace Fungus.Script
{
GenericMenu menu = new GenericMenu ();
menu.AddItem(new GUIContent ("Boolean"), false, AddBooleanVariable, t);
menu.AddItem (new GUIContent ("Integer"), false, AddIntegerVariable, t);
menu.AddItem (new GUIContent ("Float"), false, AddFloatVariable, t);
menu.AddItem (new GUIContent ("String"), false, AddStringVariable, t);
menu.AddItem(new GUIContent ("Boolean"), false, AddVariable<BooleanVariable>, t);
menu.AddItem (new GUIContent ("Integer"), false, AddVariable<IntegerVariable>, t);
menu.AddItem (new GUIContent ("Float"), false, AddVariable<FloatVariable>, t);
menu.AddItem (new GUIContent ("String"), false, AddVariable<StringVariable>, t);
menu.ShowAsContext ();
}
@ -103,7 +103,7 @@ namespace Fungus.Script
serializedObject.ApplyModifiedProperties();
}
void AddBooleanVariable(object obj)
void AddVariable<T>(object obj) where T : FungusVariable
{
FungusScript fungusScript = obj as FungusScript;
if (fungusScript == null)
@ -111,50 +111,8 @@ namespace Fungus.Script
return;
}
Undo.RecordObject(fungusScript, "Add Boolean");
BooleanVariable variable = fungusScript.gameObject.AddComponent<BooleanVariable>();
variable.key = MakeUniqueKey(fungusScript);
fungusScript.variables.Add(variable);
}
void AddIntegerVariable(object obj)
{
FungusScript fungusScript = obj as FungusScript;
if (fungusScript == null)
{
return;
}
Undo.RecordObject(fungusScript, "Add Integer");
IntegerVariable variable = fungusScript.gameObject.AddComponent<IntegerVariable>();
variable.key = MakeUniqueKey(fungusScript);
fungusScript.variables.Add(variable);
}
void AddFloatVariable(object obj)
{
FungusScript fungusScript = obj as FungusScript;
if (fungusScript == null)
{
return;
}
Undo.RecordObject(fungusScript, "Add Float");
FloatVariable variable = fungusScript.gameObject.AddComponent<FloatVariable>();
variable.key = MakeUniqueKey(fungusScript);
fungusScript.variables.Add(variable);
}
void AddStringVariable(object obj)
{
FungusScript fungusScript = obj as FungusScript;
if (fungusScript == null)
{
return;
}
Undo.RecordObject(fungusScript, "Add String");
StringVariable variable = fungusScript.gameObject.AddComponent<StringVariable>();
Undo.RecordObject(fungusScript, "Add Variable");
T variable = fungusScript.gameObject.AddComponent<T>();
variable.key = MakeUniqueKey(fungusScript);
fungusScript.variables.Add(variable);
}

121
Assets/Fungus/Editor/FungusScript/FungusVariableListAdaptor.cs

@ -10,16 +10,80 @@ using Rotorz.ReorderableList;
namespace Fungus.Script
{
public class FungusVariableListAdaptor : IReorderableListAdaptor {
public class FungusVariableListAdaptor : SerializedPropertyAdaptor
{
public FungusVariableListAdaptor(SerializedProperty arrayProperty, float fixedItemHeight) : base(arrayProperty, fixedItemHeight)
{}
private SerializedProperty _arrayProperty;
public float fixedItemHeight;
public FungusVariableListAdaptor(SerializedProperty arrayProperty) : this(arrayProperty, 0f)
{}
public SerializedProperty this[int index] {
get { return _arrayProperty.GetArrayElementAtIndex(index); }
}
public SerializedProperty arrayProperty {
get { return _arrayProperty; }
}
public override void DrawItem(Rect position, int index)
public FungusVariableListAdaptor(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 FungusVariableListAdaptor(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) {
// Remove the Fungus Variable component
FungusVariable variable = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as FungusVariable;
Undo.DestroyObjectImmediate(variable);
_arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue = null;
_arrayProperty.DeleteArrayElementAtIndex(index);
}
public void Move(int sourceIndex, int destIndex) {
if (destIndex > sourceIndex)
--destIndex;
_arrayProperty.MoveArrayElement(sourceIndex, destIndex);
}
public void Clear() {
_arrayProperty.ClearArray();
}
public void DrawItem(Rect position, int index)
{
FungusVariable variable = this[index].objectReferenceValue as FungusVariable;
@ -111,6 +175,49 @@ namespace Fungus.Script
}
}
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;
}
}
}
}

Loading…
Cancel
Save