Next: , Previous: ClutterScriptable, Up: Top


55 ClutterScript

Loads a scene from UI definition data

55.1 Overview

<clutter-script> is an object used for loading and building parts or a complete scenegraph from external definition data in forms of string buffers or files.

The UI definition format is JSON, the JavaScript Object Notation as described by RFC 4627. <clutter-script> can load a JSON data stream, parse it and build all the objects defined into it. Each object must have an "id" and a "type" properties defining the name to be used to retrieve it from <clutter-script> with clutter-script-get-object, and the class type to be instanciated. Every other attribute will be mapped to the class properties.

A <clutter-script> holds a reference on every object it creates from the definition data, except for the stage. Every non-actor object will be finalized when the <clutter-script> instance holding it will be finalized, so they need to be referenced using g-object-ref in order for them to survive.

A simple object might be defined as:

     
     {
       "id"     : "red-button",
       "type"   : "ClutterRectangle",
       "width"  : 100,
       "height" : 100,
       "color"  : "&num;ff0000ff"
     }

This will produce a red <clutter-rectangle>, 100x100 pixels wide, and with a ClutterScript id of "red-button"; it can be retrieved by calling:

     
     ClutterActor *red_button;
     
     red_button = CLUTTER_ACTOR (clutter_script_get_object (script, "red-button"));

and then manipulated with the Clutter API. For every object created using ClutterScript it is possible to check the id by calling clutter-get-script-id.

Packing can be represented using the "children" member, and passing an array of objects or ids of objects already defined (but not packed: the packing rules of Clutter still apply, and an actor cannot be packed in multiple containers without unparenting it in between).

Behaviours and timelines can also be defined inside a UI definition buffer:

     
     {
       "id"          : "rotate-behaviour",
       "type"        : "ClutterBehaviourRotate",
       "angle-start" : 0.0,
       "angle-end"   : 360.0,
       "axis"        : "z-axis",
       "alpha"       : {
         "timeline" : { "duration" : 4000, "loop" : true },
         "mode"     : "easeInSine"
       }
     }

And then to apply a defined behaviour to an actor defined inside the definition of an actor, the "behaviour" member can be used:

     
     {
       "id" : "my-rotating-actor",
       "type" : "ClutterTexture",
       ...
       "behaviours" : [ "rotate-behaviour" ]
     }

A <clutter-alpha> belonging to a <clutter-behaviour> can only be defined implicitly like in the example above, or explicitly by setting the "alpha" property to point to a previously defined <clutter-alpha>, e.g.:

     
     {
       "id"          : "rotate-behaviour",
       "type"        : "ClutterBehaviourRotate",
       "angle-start" : 0.0,
       "angle-end"   : 360.0,
       "axis"        : "z-axis",
       "alpha"       : {
         "id"       : "rotate-alpha",
         "type"     : "ClutterAlpha",
         "timeline" : {
           "id"       : "rotate-timeline",
           "type      : "ClutterTimeline",
           "duration" : 4000,
           "loop"     : true
         },
         "function" : "custom_sine_alpha"
       }
     }

Implicitely defined <clutter-alpha>s and <clutter-timeline>s can omit the well as the clutter-script-get-object (they can, however, be extracted using the <clutter-behaviour> and <clutter-alpha> API respectively).

Signal handlers can be defined inside a Clutter UI definition file and then autoconnected to their respective signals using the clutter-script-connect-signals function:

     
       ...
       "signals" : [
         { "name" : "button-press-event", "handler" : "on_button_press" },
         {
           "name" : "foo-signal",
           "handler" : "after_foo",
           "after" : true
         },
       ],
       ...

Signal handler definitions must have a "name" and a "handler" members; they can also have the "after" and "swapped" boolean members (for the signal connection flags ‘G_CONNECT_AFTER’ and ‘G_CONNECT_SWAPPED’ respectively) and the "object" string member for calling g-signal-connect-object instead of g-signal-connect.

Signals can also be directly attached to a specific state defined inside a <clutter-state> instance, for instance:

     
       ...
       "signals" : [
         {
           "name" : "enter-event",
           "states" : "button-states",
           "target-state" : "hover"
         },
         {
           "name" : "leave-event",
           "states" : "button-states",
           "target-state" : "base"
         },
         {
           "name" : "button-press-event",
           "states" : "button-states",
           "target-state" : "active",
         },
         {
           "name" : "key-press-event",
           "states" : "button-states",
           "target-state" : "key-focus",
           "warp" : true
         }
       ],
       ...

The "states" key defines the <clutter-state> instance to be used to resolve the "target-state" key; it can be either a script id for a <clutter-state> built by the same <clutter-script> instance, or to a <clutter-state> built in code and associated to the <clutter-script> instance through the clutter-script-add-states function. If no "states" key is present, then the default <clutter-state> associated to the <clutter-script> instance will be used; the default <clutter-state> can be set using clutter-script-add-states using a ‘#f’ name. The "warp" key can be used to warp to a specific state instead of animating to it. State changes on signal emission will not affect the signal emission chain.

Clutter reserves the following names, so classes defining properties through the usual GObject registration process should avoid using these names to avoid collisions:

     
       "id"         := the unique name of a ClutterScript object
       "type"       := the class literal name, also used to infer the type
                       function
       "type_func"  := the GType function name, for non-standard classes
       "children"   := an array of names or objects to add as children
       "behaviours" := an array of names or objects to apply to an actor
       "signals"    := an array of signal definitions to connect to an object
       "is-default" := a boolean flag used when defining the #ClutterStage;
                       if set to "true" the default stage will be used instead
                       of creating a new #ClutterStage instance

