~oem-solutions-group/unity-2d/clutter-1.0

« back to all changes in this revision

Viewing changes to tests/interactive/test-bin-layout.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-03-21 13:27:56 UTC
  • mto: (2.1.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20100321132756-nf8yd30yxo3zzwcm
Tags: upstream-1.2.2
ImportĀ upstreamĀ versionĀ 1.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdlib.h>
 
2
#include <gmodule.h>
 
3
#include <cairo/cairo.h>
 
4
#include <clutter/clutter.h>
 
5
 
 
6
static ClutterActor *
 
7
make_background (const ClutterColor *color,
 
8
                 gfloat              width,
 
9
                 gfloat              height)
 
10
{
 
11
  ClutterActor *tex = clutter_cairo_texture_new (width, height);
 
12
  cairo_t *cr = clutter_cairo_texture_create (CLUTTER_CAIRO_TEXTURE (tex));
 
13
  cairo_pattern_t *pat;
 
14
  gfloat x, y;
 
15
 
 
16
#define BG_ROUND_RADIUS         12
 
17
 
 
18
  x = y = 0;
 
19
 
 
20
  cairo_move_to (cr, BG_ROUND_RADIUS, y);
 
21
  cairo_line_to (cr, width - BG_ROUND_RADIUS, y);
 
22
  cairo_curve_to (cr, width, y, width, y, width, BG_ROUND_RADIUS);
 
23
  cairo_line_to (cr, width, height - BG_ROUND_RADIUS);
 
24
  cairo_curve_to (cr, width, height, width, height, width - BG_ROUND_RADIUS, height);
 
25
  cairo_line_to (cr, BG_ROUND_RADIUS, height);
 
26
  cairo_curve_to (cr, x, height, x, height, x, height - BG_ROUND_RADIUS);
 
27
  cairo_line_to (cr, x, BG_ROUND_RADIUS);
 
28
  cairo_curve_to (cr, x, y, x, y, BG_ROUND_RADIUS, y);
 
29
 
 
30
  cairo_close_path (cr);
 
31
 
 
32
  clutter_cairo_set_source_color (cr, color);
 
33
  cairo_stroke (cr);
 
34
 
 
35
  x += 4;
 
36
  y += 4;
 
37
  width -= 4;
 
38
  height -= 4;
 
39
 
 
40
  cairo_move_to (cr, BG_ROUND_RADIUS, y);
 
41
  cairo_line_to (cr, width - BG_ROUND_RADIUS, y);
 
42
  cairo_curve_to (cr, width, y, width, y, width, BG_ROUND_RADIUS);
 
43
  cairo_line_to (cr, width, height - BG_ROUND_RADIUS);
 
44
  cairo_curve_to (cr, width, height, width, height, width - BG_ROUND_RADIUS, height);
 
45
  cairo_line_to (cr, BG_ROUND_RADIUS, height);
 
46
  cairo_curve_to (cr, x, height, x, height, x, height - BG_ROUND_RADIUS);
 
47
  cairo_line_to (cr, x, BG_ROUND_RADIUS);
 
48
  cairo_curve_to (cr, x, y, x, y, BG_ROUND_RADIUS, y);
 
49
 
 
50
  cairo_close_path (cr);
 
51
 
 
52
  pat = cairo_pattern_create_linear (0, 0, 0, height);
 
53
  cairo_pattern_add_color_stop_rgba (pat, 1, .85, .85, .85, 1);
 
54
  cairo_pattern_add_color_stop_rgba (pat, .95, 1, 1, 1, 1);
 
55
  cairo_pattern_add_color_stop_rgba (pat, .05, 1, 1, 1, 1);
 
56
  cairo_pattern_add_color_stop_rgba (pat, 0, .85, .85, .85, 1);
 
57
 
 
58
  cairo_set_source (cr, pat);
 
59
  cairo_fill (cr);
 
60
 
 
61
  cairo_pattern_destroy (pat);
 
62
  cairo_destroy (cr);
 
63
 
 
64
#undef BG_ROUND_RADIUS
 
65
 
 
66
  return tex;
 
67
}
 
68
 
 
69
static gboolean
 
70
on_box_enter (ClutterActor *box,
 
71
              ClutterEvent *event,
 
72
              ClutterActor *emblem)
 
73
{
 
74
  clutter_actor_animate (emblem, CLUTTER_LINEAR, 150,
 
75
                         "opacity", 255,
 
76
                         NULL);
 
77
 
 
78
  return TRUE;
 
79
}
 
80
 
 
81
static gboolean
 
82
on_box_leave (ClutterActor *box,
 
83
              ClutterEvent *event,
 
84
              ClutterActor *emblem)
 
85
{
 
86
  clutter_actor_animate (emblem, CLUTTER_LINEAR, 150,
 
87
                         "opacity", 0,
 
88
                         NULL);
 
89
 
 
90
  return TRUE;
 
91
}
 
92
 
 
93
G_MODULE_EXPORT int
 
94
test_bin_layout_main (int argc, char *argv[])
 
95
{
 
96
  ClutterActor *stage, *box, *rect;
 
97
  ClutterLayoutManager *layout;
 
98
  ClutterColor stage_color = { 0xe0, 0xf2, 0xfc, 0xff };
 
99
  ClutterColor bg_color = { 0xcc, 0xcc, 0xcc, 0x99 };
 
100
  ClutterColor *color;
 
101
 
 
102
  clutter_init (&argc, &argv);
 
103
 
 
104
  stage = clutter_stage_get_default ();
 
105
  clutter_stage_set_title (CLUTTER_STAGE (stage), "Box test");
 
106
  clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
 
107
  clutter_actor_set_size (stage, 640, 480);
 
108
 
 
109
  layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
 
110
                                   CLUTTER_BIN_ALIGNMENT_CENTER);
 
111
 
 
112
  box = clutter_box_new (layout);
 
113
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), box);
 
