Debian Codegraph @

tests/interactive/test-path-constraint.c

raw
1 #include <stdlib.h>
2 #include <gmodule.h>
3 #include <clutter/clutter.h>
4
5 #define PATH_DESCRIPTION \
6 "M 0, 0 " \
7 "L 0, 300 " \
8 "L 300, 300 " \
9 "L 300, 0 " \
10 "L 0, 0"
11
12 static gboolean toggled = FALSE;
13
14 static gboolean
15 on_button_press (ClutterActor *actor,
16 const ClutterEvent *event,
17 gpointer dummy G_GNUC_UNUSED)
18 {
19 if (!toggled)
20 clutter_actor_animate (actor, CLUTTER_EASE_OUT_CUBIC, 500,
21 "@constraints.path.offset", 1.0,
22 NULL);
23 else
24 clutter_actor_animate (actor, CLUTTER_EASE_OUT_CUBIC, 500,
25 "@constraints.path.offset", 0.0,
26 NULL);
27
28 toggled = !toggled;
29
30 return TRUE;
31 }
32
33 static gchar *
34 node_to_string (const ClutterPathNode *node)
35 {
36 GString *buffer = g_string_sized_new (256);
37 gsize len = 0, i;
38
39 switch (node->type)
40 {
41 case CLUTTER_PATH_MOVE_TO:
42 g_string_append (buffer, "move-to ");
43 len = 1;
44 break;
45
46 case CLUTTER_PATH_LINE_TO:
47 g_string_append (buffer, "line-to ");
48 len = 1;
49 break;
50
51 case CLUTTER_PATH_CURVE_TO:
52 g_string_append (buffer, "curve-to ");
53 len = 3;
54 break;
55
56 case CLUTTER_PATH_CLOSE:
57 g_string_append (buffer, "close");
58 len = 0;
59 break;
60
61 default:
62 break;
63 }
64
65 for (i = 0; i < len; i++)
66 {
67 if (i == 0)
68 g_string_append (buffer, "[ ");
69
70 g_string_append_printf (buffer, "[ %d, %d ]",
71 node->points[i].x,
72 node->points[i].y);
73
74 if (i == len - 1)
75 g_string_append (buffer, " ]");
76 }
77
78 return g_string_free (buffer, FALSE);
79 }
80
81 static void
82 on_node_reached (ClutterPathConstraint *constraint,
83 ClutterActor *actor,
84 guint index_)
85 {
86 ClutterPath *path = clutter_path_constraint_get_path (constraint);
87 ClutterPathNode node;
88 gchar *str;
89
90 clutter_path_get_node (path, index_, &node);
91
92 str = node_to_string (&node);
93 g_print ("Node %d reached: %s\n", index_, str);
94 g_free (str);
95 }
96
97 G_MODULE_EXPORT int
98 test_path_constraint_main (int argc,
99 char *argv[])
100 {
101 ClutterActor *stage, *rect;
102 ClutterPath *path;
103 ClutterColor rect_color = { 0xcc, 0x00, 0x00, 0xff };
104
105 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
106 return 1;
107
108 stage = clutter_stage_new ();
109 clutter_stage_set_title (CLUTTER_STAGE (stage), "Path Constraint");
110 g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
111
112 path = clutter_path_new ();
113 clutter_path_set_description (path, PATH_DESCRIPTION);
114
115 rect = clutter_rectangle_new ();
116 clutter_rectangle_set_color (CLUTTER_RECTANGLE (rect), &rect_color);
117 clutter_actor_set_size (rect, 128, 128);
118 clutter_actor_set_reactive (rect, TRUE);
119 clutter_actor_add_constraint_with_name (rect, "path", clutter_path_constraint_new (path, 0.0));
120 clutter_container_add_actor (CLUTTER_CONTAINER (stage), rect);
121
122 g_signal_connect (rect, "button-press-event", G_CALLBACK (on_button_press), NULL);
123 g_signal_connect (clutter_actor_get_constraint (rect, "path"),
124 "node-reached",
125 G_CALLBACK (on_node_reached),
126 NULL);
127
128 clutter_actor_show (stage);
129
130 clutter_main ();
131
132 return EXIT_SUCCESS;
133 }