cookbook: Added example of connecting signals in ClutterScript
authorElliot Smith <elliot.smith@intel.com>
Fri, 27 Aug 2010 15:31:00 +0000 (16:31 +0100)
committerElliot Smith <elliot.smith@intel.com>
Tue, 31 Aug 2010 09:06:08 +0000 (10:06 +0100)
To support recipe about connecting signals in script.

doc/cookbook/examples/Makefile.am
doc/cookbook/examples/script-signals.c [new file with mode: 0644]
doc/cookbook/examples/script-signals.json [new file with mode: 0644]

index 343b7c6..28db1fe 100644 (file)
@@ -19,6 +19,7 @@ noinst_PROGRAMS = \
        textures-crossfade-cogl \
        textures-crossfade-slideshow    \
        script-ui       \
+       script-signals  \
        $(NULL)
 
 INCLUDES = \
@@ -56,5 +57,6 @@ textures_crossfade_SOURCES                 = textures-crossfade.c
 textures_crossfade_cogl_SOURCES            = textures-crossfade-cogl.c
 textures_crossfade_slideshow_SOURCES       = textures-crossfade-slideshow.c
 script_ui_SOURCES                          = script-ui.c
+script_signals_SOURCES                     = script-signals.c
 
 -include $(top_srcdir)/build/autotools/Makefile.am.gitignore
diff --git a/doc/cookbook/examples/script-signals.c b/doc/cookbook/examples/script-signals.c
new file mode 100644 (file)
index 0000000..99f9284
--- /dev/null
@@ -0,0 +1,91 @@
+#include <stdlib.h>
+#include <clutter/clutter.h>
+
+/* callbacks cannot be declared static as they
+ * are looked up dynamically by ClutterScript
+ */
+gboolean
+foo_pointer_motion_cb (ClutterActor *actor,
+                       ClutterEvent *event,
+                       gpointer      user_data)
+{
+  gfloat x, y;
+  clutter_event_get_coords (event, &x, &y);
+
+  g_print ("Pointer movement at %.0f,%.0f\n", x, y);
+
+  return TRUE;
+}
+
+void
+foo_button_clicked_cb (ClutterClickAction *action,
+                       ClutterActor       *actor,
+                       gpointer            user_data)
+{
+  /* get the UI definition passed to the handler */
+  ClutterScript *ui = CLUTTER_SCRIPT (user_data);
+
+  /* get the rectangle defined in the JSON */
+  ClutterActor *rectangle;
+  clutter_script_get_objects (ui,
+                              "rectangle", &rectangle,
+                              NULL);
+
+  /* do nothing if the actor is already animating */
+  if (clutter_actor_get_animation (rectangle) != NULL)
+    return;
+
+  /* get the current rotation and increment it */
+  gfloat z_angle;
+  z_angle = clutter_actor_get_rotation (rectangle,
+                                        CLUTTER_Z_AXIS,
+                                        NULL, NULL, NULL);
+
+  z_angle += 90.0;
+
+  /* animate to new rotation angle */
+  clutter_actor_animate (rectangle,
+                         CLUTTER_EASE_OUT_CUBIC,
+                         1000,
+                         "rotation-angle-z", z_angle,
+                         NULL);
+}
+
+int
+main (int argc, char *argv[])
+{
+  clutter_init (&argc, &argv);
+
+  ClutterScript *ui = clutter_script_new ();
+
+  gchar *filename = "script-signals.json";
+  GError *error = NULL;
+
+  clutter_script_load_from_file (ui, filename, &error);
+
+  if (error != NULL)
+    {
+      g_critical ("Error loading ClutterScript file %s\n%s", filename, error->message);
+      g_error_free (error);
+      exit (EXIT_FAILURE);
+    }
+
+  ClutterActor *stage;
+  clutter_script_get_objects (ui,
+                              "stage", &stage,
+                              NULL);
+
+  /* make the objects in the script available to all signals
+   * by passing the script as the second argument
+   * to clutter_script_connect_signals()
+   */
+  clutter_script_connect_signals (ui, ui);
+
+  clutter_actor_show (stage);
+
+  clutter_main ();
+
+  g_object_unref (ui);
+
+  return EXIT_SUCCESS;
+}
diff --git a/doc/cookbook/examples/script-signals.json b/doc/cookbook/examples/script-signals.json
new file mode 100644 (file)
index 0000000..239428b
--- /dev/null
@@ -0,0 +1,40 @@
+[
+  {
+    "id" : "stage",
+    "type" : "ClutterStage",
+    "width" : 300,
+    "height" : 300,
+    "color" : "#335",
+
+    "signals" : [
+      { "name" : "destroy", "handler" : "clutter_main_quit" }
+    ],
+
+    "children" : [ "rectangle" ]
+  },
+
+  {
+    "id" : "rectangle",
+    "type" : "ClutterRectangle",
+    "width" : 200,
+    "height" : 200,
+    "x" : 50,
+    "y" : 50,
+    "color" : "#a90",
+    "rotation-center-z-gravity" : "center",
+    "reactive" : true,
+
+    "signals" : [
+      { "name" : "motion-event", "handler" : "foo_pointer_motion_cb" }
+    ],
+
+    "actions" : [
+      {
+        "type" : "ClutterClickAction",
+        "signals" : [
+          { "name" : "clicked", "handler" : "foo_button_clicked_cb" }
+        ]
+      }
+    ]
+  }
+]