Expressions and symbols


#1
RelativeRectangle labelRect;
Expression labelHeight(20);
		
labelRect.left=RelativeCoordinate("5");
labelRect.right=RelativeCoordinate("parent.width - 30");
labelRect.top=RelativeCoordinate(Expression("parent.height / 2") - labelHeight/Expression(2));
labelRect.bottom=RelativeCoordinate(Expression("top")+labelHeight);

setBounds(labelRect);

While the above works, its kinda clumsy(deliberately so for clarity). What i’m wondering is if there’s a way to define a symbol for an expression(without using markers). In this case I want labelHeight to be 20… If that’s somehow possible then I could simplify the top definition to be

 RelativeCoordinate("parent.height/2 - labelHeight/2");

And then i’m hoping changing the symbol labelHeight to a different number would update the RelativeRectangle with no interference by me!
Does that make sense?


#2

Sure you can i created a very elaborate expression subsystem in my app (with math functions etc), you just need to subclass Expression::Scope and pass your own to the Expression you are evaluating, you can supply your own symbols and functions.


#3

Thanks Atom, I’m still trying to wrap my head around everything with expressions. Seems very powerful.

For what I want though would this(Custom scope) work? I think RelativeRectangle’s applyToComponent is used to set the bounds and it seems to always supply a null scope which then makes a RelativeRectangleLocalScope.

Do you use expressions and custom Symbols/Functions to setBounds? (Without needing to manually evaluate the expression?)


#4

I use expressions for lots of stuff, didn’t use them for relative bounds etc. I use them to evaluate certain values so that end users have flexibility when setting some stuff. Have a look here:
http://ctrlrv4.svn.sourceforge.net/viewvc/ctrlrv4/nightly/Source/Core/CtrlrModulator/CtrlrModulatorProcessor.cpp?revision=1118&view=markup and look for the evaluateFunction () and getSymbolValue () methods (those are Expression::Scope methods)
For the functions and their parameters: http://ctrlrv4.svn.sourceforge.net/viewvc/ctrlrv4/nightly/Source/Application/CtrlrUtilities.cpp?revision=1124&view=markup the method is evaluateFormulaFunction ()


#5

Thanks Atom, that’s very useful for the future.

I looked more at the sources, and a markerlist seems to be the way to go. I was keeping away from them because I didn’t want to have to subclass all the child components. However it seems Jules has thought ahead(as usual) and checks the parent component for a markerlist. So I think that will work for me!


#6

Just a follow up, this works great(using a marker list in my containing component)

ie.

markerList.setMarker("indent",20);
markerList.setMarker("buttonWidth",100);
markerList.setMarker("buttonHeight",25);

transferButton->setComponentID("transferButton"); // Important for other Expressions to reference this component...
transferButton->setBounds("indent+5,parent.height / 2 - buttonHeight/2,left+buttonWidth,top+buttonHeight"); // keeps vertically centred in parent

And sure enough, later on changing

markerList.setMarker("buttonWidth",250);

updates the bounds properly.

(I wanted to do this for buttons whose image is generated dynamically into an image with text drawn into it(with dropshadows and stuff), and depending on the font size preferences this button’s dimensions should grow). So then I can set constants in the marker list by querying the image for its ideal dimensions when the font size changes.