gtkshaderstack.c 9.04 KB
Newer Older
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
272
273
274
275
276
277
278
279
280
281
282
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "gtkshaderstack.h"

struct _GtkShaderStack
{
  GtkWidget parent_instance;

  GskGLShader *shader;
  GPtrArray *children;
  int current;
  int next;
  gboolean backwards;

  guint tick_id;
  float time;
  float duration;
  gint64 start_time;
};

struct _GtkShaderStackClass
{
  GtkWidgetClass parent_class;
};


enum {
  PROP_DURATION = 1,
  NUM_PROPERTIES
};

static GParamSpec *properties[NUM_PROPERTIES] = { NULL };

G_DEFINE_TYPE (GtkShaderStack, gtk_shader_stack, GTK_TYPE_WIDGET)

static void
gtk_shader_stack_finalize (GObject *object)
{
  GtkShaderStack *self = GTK_SHADER_STACK (object);

  g_object_unref (self->shader);

  G_OBJECT_CLASS (gtk_shader_stack_parent_class)->finalize (object);
}

static void
update_child_visible (GtkShaderStack *self)
{
  int i;

  for (i = 0; i < self->children->len; i++)
    {
      GtkWidget *child = g_ptr_array_index (self->children, i);

      gtk_widget_set_child_visible (child,
                                    i == self->current || i == self->next);
    }
}

static gboolean
transition_cb (GtkWidget     *widget,
               GdkFrameClock *clock,
               gpointer       unused)
{
  GtkShaderStack *self = GTK_SHADER_STACK (widget);
  gint64 frame_time;

  frame_time = gdk_frame_clock_get_frame_time (clock);

  if (self->start_time == 0)
    self->start_time = frame_time;

  self->time = (frame_time - self->start_time) / (float)G_USEC_PER_SEC;

  gtk_widget_queue_draw (widget);

  if (self->time >= self->duration)
    {
      self->current = self->next;
      self->next = -1;

      update_child_visible (self);

      return G_SOURCE_REMOVE;
    }
  else
    return G_SOURCE_CONTINUE;
}

static void
start_transition (GtkShaderStack *self)
{
  self->start_time = 0;
  self->tick_id = gtk_widget_add_tick_callback (GTK_WIDGET (self),
                                                transition_cb,
                                                NULL, NULL);
}

static void
stop_transition (GtkShaderStack *self)
{
  if (self->tick_id != 0)
    {
      gtk_widget_remove_tick_callback (GTK_WIDGET (self), self->tick_id);
      self->tick_id = 0;
    }

  if (self->next != -1)
    self->current = self->next;
  self->next = -1;

  update_child_visible (self);
}

