From b532f9591786f4b5381269718e7e0d479ad58c4d Mon Sep 17 00:00:00 2001 From: Elliot Smith Date: Fri, 27 Aug 2010 11:20:29 +0100 Subject: [PATCH] cookbook: Added introduction to and recipe on ClutterScript Wrote an introduction to using ClutterScript with JSON. Focus is on explaining why you might want to use it, basic principles of operation (with annotated JSON sample), and how to map data types from C to JSON. Written simultaneously with a short recipe (uses the same sample code) showing how to load a JSON file and retrieve objects from it in code. --- doc/cookbook/script.xml | 394 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 303 insertions(+), 91 deletions(-) diff --git a/doc/cookbook/script.xml b/doc/cookbook/script.xml index 2b9315e..e030a81 100644 --- a/doc/cookbook/script.xml +++ b/doc/cookbook/script.xml @@ -19,11 +19,11 @@ described entirely in code: declarations of UI elements become entwined with procedural code for handling interactions. This can make refactoring tough, as - you have to find the right place in the code ("Where did I - set the color of that rectangle?") and make sure your edits - don't break any behaviour. + you have to find the right place in the code to modify the UI + ("Where did I set the color of that rectangle?") and make sure + your UI modifications don't break any behaviour. - Other frameworks separate presentation from programming + Many frameworks separate presentation from programming logic, making it easier to change the appearance of the UI without affecting its behaviour (and vice versa). For example, in web development you can use HTML and CSS to define @@ -31,8 +31,8 @@ ClutterScript enables a similar separation: you can define the UI declaratively using - JSON; then load - the UI from the JSON and handle interactions with it through Clutter code + JSON, load + the UI from the JSON, then handle interactions with it through Clutter code (in C, Python, Vala or some other language). This has several benefits, including: @@ -44,7 +44,8 @@ More concise code: typically, describing a UI in JSON requires far fewer characters than the equivalent procedural - code. + code (at least, once you have more than three or four actors in + your application). If you write your JSON in external files, you can make the @@ -55,42 +56,45 @@ Creating and configuring some objects (e.g. animations, - layouts) becomes much simpler in JSON. + layouts) can be much simpler in JSON. Less compilation (if you're using a compiled language): - because you can edit the UI by editing external JSON files, + because you can change the UI by editing external JSON files, you can make changes to it without needing to recompile the whole application. - The ClutterScript API reference describes many - of its useful features. However, the following sections are intended + The following sections are intended to give an overview of how ClutterScript works, and - how to use it in an application. - - The recipes in this chapter give more detail about - particular aspects of ClutterScript, such as - how to connect signals and how merge multiple JSON definitions - in a single script. + how to use it in an application. The recipes in this chapter + then provide more detail about particular aspects of + ClutterScript, such as how to connect signals to handlers, + how to merge multiple JSON definitions in a single script, etc. + There is also a lot of useful information in the ClutterScript + API reference.
Basic principles of <type>ClutterScript</type> - Clutter is built on top of GObject, - an object system for C. As in other object systems, properties - are fundamental to GObject. ClutterScript - effectively provides a way to create instances of GObjects and + Clutter is built on top of + GObject, + an object system for C. ClutterScript + provides a way to create instances of GObjects and set their properties. For example: - - + + Example UI definition in JSON for use with + <type>ClutterScript</type> + + + [ { "id" : "stage", "type" : "ClutterStage", - "is-default" : true, + "is-default" : true, "width" : 400, "height" : 400, "color" : "#333355ff", @@ -120,104 +124,259 @@ ] } ] - - - - - N.B. The numbers in brackets in the example further - explain the JSON structure, and are not part of the UI - definition. See below for more details. - - - - - list bracket??? - - - object bracket??? - - - ID??? - - - type??? - - - color??? - - - referencing a child by ID??? - - - no ID for implicit objects??? - - - embedded child??? - - - color??? - - + + + + N.B. The numbers in brackets in the example further + explain the JSON structure, and are not part of the UI + definition. + + + + + All the objects defined for the UI sit inside a JSON + list structure, marked with square brackets. + + + A pair of braces surrounds each object definition; + inside the braces, key-value pairs set properties on the + object. See the + section on + datatypes for more about the acceptable values. + + + An id is required for objects which + are referred to elsewhere in the JSON or which need to be + accessible from code (see + this recipe for the basics of + using object IDs from code). + In cases where an object doesn't need to be accessible + from code and is not referenced elsewhere in the JSON file, + the id can be omitted. + + + The type key is mandatory, and + specifies the type of the object; usually this will be + one of the Clutter object types. + + + is-default is a special stage-only + property, which tells Clutter that the ClutterStage + instance used should be the default stage. + + + Colors can be set using hexadecimal color code strings, + as used in HTML and CSS; or by using color words. The + range of acceptable values is as for the + pango_color_from_string() function. + + + Children can be associated with a parent through + the children property. Children are + either added to the children list by ID; + or by directly embedding the child JSON object as an element + within the list. The two can be mixed in a single + list of children. + + + + + Once you grasp that Clutter objects are GObjects, and you - are setting their properties, you can determine what is scriptable + are setting their properties, you can work out what is "scriptable" by referring to the Properties sections - in the Clutter API reference. Any of the properties described - there can be set using ClutterScript. + of the API reference for each Clutter type. Any of the properties + described there can be set using ClutterScript. + + Having said this, there are some special properties which + aren't obvious, but which can be set via JSON; + layout properties are one example. These aren't + listed as properties of ClutterActor but can be set + as part of a ClutterActor object definition + (using the layout::<property name> + syntax for the key). Some of these are covered in recipes later in + this chapter.
-
- Data types and JSON +
+ Data types + + ClutterScript uses the standard JSON format. + It is very important that you respect the data type of the property + you are setting, ensuring that you use the right JSON data type. + You may get unexpected results or errors if you try to set a property + using the wrong data type: for example, setting a property + to an integer number in the JSON, when the Clutter property + is expecting a gfloat, may cause errors. + + To assist in using the right data types in your JSON + definitions, the table below shows how Clutter and GLib data + types map to JSON: + + + + + C data type (Clutter/GLib) + Maps to JSON + Example (C => JSON) + + + + + floating point number (gfloat, gdouble) + number (int frac, int exp, int frac exp) + + 1.0 => 1.0 + 1e-1 => 1e-1 + 1E-1 => 1E-1 + 0.1E-1 => 0.1E-1 + + + + integer (guint8, gint) + number (int) + + 1 => 1 + 0x00 => 0 (no hex in JSON) + 01 => 1 (no octal in JSON) + + + + gboolean + true/false + + TRUE => true + FALSE => false + + + + gchar + string + "hello world" => "hello world" + + + enum (e.g. Clutter constants) + string + CLUTTER_ALIGN_X_AXIS => + "CLUTTER_ALIGN_X_AXIS" + + + ClutterColor + color string + + clutter_color_new (255, 0, 0, 255) => + "red" or "#f00f" or + "#ff0000ff"; alternatively, + "#f00" or "#ff0000" + (implicitly sets alpha value to 255) + + + + ClutterActor (or other Clutter type) + object + + clutter_rectangle_new () => + { "type" : "ClutterRectangle" } + + + + Property which takes a list or array of values + array of objects and/or IDs + + clutter_container_add_actor (stage, rectangle) => + +{ + "id" : "stage", + "type" : "ClutterStage", + ..., + + "children" : [ + { + "id" : "rectangle", + "type" : "ClutterRectangle", + ... + } + ] +} + + + + + NULL + null + - + + + - The next important consideration is the data type of the - property.??? null, float, int, string, color - - special properties which aren't obvious: layout::* ??? perhaps - leave to the appropriate recipe - - properties with multiple values??? e.g. children, constraints - - objects as property values??? referencing by ID??? objects without IDs???
- Defining a user interface using <type>ClutterScript</type> + Defining a user interface with JSON
Problem - ??? + You want to create a user interface as quickly as + possible; you also need to change it easily as requirements shift. + + This need can arise when: + + + + you are prototyping a user interface, and you need to + quickly test new ideas. + + + the user interface you are building is likely to contain many + elements and relationships between them. + +
Solution - ??? -
+ Define the user interface in an external JSON file. Then + create a ClutterScript object and load the JSON + into it from the file. -
- Discussion + This keeps the UI definition separate from the application + logic and makes it easier to manage. - ???converting an existing program to ClutterScript -
+ + See the introduction + for the reasons why ClutterScript is a good solution, + and for an overview of how JSON definitions work. + -
- Full example + Here's an example JSON definition to put in the file: - - Description of a user interface in JSON + a code sample should be here... but isn't - + + + In the application, load the JSON from the file with + clutter_script_load_from_file(). (You can + also load JSON from a string (gchar*) with + clutter_script_load_from_data().) - - Loading a JSON user interface definition in Clutter + Then retrieve objects by ID to use them in your code: + + + Loading JSON from a file and retrieving objects + defined by it a code sample should be here... but isn't @@ -225,6 +384,59 @@ + Although we only retrieved the stage in the example above, + clutter_script_get_objects() can + retrieve multiple objects with a single call: + + + + + + + + You can also use clutter_script_get_object() + to retrieve a single object, though you may have to cast + it to the right type before use; for example: + + + +ClutterStage *stage = CLUTTER_STAGE (clutter_script_get_object (script, "stage)); + + + +
+ +
+ Discussion + + In the sample code, the stage is part of the JSON definition. + However, it doesn't have to be: it is possible to create the + stage in application code; then load more components from one + or more JSON definitions and attach them to the stage you + constructed in code. + + However, keeping most of the user interface definition + in external JSON files makes it easier to change + the UI without having to touch any code. If you have some user + interface elements constructed in code and some in JSON, it can + make refactoring more difficult. +
-- 2.7.4