Debian Codegraph @

tests/interactive/test-script.c

raw
1 #include "config.h"
2
3 #include <locale.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 #include <math.h>
8
9 #include <glib.h>
10 #include <glib/gi18n.h>
11 #include <gmodule.h>
12 #include <clutter/clutter.h>
13
14 static ClutterScript *script = NULL;
15 static guint merge_id = 0;
16
17 static const gchar *test_unmerge =
18 "["
19 " {"
20 " \"id\" : \"main-stage\","
21 " \"type\" : \"ClutterStage\","
22 " \"children\" : [ \"blue-button\" ]"
23 " },"
24 " {"
25 " \"id\" : \"blue-button\","
26 " \"type\" : \"ClutterRectangle\","
27 " \"color\" : \"#0000ffff\","
28 " \"x\" : 350,"
29 " \"y\" : 50,"
30 " \"width\" : 100,"
31 " \"height\" : 100,"
32 " \"visible\" : true,"
33 " \"reactive\" : true"
34 " }"
35 "]";
36
37 static const gchar *test_behaviour =
38 "["
39 " {"
40 " \"id\" : \"main-timeline\","
41 " \"type\" : \"ClutterTimeline\","
42 " \"duration\" : 5000,"
43 " \"loop\" : true"
44 " },"
45 " {"
46 " \"id\" : \"sine-alpha\","
47 " \"type\" : \"ClutterAlpha\","
48 " \"function\" : \"sine_alpha\","
49 " \"timeline\" : \"main-timeline\""
50 " },"
51 " {"
52 " \"id\" : \"path-behaviour\","
53 " \"type\" : \"ClutterBehaviourPath\","
54 " \"path\" : \"M 50 50 L 100 100\","
55 " \"alpha\" : {"
56 " \"timeline\" : \"main-timeline\","
57 " \"function\" : \"double_ramp_alpha\""
58 " }"
59 " },"
60 " {"
61 " \"id\" : \"rotate-behaviour\","
62 " \"type\" : \"ClutterBehaviourRotate\","
63 " \"angle-start\" : 0.0,"
64 " \"angle-end\" : 360.0,"
65 " \"axis\" : \"y-axis\","
66 " \"alpha\" : \"sine-alpha\""
67 " },"
68 " {"
69 " \"id\" : \"fade-behaviour\","
70 " \"type\" : \"ClutterBehaviourOpacity\","
71 " \"opacity-start\" : 255,"
72 " \"opacity-end\" : 0,"
73 " \"alpha\" : {"
74 " \"id\" : \"fade-alpha\","
75 " \"type\" : \"ClutterAlpha\","
76 " \"timeline\" : \"main-timeline\","
77 " \"mode\" : \"linear\""
78 " }"
79 " }"
80 "]";
81
82 gdouble
83 sine_alpha (ClutterAlpha *alpha,
84 gpointer dummy G_GNUC_UNUSED)
85 {
86 ClutterTimeline *timeline = clutter_alpha_get_timeline (alpha);
87
88 return sin (clutter_timeline_get_progress (timeline) * G_PI);
89 }
90
91 gdouble
92 double_ramp_alpha (ClutterAlpha *alpha,
93 gpointer dummy G_GNUC_UNUSED)
94 {
95 ClutterTimeline *timeline = clutter_alpha_get_timeline (alpha);
96 gdouble progress = clutter_timeline_get_progress (timeline);
97
98 if (progress >= 0.5)
99 return 1.0 - progress;
100
101 return progress;
102 }
103
104 static gboolean
105 blue_button_press (ClutterActor *actor,
106 ClutterButtonEvent *event,
107 gpointer data)
108 {
109 g_print ("[*] Pressed '%s'\n", clutter_get_script_id (G_OBJECT (actor)));
110 g_print ("[*] Unmerging objects with merge id: %d\n", merge_id);
111
112 clutter_script_unmerge_objects (script, merge_id);
113
114 return TRUE;
115 }
116
117 static gboolean
118 red_button_press (ClutterActor *actor,
119 ClutterButtonEvent *event,
120 gpointer data)
121 {
122 GObject *timeline;
123
124 g_print ("[*] Pressed '%s'\n", clutter_get_script_id (G_OBJECT (actor)));
125
126 timeline = clutter_script_get_object (script, "main-timeline");
127 g_assert (CLUTTER_IS_TIMELINE (timeline));
128
129 if (!clutter_timeline_is_playing (CLUTTER_TIMELINE (timeline)))
130 clutter_timeline_start (CLUTTER_TIMELINE (timeline));
131 else
132 clutter_timeline_pause (CLUTTER_TIMELINE (timeline));
133
134 return TRUE;
135 }
136
137 G_MODULE_EXPORT int
138 test_script_main (int argc, char *argv[])
139 {
140 GObject *stage, *blue_button, *red_button;
141 GError *error = NULL;
142 gchar *file;
143 gint res;
144
145 setlocale (LC_ALL, "");
146 textdomain (GETTEXT_PACKAGE);
147
148 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
149 return 1;
150
151 script = clutter_script_new ();
152 g_assert (CLUTTER_IS_SCRIPT (script));
153
154 clutter_script_load_from_data (script, test_behaviour, -1, &error);
155 if (error)
156 {
157 g_print ("*** Error:\n"
158 "*** %s\n", error->message);
159 g_error_free (error);
160 g_object_unref (script);
161 return EXIT_FAILURE;
162 }
163
164 file = g_build_filename (TESTS_DATADIR, "test-script.json", NULL);
165 clutter_script_load_from_file (script, file, &error);
166 if (error)
167 {
168 g_print ("*** Error:\n"
169 "*** %s\n", error->message);
170 g_error_free (error);
171 g_object_unref (script);
172 g_free (file);
173 return EXIT_FAILURE;
174 }
175
176 g_free (file);
177
178 merge_id = clutter_script_load_from_data (script, test_unmerge, -1, &error);
179 if (error)
180 {
181 g_print ("*** Error:\n"
182 "*** %s\n", error->message);
183 g_error_free (error);
184 g_object_unref (script);
185 return EXIT_FAILURE;
186 }
187
188 clutter_script_connect_signals (script, NULL);
189
190 res = clutter_script_get_objects (script,
191 "main-stage", &stage,
192 "red-button", &red_button,
193 "blue-button", &blue_button,
194 NULL);
195 g_assert (res == 3);
196
197 clutter_actor_show (CLUTTER_ACTOR (stage));
198
199 g_signal_connect (red_button,
200 "button-press-event",
201 G_CALLBACK (red_button_press),
202 NULL);
203
204 g_signal_connect (blue_button,
205 "button-press-event",
206 G_CALLBACK (blue_button_press),
207 NULL);
208
209 clutter_main ();
210
211 g_object_unref (script);
212
213 return EXIT_SUCCESS;
214 }