Browse Source

Pick variable or constant value

master
chrisgregan 11 years ago
parent
commit
239b9e7416
  1. 8
      Assets/Fungus/Editor/FungusScript/AddOptionEditor.cs
  2. 38
      Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs
  3. 94
      Assets/Fungus/Editor/FungusScript/JumpEditor.cs
  4. 8
      Assets/Fungus/Editor/FungusScript/SayEditor.cs
  5. 38
      Assets/Fungus/Editor/FungusScript/SequenceEditor.cs
  6. 83
      Assets/Fungus/Editor/FungusScript/SetEditor.cs
  7. 54
      Assets/Fungus/VisualScripting/Jump.cs
  8. 54
      Assets/Fungus/VisualScripting/Set.cs
  9. 35
      Assets/Fungus/VisualScripting/Variable.cs
  10. 8
      Assets/Fungus/VisualScripting/Variable.cs.meta

8
Assets/Fungus/Editor/FungusScript/AddOptionEditor.cs

@ -25,10 +25,10 @@ namespace Fungus.Script
if (showCondition == AddOption.ShowCondition.BooleanIsFalse || if (showCondition == AddOption.ShowCondition.BooleanIsFalse ||
showCondition == AddOption.ShowCondition.BooleanIsTrue) showCondition == AddOption.ShowCondition.BooleanIsTrue)
{ {
booleanVariable = SequenceEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"), booleanVariable = FungusVariableEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"),
t.GetFungusScript (), t.GetFungusScript (),
t.booleanVariable, t.booleanVariable,
v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable; v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable;
} }
if (EditorGUI.EndChangeCheck()) if (EditorGUI.EndChangeCheck())

38
Assets/Fungus/Editor/FungusScript/FungusVariableEditor.cs

@ -1,6 +1,7 @@
using UnityEditor; using UnityEditor;
using UnityEditorInternal; using UnityEditorInternal;
using UnityEngine; using UnityEngine;
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -26,6 +27,43 @@ namespace Fungus.Script
t.scope = scope; t.scope = scope;
} }
} }
static public FungusVariable VariableField(GUIContent label, FungusScript fungusScript, FungusVariable variable, Func<FungusVariable, bool> filter = null)
{
List<string> variableKeys = new List<string>();
List<FungusVariable> variableObjects = new List<FungusVariable>();
variableKeys.Add("<None>");
variableObjects.Add(null);
FungusVariable[] variables = fungusScript.GetComponents<FungusVariable>();
int index = 0;
int selectedIndex = 0;
foreach (FungusVariable v in variables)
{
if (filter != null)
{
if (!filter(v))
{
continue;
}
}
variableKeys.Add(v.key);
variableObjects.Add(v);
index++;
if (v == variable)
{
selectedIndex = index;
}
}
selectedIndex = EditorGUILayout.Popup(label.text, selectedIndex, variableKeys.ToArray());
return variableObjects[selectedIndex];
}
} }
} }

94
Assets/Fungus/Editor/FungusScript/JumpEditor.cs

