|
|
|
@ -4,21 +4,12 @@ using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Fungus |
|
|
|
|
{ |
|
|
|
|
public enum CompareOperator |
|
|
|
|
{ |
|
|
|
|
Equals, // == |
|
|
|
|
NotEquals, // != |
|
|
|
|
LessThan, // < |
|
|
|
|
GreaterThan, // > |
|
|
|
|
LessThanOrEquals, // <= |
|
|
|
|
GreaterThanOrEquals // >= |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[CommandInfo("Scripting", |
|
|
|
|
"If", |
|
|
|
|
"If the test expression is true, execute the following block of commands.")] |
|
|
|
|
[AddComponentMenu("")] |
|
|
|
|
public class If : Command |
|
|
|
|
public class If : Condition |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
[Tooltip("Variable to use in expression")] |
|
|
|
@ -28,9 +19,6 @@ namespace Fungus
|
|
|
|
|
typeof(StringVariable))] |
|
|
|
|
public Variable variable; |
|
|
|
|
|
|
|
|
|
[Tooltip("The type of comparison to be performed")] |
|
|
|
|
public CompareOperator compareOperator; |
|
|
|
|
|
|
|
|
|
[Tooltip("Boolean value to compare against")] |
|
|
|
|
public BooleanData booleanData; |
|
|
|
|
|
|
|
|
@ -50,143 +38,93 @@ namespace Fungus
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool condition = false; |
|
|
|
|
|
|
|
|
|
if (variable == null) |
|
|
|
|
{ |
|
|
|
|
Continue(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (variable.GetType() == typeof(BooleanVariable)) |
|
|
|
|
{ |
|
|
|
|
bool lhs = (variable as BooleanVariable).value; |
|
|
|
|
bool rhs = booleanData.Value; |
|
|
|
|
EvaluateCondition(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch (compareOperator) |
|
|
|
|
{ |
|
|
|
|
case CompareOperator.Equals: |
|
|
|
|
condition = lhs == rhs; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.NotEquals: |
|
|
|
|
default: |
|
|
|
|
condition = lhs != rhs; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
protected void EvaluateCondition() |
|
|
|
|
{ |
|
|
|
|
BooleanVariable booleanVariable = variable as BooleanVariable; |
|
|
|
|
IntegerVariable integerVariable = variable as IntegerVariable; |
|
|
|
|
FloatVariable floatVariable = variable as FloatVariable; |
|
|
|
|
StringVariable stringVariable = variable as StringVariable; |
|
|
|
|
|
|
|
|
|
bool condition = false; |
|
|
|
|
|
|
|
|
|
if (booleanVariable != null) |
|
|
|
|
{ |
|
|
|
|
condition = booleanVariable.Evaluate(compareOperator, booleanData.Value); |
|
|
|
|
} |
|
|
|
|
else if (variable.GetType() == typeof(IntegerVariable)) |
|
|
|
|
else if (integerVariable != null) |
|
|
|
|
{ |
|
|
|
|
int lhs = (variable as IntegerVariable).value; |
|
|
|
|
int rhs = integerData.Value; |
|
|
|
|
|
|
|
|
|
switch (compareOperator) |
|
|
|
|
{ |
|
|
|
|
case CompareOperator.Equals: |
|
|
|
|
condition = lhs == rhs; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.NotEquals: |
|
|
|
|
condition = lhs != rhs; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.LessThan: |
|
|
|
|
condition = lhs < rhs; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.GreaterThan: |
|
|
|
|
condition = lhs > rhs; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.LessThanOrEquals: |
|
|
|
|
condition = lhs <= rhs; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.GreaterThanOrEquals: |
|
|
|
|
condition = lhs >= rhs; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
condition = integerVariable.Evaluate(compareOperator, integerData.Value); |
|
|
|
|
} |
|
|
|
|
else if (variable.GetType() == typeof(FloatVariable)) |
|
|
|
|
else if (floatVariable != null) |
|
|
|
|
{ |
|
|
|
|
float lhs = (variable as FloatVariable).value; |
|
|
|
|
float rhs = floatData.Value; |
|
|
|
|
|
|
|
|
|
switch (compareOperator) |
|
|
|
|
{ |
|
|
|
|
case CompareOperator.Equals: |
|
|
|
|
condition = lhs == rhs; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.NotEquals: |
|
|
|
|
condition = lhs != rhs; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.LessThan: |
|
|
|
|
condition = lhs < rhs; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.GreaterThan: |
|
|
|
|
condition = lhs > rhs; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.LessThanOrEquals: |
|
|
|
|
condition = lhs <= rhs; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.GreaterThanOrEquals: |
|
|
|
|
condition = lhs >= rhs; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
condition = floatVariable.Evaluate(compareOperator, floatData.Value); |
|
|
|
|
} |
|
|
|
|
else if (variable.GetType() == typeof(StringVariable)) |
|
|
|
|
else if (stringVariable != null) |
|
|
|
|
{ |
|
|
|
|
string lhs = (variable as StringVariable).value; |
|
|
|
|
string rhs = stringData.Value; |
|
|
|
|
|
|
|
|
|
switch (compareOperator) |
|
|
|
|
{ |
|
|
|
|
case CompareOperator.Equals: |
|
|
|
|
condition = lhs == rhs; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.NotEquals: |
|
|
|
|
default: |
|
|
|
|
condition = lhs != rhs; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
condition = stringVariable.Evaluate(compareOperator, stringData.Value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (condition) |
|
|
|
|
{ |
|
|
|
|
Continue(); |
|
|
|
|
OnTrue(); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
// Find the next Else or EndIf command at the same indent level as this If command |
|
|
|
|
for (int i = commandIndex + 1; i < parentSequence.commandList.Count; ++i) |
|
|
|
|
{ |
|
|
|
|
Command nextCommand = parentSequence.commandList[i]; |
|
|
|
|
OnFalse(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void OnTrue() |
|
|
|
|
{ |
|
|
|
|
Continue(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Find next command at same indent level as this If command |
|
|
|
|
// Skip disabled commands & comments |
|
|
|
|
if (!nextCommand.enabled || |
|
|
|
|
nextCommand.GetType() == typeof(Comment) || |
|
|
|
|
nextCommand.indentLevel != indentLevel) |
|
|
|
|
public void OnFalse() |
|
|
|
|
{ |
|
|
|
|
// Find the next Else or EndIf command at the same indent level as this If command |
|
|
|
|
for (int i = commandIndex; i < parentSequence.commandList.Count; ++i) |
|
|
|
|
{ |
|
|
|
|
Command nextCommand = parentSequence.commandList[i]; |
|
|
|
|
|
|
|
|
|
// Find next command at same indent level as this If command |
|
|
|
|
// Skip disabled commands & comments |
|
|
|
|
if (!nextCommand.enabled || |
|
|
|
|
nextCommand.GetType() == typeof(Comment) || |
|
|
|
|
nextCommand.indentLevel != indentLevel) |
|
|
|
|
{ |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
System.Type type = nextCommand.GetType(); |
|
|
|
|
if (type == typeof(Else) || |
|
|
|
|
type == typeof(EndIf) || // Legacy support for old EndIf command |
|
|
|
|
type == typeof(End)) |
|
|
|
|
{ |
|
|
|
|
if (i >= parentSequence.commandList.Count - 1) |
|
|
|
|
{ |
|
|
|
|
continue; |
|
|
|
|
// Last command in Sequence, so stop |
|
|
|
|
Stop(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
System.Type type = nextCommand.GetType(); |
|
|
|
|
if (type == typeof(Else) || |
|
|
|
|
type == typeof(EndIf) || // Legacy support for old EndIf command |
|
|
|
|
type == typeof(End)) |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
if (i >= parentSequence.commandList.Count - 1) |
|
|
|
|
{ |
|
|
|
|
// Last command in Sequence, so stop |
|
|
|
|
Stop(); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
// Execute command immediately after the Else or End command |
|
|
|
|
Continue(nextCommand); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
// Execute command immediately after the Else or End command |
|
|
|
|
Continue(nextCommand); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// No matching End command found, so just stop the sequence |
|
|
|
|
Stop(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// No matching End command found, so just stop the sequence |
|
|
|
|
Stop(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override string GetSummary() |
|
|
|
@ -196,28 +134,8 @@ namespace Fungus
|
|
|
|
|
return "Error: No variable selected"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string summary = variable.key; |
|
|
|
|
switch (compareOperator) |
|
|
|
|
{ |
|
|
|
|
case CompareOperator.Equals: |
|
|
|
|
summary += " == "; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.NotEquals: |
|
|
|
|
summary += " != "; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.LessThan: |
|
|
|
|
summary += " < "; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.GreaterThan: |
|
|
|
|
summary += " > "; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.LessThanOrEquals: |
|
|
|
|
summary += " <= "; |
|
|
|
|
break; |
|
|
|
|
case CompareOperator.GreaterThanOrEquals: |
|
|
|
|
summary += " >= "; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
string summary = variable.key + " "; |
|
|
|
|
summary += Condition.GetOperatorDescription(compareOperator) + " "; |
|
|
|
|
|
|
|
|
|
if (variable.GetType() == typeof(BooleanVariable)) |
|
|
|
|
{ |
|
|
|
|