summaryrefslogtreecommitdiff
path: root/demos/gtk-demo/demo2widget.c
blob: 4b1aebadb55db629c8e2775b78946ea4b35677ff (plain)
1
2
3
4
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "demo2widget.h"
#include "demo2layout.h"

struct _Demo2Widget
{
  GtkWidget parent_instance;

  gint64 start_time;
  gint64 end_time;
  float start_position;
  float end_position;
  float start_offset;
  float end_offset;
  gboolean animating;
};

struct _Demo2WidgetClass
{
  GtkWidgetClass parent_class;
};

G_DEFINE_TYPE (Demo2Widget, demo2_widget, GTK_TYPE_WIDGET)

static void
demo2_widget_init (Demo2Widget *self)
{
  gtk_widget_set_focusable (GTK_WIDGET (self), TRUE);
}

static void
demo2_widget_dispose (GObject *object)
{
  GtkWidget *child;

  while ((child = gtk_widget_get_first_child (GTK_WIDGET (object))))
    gtk_widget_unparent (child);

  G_OBJECT_CLASS (demo2_widget_parent_class)->dispose (object);
}

/* From clutter-easing.c, based on Robert Penner's
 * infamous easing equations, MIT license.
 */
static double
ease_out_cubic (double t)
{
  double p = t - 1;

  return p * p * p + 1;
}

static gboolean
update_position (GtkWidget     *widget,
                 GdkFrameClock *clock,
                 gpointer       data)
{
  Demo2Widget *self = DEMO2_WIDGET (widget);
  Demo2Layout *layout = DEMO2_LAYOUT (gtk_widget_get_layout_manager (widget));
  gint64 now;
  double t;

  now = gdk_frame_clock_get_frame_time (clock);

  if (now >= self->end_time)
    {
      self->animating = FALSE;

      return G_SOURCE_REMOVE;
    }

  t = (now - self->start_time) / (double) (self->end_time - self->start_time);

  t = ease_out_cubic (t);

  demo2_layout_set_position (layout, self->start_position + t * (self->end_position - self->start_position));
  demo2_layout_set_offset (layout, self->start_offset + t * (self->end_offset - self->start_offset));
  gtk_widget_queue_allocate (widget);

  return G_SOURCE_CONTINUE;
}

static void
rotate_sphere (GtkWidget  *widget,
               const char *action,
               GVariant   *parameters)
{
  Demo2Widget *self = DEMO2_WIDGET (widget);
  Demo2Layout *layout = DEMO2_LAYOUT (gtk_widget_get_layout_manager (widget));
  GtkOrientation orientation;
  int direction;

  g_variant_get (parameters, "(ii)", &orientation, &direction);

  self->end_position = self->start_position = demo2_layout_get_position (layout);
  self->end_offset = self->start_offset = demo2_layout_get_offset (layout);
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    self->end_position += 10 * direction;
  else
    self->end_offset += 10 * direction;
  self->start_time = g_get_monotonic_time ();
  self->end_time = self->start_time + 0.5 * G_TIME_SPAN_SECOND;

  if (!self->animating)
    {
      gtk_widget_add_tick_callback (widget, update_position, NULL, NULL);
      self->animating = TRUE;
    }
}

static void
demo2_widget_snapshot (GtkWidget   *widget,
                       GtkSnapshot *snapshot)
{
  GtkWidget *child;

  for (child = gtk_widget_get_first_child (widget);
       child != NULL;
       child = gtk_widget_get_next_sibling (child))
    {
      /* our layout manager sets this for children that are out of view */
      if (!gtk_widget_get_child_visible (child))
        continue;

      gtk_widget_snapshot_child (widget, child, snapshot);
    }
}

static void
demo2_widget_class_init (Demo2WidgetClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

  object_class->dispose = demo2_widget_dispose;

  widget_class->snapshot = demo2_widget_snapshot;

  gtk_widget_class_install_action (widget_class, "rotate", "(ii)", rotate_sphere);

  gtk_widget_class_add_binding_action (widget_class,
                                       GDK_KEY_Left, 0,
                                       "rotate",
                                       "(ii)", GTK_ORIENTATION_HORIZONTAL, -1);
  gtk_widget_class_add_binding_action (widget_class,
                                       GDK_KEY_Right, 0,
                                       "rotate",
                                       "(ii)", GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_widget_class_add_binding_action (widget_class,
                                       GDK_KEY_Up, 0,
                                       "rotate",
                                       "(ii)", GTK_ORIENTATION_VERTICAL, 1);
  gtk_widget_class_add_binding_action (widget_class,
                                       GDK_KEY_Down, 0,
                                       "rotate",
                                       "(ii)", GTK_ORIENTATION_VERTICAL, -1);

  /* here is where we use our custom layout manager */
  gtk_widget_class_set_layout_manager_type (widget_class, DEMO2_TYPE_LAYOUT);
}

GtkWidget *
demo2_widget_new (void)
{
  return g_object_new (DEMO2_TYPE_WIDGET, NULL);
}

void
demo2_widget_add_child (Demo2Widget *self,
                        GtkWidget   *child)
{
  gtk_widget_set_parent (child, GTK_WIDGET (self));
}