@ -37,10 +37,10 @@ namespace Fungus.Script
return; return;
} }
FungusVariable fungusVariable = SequenceEditor.VariableField(new GUIContent("Compare Variable", "Variable to use in compare operation"), FungusVariable fungusVariable = FungusVariableEditor.VariableField(new GUIContent("Compare Variable", "Variable to use in compare operation"),
t.GetFungusScript(), t.GetFungusScript(),
t.variable, t.variable,
null); null);
if (fungusVariable != t.variable) if (fungusVariable != t.variable)
{ {
@ -75,47 +75,61 @@ namespace Fungus.Script
t.compareOperator = compareOperator; t.compareOperator = compareOperator;
} }
bool booleanValue = t.booleanData.value; FungusVariable compareVariable = FungusVariableEditor.VariableField(new GUIContent("Compare Variable", "Variable to compare with"),
int integerValue = t.integerData.value; t.GetFungusScript(),
float floatValue = t.floatData.value; t.compareVariable,
string stringValue = t.stringData.value; v => v.GetType() == fungusVariable.GetType());
if (t.variable.GetType() == typeof(BooleanVariable)) if (compareVariable != t.compareVariable)
{ {
booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue); Undo.RecordObject(t, "Select Compare Variable");
} t.compareVariable = compareVariable;
else if (t.variable.GetType() == typeof(IntegerVariable))
{
integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue);
}
else if (t.variable.GetType() == typeof(FloatVariable))
{
floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue);
}
else if (t.variable.GetType() == typeof(StringVariable))
{
stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue);
} }
if (booleanValue != t.booleanData.value) if (compareVariable == null)
{
Undo.RecordObject(t, "Set boolean value");
t.booleanData.value = booleanValue;
}
else if (integerValue != t.integerData.value)
{ {
Undo.RecordObject(t, "Set integer value"); bool booleanValue = t.booleanValue;
t.integerData.value = integerValue; int integerValue = t.integerValue;
} float floatValue = t.floatValue;
else if (floatValue != t.floatData.value) string stringValue = t.stringValue;
{
Undo.RecordObject(t, "Set float value"); if (t.variable.GetType() == typeof(BooleanVariable))
t.floatData.value = floatValue; {
} booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue);
else if (stringValue != t.stringData.value) }
{ else if (t.variable.GetType() == typeof(IntegerVariable))
Undo.RecordObject(t, "Set string value"); {
t.stringData.value = stringValue; integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue);
}
else if (t.variable.GetType() == typeof(FloatVariable))
{
floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue);
}
else if (t.variable.GetType() == typeof(StringVariable))
{
stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue);
}
if (booleanValue != t.booleanValue)
{
Undo.RecordObject(t, "Set boolean value");
t.booleanValue = booleanValue;
}
else if (integerValue != t.integerValue)
{
Undo.RecordObject(t, "Set integer value");
t.integerValue = integerValue;
}
else if (floatValue != t.floatValue)
{
Undo.RecordObject(t, "Set float value");
t.floatValue = floatValue;
}
else if (stringValue != t.stringValue)
{
Undo.RecordObject(t, "Set string value");
t.stringValue = stringValue;
}
} }
Sequence onTrue = SequenceEditor.SequenceField(new GUIContent("On True Sequence", "Sequence to execute if comparision is true"), Sequence onTrue = SequenceEditor.SequenceField(new GUIContent("On True Sequence", "Sequence to execute if comparision is true"),

8
Assets/Fungus/Editor/FungusScript/SayEditor.cs

@ -29,10 +29,10 @@ namespace Fungus.Script
if (showCondition == Say.ShowCondition.BooleanIsFalse || if (showCondition == Say.ShowCondition.BooleanIsFalse ||
showCondition == Say.ShowCondition.BooleanIsTrue) showCondition == Say.ShowCondition.BooleanIsTrue)
{ {
booleanVariable = SequenceEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"), booleanVariable = FungusVariableEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"),
t.GetFungusScript (), t.GetFungusScript (),
t.booleanVariable, t.booleanVariable,
v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable; v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable;
} }
if (EditorGUI.EndChangeCheck()) if (EditorGUI.EndChangeCheck())

38
Assets/Fungus/Editor/FungusScript/SequenceEditor.cs

@ -3,7 +3,6 @@ using UnityEngine;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Fungus.Script namespace Fungus.Script
{ {
@ -48,43 +47,6 @@ namespace Fungus.Script
return result; return result;
} }
static public FungusVariable VariableField(GUIContent label, FungusScript fungusScript, FungusVariable variable, Func<FungusVariable, bool> filter = null)
{
List<string> variableKeys = new List<string>();
List<FungusVariable> variableObjects = new List<FungusVariable>();
variableKeys.Add("<None>");
variableObjects.Add(null);
FungusVariable[] variables = fungusScript.GetComponents<FungusVariable>();
int index = 0;
int selectedIndex = 0;
foreach (FungusVariable v in variables)
{
if (filter != null)
{
if (!filter(v))
{
continue;
}
}
variableKeys.Add(v.key);
variableObjects.Add(v);
index++;
if (v == variable)
{
selectedIndex = index;
}
}
selectedIndex = EditorGUILayout.Popup(label.text, selectedIndex, variableKeys.ToArray());
return variableObjects[selectedIndex];
}
} }
} }

83
Assets/Fungus/Editor/FungusScript/SetEditor.cs

