Blame examples/gtk-clutter-test.c

Packit Service 6a0f92
#include <stdlib.h>
Packit Service 6a0f92
#include <math.h>
Packit Service 6a0f92
Packit Service 6a0f92
#include <gtk/gtk.h>
Packit Service 6a0f92
#include <clutter/clutter.h>
Packit Service 6a0f92
Packit Service 6a0f92
#include <clutter-gtk/clutter-gtk.h>
Packit Service 6a0f92
Packit Service 6a0f92
#define NHANDS          4
Packit Service 6a0f92
#define WINWIDTH        400
Packit Service 6a0f92
#define WINHEIGHT       400
Packit Service 6a0f92
#define RADIUS          150
Packit Service 6a0f92
Packit Service 6a0f92
#ifndef EXAMPLES_DATADIR
Packit Service 6a0f92
#define EXAMPLES_DATADIR "."
Packit Service 6a0f92
#endif
Packit Service 6a0f92
Packit Service 6a0f92
typedef struct SuperOH
Packit Service 6a0f92
{
Packit Service 6a0f92
  ClutterActor *stage;
Packit Service 6a0f92
  ClutterActor *hand[NHANDS];
Packit Service 6a0f92
  ClutterActor *bgtex;
Packit Service 6a0f92
  ClutterActor *group;
Packit Service 6a0f92
} SuperOH; 
Packit Service 6a0f92
Packit Service 6a0f92
static gboolean fade = FALSE;
Packit Service 6a0f92
static gboolean fullscreen = FALSE;
Packit Service 6a0f92
Packit Service 6a0f92
/* input handler */
Packit Service 6a0f92
static gboolean
Packit Service 6a0f92
input_cb (ClutterStage *stage,
Packit Service 6a0f92
	  ClutterEvent *event,
Packit Service 6a0f92
	  gpointer      data)
Packit Service 6a0f92
{
Packit Service 6a0f92
  ClutterEventType event_type = clutter_event_type (event);
Packit Service 6a0f92
  SuperOH *oh = data;
Packit Service 6a0f92
Packit Service 6a0f92
  if (event_type == CLUTTER_BUTTON_PRESS)
Packit Service 6a0f92
    {
Packit Service 6a0f92
      ClutterActor *a;
Packit Service 6a0f92
      gfloat x, y;
Packit Service 6a0f92
Packit Service 6a0f92
      clutter_event_get_coords (event, &x, &y);
Packit Service 6a0f92
Packit Service 6a0f92
      a = clutter_stage_get_actor_at_pos (stage, CLUTTER_PICK_ALL, x, y);
Packit Service 6a0f92
      if (a != NULL && (CLUTTER_IS_TEXTURE (a) || CLUTTER_IS_CLONE (a)))
Packit Service 6a0f92
	clutter_actor_hide (a);
Packit Service 6a0f92
    }
Packit Service 6a0f92
  else if (event->type == CLUTTER_KEY_PRESS)
Packit Service 6a0f92
    {
Packit Service 6a0f92
      g_print ("*** key press event (key:%c) ***\n",
Packit Service 6a0f92
	       clutter_event_get_key_symbol (event));
Packit Service 6a0f92
      
Packit Service 6a0f92
      if (clutter_event_get_key_symbol (event) == CLUTTER_KEY_q)
Packit Service 6a0f92
	gtk_main_quit ();
Packit Service 6a0f92
      else if (clutter_event_get_key_symbol (event) == CLUTTER_KEY_r)
Packit Service 6a0f92
        {
Packit Service 6a0f92
          int i;
Packit Service 6a0f92
Packit Service 6a0f92
          for (i = 0; i < NHANDS; i++)
Packit Service 6a0f92
            clutter_actor_show (oh->hand[i]);
Packit Service 6a0f92
        }
Packit Service 6a0f92
    }
Packit Service 6a0f92
Packit Service 6a0f92
  return TRUE;
Packit Service 6a0f92
}
Packit Service 6a0f92
Packit Service 6a0f92
Packit Service 6a0f92
/* Timeline handler */
Packit Service 6a0f92
void
Packit Service 6a0f92
frame_cb (ClutterTimeline *timeline, 
Packit Service 6a0f92
	  gint             msecs,
Packit Service 6a0f92
	  gpointer         data)
