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:
Then register a new operator and use the existing matrix for &&
(See the ExpressionParserZero source and documentation for more details)
Overloads
Suppose you want to add a long to a string
Get a reference to the default ExpressionParser:
Then simply register the overload like this
and to add a string to a long:
Putting the above into action, you can then start to really have some fun
Last updated