Difference between revisions of "Buttons"

m
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
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.
+
[[Category: Buttons]]
  
The ''Ideal Gas Law'' example model illustrates a simplistic use of buttons.
+
__TOC__
  
[[image:Ideal_gas_law.png]]
+
A [[Button]] is a [[User_interfaces|User interface]] control that you can click to do something by evaluating its [[OnClick]] attribute.
 +
It usually appear with a beveled edge, with a Title that gives an indication of what they do when clicked. An [[OnClick]] attribute can contain almost any kind of expression, including assigning to global variables (which is not usually allowed in Definitions of Variables). It can open or close windows, hide or show variables, and more, to add features that go beyond standard Analytica functionality.
  
This interface contains four buttons: <code>Find_P</code>, <code>Find_V</code>, <code>Find_n</code> and <code>Find_T</code>. When you press <code>Find_P</code>, in calculates the pressure in an ideal gas given the volume, number of molecules and temperature, and then sets the value of <code>P</code> to this value. We say the button has a ''side-effect'' of changing the value of variable <code>P</code>.
+
The ''Ideal Gas Law'' example model illustrates a simple use of buttons.
  
= Creating a Button =
+
:[[image:Ideal_gas_law.png]]
  
From Analytica Enterprise, a button is created by dragging a button node from the toolbar while in edit mode:
+
This interface contains four buttons: <code>Find_P</code>, <code>Find_V</code>, <code>Find_n</code> and <code>Find_T</code>. When you press <code>Find_P</code>, it calculates the pressure in an ideal gas given the volume, number of molecules and temperature, and then sets the value of <code>P</code> to this value. We say the button has a ''side-effect'' of changing the value of variable <code>P</code>.
  
[[image:button_toolbar.png]]
+
== Creating a Button ==
  
= Pressing a button =
+
You need Analytica Enterprise or above to create a button. You simply drag the button icon from the toolbar while in edit mode, like any other class of object:
  
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.
+
:[[image:button_toolbar.png]]
  
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.
+
== Pressing a button ==
  
= OnClick attribute =
+
When in browse mode, you click a button simply by pressing the mouse button once with the cursor over the button. In edit mode, mouse over a button to see a green arrow head below the button. Click this is the same as clicking the button in browse mode.
  
''new to [[Analytica 4.6]]''
+
You can also press the tab key until you have selected the button, and then press ''Enter''.
  
When a button is pressed, the [[OnClick]] attribute of the button is evaluated. The [[OnClick]] attribute [[Expression Syntax|an expression]] using precisely the same syntax as the '''[[Definition]]''' of a variable. From the [[OnClick]] expression, you can make use of any [[Alphabetical Function List|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.  
+
== OnClick attribute ==
 +
 
 +
When you click a button, it evaluates its [[OnClick]] attribute. The [[OnClick]] attribute [[Expression Syntax|an expression]] using the same syntax as the [[Definition]] of a variable. From the [[OnClick]] expression, you can make use of any [[Alphabetical Function List|Analytica function]]. Unlike a definition, you can use assignments to global variables or attributes in an Onclick attribute, it does not store or return the final result of evaluation if any.  
  
 
For example, in the ideal gas law model (ignoring for the moment the flexibility to select units of measurement), the <code>Find_P</code> script would be an [[Assignment_Operator::_::%3D|Assignment]] to <code>P</code> as follows.
 
For example, in the ideal gas law model (ignoring for the moment the flexibility to select units of measurement), the <code>Find_P</code> script would be an [[Assignment_Operator::_::%3D|Assignment]] to <code>P</code> as follows.
P := n * R * T / V
+
:<code>P := n*R*T/V</code>
 
 
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 =
 
  
The [[Script]] attribute of the button contains [[Typescript|typescript]] code that you write, and is executed after the [[OnClick]] expression evaluates. If an error occurs during the evaluation of [[OnClick]], the [[Script]] is not executed. [[Typescript]] uses a different language from the [[Expression Syntax]] used in a variable ''Definition''.
+
== Picture nodes ==
  
The [[Script]] attribute is deprecated as of [[Analytica 4.6]] as a result of the introduction of the [[OnClick]] attribute. The problem with the [[Script]] attribute is that because it uses what is essentially a different language, it means that you need to learn about [[Typescript]] in addition to [[Expression Syntax|Analytica expressions]]. The [[Script]] attribute continues to function so that legacy models work fine, and in some rare circumstances, you may simply want to run a particular typescript command, in which case [[Script]] would be more convenient.
+
A picture node, created when you copy/paste and image into a diagram, will also act like a button if you define an [[OnClick]]. Click on the image in browse mode, or ''Alt+click'' in Edit mode, to evaluate the [[OnClick]] attribute (or [[Script]]) attribute), just as for a button.
  
Because there are several gotchas with writing button scripts -- especially the confusion that often arises between the [[Assignment_Operator::_::%3D|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.
+
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'').
  
= Picture nodes =
+
== click_x and click_y ==
  
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.
+
You can use the variables <code>click_x</code> and <code>click_y</code> in an [[OnClick]] expression. They contain the cursor coordinates  when the button or Picture was clicked. Coordinates are in pixels relative to the top-left corner of the button or image for a picture node. This can be handy if you want to do something that varies according to where the user clicks on a button or picture. Coordinates are null if you press the button without a mouse click -- e.g. press enter when the button was selected.
  
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).
+
== Script Attribute ==
  
= click_x and click_y =
+
The [[Script]] attribute was replaced by the  [[OnClick]] attribute in [[Analytica 4.6]]. The [[Script]] attribute still works but is deprecated. The [[Script]] attribute contains [[Typescript]] language which is subtly (and confusingly) different from the [[Expression Syntax]] used in a variable ''Definition'' or  [[OnClick]] attribute -- which is  why we  replaced it.  There are several gotchas with writing button scripts -- especially the confusion that often arises between the [[Assignment_Operator::_::%3D|assignment operator]] in expression syntax and typescript assignment, both of which use ":=".
  
From within the [[OnClick]] expression, you may refer to the variables <code>click_x</code> and <code>click_y</code>, 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 a button contains both [[OnClick]] and [[Script]] attributes (not recommended), 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]] or [[OnChange]], it does not execute the [[Script]].
  