Packit Service 6a0f92
{
Packit Service 6a0f92
  SuperOH        *oh = (SuperOH *)data;
Packit Service 6a0f92
  gint            i;
Packit Service 6a0f92
  guint           rotation = clutter_timeline_get_progress (timeline) * 360.0f;
Packit Service 6a0f92
Packit Service 6a0f92
  /* Rotate everything clockwise about stage center*/
Packit Service 6a0f92
  clutter_actor_set_rotation_angle (oh->group, CLUTTER_Z_AXIS, rotation);
Packit Service 6a0f92
Packit Service 6a0f92
  for (i = 0; i < NHANDS; i++)
Packit Service 6a0f92
    {
Packit Service 6a0f92
      /* rotate each hand around there centers */
Packit Service 6a0f92
      clutter_actor_set_rotation_angle (oh->hand[i],
Packit Service 6a0f92
                                        CLUTTER_Z_AXIS,
Packit Service 6a0f92
                                        - 6.0 * rotation);
Packit Service 6a0f92
      if (fade == TRUE)
Packit Service 6a0f92
        clutter_actor_set_opacity (oh->hand[i], (255 - (rotation % 255)));
Packit Service 6a0f92
    }
Packit Service 6a0f92
}
Packit Service 6a0f92
Packit Service 6a0f92
static void
Packit Service 6a0f92
clickity (GtkButton *button,
Packit Service 6a0f92
          gpointer   stack)
Packit Service 6a0f92
{
Packit Service 6a0f92
  if (g_strcmp0 (gtk_stack_get_visible_child_name (GTK_STACK (stack)), "label") == 0)
Packit Service 6a0f92
    gtk_stack_set_visible_child_name (GTK_STACK (stack), "clutter");
Packit Service 6a0f92
  else
Packit Service 6a0f92
    gtk_stack_set_visible_child_name (GTK_STACK (stack), "label");
Packit Service 6a0f92
Packit Service 6a0f92
  fade = !fade;
Packit Service 6a0f92
}
Packit Service 6a0f92
Packit Service 6a0f92
static void
Packit Service 6a0f92
on_fullscreen (GtkButton *button,
Packit Service 6a0f92
               GtkWindow *window)
