[clutter/clutter-1.18] interval: Implement ClutterScriptable interface



commit e56785501b5cd5bf12b4d14b98718851fafbe740
Author: Bastian Winkler <buz netbuz org>
Date:   Fri Jan 18 10:55:59 2013 +0200

    interval: Implement ClutterScriptable interface
    
    This allows the creation of ClutterTransition objects in ClutterScript:
     {
       "id" : "scripted-transition",
       "type" : "ClutterPropertyTransition",
       "property-name" : "background-color",
       "interval" : {
         "type" : "ClutterInterval",
         "value-type" : "ClutterColor",
         "initial" : "red",
         "final" : "blue"
       }
     }

 clutter/clutter-interval.c           |   50 +++++++++++++++++++++++++++++++++-
 tests/conform/script-parser.c        |   35 +++++++++++++++++++++++
 tests/conform/test-conform-main.c    |    1 +
 tests/data/Makefile.am               |    1 +
 tests/data/test-script-interval.json |   16 +++++++++++
 5 files changed, 102 insertions(+), 1 deletions(-)
---
diff --git a/clutter/clutter-interval.c b/clutter/clutter-interval.c
index 601ad0f..861a5fe 100644
--- a/clutter/clutter-interval.c
+++ b/clutter/clutter-interval.c
@@ -61,6 +61,8 @@
 #include "clutter-interval.h"
 #include "clutter-private.h"
 #include "clutter-units.h"
+#include "clutter-scriptable.h"
+#include "clutter-script-private.h"
 
 #include "deprecated/clutter-fixed.h"
 
@@ -93,7 +95,14 @@ struct _ClutterIntervalPrivate
   GValue *values;
 };
 
-G_DEFINE_TYPE_WITH_PRIVATE (ClutterInterval, clutter_interval, G_TYPE_INITIALLY_UNOWNED)
+static void clutter_scriptable_iface_init (ClutterScriptableIface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (ClutterInterval,
+                         clutter_interval,
+                         G_TYPE_INITIALLY_UNOWNED,
+                         G_ADD_PRIVATE (ClutterInterval)
+                         G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_SCRIPTABLE,
+                                                clutter_scriptable_iface_init));
 
 static gboolean
 clutter_interval_real_validate (ClutterInterval *interval,
@@ -479,6 +488,45 @@ clutter_interval_get_property (GObject    *gobject,
     }
 }
 
+static gboolean
+clutter_interval_parse_custom_node (ClutterScriptable *scriptable,
+                                    ClutterScript     *script,
+                                    GValue            *value,
+                                    const gchar       *name,
+                                    JsonNode          *node)
+{
+  ClutterIntervalPrivate *priv = CLUTTER_INTERVAL (scriptable)->priv;
+
+  if ((strcmp (name, "initial") == 0) || (strcmp (name, "final") == 0))
+    {
+      g_value_init (value, priv->value_type);
+      return _clutter_script_parse_node (script, value, name, node, NULL);
+    }
+
+  return FALSE;
+}
+
+static void
+clutter_interval_set_custom_property (ClutterScriptable *scriptable,
+                                      ClutterScript     *script,
+                                      const gchar       *name,
+                                      const GValue      *value)
+{
+  ClutterInterval *self = CLUTTER_INTERVAL (scriptable);
+
+  if (strcmp (name, "initial") == 0)
+    clutter_interval_set_initial_value (self, value);
+  else if (strcmp (name, "final") == 0)
+    clutter_interval_set_final_value (self, value);
+}
+
+static void
+clutter_scriptable_iface_init (ClutterScriptableIface *iface)
+{
+  iface->parse_custom_node = clutter_interval_parse_custom_node;
+  iface->set_custom_property = clutter_interval_set_custom_property;
+}
+
 static void
 clutter_interval_class_init (ClutterIntervalClass *klass)
 {
diff --git a/tests/conform/script-parser.c b/tests/conform/script-parser.c
index 0b455e1..73ec954 100644
--- a/tests/conform/script-parser.c
+++ b/tests/conform/script-parser.c
@@ -442,3 +442,38 @@ script_margin (TestConformSimpleFixture *fixture,
   g_object_unref (script);
   g_free (test_file);
 }
+
+void
+script_interval (TestConformSimpleFixture *fixture,
+                 gpointer                  dummy)
+{
+  ClutterScript *script = clutter_script_new ();
+  ClutterInterval *interval;
+  gchar *test_file;
+  GError *error = NULL;
+  GValue *initial, *final;
+
+  test_file = clutter_test_get_data_file ("test-script-interval.json");
+  clutter_script_load_from_file (script, test_file, &error);
+  if (g_test_verbose () && error)
+    g_print ("Error: %s", error->message);
+
+  g_assert_no_error (error);
+
+  interval = CLUTTER_INTERVAL (clutter_script_get_object (script, "int-1"));
+  initial = clutter_interval_peek_initial_value (interval);
+  g_assert (G_VALUE_HOLDS (initial, G_TYPE_FLOAT));
+  g_assert_cmpfloat (g_value_get_float (initial), ==, 23.3f);
+  final = clutter_interval_peek_final_value (interval);
+  g_assert (G_VALUE_HOLDS (final, G_TYPE_FLOAT));
+  g_assert_cmpfloat (g_value_get_float (final), ==, 42.2f);
+
+  interval = CLUTTER_INTERVAL (clutter_script_get_object (script, "int-2"));
+  initial = clutter_interval_peek_initial_value (interval);
+  g_assert (G_VALUE_HOLDS (initial, CLUTTER_TYPE_COLOR));
+  final = clutter_interval_peek_final_value (interval);
+  g_assert (G_VALUE_HOLDS (final, CLUTTER_TYPE_COLOR));
+
+  g_object_unref (script);
+  g_free (test_file);
+}
diff --git a/tests/conform/test-conform-main.c b/tests/conform/test-conform-main.c
index d05c537..1bb185a 100644
--- a/tests/conform/test-conform-main.c
+++ b/tests/conform/test-conform-main.c
@@ -198,6 +198,7 @@ main (int argc, char **argv)
   TEST_CONFORM_SIMPLE ("/script", animator_multi_properties);
   TEST_CONFORM_SIMPLE ("/script", state_base);
   TEST_CONFORM_SIMPLE ("/script", script_margin);
+  TEST_CONFORM_SIMPLE ("/script", script_interval);
 
   TEST_CONFORM_SKIP (g_test_slow (), "/timeline", timeline_base);
   TEST_CONFORM_SIMPLE ("/timeline", timeline_markers_from_script);
diff --git a/tests/data/Makefile.am b/tests/data/Makefile.am
index edbe23c..4e6878e 100644
--- a/tests/data/Makefile.am
+++ b/tests/data/Makefile.am
@@ -18,6 +18,7 @@ json_files = \
        test-state-1.json                       \
        test-script-timeline-markers.json       \
        test-script-margin.json                 \
+       test-script-interval.json               \
        $(NULL)
 
 EXTRA_DIST += $(json_files)
diff --git a/tests/data/test-script-interval.json b/tests/data/test-script-interval.json
new file mode 100644
index 0000000..35fe5c2
--- /dev/null
+++ b/tests/data/test-script-interval.json
@@ -0,0 +1,16 @@
+[
+  {
+    "id" : "int-1",
+    "type" : "ClutterInterval",
+    "value-type" : "gfloat",
+    "initial" : 23.3,
+    "final" : 42.2
+  },
+  {
+    "id" : "int-2",
+    "type" : "ClutterInterval",
+    "value-type" : "ClutterColor",
+    "initial" : "red",
+    "final" : "blue"
+  }
+]


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]