114
  clutter_actor_set_anchor_point_from_gravity (box, CLUTTER_GRAVITY_CENTER);
 
115
  clutter_actor_set_position (box, 320, 240);
 
116
  clutter_actor_set_reactive (box, TRUE);
 
117
  clutter_actor_set_name (box, "box");
 
118
 
 
119
  rect = make_background (&bg_color, 200, 200);
 
120
 
 
121
  /* first method: use clutter_box_pack() */
 
122
  clutter_box_pack (CLUTTER_BOX (box), rect,
 
123
                    "x-align", CLUTTER_BIN_ALIGNMENT_FILL,
 
124
                    "y-align", CLUTTER_BIN_ALIGNMENT_FILL,
 
125
                    NULL);
 
126
 
 
127
  clutter_actor_lower_bottom (rect);
 
128
  clutter_actor_set_name (rect, "background");
 
129
 
 
130
  {
 
131
    ClutterActor *tex;
 
132
    GError *error;
 
133
    gchar *file;
 
134
 
 
135
    error = NULL;
 
136
    file = g_build_filename (TESTS_DATADIR, "redhand.png", NULL);
 
137
    tex = clutter_texture_new_from_file (file, &error);
 
138
    if (error)
 
139
      g_error ("Unable to create texture: %s", error->message);
 
140
 
 
141
    clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (tex), TRUE);
 
142
 
 
143
    /* second method: use clutter_bin_layout_add() */
 
144
    clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (layout), tex,
 
145
                            CLUTTER_BIN_ALIGNMENT_CENTER,
 
146
                            CLUTTER_BIN_ALIGNMENT_CENTER);
 
147
 
 
148
    clutter_actor_raise (tex, rect);
 
149
    clutter_actor_set_width (tex, 175);
 
150
    clutter_actor_set_name (tex, "texture");
 
151
 
 
152
    g_free (file);
 
153
  }
 
154
 
 
155
  color = clutter_color_new (g_random_int_range (0, 255),
 
156
                             g_random_int_range (0, 255),
 
157
                             g_random_int_range (0, 255),
 
158
                             224);
 
159
 
 
160
  rect = clutter_rectangle_new_with_color (color);
 
161
 
 
162
  /* third method: container_add() and set_alignment() */
 
163
  clutter_container_add_actor (CLUTTER_CONTAINER (box), rect);
 
164
  clutter_bin_layout_set_alignment (CLUTTER_BIN_LAYOUT (layout), rect,
 
165
                                    CLUTTER_BIN_ALIGNMENT_END,
 
166
                                    CLUTTER_BIN_ALIGNMENT_END);
 
167
 
 
168
  clutter_actor_set_size (rect, 50, 50);
 
169
  clutter_actor_set_opacity (rect, 0);
 
170
  clutter_actor_raise_top (rect);
 
171
  clutter_actor_set_name (rect, "emblem");
 
172
 
 
173
 
 
174
  g_signal_connect (box,
 
175
                    "enter-event", G_CALLBACK (on_box_enter),
 
176
                    rect);
 
177
  g_signal_connect (box,
 
178
                    "leave-event", G_CALLBACK (on_box_leave),
 
179
                    rect);
 
180
 
 
181
  clutter_actor_show_all (stage);
 
182
 
 
183
  clutter_main ();
 
184
 
 
185
  clutter_color_free (color);
 
186
 
 
187
  return EXIT_SUCCESS;
 
188
}