@ -19,9 +19,9 @@ namespace Fungus.Script
return; return;
} }
FungusVariable variable = SequenceEditor.VariableField(new GUIContent("Variable", "Variable to set"), FungusVariable variable = FungusVariableEditor.VariableField(new GUIContent("Variable", "Variable to set"),
fungusScript, fungusScript,
t.variable); t.variable);
if (variable != t.variable) if (variable != t.variable)
{ {
@ -120,47 +120,52 @@ namespace Fungus.Script
t.setOperator = setOperator; t.setOperator = setOperator;
} }
bool booleanValue = t.booleanData.value; FungusVariable setVariable = FungusVariableEditor.VariableField(new GUIContent("Other Variable", "The variable to read the assigned value from."),
int integerValue = t.integerData.value; t.GetFungusScript(),
float floatValue = t.floatData.value; t.setVariable,
string stringValue = t.stringData.value; v => v.GetType() == variable.GetType ());
if (variable.GetType() == typeof(BooleanVariable)) if (setVariable != t.setVariable)
{
booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue);
}
else if (variable.GetType() == typeof(IntegerVariable))
{
integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue);
}
else if (variable.GetType() == typeof(FloatVariable))
{
floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue);
}
else if (variable.GetType() == typeof(StringVariable))
{ {
stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue); Undo.RecordObject(t, "Select Set Variable");
t.setVariable = setVariable;
} }
if (booleanValue != t.booleanData.value) if (setVariable == null)
{ {
Undo.RecordObject(t, "Set boolean value"); EditorGUI.BeginChangeCheck();
t.booleanData.value = booleanValue;
} bool booleanValue = t.booleanValue;
else if (integerValue != t.integerData.value) int integerValue = t.integerValue;
{ float floatValue = t.floatValue;
Undo.RecordObject(t, "Set integer value"); string stringValue = t.stringValue;
t.integerData.value = integerValue;
} if (variable.GetType() == typeof(BooleanVariable))
else if (floatValue != t.floatData.value) {
{ booleanValue = EditorGUILayout.Toggle (new GUIContent("Boolean Value", "A constant boolean value to set the variable with"), booleanValue);
Undo.RecordObject(t, "Set float value"); }
t.floatData.value = floatValue; else if (variable.GetType() == typeof(IntegerVariable))
} {
else if (stringValue != t.stringData.value) integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "A constant integer value to set the variable with"), integerValue);
{ }
Undo.RecordObject(t, "Set string value"); else if (variable.GetType() == typeof(FloatVariable))
t.stringData.value = stringValue; {
floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "A constant float value to set the variable with"), floatValue);
}
else if (variable.GetType() == typeof(StringVariable))
{
stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "A constant string value to set the variable with"), stringValue);
}
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(t, "Set Variable Data");
t.booleanValue = booleanValue;
t.integerValue = integerValue;
t.floatValue = floatValue;
t.stringValue = stringValue;
}
} }
} }
} }

54
Assets/Fungus/VisualScripting/Jump.cs