Packit Service 6a0f92
{
Packit Service 6a0f92
  if (!fullscreen)
Packit Service 6a0f92
    {
Packit Service 6a0f92
      gtk_window_fullscreen (window);
Packit Service 6a0f92
      fullscreen = TRUE;
Packit Service 6a0f92
    }
Packit Service 6a0f92
  else
Packit Service 6a0f92
    {
Packit Service 6a0f92
      gtk_window_unfullscreen (window);
Packit Service 6a0f92
      fullscreen = FALSE;
Packit Service 6a0f92
    }
Packit Service 6a0f92
}
Packit Service 6a0f92
Packit Service 6a0f92
int
Packit Service 6a0f92
main (int argc, char *argv[])
Packit Service 6a0f92
{
Packit Service 6a0f92
  ClutterTimeline *timeline;
Packit Service 6a0f92
  ClutterActor *stage;
Packit Service 6a0f92
  GtkWidget *window, *stack, *clutter;
Packit Service 6a0f92
  GtkWidget *label, *button, *vbox;
Packit Service 6a0f92
  GdkPixbuf *pixbuf;
Packit Service 6a0f92
  SuperOH *oh;
Packit Service 6a0f92
  gint i;
Packit Service 6a0f92
  GError *error;
Packit Service 6a0f92
Packit Service 6a0f92
  error = NULL;
Packit Service 6a0f92
  if (gtk_clutter_init_with_args (&argc, &argv,
Packit Service 6a0f92
                                  NULL,
Packit Service 6a0f92
                                  NULL,
Packit Service 6a0f92
                                  NULL,
Packit Service 6a0f92
                                  &error) != CLUTTER_INIT_SUCCESS)
Packit Service 6a0f92
    {
Packit Service 6a0f92
      if (error)
Packit Service 6a0f92
        {
Packit Service 6a0f92
          g_critical ("Unable to initialize Clutter-GTK: %s", error->message);
Packit Service 6a0f92
          g_error_free (error);
Packit Service 6a0f92
          return EXIT_FAILURE;
Packit Service 6a0f92
        }
Packit Service 6a0f92
      else
Packit Service 6a0f92
        g_error ("Unable to initialize Clutter-GTK");
Packit Service 6a0f92
    }
Packit Service 6a0f92
Packit Service 6a0f92
  /* calling gtk_clutter_init* multiple times should be safe */
Packit Service 6a0f92
  g_assert (gtk_clutter_init (NULL, NULL) == CLUTTER_INIT_SUCCESS);
Packit Service 6a0f92
Packit Service 6a0f92
  pixbuf = gdk_pixbuf_new_from_file (EXAMPLES_DATADIR G_DIR_SEPARATOR_S "redhand.png", NULL);
Packit Service 6a0f92
Packit Service 6a0f92
  if (!pixbuf)
Packit Service 6a0f92
    g_error("pixbuf load failed");
Packit Service 6a0f92
Packit Service 6a0f92
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
Packit Service 6a0f92
  gtk_window_set_default_size (GTK_WINDOW (window), WINWIDTH, WINHEIGHT);
Packit Service 6a0f92
  gtk_window_set_title (GTK_WINDOW (window), "Clutter Embedding");
Packit Service 6a0f92
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
Packit Service 6a0f92
Packit Service 6a0f92
  vbox = gtk_grid_new ();
Packit Service 6a0f92
  gtk_orientable_set_orientation (GTK_ORIENTABLE (vbox), GTK_ORIENTATION_VERTICAL);
Packit Service 6a0f92
  gtk_widget_set_hexpand (vbox, TRUE);
Packit Service 6a0f92
  gtk_widget_set_vexpand (vbox, TRUE);
Packit Service 6a0f92
  gtk_container_add (GTK_CONTAINER (window), vbox);
Packit Service 6a0f92
Packit Service 6a0f92
  stack = gtk_stack_new ();
Packit Service 6a0f92
  gtk_container_add (GTK_CONTAINER (vbox), stack);
Packit Service 6a0f92
Packit Service 6a0f92
  label = gtk_label_new ("This is a label in a stack");
Packit Service 6a0f92
  gtk_stack_add_named (GTK_STACK (stack), label, "label");
Packit Service 6a0f92
Packit Service 6a0f92
  clutter = gtk_clutter_embed_new ();
Packit Service 6a0f92
  gtk_stack_add_named (GTK_STACK (stack), clutter, "clutter");
Packit Service 6a0f92
  gtk_widget_realize (clutter);
Packit Service 6a0f92
Packit Service 6a0f92
  stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (clutter));
Packit Service 6a0f92
  clutter_actor_set_background_color (stage, CLUTTER_COLOR_LightSkyBlue);
Packit Service 6a0f92
Packit Service 6a0f92
  label = gtk_label_new ("This is a label");
Packit Service 6a0f92
  gtk_container_add (GTK_CONTAINER (vbox), label);
Packit Service 6a0f92
  gtk_widget_set_hexpand (label, TRUE);
Packit Service 6a0f92
Packit Service 6a0f92
  button = gtk_button_new_with_label ("This is a button...clicky");
Packit Service 6a0f92
  g_signal_connect (button, "clicked", G_CALLBACK (clickity), stack);
Packit Service 6a0f92
  gtk_container_add (GTK_CONTAINER (vbox), button);
Packit Service 6a0f92
  gtk_widget_set_hexpand (button, TRUE);
Packit Service 6a0f92
Packit Service 6a0f92
  button = gtk_button_new_with_mnemonic ("_Fullscreen");
Packit Service 6a0f92
  g_signal_connect (button, "clicked",
Packit Service 6a0f92
                    G_CALLBACK (on_fullscreen),
Packit Service 6a0f92
                    window);
Packit Service 6a0f92
  gtk_container_add (GTK_CONTAINER (vbox), button);
Packit Service 6a0f92
  gtk_widget_set_hexpand (button, TRUE);
Packit Service 6a0f92
Packit Service 6a0f92
  button = gtk_button_new_with_mnemonic ("_Quit");
Packit Service 6a0f92
  g_signal_connect_swapped (button, "clicked",
Packit Service 6a0f92
                            G_CALLBACK (gtk_widget_destroy),
Packit Service 6a0f92
                            window);
