actor-model.c 15 KB
Newer Older
1 2 3 4
#include <glib-object.h>
#include <gio/gio.h>
#include <clutter/clutter.h>

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
/* {{{ MenuItemModel */

/* This is our "model" of a Menu item; it has a "label" property, and
 * a "selected" state property. The user is supposed to operate on the
 * model instance, and change its state.
 */

#define EXAMPLE_TYPE_MENU_ITEM_MODEL (example_menu_item_model_get_type ())

G_DECLARE_FINAL_TYPE (ExampleMenuItemModel, example_menu_item_model, EXAMPLE, MENU_ITEM_MODEL, GObject)

struct _ExampleMenuItemModel
{
  GObject parent_instance;

  char *label;

  gboolean selected;
};

struct _ExampleMenuItemModelClass
{
  GObjectClass parent_class;
};

enum {
  MENU_ITEM_MODEL_PROP_LABEL = 1,
  MENU_ITEM_MODEL_PROP_SELECTED,
  MENU_ITEM_MODEL_N_PROPS
};

static GParamSpec *menu_item_model_props[MENU_ITEM_MODEL_N_PROPS] = { NULL, };

G_DEFINE_TYPE (ExampleMenuItemModel, example_menu_item_model, G_TYPE_OBJECT)

static void
example_menu_item_model_finalize (GObject *gobject)
{
  ExampleMenuItemModel *self = (ExampleMenuItemModel *) gobject;

  g_free (self->label);

  G_OBJECT_CLASS (example_menu_item_model_parent_class)->finalize (gobject);
}

static void
example_menu_item_model_set_property (GObject      *gobject,
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
  ExampleMenuItemModel *self = (ExampleMenuItemModel *) gobject;

  switch (prop_id)
    {
    case MENU_ITEM_MODEL_PROP_LABEL:
      g_free (self->label);
      self->label = g_value_dup_string (value);
      break;

    case MENU_ITEM_MODEL_PROP_SELECTED:
      self->selected = g_value_get_boolean (value);
      break;
    }
}

static void
example_menu_item_model_get_property (GObject    *gobject,
                                      guint       prop_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
  ExampleMenuItemModel *self = (ExampleMenuItemModel *) gobject;

  switch (prop_id)
    {
    case MENU_ITEM_MODEL_PROP_LABEL:
      g_value_set_string (value, self->label);
      break;

    case MENU_ITEM_MODEL_PROP_SELECTED:
      g_value_set_boolean (value, self->selected);
      break;
    }
}