static void
gtk_shader_stack_dispose (GObject *object)
{
  GtkShaderStack *self = GTK_SHADER_STACK (object);

  stop_transition (self);

  g_clear_pointer (&self->children, g_ptr_array_unref);

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

void
gtk_shader_stack_transition (GtkShaderStack *self,
                             gboolean forward)
{
  stop_transition (self);

  self->backwards = !forward;
  if (self->backwards)
    self->next = (self->current + self->children->len - 1) % self->children->len;
  else
    self->next = (self->current + 1) % self->children->len;

  update_child_visible (self);

  start_transition (self);
}

static void
gtk_shader_stack_init (GtkShaderStack *self)
{
  self->children = g_ptr_array_new_with_free_func ((GDestroyNotify)gtk_widget_unparent);
  self->current = -1;
  self->next = -1;
  self->backwards = FALSE;
  self->duration = 1.0;
}

static void
gtk_shader_stack_measure (GtkWidget      *widget,
                          GtkOrientation  orientation,
                          int             for_size,
                          int            *minimum,
                          int            *natural,
                          int            *minimum_baseline,
                          int            *natural_baseline)
{
  GtkShaderStack *self = GTK_SHADER_STACK (widget);
  int i;

  *minimum = 0;
  *natural = 0;

  for (i = 0; i < self->children->len; i++)
    {
      GtkWidget *child = g_ptr_array_index (self->children, i);
      int child_min, child_nat;

      if (gtk_widget_get_visible (child))
        {
          gtk_widget_measure (child, orientation, for_size, &child_min, &child_nat, NULL, NULL);

          *minimum = MAX (*minimum, child_min);
          *natural = MAX (*natural, child_nat);
        }
    }
}

static void
gtk_shader_stack_size_allocate (GtkWidget *widget,
                                int        width,
                                int        height,
                                int        baseline)
{
  GtkShaderStack *self = GTK_SHADER_STACK (widget);
  GtkAllocation child_allocation;
  GtkWidget *child;
  int i;

  child_allocation.x = 0;
  child_allocation.y = 0;
  child_allocation.width = width;
  child_allocation.height = height;

  for (i = 0; i < self->children->len; i++)
    {
      child = g_ptr_array_index (self->children, i);
      if (gtk_widget_get_visible (child))
        gtk_widget_size_allocate (child, &child_allocation, -1);
    }
}

static void
gtk_shader_stack_snapshot (GtkWidget   *widget,
                           GtkSnapshot *snapshot)
{
  GtkShaderStack *self = GTK_SHADER_STACK (widget);
  int width, height;
  GtkWidget *current, *next;

  width = gtk_widget_get_width (widget);
  height = gtk_widget_get_height (widget);

  current = g_ptr_array_index (self->children, self->current);

  if (self->next == -1)
    {
      gtk_widget_snapshot_child (widget, current, snapshot);
    }
  else
    {
      GtkNative *native = gtk_widget_get_native (widget);
      GskRenderer *renderer = gtk_native_get_renderer (native);
      float progress;

      next = g_ptr_array_index (self->children, self->next);

      progress = self->time / self->duration;

      if (self->backwards)
        {
          GtkWidget *tmp = next;
          next = current;
          current = tmp;
          progress = 1. - progress;
        }

      if (gsk_gl_shader_compile (self->shader, renderer, NULL))
        {
          gtk_snapshot_push_gl_shader (snapshot,
                                       self->shader,
                                       &GRAPHENE_RECT_INIT(0, 0, width, height),
                                       gsk_gl_shader_format_args (self->shader,
                                                                  "progress", progress,
                                                                  NULL));

          gtk_widget_snapshot_child (widget, current, snapshot);
          gtk_snapshot_gl_shader_pop_texture (snapshot); /* current child */
          gtk_widget_snapshot_child (widget, next, snapshot);
          gtk_snapshot_gl_shader_pop_texture (snapshot); /* next child */
          gtk_snapshot_pop (snapshot);
        }
      else
        {
          /* Non-shader fallback */
          gtk_widget_snapshot_child (widget, current, snapshot);
        }
    }
}

static void
gtk_shader_stack_get_property (GObject      *object,
                               guint         prop_id,
                               GValue       *value,
                               GParamSpec   *pspec)
{
  GtkShaderStack *self = GTK_SHADER_STACK (object);

  switch (prop_id)
    {
    case PROP_DURATION:
      g_value_set_float (value, self->duration);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_shader_stack_set_property (GObject      *object,
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  GtkShaderStack *self = GTK_SHADER_STACK (object);

  switch (prop_id)
    {
    case PROP_DURATION:
      self->duration = g_value_get_float (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_shader_stack_class_init (GtkShaderStackClass *class)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->finalize = gtk_shader_stack_finalize;
  object_class->dispose = gtk_shader_stack_dispose;
  object_class->get_property = gtk_shader_stack_get_property;
  object_class->set_property = gtk_shader_stack_set_property;

  widget_class->snapshot = gtk_shader_stack_snapshot;
  widget_class->measure = gtk_shader_stack_measure;
  widget_class->size_allocate = gtk_shader_stack_size_allocate;

  properties[PROP_DURATION] =
      g_param_spec_float ("duration", "Duration", "Duration",
                          0.1, 3.0, 1.0,
                          G_PARAM_READWRITE);

  g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
}

GtkWidget *
gtk_shader_stack_new (void)
{
  return g_object_new (GTK_TYPE_SHADER_STACK, NULL);
}

void
gtk_shader_stack_set_shader (GtkShaderStack *self,
                             GskGLShader    *shader)
{
  g_set_object (&self->shader, shader);
}

void
gtk_shader_stack_add_child (GtkShaderStack *self,
                            GtkWidget      *child)
{
  g_ptr_array_add (self->children, child);
  gtk_widget_set_parent (child, GTK_WIDGET (self));
  gtk_widget_queue_resize (GTK_WIDGET (self));

  if (self->current == -1)
    self->current = 0;
  else
    gtk_widget_set_child_visible (child, FALSE);
}
353
354
355
356
357
358
359
360
361

void
gtk_shader_stack_set_active (GtkShaderStack *self,
                             int             index)
{
  stop_transition (self);
  self->current = MIN (index, self->children->len);
  update_child_visible (self);
}