Advanced Usage - Functions, aliases and operator-overloads
z:Bind
uses FunctionZero.ExpressionParserZero
to do the heavy lifting, so take a look at the documentation if you want to take a deeper dive. Here is a taster ...
Functions
Sin
, Cos
and Tan
are registered by default, as are the aliases listed above.
<Label TranslationX="{z:Bind Sin(Count / 25.0) * 100.0}" ...
Suppose you wanted a new function to to do a linear interpolation between two values, like this: (Spoiler: Lerp is also pre-registered)
float Lerp(float a, float b, float t)
{
return a + t * (b - a);
}
For use like this:
<Label Rotation={z:Bind Lerp(0, 360, rotationPercent / 100.0)} ...
First you will need a reference to the default ExpressionParser
var ep = ExpressionParserFactory.GetExpressionParser();
Then register a function that takes 3 parameters:
ep.RegisterFunction("Lerp", DoLerp, 3);
Finally write the DoLerp method referenced above.
private static void DoLerp(Stack<IOperand> stack, IBackingStore backingStore, long paramCount)
{
// Pop the correct number of parameters from the operands stack, ** in reverse order **
// If an operand is a variable, it is resolved from the backing store provided
IOperand third = OperatorActions.PopAndResolve(operands, backingStore);
IOperand second = OperatorActions.PopAndResolve(operands, backingStore);
IOperand first = OperatorActions.PopAndResolve(operands, backingStore);
float a = Convert.ToSingle(first.GetValue());
float b = Convert.ToSingle(second.GetValue());
float t = Convert.ToSingle(third.GetValue());
// The result is of type float
float result = a + t * (b - a);
// Push the result back onto the operand stack
stack.Push(new Operand(-1, OperandType.Float, result));
}
Aliases
Get a reference to the default ExpressionParser:
var ep = ExpressionParserFactory.GetExpressionParser();
Then register a new operator
and use the existing matrix
for &&
(See the ExpressionParserZero
source and documentation for more details)
ep.RegisterOperator("AND", 4, LogicalAndMatrix.Create());
Overloads
Suppose you want to add a long
to a string
Get a reference to the default ExpressionParser:
var ep = ExpressionParserFactory.GetExpressionParser();
Then simply register the overload like this
// Overload that will allow a long to be appended to a string
// To add a string to a long you'll need to add another overload
ep.RegisterOverload("+", OperandType.String, OperandType.Long,
(left, right) => new Operand(OperandType.String, (string)left.GetValue() + ((long)right.GetValue()).ToString()));
and to add a string
to a long
:
// Overload that will allow a string to be appended to a long
// To add a long to a string you'll need to add another overload
ep.RegisterOverload("+", OperandType.Long, OperandType.String,
(left, right) => new Operand(OperandType.String, (long)left.GetValue() + ((string)right.GetValue()).ToString()));
Putting the above into action, you can then start to really have some fun
<Label
Text="{z:Bind '\'Player 1 score \' + playerOne.Score + \'points\''}
Rotation="{z:Bind 'Lerp(0, 360, rotationPercent / 100.0)'"}
/>
Last updated