Browse Source

Implemented Compare command

master
chrisgregan 10 years ago
parent
commit
88521c9381
  1. 138
      Assets/Fungus/Editor/CompareCommandEditor.cs
  2. 8
      Assets/Fungus/Editor/CompareCommandEditor.cs.meta
  3. 2
      Assets/Fungus/Editor/SequenceEditor.cs
  4. 32
      Assets/Fungus/Editor/SetVariableCommandEditor.cs
  5. 2
      Assets/Fungus/Editor/SetVariableCommandEditor.cs.meta
  6. BIN
      Assets/Fungus/Tests/Sequence/SequenceTest.unity
  7. 46
      Assets/Fungus/VisualScripting/CompareCommand.cs
  8. 35
      Assets/Fungus/VisualScripting/SetVariableCommand.cs

138
Assets/Fungus/Editor/CompareCommandEditor.cs

@ -0,0 +1,138 @@
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Fungus
{
[CustomEditor (typeof(CompareCommand))]
public class CompareCommandEditor : FungusCommandEditor
{
public override void DrawCommandInspectorGUI()
{
CompareCommand t = target as CompareCommand;
SequenceController sc = t.GetParentSequenceController();
if (sc == null)
{
return;
}
List<string> keys = new List<string>();
keys.Add("<None>");
int index = 0;
for (int i = 0; i < sc.variables.Count; ++i)
{
Variable v = sc.variables[i];
keys.Add(v.key);
if (v.key == t.variableKey &&
index == 0)
{
index = i + 1;
}
}
int newIndex = EditorGUILayout.Popup("Variable", index, keys.ToArray());
bool keyChanged = (t.variableKey != keys[newIndex]);
if (keyChanged)
{
Undo.RecordObject(t, "Select variable");
t.variableKey = keys[newIndex];
}
if (t.variableKey == "<None>")
{
return;
}
VariableType variableType = sc.variables[newIndex - 1].type;
List<GUIContent> operatorList = new List<GUIContent>();
operatorList.Add(new GUIContent("=="));
operatorList.Add(new GUIContent("!="));
if (variableType == VariableType.Integer ||
variableType == VariableType.Float)
{
operatorList.Add(new GUIContent("<"));
operatorList.Add(new GUIContent(">"));
operatorList.Add(new GUIContent("<="));
operatorList.Add(new GUIContent(">="));
}
CompareOperator compareOperator = (CompareOperator)EditorGUILayout.Popup(new GUIContent("Operator",
"The comparison operator to use when comparing values"),
(int)t.compareOperator,
operatorList.ToArray());
if (compareOperator != t.compareOperator)
{
Undo.RecordObject(t, "Select compare operator");
t.compareOperator = compareOperator;
}
bool booleanValue = t.booleanData.value;
int integerValue = t.integerData.value;
float floatValue = t.floatData.value;
string stringValue = t.stringData.value;
switch (variableType)
{
case VariableType.Boolean:
booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue);
break;
case VariableType.Integer:
integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue);
break;
case VariableType.Float:
floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue);
break;
case VariableType.String:
stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue);
break;
}
if (booleanValue != t.booleanData.value)
{
Undo.RecordObject(t, "Set boolean value");
t.booleanData.value = booleanValue;
}
else if (integerValue != t.integerData.value)
{
Undo.RecordObject(t, "Set integer value");
t.integerData.value = integerValue;
}
else if (floatValue != t.floatData.value)
{
Undo.RecordObject(t, "Set float value");
t.floatData.value = floatValue;
}
else if (stringValue != t.stringData.value)
{
Undo.RecordObject(t, "Set string value");
t.stringData.value = stringValue;
}
Sequence onTrue = SequenceEditor.SequenceField(new GUIContent("On True Sequence", "Sequence to execute if comparision is true"),
t.GetParentSequenceController(),
t.onTrueSequence);
Sequence onFalse = SequenceEditor.SequenceField(new GUIContent("On False Sequence", "Sequence to execute if comparision is false"),
t.GetParentSequenceController(),
t.onFalseSequence);
if (onTrue != t.onTrueSequence)
{
Undo.RecordObject(t, "Set On True Sequence");
t.onTrueSequence = onTrue;
}
if (onFalse != t.onFalseSequence)
{
Undo.RecordObject(t, "Set On False Sequence");
t.onFalseSequence = onFalse;
}
}
}
}

8
Assets/Fungus/Editor/CompareCommandEditor.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d0cbb7e4ccb4a41db8ced7c87070df1d
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

2
Assets/Fungus/Editor/SequenceEditor.cs

