Debian Codegraph @

tests/interactive/test-scrolling.c

raw
1 #include <stdlib.h>
2 #include <math.h>
3 #include <gmodule.h>
4 #include <clutter/clutter.h>
5
6 #define RECT_WIDTH 400
7 #define RECT_HEIGHT 300
8 #define N_RECTS 7
9
10 static const gchar *rect_color[N_RECTS] = {
11 "#edd400",
12 "#f57900",
13 "#c17d11",
14 "#73d216",
15 "#3465a4",
16 "#75507b",
17 "#cc0000"
18 };
19
20 static ClutterActor *rectangle[N_RECTS];
21 static ClutterActor *viewport = NULL;
22
23 static void
24 on_drag_end (ClutterDragAction *action,
25 ClutterActor *actor,
26 gfloat event_x,
27 gfloat event_y,
28 ClutterModifierType modifiers)
29 {
30 gfloat viewport_x = clutter_actor_get_x (viewport);
31 gfloat offset_x;
32 gint child_visible;
33
34 /* check if we're at the viewport edges */
35 if (viewport_x > 0)
36 {
37 clutter_actor_save_easing_state (viewport);
38 clutter_actor_set_easing_mode (viewport, CLUTTER_EASE_OUT_BOUNCE);
39 clutter_actor_set_x (viewport, 0);
40 clutter_actor_restore_easing_state (viewport);
41 return;
42 }
43
44 if (viewport_x < (-1.0f * (RECT_WIDTH * (N_RECTS - 1))))
45 {
46 clutter_actor_save_easing_state (viewport);
47 clutter_actor_set_easing_mode (viewport, CLUTTER_EASE_OUT_BOUNCE);
48 clutter_actor_set_x (viewport, -1.0f * (RECT_WIDTH * (N_RECTS - 1)));
49 clutter_actor_restore_easing_state (viewport);
50 return;
51 }
52
53 /* animate the viewport to fully show the child once we pass
54 * a certain threshold with the dragging action
55 */
56 offset_x = fabsf (viewport_x) / RECT_WIDTH + 0.5f;
57 if (offset_x > (RECT_WIDTH * 0.33))
58 child_visible = (int) offset_x + 1;
59 else
60 child_visible = (int) offset_x;
61
62 /* sanity check on the children number */
63 child_visible = CLAMP (child_visible, 0, N_RECTS);
64
65 clutter_actor_save_easing_state (viewport);
66 clutter_actor_set_x (viewport, -1.0f * RECT_WIDTH * child_visible);
67 clutter_actor_restore_easing_state (viewport);
68 }
69
70 G_MODULE_EXPORT int
71 test_scrolling_main (int argc, char *argv[])
72 {
73 ClutterActor *stage;
74 ClutterActor *scroll;
75 ClutterAction *action;
76 gint i;
77
78 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
79 return 1;
80
81 stage = clutter_stage_new ();
82 clutter_stage_set_title (CLUTTER_STAGE (stage), "Scrolling");
83 clutter_actor_set_size (stage, 800, 600);
84 g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
85
86 /* scroll: the group that contains the scrolling viewport; we set its
87 * size to be the same as one rectangle, position it in the middle of
88 * the stage and set it to clip its contents to the allocated size
89 */
90 scroll = clutter_actor_new ();
91 clutter_actor_add_child (stage, scroll);
92 clutter_actor_set_size (scroll, RECT_WIDTH, RECT_HEIGHT);
93 clutter_actor_add_constraint (scroll, clutter_align_constraint_new (stage, CLUTTER_ALIGN_BOTH, 0.5));
94 clutter_actor_set_clip_to_allocation (scroll, TRUE);
95
96 /* viewport: the actual container for the children; we scroll it using
97 * the Drag action constrained to the horizontal axis, and every time
98 * the dragging ends we check whether we're dragging past the end of
99 * the viewport
100 */
101 viewport = clutter_actor_new ();
102 clutter_actor_set_layout_manager (viewport, clutter_box_layout_new ());
103 clutter_actor_add_child (scroll, viewport);
104
105 /* add dragging capabilities to the viewport; the heavy lifting is
106 * all done by the DragAction itself, plus the ::drag-end signal
107 * handler in our code
108 */
109 action = clutter_drag_action_new ();
110 clutter_actor_add_action (viewport, action);
111 clutter_drag_action_set_drag_axis (CLUTTER_DRAG_ACTION (action),
112 CLUTTER_DRAG_X_AXIS);
113 g_signal_connect (action, "drag-end", G_CALLBACK (on_drag_end), NULL);
114 clutter_actor_set_reactive (viewport, TRUE);
115
116 /* children of the viewport */
117 for (i = 0; i < N_RECTS; i++)
118 {
119 ClutterColor color;
120
121 clutter_color_from_string (&color, rect_color[i]);
122
123 rectangle[i] = clutter_actor_new ();
124 clutter_actor_set_background_color (rectangle[i], &color);
125 clutter_actor_add_child (viewport, rectangle[i]);
126 clutter_actor_set_size (rectangle[i], RECT_WIDTH, RECT_HEIGHT);
127 }
128
129 clutter_actor_show (stage);
130
131 clutter_main ();
132
133 return EXIT_SUCCESS;
134 }