Buttons


Buttons are User interface controls that you can place on your diagrams. They usually appear with a beveled edge, with a Title that gives an indication of what they do when clicked. When a user of your model clicks on the button, an action that you define is launched. A wide variety of things can be done when a button is clicked, including changing other variables, modifying the model itself, launching evaluations, opening or closing windows, evaluating expressions, and more. In some cases you can "implement" functionality that extends the functionality that comes with Analytica.

The Ideal Gas Law example model illustrates a simplistic use of buttons.

Ideal gas law.png

This interface contains four buttons: Find_P, Find_V, Find_n and Find_T. When you press Find_P, it calculates the pressure in an ideal gas given the volume, number of molecules and temperature, and then sets the value of P to this value. We say the button has a side-effect of changing the value of variable P.

Creating a Button

From Analytica Enterprise, a button is created by dragging a button node from the toolbar while in edit mode:

Button toolbar.png

Pressing a button

When in browse mode, the button can be clicked by pressing the mouse button once over the button. You can also press the tab key until the button becomes selected, and then press Enter.

In edit mode, a mouse click selects the object, allowing you to move, resize or edit it. To press the button, causing it to execute its action, you can hold down the ALT key when you click on it.

OnClick attribute

new to Analytica 4.6

When a button is pressed, the OnClick attribute of the button is evaluated. The OnClick attribute an expression using precisely the same syntax as the Definition of a variable. From the OnClick expression, you can make use of any Analytica function. Unlike a variable, side-effects such as assignments to other variables or attributes is allowed, and the final result of evaluation is not stored or returned.

For example, in the ideal gas law model (ignoring for the moment the flexibility to select units of measurement), the Find_P script would be an Assignment to P as follows.

P := n*R*T/V

Like a definition, expressions can span multiple lines, and sequential expressions are delineated by a semi-colon (;).

The OnClick attribute was introduced in Analytica 4.6. In Analytica 4.5 and earlier, you need to use the Script attribute.

Script Attribute

In Analytica 4.6 , the new OnClick attribute replaces the Script attribute, which still works but is now deprecated. We describe the Script attribute for those that need to deal with it in legacy models, or very rare situations when you might still want to use a Typescript command. The Script attribute contains Typescript language which is somewhat different from the Expression Syntax used in a variable Definition or OnClick attribute -- which is one reason why we replaced it.

If a button contains both OnClick and Script attribute, it executes the OnClick expression before the Script command. Similarly, if a variable contains both an OnChange and Script attribute, it executes the OnChange before the Script attribute. If an error occurs during the evaluation of OnClick pr OnChange, it does not execute the Script.

Because there are several gotchas with writing button scripts -- especially the confusion that often arises between the assignment operator in expression syntax and typescript assignment, both of which use := -- a recommended practice is to place all your code in a User-Defined Function, and limit your button script to a call to the function.

Picture nodes

A picture node, created when you copy/paste and image into a diagram, will also act like a button if you define an OnClick or Script attribute. If you click on the image in browse mode, or Alt+click in Edit mode, the OnClick attribute in evaluated and the Script attribute in run, just as for a button.

A picture node extent does not have to match the image extent. You can see this in edit mode when you select the node and resize the handles -- the image stays the same size, but the rectangle that defines the node boundaries changes. You will also see this if you select Fill in Set Node Style.... When you mouse click on a picture node, it is the node-extent, not the image extent, that determines the clickable rectangle. Usually it is best to size the node to the image (you can do this by pressing Ctrl+T).

click_x and click_y

From within the OnClick expression, you may refer to the variables click_x and click_y, which contain the coordinates of the mouse when the button was clicked relative to the top-left corner of the button or the top-left corner of the image for a picture node.

If the button was pressed without a mouse click (e.g., pressing enter when it was selected), then click_x and click_y are both null.

See Also

Comments


You are not allowed to post comments.