| 1 |
#include <stdlib.h> |
| 2 |
#include <clutter/clutter.h> |
| 3 |
|
| 4 |
#define RECT_SIZE 128 |
| 5 |
|
| 6 |
#define H_PADDING 32 |
| 7 |
#define V_PADDING 32 |
| 8 |
|
| 9 |
enum |
| 10 |
{ |
| 11 |
NorthWest, North, NorthEast, |
| 12 |
West, Center, East, |
| 13 |
SouthWest, South, SouthEast, |
| 14 |
|
| 15 |
N_RECTS |
| 16 |
}; |
| 17 |
|
| 18 |
static ClutterActor *rects[N_RECTS] = { NULL, }; |
| 19 |
static const gchar *colors[N_RECTS] = { |
| 20 |
"#8ae234", "#73d216", "#4e9a06", |
| 21 |
"#729fcf", "#3465a4", "#204a87", |
| 22 |
"#ef2929", "#cc0000", "#a40000" |
| 23 |
}; |
| 24 |
static const gchar *names[N_RECTS] = { |
| 25 |
"North West", "North", "North East", |
| 26 |
"West", "Center", "East", |
| 27 |
"South West", "South", "South East" |
| 28 |
}; |
| 29 |
|
| 30 |
static const gchar *desaturare_glsl_shader = |
| 31 |
"uniform sampler2D tex;\n" |
| 32 |
"uniform float factor;\n" |
| 33 |
"\n" |
| 34 |
"vec3 desaturate (const vec3 color, const float desaturation)\n" |
| 35 |
"{\n" |
| 36 |
" const vec3 gray_conv = vec3 (0.299, 0.587, 0.114);\n" |
| 37 |
" vec3 gray = vec3 (dot (gray_conv, color));\n" |
| 38 |
" return vec3 (mix (color.rgb, gray, desaturation));\n" |
| 39 |
"}\n" |
| 40 |
"\n" |
| 41 |
"void main ()\n" |
| 42 |
"{\n" |
| 43 |
" vec4 color = cogl_color_in * texture2D (tex, vec2 (cogl_tex_coord_in[0].xy));\n" |
| 44 |
" color.rgb = desaturate (color.rgb, factor);\n" |
| 45 |
" cogl_color_out = color;\n" |
| 46 |
"}\n"; |
| 47 |
|
| 48 |
static gboolean is_expanded = FALSE; |
| 49 |
|
| 50 |
static gboolean |
| 51 |
on_button_release (ClutterActor *actor, |
| 52 |
ClutterEvent *event, |
| 53 |
gpointer data G_GNUC_UNUSED) |
| 54 |
{ |
| 55 |
if (!is_expanded) |
| 56 |
{ |
| 57 |
gfloat north_offset, south_offset; |
| 58 |
gfloat west_offset, east_offset; |
| 59 |
|
| 60 |
/* expand the 8 rectangles by animating the offset of the |
| 61 |
* bind constraints |
| 62 |
*/ |
| 63 |
|
| 64 |
north_offset = (clutter_actor_get_height (rects[Center]) + V_PADDING) |
| 65 |
* -1.0f; |
| 66 |
south_offset = (clutter_actor_get_height (rects[Center]) + V_PADDING); |
| 67 |
|
| 68 |
west_offset = (clutter_actor_get_width (rects[Center]) + H_PADDING) |
| 69 |
* -1.0f; |
| 70 |
east_offset = (clutter_actor_get_width (rects[Center]) + H_PADDING); |
| 71 |
|
| 72 |
clutter_actor_animate (rects[NorthWest], CLUTTER_EASE_OUT_EXPO, 500, |
| 73 |
"opacity", 255, |
| 74 |
"@constraints.x-bind.offset", west_offset, |
| 75 |
"@constraints.y-bind.offset", north_offset, |
| 76 |
"reactive", TRUE, |
| 77 |
NULL); |
| 78 |
clutter_actor_animate (rects[North], CLUTTER_EASE_OUT_EXPO, 500, |
| 79 |
"opacity", 255, |
| 80 |
"@constraints.y-bind.offset", north_offset, |
| 81 |
"reactive", TRUE, |
| 82 |
NULL); |
| 83 |
clutter_actor_animate (rects[NorthEast], CLUTTER_EASE_OUT_EXPO, 500, |
| 84 |
"opacity", 255, |
| 85 |
"@constraints.x-bind.offset", east_offset, |
| 86 |
"@constraints.y-bind.offset", north_offset, |
| 87 |
"reactive", TRUE, |
| 88 |
NULL); |
| 89 |
|
| 90 |
clutter_actor_animate (rects[West], CLUTTER_EASE_OUT_EXPO, 500, |
| 91 |
"opacity", 255, |
| 92 |
"@constraints.x-bind.offset", west_offset, |
| 93 |
"reactive", TRUE, |
| 94 |
NULL); |
| 95 |
/* turn on the desaturation effect and set the center |
| 96 |
* rectangle not reactive |
| 97 |
*/ |
| 98 |
clutter_actor_animate (rects[Center], CLUTTER_LINEAR, 500, |
| 99 |
"@effects.desaturate.enabled", TRUE, |
| 100 |
"reactive", FALSE, |
| 101 |
NULL); |
| 102 |
clutter_actor_animate (rects[East], CLUTTER_EASE_OUT_EXPO, 500, |
| 103 |
"opacity", 255, |
| 104 |
"@constraints.x-bind.offset", east_offset, |
| 105 |
"reactive", TRUE, |
| 106 |
NULL); |
| 107 |
|
| 108 |
clutter_actor_animate (rects[SouthWest], CLUTTER_EASE_OUT_EXPO, 500, |
| 109 |
"opacity", 255, |
| 110 |
"@constraints.x-bind.offset", west_offset, |
| 111 |
"@constraints.y-bind.offset", south_offset, |
| 112 |
"reactive", TRUE, |
| 113 |
NULL); |
| 114 |
clutter_actor_animate (rects[South], CLUTTER_EASE_OUT_EXPO, 500, |
| 115 |
"opacity", 255, |
| 116 |
"@constraints.y-bind.offset", south_offset, |
| 117 |
"reactive", TRUE, |
| 118 |
NULL); |
| 119 |
clutter_actor_animate (rects[SouthEast], CLUTTER_EASE_OUT_EXPO, 500, |
| 120 |
"opacity", 255, |
| 121 |
"@constraints.x-bind.offset", east_offset, |
| 122 |
"@constraints.y-bind.offset", south_offset, |
| 123 |
"reactive", TRUE, |
| 124 |
NULL); |
| 125 |
} |
| 126 |
else |
| 127 |
{ |
| 128 |
gint i; |
| 129 |
|
| 130 |
clutter_actor_animate (rects[Center], CLUTTER_LINEAR, 500, |
| 131 |
"@effects.desaturate.enabled", FALSE, |
| 132 |
"reactive", TRUE, |
| 133 |
NULL); |
| 134 |
|
| 135 |
for (i = NorthWest; i < N_RECTS; i++) |
| 136 |
{ |
| 137 |
if (i == Center) |
| 138 |
continue; |
| 139 |
|
| 140 |
/* put the 8 rectangles back into their initial state */ |
| 141 |
clutter_actor_animate (rects[i], CLUTTER_EASE_OUT_EXPO, 500, |
| 142 |
"opacity", 0, |
| 143 |
"@constraints.x-bind.offset", 0.0f, |
| 144 |
"@constraints.y-bind.offset", 0.0f, |
| 145 |
"reactive", FALSE, |
| 146 |
NULL); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
is_expanded = !is_expanded; |
| 151 |
|
| 152 |
g_print ("Selected: [%s]\n", clutter_actor_get_name (actor)); |
| 153 |
|
| 154 |
return TRUE; |
| 155 |
} |
| 156 |
|
| 157 |
G_MODULE_EXPORT const char * |
| 158 |
test_bind_constraint_describe (void) |
| 159 |
{ |
| 160 |
return "Demonstrate the usage of ClutterBindConstraint"; |
| 161 |
} |
| 162 |
|
| 163 |
G_MODULE_EXPORT int |
| 164 |
test_bind_constraint_main (int argc, char *argv[]) |
| 165 |
{ |
| 166 |
ClutterActor *stage, *rect; |
| 167 |
ClutterConstraint *constraint; |
| 168 |
ClutterEffect *effect; |
| 169 |
ClutterColor rect_color; |
| 170 |
gint i; |
| 171 |
|
| 172 |
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) |
| 173 |
return 1; |
| 174 |
|
| 175 |
stage = clutter_stage_new (); |
| 176 |
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); |
| 177 |
clutter_stage_set_title (CLUTTER_STAGE (stage), "Constraints"); |
| 178 |
clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE); |
| 179 |
clutter_actor_set_size (stage, 800, 600); |
| 180 |
|
| 181 |
/* main rectangle */ |
| 182 |
clutter_color_from_string (&rect_color, "#3465a4"); |
| 183 |
rect = clutter_actor_new (); |
| 184 |
g_signal_connect (rect, "button-release-event", |
| 185 |
G_CALLBACK (on_button_release), |
| 186 |
NULL); |
| 187 |
clutter_actor_set_background_color (rect, &rect_color); |
| 188 |
clutter_actor_set_size (rect, RECT_SIZE, RECT_SIZE); |
| 189 |
clutter_actor_set_reactive (rect, TRUE); |
| 190 |
clutter_actor_set_name (rect, names[Center]); |
| 191 |
clutter_actor_add_child (stage, rect); |
| 192 |
|
| 193 |
/* align the center rectangle to the center of the stage */ |
| 194 |
constraint = clutter_align_constraint_new (stage, CLUTTER_ALIGN_BOTH, 0.5); |
| 195 |
clutter_actor_add_constraint_with_name (rect, "align", constraint); |
| 196 |
|
| 197 |
/* this is the equivalent of the DesaturateEffect; we cannot animate |
| 198 |
* the factor because the animation API only understands GObject |
| 199 |
* properties; so we use the ActorMeta:enabled property to toggle |
| 200 |
* the shader |
| 201 |
*/ |
| 202 |
effect = clutter_shader_effect_new (CLUTTER_FRAGMENT_SHADER); |
| 203 |
clutter_shader_effect_set_shader_source (CLUTTER_SHADER_EFFECT (effect), |
| 204 |
desaturare_glsl_shader); |
| 205 |
clutter_shader_effect_set_uniform (CLUTTER_SHADER_EFFECT (effect), |
| 206 |
"tex", G_TYPE_INT, 1, 0); |
| 207 |
clutter_shader_effect_set_uniform (CLUTTER_SHADER_EFFECT (effect), |
| 208 |
"factor", G_TYPE_FLOAT, 1, 0.66); |
| 209 |
clutter_actor_meta_set_enabled (CLUTTER_ACTOR_META (effect), FALSE); |
| 210 |
clutter_actor_add_effect_with_name (rect, "desaturate", effect); |
| 211 |
|
| 212 |
rects[Center] = rect; |
| 213 |
|
| 214 |
/* build the other rectangles, and bind their position and size |
| 215 |
* to the center rectangle. we are going to animate the offset |
| 216 |
* of the BindConstraints |
| 217 |
*/ |
| 218 |
for (i = 0; i < N_RECTS; i++) |
| 219 |
{ |
| 220 |
if (i == Center) |
| 221 |
continue; |
| 222 |
|
| 223 |
clutter_color_from_string (&rect_color, colors[i]); |
| 224 |
|
| 225 |
rect = clutter_actor_new (); |
| 226 |
clutter_actor_set_background_color (rect, &rect_color); |
| 227 |
clutter_actor_set_opacity (rect, 0); |
| 228 |
clutter_actor_set_name (rect, names[i]); |
| 229 |
clutter_actor_add_child (stage, rect); |
| 230 |
|
| 231 |
constraint = clutter_bind_constraint_new (rects[Center], CLUTTER_BIND_X, 0.0); |
| 232 |
clutter_actor_add_constraint_with_name (rect, "x-bind", constraint); |
| 233 |
|
| 234 |
constraint = clutter_bind_constraint_new (rects[Center], CLUTTER_BIND_Y, 0.0); |
| 235 |
clutter_actor_add_constraint_with_name (rect, "y-bind", constraint); |
| 236 |
|
| 237 |
constraint = clutter_bind_constraint_new (rects[Center], CLUTTER_BIND_SIZE, 0.0); |
| 238 |
clutter_actor_add_constraint_with_name (rect, "size-bind", constraint); |
| 239 |
|
| 240 |
g_signal_connect (rect, "button-release-event", |
| 241 |
G_CALLBACK (on_button_release), |
| 242 |
NULL); |
| 243 |
|
| 244 |
rects[i] = rect; |
| 245 |
} |
| 246 |
|
| 247 |
clutter_actor_show (stage); |
| 248 |
|
| 249 |
clutter_main (); |
| 250 |
|
| 251 |
return EXIT_SUCCESS; |
| 252 |
} |