static void
example_menu_item_model_class_init (ExampleMenuItemModelClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->set_property = example_menu_item_model_set_property;
  gobject_class->get_property = example_menu_item_model_get_property;
  gobject_class->finalize = example_menu_item_model_finalize;

  menu_item_model_props[MENU_ITEM_MODEL_PROP_LABEL] =
    g_param_spec_string ("label", NULL, NULL,
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  menu_item_model_props[MENU_ITEM_MODEL_PROP_SELECTED] =
    g_param_spec_boolean ("selected", NULL, NULL,
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_properties (gobject_class, MENU_ITEM_MODEL_N_PROPS, menu_item_model_props);
}

static void
example_menu_item_model_init (ExampleMenuItemModel *self)
{
}
/* }}} */

117
/* {{{ MenuItemView */
118

119 120 121 122 123 124 125
/* This is our "view" of a Menu item; it changes state depending on whether
 * the "selected" property is set. The "view" reflects the state of the
 * "model" instance, though it has no direct connection to it.
 */
#define EXAMPLE_TYPE_MENU_ITEM_VIEW (example_menu_item_view_get_type ())

G_DECLARE_FINAL_TYPE (ExampleMenuItemView, example_menu_item_view, EXAMPLE, MENU_ITEM_VIEW, ClutterText)
126

127
struct _ExampleMenuItemView
128 129 130 131 132 133
{
  ClutterText parent_instance;

  gboolean is_selected;
};

134
struct _ExampleMenuItemViewClass
135 136 137 138
{
  ClutterTextClass parent_class;
};

139
G_DEFINE_TYPE (ExampleMenuItemView, example_menu_item_view, CLUTTER_TYPE_TEXT)
140 141

enum {
142 143
  MENU_ITEM_VIEW_PROP_SELECTED = 1,
  MENU_ITEM_VIEW_N_PROPS
144 145
};

146
static GParamSpec *menu_item_view_props[MENU_ITEM_VIEW_N_PROPS] = { NULL, };
147 148

static void
149 150
example_menu_item_view_set_selected (ExampleMenuItemView *self,
                                     gboolean             selected)
151 152 153 154 155 156 157 158 159 160 161 162
{
  selected = !!selected;
  if (self->is_selected == selected)
    return;

  self->is_selected = selected;

  if (self->is_selected)
    clutter_text_set_color (CLUTTER_TEXT (self), CLUTTER_COLOR_LightSkyBlue);
  else
    clutter_text_set_color (CLUTTER_TEXT (self), CLUTTER_COLOR_White);

163
  g_object_notify_by_pspec (G_OBJECT (self), menu_item_view_props[MENU_ITEM_VIEW_PROP_SELECTED]);
164 165 166
}

static void
167 168 169 170
example_menu_item_view_set_property (GObject      *gobject,
                                     guint         prop_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
171 172 173
{
  switch (prop_id)
    {
174 175 176
    case MENU_ITEM_VIEW_PROP_SELECTED:
      example_menu_item_view_set_selected (EXAMPLE_MENU_ITEM_VIEW (gobject),
                                           g_value_get_boolean (value));
177 178 179 180 181
      break;
    }
}

static void
182 183 184 185
example_menu_item_view_get_property (GObject    *gobject,
                                     guint       prop_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
186 187 188
{
  switch (prop_id)
    {
189 190
    case MENU_ITEM_VIEW_PROP_SELECTED:
      g_value_set_boolean (value, EXAMPLE_MENU_ITEM_VIEW (gobject)->is_selected);
191 192 193 194 195
      break;
    }
}

static void
196
example_menu_item_view_class_init (ExampleMenuItemViewClass *klass)
197 198 199
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

200 201
  gobject_class->set_property = example_menu_item_view_set_property;
  gobject_class->get_property = example_menu_item_view_get_property;
202

203
  menu_item_view_props[MENU_ITEM_VIEW_PROP_SELECTED] =
204 205 206 207 208
    g_param_spec_boolean ("selected", NULL, NULL,
                          FALSE,
                          G_PARAM_READWRITE |
                          G_PARAM_STATIC_STRINGS);

209
  g_object_class_install_properties (gobject_class, MENU_ITEM_VIEW_N_PROPS, menu_item_view_props);
210 211
}

212 213 214 215 216 217 218 219 220
static void
example_menu_item_view__transition_stopped (ClutterActor *actor,
                                            const char   *transition,
                                            gboolean      is_finished)
{
  clutter_actor_set_scale (actor, 1.0, 1.0);
  clutter_actor_set_opacity (actor, 255);
}

221
static void
222
example_menu_item_view_init (ExampleMenuItemView *self)
223 224 225
{
  ClutterText *text = CLUTTER_TEXT (self);
  ClutterActor *actor = CLUTTER_ACTOR (self);
226 227
  ClutterTransition *scalex_trans, *scaley_trans, *fade_trans;
  ClutterTransition *group;
228 229 230 231 232 233

  clutter_text_set_font_name (text, "Sans Bold 24px");
  clutter_text_set_color (text, CLUTTER_COLOR_White);

  clutter_actor_set_margin_left (actor, 12);
  clutter_actor_set_margin_right (actor, 12);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

  clutter_actor_set_pivot_point (actor, 0.5, 0.5);

  scalex_trans = clutter_property_transition_new ("scale-x");
  clutter_transition_set_from (scalex_trans, G_TYPE_FLOAT, 1.0);
  clutter_transition_set_to (scalex_trans, G_TYPE_FLOAT, 3.0);

  scaley_trans = clutter_property_transition_new ("scale-y");
  clutter_transition_set_from (scaley_trans, G_TYPE_FLOAT, 1.0);
  clutter_transition_set_to (scaley_trans, G_TYPE_FLOAT, 3.0);

  fade_trans = clutter_property_transition_new ("opacity");
  clutter_transition_set_to (fade_trans, G_TYPE_UINT, 0);

  group = clutter_transition_group_new ();
  clutter_transition_group_add_transition (CLUTTER_TRANSITION_GROUP (group), scalex_trans);
  clutter_transition_group_add_transition (CLUTTER_TRANSITION_GROUP (group), scaley_trans);
  clutter_transition_group_add_transition (CLUTTER_TRANSITION_GROUP (group), fade_trans);
  clutter_timeline_set_duration (CLUTTER_TIMELINE (group), 250);
  clutter_timeline_set_progress_mode (CLUTTER_TIMELINE (group), CLUTTER_EASE_OUT);

  clutter_actor_add_transition (actor, "activateTransition", group);
  g_object_unref (group);

  clutter_timeline_stop (CLUTTER_TIMELINE (group));

  g_signal_connect (actor, "transition-stopped",
                    G_CALLBACK (example_menu_item_view__transition_stopped),
                    group);
}

static void
example_menu_item_view_activate (ExampleMenuItemView *self)
{
  ClutterTransition *t;

  t = clutter_actor_get_transition (CLUTTER_ACTOR (self), "activateTransition");
  clutter_timeline_start (CLUTTER_TIMELINE (t));
272 273 274 275 276
}

/* }}} */

/* {{{ Menu */
277 278 279 280 281 282

/* This is our container actor, which binds the GListStore with the
 * ExampleMenuItemModel instances to the ExampleMenuItemView actors
 */

#define EXAMPLE_TYPE_MENU (example_menu_get_type ())
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

G_DECLARE_FINAL_TYPE (ExampleMenu, example_menu, EXAMPLE, MENU, ClutterActor)

struct _ExampleMenu
{
  ClutterActor parent_instance;

  int current_idx;
};

struct _ExampleMenuClass
{
  ClutterActorClass parent_class;
};

G_DEFINE_TYPE (ExampleMenu, example_menu, CLUTTER_TYPE_ACTOR)

static void
example_menu_class_init (ExampleMenuClass *klass)
{
}

static void
example_menu_init (ExampleMenu *self)
{
  ClutterActor *actor = CLUTTER_ACTOR (self);
  ClutterLayoutManager *layout;

  layout = clutter_box_layout_new ();
  clutter_box_layout_set_orientation (CLUTTER_BOX_LAYOUT (layout), CLUTTER_ORIENTATION_VERTICAL);
  clutter_box_layout_set_spacing (CLUTTER_BOX_LAYOUT (layout), 12);

  clutter_actor_set_layout_manager (actor, layout);
  clutter_actor_set_background_color (actor, CLUTTER_COLOR_Black);

  self->current_idx = -1;
}

static ClutterActor *
example_menu_select_item (ExampleMenu *self,
                          int          idx)
{
  ClutterActor *item;

327 328
  /* Any change in the view is reflected into the model */

329 330 331 332 333
  if (idx == self->current_idx)
    return clutter_actor_get_child_at_index (CLUTTER_ACTOR (self), self->current_idx);

  item = clutter_actor_get_child_at_index (CLUTTER_ACTOR (self), self->current_idx);
  if (item != NULL)
334
    example_menu_item_view_set_selected ((ExampleMenuItemView *) item, FALSE);
335 336 337 338 339 340 341 342 343 344

  if (idx < 0)
    idx = clutter_actor_get_n_children (CLUTTER_ACTOR (self)) - 1;
  else if (idx >= clutter_actor_get_n_children (CLUTTER_ACTOR (self)))
    idx = 0;

  self->current_idx = idx;

  item = clutter_actor_get_child_at_index (CLUTTER_ACTOR (self), self->current_idx);
  if (item != NULL)
345
    example_menu_item_view_set_selected ((ExampleMenuItemView *) item, TRUE);
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

  return item;
}

static ClutterActor *
example_menu_select_next (ExampleMenu *self)
{
  return example_menu_select_item (self, self->current_idx + 1);
}

static ClutterActor *
example_menu_select_prev (ExampleMenu *self)
{
  return example_menu_select_item (self, self->current_idx - 1);
}

362 363 364 365 366 367 368 369 370 371 372 373 374
static void
example_menu_activate_item (ExampleMenu *self)
{
  ClutterActor *child;

  child = clutter_actor_get_child_at_index (CLUTTER_ACTOR (self),
                                            self->current_idx);
  if (child == NULL)
    return;

  example_menu_item_view_activate ((ExampleMenuItemView *) child);
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
/* }}} */

/* {{{ main */
static gboolean
on_key_press (ClutterActor *stage,
              ClutterEvent *event)
{
  ClutterActor *scroll = clutter_actor_get_first_child (stage);
  ClutterActor *menu = clutter_actor_get_first_child (scroll);
  ClutterActor *item = NULL;
  guint key = clutter_event_get_key_symbol (event);
  ClutterPoint p;

  switch (key)
    {
    case CLUTTER_KEY_q:
      clutter_main_quit ();
      break;

    case CLUTTER_KEY_Up:
      item = example_menu_select_prev ((ExampleMenu *) menu);
      clutter_actor_get_position (item, &p.x, &p.y);
      break;

    case CLUTTER_KEY_Down:
      item = example_menu_select_next ((ExampleMenu *) menu);
      clutter_actor_get_position (item, &p.x, &p.y);
      break;
403 404 405 406 407

    case CLUTTER_KEY_Return:
    case CLUTTER_KEY_KP_Enter:
      example_menu_activate_item ((ExampleMenu *) menu);
      break;
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    }

  if (item != NULL)
    clutter_scroll_actor_scroll_to_point (CLUTTER_SCROLL_ACTOR (scroll), &p);

  return CLUTTER_EVENT_PROPAGATE;
}

static void
on_model_item_selection (GObject    *model_item,
                         GParamSpec *pspec,
                         gpointer    data)
{
  char *label = NULL;
  gboolean is_selected = FALSE;

  g_object_get (model_item, "label", &label, "selected", &is_selected, NULL);

  if (is_selected)
    g_print ("Item '%s' selected!\n", label);

  g_free (label);
}

static ClutterActor *
create_menu_actor (void)
{
435
  /* Our store of menu item models */
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
  GListStore *model = g_list_store_new (EXAMPLE_TYPE_MENU_ITEM_MODEL);
  ClutterActor *menu = g_object_new (EXAMPLE_TYPE_MENU, NULL);
  int i;

  /* Populate the model */
  for (i = 0; i < 12; i++)
    {
      char *label = g_strdup_printf ("Option %02d", i + 1);

      ExampleMenuItemModel *item = g_object_new (EXAMPLE_TYPE_MENU_ITEM_MODEL,
                                                 "label", label,
                                                 NULL);

      g_list_store_append (model, item);

451 452 453 454
      g_signal_connect (item, "notify::selected",
                        G_CALLBACK (on_model_item_selection),
                        NULL);

455 456 457 458
      g_object_unref (item);
      g_free (label);
    }

459 460 461 462
  /* Bind the list of menu item models to the menu actor; this will
   * create ClutterActor views of each item in the model, and add them
   * to the menu actor
   */
463 464 465 466 467
  clutter_actor_bind_model_with_properties (menu, G_LIST_MODEL (model),
                                            EXAMPLE_TYPE_MENU_ITEM_VIEW,
                                            "label", "text", G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE,
                                            "selected", "selected", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE,
                                            NULL);
468

469 470 471 472
  /* We don't need a pointer to the model any more, so we transfer ownership
   * to the menu actor; this means that the model will go away when the menu
   * actor is destroyed
   */
473 474
  g_object_unref (model);

475
  /* Select the first item in the menu */
476 477 478 479 480
  example_menu_select_item ((ExampleMenu *) menu, 0);

  return menu;
}

481
/* The scrolling container for the menu */
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
static ClutterActor *
create_scroll_actor (void)
{
  ClutterActor *menu = clutter_scroll_actor_new ();
  clutter_actor_set_name (menu, "scroll");
  clutter_scroll_actor_set_scroll_mode (CLUTTER_SCROLL_ACTOR (menu),
                                        CLUTTER_SCROLL_VERTICALLY);
  clutter_actor_set_easing_duration (menu, 250);
  clutter_actor_add_child (menu, create_menu_actor ());

  return menu;
}

int
main (int argc, char *argv[])
{
  ClutterActor *stage, *menu;

  if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
    return 1;

  stage = clutter_stage_new ();
  clutter_stage_set_title (CLUTTER_STAGE (stage), "Actor Model");
  clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
  g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
  g_signal_connect (stage, "key-press-event", G_CALLBACK (on_key_press), NULL);
  clutter_actor_show (stage);

#define PADDING 18.f

  menu = create_scroll_actor ();
  clutter_actor_set_position (menu, 0, PADDING);
  clutter_actor_add_constraint (menu, clutter_align_constraint_new (stage, CLUTTER_ALIGN_X_AXIS, 0.5));
  clutter_actor_add_constraint (menu, clutter_bind_constraint_new (stage, CLUTTER_BIND_HEIGHT, -PADDING * 2));
  clutter_actor_add_child (stage, menu);

  clutter_main ();

  return 0;
}
/* }}} */