Packit Service 6a0f92
  gtk_container_add (GTK_CONTAINER (vbox), button);
Packit Service 6a0f92
  gtk_widget_set_hexpand (button, TRUE);
Packit Service 6a0f92
Packit Service 6a0f92
  oh = g_new (SuperOH, 1);
Packit Service 6a0f92
  oh->stage = stage;
Packit Service 6a0f92
Packit Service 6a0f92
  oh->group = clutter_actor_new ();
Packit Service 6a0f92
  clutter_actor_set_pivot_point (oh->group, 0.5, 0.5);
Packit Service 6a0f92
  
Packit Service 6a0f92
  for (i = 0; i < NHANDS; i++)
Packit Service 6a0f92
    {
Packit Service 6a0f92
      gint x, y, w, h;
Packit Service 6a0f92
Packit Service 6a0f92
      /* Create a texture from pixbuf, then clone in to same resources */
Packit Service 6a0f92
      if (i == 0)
Packit Service 6a0f92
        {
Packit Service 6a0f92
          oh->hand[i] = gtk_clutter_texture_new ();
Packit Service 6a0f92
          gtk_clutter_texture_set_from_pixbuf (GTK_CLUTTER_TEXTURE (oh->hand[i]), pixbuf, NULL);
Packit Service 6a0f92
        }
Packit Service 6a0f92
      else
Packit Service 6a0f92
        oh->hand[i] = clutter_clone_new (oh->hand[0]);
Packit Service 6a0f92
Packit Service 6a0f92
      /* Place around a circle */
Packit Service 6a0f92
      w = clutter_actor_get_width (oh->hand[0]);
Packit Service 6a0f92
      h = clutter_actor_get_height (oh->hand[0]);
Packit Service 6a0f92
Packit Service 6a0f92
      x = WINWIDTH / 2  + RADIUS * cos (i * M_PI / (NHANDS / 2)) - w / 2;
Packit Service 6a0f92
      y = WINHEIGHT / 2 + RADIUS * sin (i * M_PI / (NHANDS / 2)) - h / 2;
Packit Service 6a0f92
Packit Service 6a0f92
      clutter_actor_set_position (oh->hand[i], x, y);
Packit Service 6a0f92
      clutter_actor_set_pivot_point (oh->hand[i], 0.5, 0.5);
Packit Service 6a0f92
Packit Service 6a0f92
      /* Add to our group group */
Packit Service 6a0f92
      clutter_actor_add_child (oh->group, oh->hand[i]);
Packit Service 6a0f92
    }
Packit Service 6a0f92
Packit Service 6a0f92
  /* Add the group to the stage */
Packit Service 6a0f92
  clutter_actor_add_child (stage, oh->group);
Packit Service 6a0f92
Packit Service 6a0f92
  clutter_actor_add_constraint (oh->group, clutter_align_constraint_new (oh->stage, CLUTTER_ALIGN_BOTH, 0.5));
Packit Service 6a0f92
Packit Service 6a0f92
  g_signal_connect (stage, "button-press-event",
Packit Service 6a0f92
		    G_CALLBACK (input_cb), 
Packit Service 6a0f92
		    oh);
Packit Service 6a0f92
  g_signal_connect (stage, "key-release-event",
Packit Service 6a0f92
		    G_CALLBACK (input_cb),
Packit Service 6a0f92
		    oh);
Packit Service 6a0f92
Packit Service 6a0f92
  gtk_widget_show_all (window);
Packit Service 6a0f92
Packit Service 6a0f92
  /* Create a timeline to manage animation */
Packit Service 6a0f92
  timeline = clutter_timeline_new (6000);
Packit Service 6a0f92
  clutter_timeline_set_repeat_count (timeline, -1);
Packit Service 6a0f92
Packit Service 6a0f92
  /* fire a callback for frame change */
Packit Service 6a0f92
  g_signal_connect (timeline, "new-frame",  G_CALLBACK (frame_cb), oh);
Packit Service 6a0f92
Packit Service 6a0f92
  /* and start it */
Packit Service 6a0f92
  clutter_timeline_start (timeline);
Packit Service 6a0f92
Packit Service 6a0f92
  gtk_main ();
Packit Service 6a0f92
Packit Service 6a0f92
  return 0;
Packit Service 6a0f92
}