ASUL scripting
As of December 2010 the ASUL framework supports user scripts, by using the D.eval library to evaluate script blocks defined in ASUL definition files. This functionality is available in the MegaZine3 engine as of version 2.0.11.
For a detailed specification on the supported script features, please refer to the D.eval documentation. But in short it can be said: the scripts are ActionScript 3.0, with the only difference that a) it is not possible to declare classes, and b) function declarations do not use types, i.e. it's more like JavaScript in that regard.
[edit] Adding scripts to ASUL
To add a user script to ASUL, it is possible to use <script> blocks in the ASUL definitions. Depending on their location, they behave slightly different in regard to when they are run. Consider the following ASUL file:
<asul> <script><![CDATA[ function myhandler(e) { printf("[1] An event of element at position " + e.currentTarget.x + ":" + e.currentTarget.y); } printf("init block 1"); ]]></script> <box id="mybox" resize="true"> <script><![CDATA[ function myhandler2(e) { printf("[2] An event of element at position " + e.currentTarget.x + ":" + e.currentTarget.y); } printf("init block 2"); ]]></script> <button width="200" height="30" background="color(#990000)" onclick="myhandler(event)"/> <button width="200" height="30" y="40" background="color(#009900)" onclick="myhandler2(event)"/> </box> </asul>
Together with this AS3 code:
trace("start"); var f:AsulFactory = new AsulFactory(); trace("load"); f.loadXML(aboveAsul); trace("done loading"); var d:DisplayObject; addChild(d = f.createAsulObjectById("mybox")); d.y = 100; addChild(f.createAsulObjectById("mybox"));
Both buttons will work fine. The difference is when the code in the script blocks is evaluated. Running this will result in the following output:
start load [Thu Dec 23 2010 04:53:56 PM] SYSTEM [D.eval] init block 1 done loading [Thu Dec 23 2010 04:53:56 PM] SYSTEM [D.eval] init block 2 [Thu Dec 23 2010 04:53:56 PM] SYSTEM [D.eval] init block 2
Top level script blocks will be evaluated while the ASUL file is parsed. Script blocks nested inside ASUL definitions will be evaluated whenever the object is created. Note they will be run every time the container object is created.
Lastly, script in the so called event attributes will be run whenever the corresponding event takes place.
|
|
See: the box object for a list of available event attributes. |
Whenever the script in such an attribute is evaluated, a variable named event is made available in the script context. This variable will be the original ActionScript3 event variable representing the event.
In general it is possible to put any code whatsoever into event attributes, but for readability and reuseability it is recommended to define the actual handler functions in script block and then calling that function.
[edit] MegaZine3 specifics
In general it is possible for the environment to specify variables that are available in the global script context.
MegaZine3 does this and provides one or two variables, depending on the file:
- in all ASUL files a variable named
megazineis available (with shortcut aliasmz), which is a reference to theMegaZineinstance that loaded the files. - in all plugin ASUL files (i.e. all except
megazine.asul) a variablepluginis made available, which is a reference to the plugin to that the ASUL definition belongs. For example in the case of the gallery plugin it will be a reference to theGalleryinstance that belongs to theMegaZineinstance.
| ASUL-related articles | |
|---|---|
| Components | Box · Button · ProgressBar · ScrollBar · ScrollPane · Text · ToggleButton · Window |
| Layouts | Horizontal Flow Layout · Vertical Flow Layout |
| ASUL Articles | ASUL Document · Editing the GUI · Layouting · ASUL scripting |