<clutter-script> is available since Clutter 0.6

55.2 Usage

— Function: clutter-script-new ⇒  (ret <clutter-script>)

Creates a new <clutter-script> instance. <clutter-script> can be used to load objects definitions for scenegraph elements, like actors, or behavioural elements, like behaviours and timelines. The definitions must be encoded using the JavaScript Object Notation (JSON) language.

ret
the newly created <clutter-script> instance. Use g-object-unref when done.

Since 0.6

— Function: clutter-script-load-from-data (self <clutter-script>) (data mchars) (length ssize_t) ⇒  (ret unsigned-int)
— Method: load-from-data

Loads the definitions from data into script and merges with the currently loaded ones, if any.

script
a <clutter-script>
data
a buffer containing the definitions
length
the length of the buffer, or -1 if data is a NUL-terminated buffer
error
return location for a <g-error>, or ‘#f
ret
on error, zero is returned and error is set accordingly. On success, the merge id for the UI definitions is returned. You can use the merge id with clutter-script-unmerge-objects.

Since 0.6

— Function: clutter-script-load-from-file (self <clutter-script>) (filename mchars) ⇒  (ret unsigned-int)
— Method: load-from-file

Loads the definitions from filename into script and merges with the currently loaded ones, if any.

script
a <clutter-script>
filename
the full path to the definition file
error
return location for a <g-error>, or ‘#f
ret
on error, zero is returned and error is set accordingly. On success, the merge id for the UI definitions is returned. You can use the merge id with clutter-script-unmerge-objects.

Since 0.6

— Function: clutter-script-load-from-resource (self <clutter-script>) (resource_path mchars) ⇒  (ret unsigned-int)
— Method: load-from-resource

Loads the definitions from a resource file into script and merges with the currently loaded ones, if any.

script
a <clutter-script>
resource-path
the resource path of the file to parse
error
return location for a <g-error>, or ‘#f
ret
on error, zero is returned and error is set accordingly. On success, the merge id for the UI definitions is returned. You can use the merge id with clutter-script-unmerge-objects.

Since 1.10

— Function: clutter-script-lookup-filename (self <clutter-script>) (filename mchars) ⇒  (ret mchars)
— Method: lookup-filename

Looks up filename inside the search paths of script. If filename is found, its full path will be returned .

script
a <clutter-script>
filename
the name of the file to lookup
ret
the full path of filename or ‘#f’ if no path was found.

Since 0.8

— Function: clutter-script-get-object (self <clutter-script>) (name mchars) ⇒  (ret <gobject>)
— Method: get-object

Retrieves the object bound to name. This function does not increment the reference count of the returned object.

script
a <clutter-script>
name
the name of the object to retrieve
ret
the named object, or ‘#f’ if no object with the given name was available.

Since 0.6

— Function: clutter-script-unmerge-objects (self <clutter-script>) (merge_id unsigned-int)
— Method: unmerge-objects

Unmerges the objects identified by merge-id.

script
a <clutter-script>
merge-id
merge id returned when loading a UI definition

Since 0.6

— Function: clutter-script-ensure-objects (self <clutter-script>)
— Method: ensure-objects

Ensure that every object defined inside script is correctly constructed. You should rarely need to use this function.

script
a <clutter-script>

Since 0.6

— Function: clutter-script-list-objects (self <clutter-script>) ⇒  (ret glist-of)
— Method: list-objects

Retrieves all the objects created by script.

Note: this function does not increment the reference count of the objects it returns.

script
a <clutter-script>
ret
a list of <gobject>s, or ‘#f’. The objects are owned by the <clutter-script> instance. Use g-list-free on the returned list when done.

Since 0.8.2

— Function: clutter-script-add-states (self <clutter-script>) (name mchars) (state <clutter-state>)
— Method: add-states

Associates a <clutter-state> to the <clutter-script> instance using the given name.

The <clutter-script> instance will use state to resolve target states when connecting signal handlers.

The <clutter-script> instance will take a reference on the <clutter-state> passed to this function.

script
a <clutter-script>
name
a name for the state, or ‘#f’ to set the default <clutter-state>.
state
a <clutter-state>

Since 1.8

— Function: clutter-script-get-states (self <clutter-script>) (name mchars) ⇒  (ret <clutter-state>)
— Method: get-states

Retrieves the <clutter-state> for the given state-name.

If name is ‘#f’, this function will return the default <clutter-state> instance.

script
a <clutter-script>
name
the name of the <clutter-state>, or ‘#f’.
ret
a pointer to the <clutter-state> for the given name. The <clutter-state> is owned by the <clutter-script> instance and it should not be unreferenced.

Since 1.8

— Function: clutter-script-get-type-from-name (self <clutter-script>) (type_name mchars) ⇒  (ret <gtype>)
— Method: get-type-from-name

Looks up a type by name, using the virtual function that <clutter-script> has for that purpose. This function should rarely be used.

script
a <clutter-script>
type-name
name of the type to look up
ret
the type for the requested type name, or ‘G_TYPE_INVALID’ if not corresponding type was found.

Since 0.6

— Function: clutter-get-script-id (gobject <gobject>) ⇒  (ret mchars)

Retrieves the Clutter script id, if any.

gobject
a <gobject>
ret
the script id, or ‘#f’ if object was not defined inside a UI definition file. The returned string is owned by the object and should never be modified or freed.

Since 0.6