@ -30,13 +30,15 @@ namespace Fungus.Script
public CompareOperator compareOperator; public CompareOperator compareOperator;
public BooleanData booleanData; public FungusVariable compareVariable;
public IntegerData integerData; public bool booleanValue;
public FloatData floatData; public int integerValue;
public StringData stringData; public float floatValue;
public string stringValue;
public Sequence onTrueSequence; public Sequence onTrueSequence;
@ -61,75 +63,87 @@ namespace Fungus.Script
if (variable.GetType() == typeof(BooleanVariable)) if (variable.GetType() == typeof(BooleanVariable))
{ {
bool lhs = (variable as BooleanVariable).Value;
bool rhs = (compareVariable as BooleanVariable) == null ? booleanValue : (compareVariable as BooleanVariable).Value;
switch (compareOperator) switch (compareOperator)
{ {
case CompareOperator.Equals: case CompareOperator.Equals:
condition = (variable as BooleanVariable).Value == booleanData.value; condition = lhs == rhs;
break; break;
case CompareOperator.NotEquals: case CompareOperator.NotEquals:
default: default:
condition = (variable as BooleanVariable).Value != booleanData.value; condition = lhs != rhs;
break; break;
} }
} }
else if (variable.GetType() == typeof(IntegerVariable)) else if (variable.GetType() == typeof(IntegerVariable))
{ {
int lhs = (variable as IntegerVariable).Value;
int rhs = (compareVariable as IntegerVariable) == null ? integerValue : (compareVariable as IntegerVariable).Value;
switch (compareOperator) switch (compareOperator)
{ {
case CompareOperator.Equals: case CompareOperator.Equals:
condition = (variable as IntegerVariable).Value == integerData.value; condition = lhs == rhs;
break; break;
case CompareOperator.NotEquals: case CompareOperator.NotEquals:
condition = (variable as IntegerVariable).Value != integerData.value; condition = lhs != rhs;
break; break;
case CompareOperator.LessThan: case CompareOperator.LessThan:
condition = (variable as IntegerVariable).Value < integerData.value; condition = lhs < rhs;
break; break;
case CompareOperator.GreaterThan: case CompareOperator.GreaterThan:
condition = (variable as IntegerVariable).Value > integerData.value; condition = lhs > rhs;
break; break;
case CompareOperator.LessThanOrEquals: case CompareOperator.LessThanOrEquals:
condition = (variable as IntegerVariable).Value <= integerData.value; condition = lhs <= rhs;
break; break;
case CompareOperator.GreaterThanOrEquals: case CompareOperator.GreaterThanOrEquals:
condition = (variable as IntegerVariable).Value >= integerData.value; condition = lhs >= rhs;
break; break;
} }
} }
else if (variable.GetType() == typeof(FloatVariable)) else if (variable.GetType() == typeof(FloatVariable))
{ {
float lhs = (variable as FloatVariable).Value;
float rhs = (compareVariable as FloatVariable) == null ? floatValue : (compareVariable as FloatVariable).Value;
switch (compareOperator) switch (compareOperator)
{ {
case CompareOperator.Equals: case CompareOperator.Equals:
condition = (variable as FloatVariable).Value == floatData.value; condition = lhs == rhs;
break; break;
case CompareOperator.NotEquals: case CompareOperator.NotEquals:
condition = (variable as FloatVariable).Value != floatData.value; condition = lhs != rhs;
break; break;
case CompareOperator.LessThan: case CompareOperator.LessThan:
condition = (variable as FloatVariable).Value < floatData.value; condition = lhs < rhs;
break; break;
case CompareOperator.GreaterThan: case CompareOperator.GreaterThan:
condition = (variable as FloatVariable).Value > floatData.value; condition = lhs > rhs;
break; break;
case CompareOperator.LessThanOrEquals: case CompareOperator.LessThanOrEquals:
condition = (variable as FloatVariable).Value <= floatData.value; condition = lhs <= rhs;
break; break;
case CompareOperator.GreaterThanOrEquals: case CompareOperator.GreaterThanOrEquals:
condition = (variable as FloatVariable).Value >= floatData.value; condition = lhs >= rhs;
break; break;
} }
} }
else if (variable.GetType() == typeof(StringVariable)) else if (variable.GetType() == typeof(StringVariable))
{ {
string lhs = (variable as StringVariable).Value;
string rhs = (compareVariable as StringVariable) == null ? stringValue : (compareVariable as StringVariable).Value;
switch (compareOperator) switch (compareOperator)
{ {
case CompareOperator.Equals: case CompareOperator.Equals:
condition = (variable as StringVariable).Value == stringData.value; condition = lhs == rhs;
break; break;
case CompareOperator.NotEquals: case CompareOperator.NotEquals:
default: default:
condition = (variable as StringVariable).Value != stringData.value; condition = lhs != rhs;
break; break;
} }
} }

54
Assets/Fungus/VisualScripting/Set.cs

