Defining widget properties
Property definitions can only appear in widget modules. They define a property which can be accessed from LiveCode Script in the usual way (e.g. the myProperty of widget 1).
The syntax for declaring properties is
The getterIdentifier and setterIdentifier can use either a variable or handler identifier. If a variable identifier is used, then the property value is fetched (and stored) from that variable. If a handler identifier is used then a handler is called instead.
A getter handler must take no arguments and return a value. A setter handler must take a single argument and return no value.
The set clause is optional. If it is not present then the property is read-only.
The rotation Property
The widget will only have one property, rotation. The vale of the rotation property will be stored in the mRotation variable.
The getter
The getter for the rotation property will return the value of mRotation.
The setter
The setter for the rotation property will call a handler, setRotation. Implementing the “setter” ourselves provides us with a little more flexibility and allows us to take multiple actions when the property is set.
Add the property definition to the widget code.
use com.livecode.widget use com.livecode.engine use com.livecode.library.widgetutils metadata title is "Rotated Text" metadata author is "Elanor Buchanan" metadata version is "1.0.0" variable mRotation as Integer property "rotation" get mRotation set setRotation end widget
The setRotation handler
The setRotation handler allows us to take multiple actions when the property is updated
- The in parameter is an integer value
- The handler does not return a value
- Update mRotation with the new value.
- Redraw the widget to reflect the property change. We do this by calling “redraw all”.
Add the setRotation handler to the widget code.
public handler setRotation(in pRotation as Integer) returns nothing end handler end widget
The Property Inspector
Any properties that are defined in the widget are automatically shown in the Basic pane of the Property Inspector.
We will be looking at integrating properties into the Property Inspector in more detail in the next lesson.