@ -21,7 +21,7 @@ public class SequenceEditor : Editor
List<GUIContent> sequenceNames = new List<GUIContent>();
int selectedIndex = 0;
sequenceNames.Add(new GUIContent("None"));
sequenceNames.Add(new GUIContent("<None>"));
Sequence[] sequences = sequenceController.GetComponentsInChildren<Sequence>();
for (int i = 0; i < sequences.Length; ++i)
{

32
Assets/Fungus/Editor/SetVariableCommandEditor.cs

@ -26,7 +26,7 @@ namespace Fungus
{
Variable v = sc.variables[i];
keys.Add(v.key);
if (v.key == t.variable.key &&
if (v.key == t.variableKey &&
index == 0)
{
index = i + 1;
@ -35,25 +35,25 @@ namespace Fungus
int newIndex = EditorGUILayout.Popup("Variable", index, keys.ToArray());
bool keyChanged = (t.variable.key != keys[newIndex]);
bool keyChanged = (t.variableKey != keys[newIndex]);
if (keyChanged)
{
Undo.RecordObject(t, "Select variable");
t.variable.key = keys[newIndex];
t.variableKey = keys[newIndex];
}
if (t.variable.key == "<None>")
if (t.variableKey == "<None>")
{
return;
}
VariableType variableType = sc.variables[newIndex - 1].type;
bool booleanValue = t.variable.booleanValue;
int integerValue = t.variable.integerValue;
float floatValue = t.variable.floatValue;
string stringValue = t.variable.stringValue;
bool booleanValue = t.booleanData.value;
int integerValue = t.integerData.value;
float floatValue = t.floatData.value;
string stringValue = t.stringData.value;
switch (variableType)
{
@ -71,25 +71,25 @@ namespace Fungus
break;
}
if (booleanValue != t.variable.booleanValue)
if (booleanValue != t.booleanData.value)
{
Undo.RecordObject(t, "Set boolean value");
t.variable.booleanValue = booleanValue;
t.booleanData.value = booleanValue;
}
else if (integerValue != t.variable.integerValue)
else if (integerValue != t.integerData.value)
{
Undo.RecordObject(t, "Set integer value");
t.variable.integerValue = integerValue;
t.integerData.value = integerValue;
}
else if (floatValue != t.variable.floatValue)
else if (floatValue != t.floatData.value)
{
Undo.RecordObject(t, "Set float value");
t.variable.floatValue = floatValue;
t.floatData.value = floatValue;
}
else if (stringValue != t.variable.stringValue)
else if (stringValue != t.stringData.value)
{
Undo.RecordObject(t, "Set string value");
t.variable.stringValue = stringValue;
t.stringData.value = stringValue;
}
}
}

2
Assets/Fungus/Editor/SetVariableCommandEditor.cs.meta

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d0cbb7e4ccb4a41db8ced7c87070df1d
guid: ab0735da2fbac40869824f8432fe3d62
MonoImporter:
serializedVersion: 2
defaultReferences: []

BIN
Assets/Fungus/Tests/Sequence/SequenceTest.unity

Binary file not shown.

46
Assets/Fungus/VisualScripting/CompareCommand.cs

@ -1,46 +0,0 @@
using UnityEngine;
using System.Collections;
using Fungus;
public class CompareCommand : FungusCommand
{
public string variableKey;
public bool booleanValue;
public int integerValue;
public float floatValue;
public string stringValue;
public Sequence trueSequence;
public Sequence falseSequence;
public override void OnEnter()
{
Variable v = parentSequenceController.GetVariable(variableKey);
if (v != null)
{
if (v.booleanValue == booleanValue)
{
if (trueSequence != null)
{
ExecuteSequence(trueSequence);
return;
}
}
else
{
if (falseSequence != null)
{
ExecuteSequence(falseSequence);
return;
}
}
}
Finish();
}
}

35
Assets/Fungus/VisualScripting/SetVariableCommand.cs

@ -4,30 +4,45 @@ using Fungus;
public class SetVariableCommand : FungusCommand
{
public Variable variable;
public string variableKey;
public BooleanData booleanData;
public IntegerData integerData;
public FloatData floatData;
public StringData stringData;
public override void OnEnter()
{
if (variable.key.Length == 0)
if (variableKey.Length == 0)
{
Finish();
return;
}
Variable v = parentSequenceController.GetVariable(variable.key);
Variable v = parentSequenceController.GetVariable(variableKey);
if (v == null)
{
Debug.LogError("Variable " + variable.key + " not defined.");
Debug.LogError("Variable " + variableKey + " not defined.");
}
else
{
if (v.IsSameType(variable))
{
v.Assign(variable);
}
else
switch (v.type)
{
Debug.LogError("Variables " + variable.key + " and " + v.key + " are not of the same type.");
case VariableType.Boolean:
v.booleanValue = booleanData.value;
break;
case VariableType.Integer:
v.integerValue = integerData.value;
break;
case VariableType.Float:
v.floatValue = floatData.value;
break;
case VariableType.String:
v.stringValue = stringData.value;
break;
}
}

Loading…
Cancel
Save