@ -20,13 +20,15 @@ namespace Fungus.Script
public SetOperator setOperator; public SetOperator setOperator;
public BooleanData booleanData; public FungusVariable setVariable;
public IntegerData integerData; public bool booleanValue;
public FloatData floatData; public int integerValue;
public StringData stringData; public float floatValue;
public string stringValue;
public override void OnEnter() public override void OnEnter()
{ {
@ -38,74 +40,80 @@ namespace Fungus.Script
if (variable.GetType() == typeof(BooleanVariable)) if (variable.GetType() == typeof(BooleanVariable))
{ {
BooleanVariable lhs = (variable as BooleanVariable);
bool rhs = (setVariable as BooleanVariable) == null ? booleanValue : (setVariable as BooleanVariable).Value;
switch (setOperator) switch (setOperator)
{ {
default: default:
case SetOperator.Assign: case SetOperator.Assign:
(variable as BooleanVariable).Value = booleanData.value; lhs.Value = rhs;
break; break;
case SetOperator.Negate: case SetOperator.Negate:
(variable as BooleanVariable).Value = !booleanData.value; lhs.Value = !rhs;
break; break;
} }
} }
else if (variable.GetType() == typeof(IntegerVariable)) else if (variable.GetType() == typeof(IntegerVariable))
{ {
IntegerVariable lhs = (variable as IntegerVariable);
int rhs = (setVariable as IntegerVariable) == null ? integerValue : (setVariable as IntegerVariable).Value;
switch (setOperator) switch (setOperator)
{ {
default: default:
case SetOperator.Assign: case SetOperator.Assign:
(variable as IntegerVariable).Value = integerData.value; lhs.Value = rhs;
break;
case SetOperator.Negate:
(variable as IntegerVariable).Value = -integerData.value;
break; break;
case SetOperator.Add: case SetOperator.Add:
(variable as IntegerVariable).Value += integerData.value; lhs.Value += rhs;
break; break;
case SetOperator.Subtract: case SetOperator.Subtract:
(variable as IntegerVariable).Value -= integerData.value; lhs.Value -= rhs;
break; break;
case SetOperator.Multiply: case SetOperator.Multiply:
(variable as IntegerVariable).Value *= integerData.value; lhs.Value *= rhs;
break; break;
case SetOperator.Divide: case SetOperator.Divide:
(variable as IntegerVariable).Value /= integerData.value; lhs.Value /= rhs;
break; break;
} }
} }
else if (variable.GetType() == typeof(FloatVariable)) else if (variable.GetType() == typeof(FloatVariable))
{ {
FloatVariable lhs = (variable as FloatVariable);
float rhs = (setVariable as FloatVariable) == null ? floatValue : (setVariable as FloatVariable).Value;
switch (setOperator) switch (setOperator)
{ {
default: default:
case SetOperator.Assign: case SetOperator.Assign:
(variable as FloatVariable).Value = floatData.value; lhs.Value = rhs;
break;
case SetOperator.Negate:
(variable as FloatVariable).Value = -floatData.value;
break; break;
case SetOperator.Add: case SetOperator.Add:
(variable as FloatVariable).Value += floatData.value; lhs.Value += rhs;
break; break;
case SetOperator.Subtract: case SetOperator.Subtract:
(variable as FloatVariable).Value -= floatData.value; lhs.Value -= rhs;
break; break;
case SetOperator.Multiply: case SetOperator.Multiply:
(variable as FloatVariable).Value *= floatData.value; lhs.Value *= rhs;
break; break;
case SetOperator.Divide: case SetOperator.Divide:
(variable as FloatVariable).Value /= floatData.value; lhs.Value /= rhs;
break; break;
} }
} }
else if (variable.GetType() == typeof(StringVariable)) else if (variable.GetType() == typeof(StringVariable))
{ {
StringVariable lhs = (variable as StringVariable);
string rhs = (setVariable as StringVariable) == null ? stringValue : (setVariable as StringVariable).Value;
switch (setOperator) switch (setOperator)
{ {
default: default:
case SetOperator.Assign: case SetOperator.Assign:
(variable as StringVariable).Value = stringData.value; lhs.Value = rhs;
break; break;
} }
} }

35
Assets/Fungus/VisualScripting/Variable.cs

@ -1,35 +0,0 @@
using UnityEngine;
using System;
using System.Collections;
namespace Fungus.Script
{
[Serializable]
public class VariableData
{
}
[Serializable]
public class BooleanData : VariableData
{
public bool value;
}
[Serializable]
public class IntegerData : VariableData
{
public int value;
}
[Serializable]
public class FloatData : VariableData
{
public float value;
}
[Serializable]
public class StringData : VariableData
{
public string value;
}
}

8
Assets/Fungus/VisualScripting/Variable.cs.meta

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 2f3853d313cd94fe184e3478cefc11f2
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
Loading…
Cancel
Save