If the button was pressed without a mouse click (e.g., pressing enter when it was selected), then <code>click_x</code> and <code>click_y</code> are both null.
+
== History ==
  
= See Also =
+
In [[Analytica 4.6]], we introduced the OnClick attribute to replace the Script attribute, which is now deprecated, but still exists in some legacy models.
  
 +
== See Also ==
 +
* [[Button creation]]
 +
* [[Analytica_Script#Button_Scripts|Button scripts]]
 
* [[OnClick]]
 
* [[OnClick]]
 
* [[OnChange]] -- particularly for checkboxes, choice dropdowns, etc.
 
* [[OnChange]] -- particularly for checkboxes, choice dropdowns, etc.
 
* [[Scripting]]
 
* [[Scripting]]
 
* [[Typescript]]
 
* [[Typescript]]
 +
* [[User interfaces]]

Latest revision as of 02:57, 17 November 2019


A Button is a User interface control that you can click to do something by evaluating its OnClick attribute. It usually appear with a beveled edge, with a Title that gives an indication of what they do when clicked. An OnClick attribute can contain almost any kind of expression, including assigning to global variables (which is not usually allowed in Definitions of Variables). It can open or close windows, hide or show variables, and more, to add features that go beyond standard Analytica functionality.

The Ideal Gas Law example model illustrates a simple 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

You need Analytica Enterprise or above to create a button. You simply drag the button icon from the toolbar while in edit mode, like any other class of object:

Button toolbar.png

Pressing a button

When in browse mode, you click a button simply by pressing the mouse button once with the cursor over the button. In edit mode, mouse over a button to see a green arrow head below the button. Click this is the same as clicking the button in browse mode.

You can also press the tab key until you have selected the button, and then press Enter.

OnClick attribute

When you click a button, it evaluates its OnClick attribute. The OnClick attribute an expression using the same syntax as the Definition of a variable. From the OnClick expression, you can make use of any Analytica function. Unlike a definition, you can use assignments to global variables or attributes in an Onclick attribute, it does not store or return the final result of evaluation if any.

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

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. Click on the image in browse mode, or Alt+click in Edit mode, to evaluate the OnClick attribute (or Script) attribute), 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

You can use the variables click_x and click_y in an OnClick expression. They contain the cursor coordinates when the button or Picture was clicked. Coordinates are in pixels relative to the top-left corner of the button or image for a picture node. This can be handy if you want to do something that varies according to where the user clicks on a button or picture. Coordinates are null if you press the button without a mouse click -- e.g. press enter when the button was selected.

Script Attribute

The Script attribute was replaced by the OnClick attribute in Analytica 4.6. The Script attribute still works but is deprecated. The Script attribute contains Typescript language which is subtly (and confusingly) different from the Expression Syntax used in a variable Definition or OnClick attribute -- which is why we replaced it. 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 ":=".

If a button contains both OnClick and Script attributes (not recommended), 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 or OnChange, it does not execute the Script.

History

In Analytica 4.6, we introduced the OnClick attribute to replace the Script attribute, which is now deprecated, but still exists in some legacy models.

See Also

Comments


You are not allowed to post comments.