1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 3 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
25
#include "libgimpmath/gimpmath.h"
27
#include "display-types.h"
29
#include "core/gimpcoords.h"
30
#include "core/gimpcoords-interpolate.h"
31
#include "core/gimpmarshal.h"
33
#include "gimpmotionbuffer.h"
36
/* Velocity unit is screen pixels per millisecond we pass to tools as 1. */
37
#define VELOCITY_UNIT 3.0
38
#define EVENT_FILL_PRECISION 6.0
39
#define DIRECTION_RADIUS (1.5 / MAX (scale_x, scale_y))
40
#define SMOOTH_FACTOR 0.3
56
/* local function prototypes */
58
static void gimp_motion_buffer_dispose (GObject *object);
59
static void gimp_motion_buffer_finalize (GObject *object);
60
static void gimp_motion_buffer_set_property (GObject *object,
64
static void gimp_motion_buffer_get_property (GObject *object,
69
static void gimp_motion_buffer_push_event_history (GimpMotionBuffer *buffer,
70
const GimpCoords *coords);
71
static void gimp_motion_buffer_pop_event_queue (GimpMotionBuffer *buffer,
74
static void gimp_motion_buffer_interpolate_stroke (GimpMotionBuffer *buffer,
76
static gboolean gimp_motion_buffer_event_queue_timeout (GimpMotionBuffer *buffer);
79
G_DEFINE_TYPE (GimpMotionBuffer, gimp_motion_buffer, GIMP_TYPE_OBJECT)
81
#define parent_class gimp_motion_buffer_parent_class
83
static guint motion_buffer_signals[LAST_SIGNAL] = { 0 };
85
static const GimpCoords default_coords = GIMP_COORDS_DEFAULT_VALUES;
89
gimp_motion_buffer_class_init (GimpMotionBufferClass *klass)
91
GObjectClass *object_class = G_OBJECT_CLASS (klass);
93
motion_buffer_signals[STROKE] =
94
g_signal_new ("stroke",
95
G_TYPE_FROM_CLASS (klass),
97
G_STRUCT_OFFSET (GimpMotionBufferClass, stroke),
99
gimp_marshal_VOID__POINTER_UINT_FLAGS,
103
GDK_TYPE_MODIFIER_TYPE);
105
motion_buffer_signals[HOVER] =
106
g_signal_new ("hover",
107
G_TYPE_FROM_CLASS (klass),
109
G_STRUCT_OFFSET (GimpMotionBufferClass, hover),
111
gimp_marshal_VOID__POINTER_FLAGS_BOOLEAN,
114
GDK_TYPE_MODIFIER_TYPE,
117
object_class->dispose = gimp_motion_buffer_dispose;
118
object_class->finalize = gimp_motion_buffer_finalize;
119
object_class->set_property = gimp_motion_buffer_set_property;
120
object_class->get_property = gimp_motion_buffer_get_property;
124
gimp_motion_buffer_init (GimpMotionBuffer *buffer)
126
buffer->event_history = g_array_new (FALSE, FALSE, sizeof (GimpCoords));
127
buffer->event_queue = g_array_new (FALSE, FALSE, sizeof (GimpCoords));
131
gimp_motion_buffer_dispose (GObject *object)
133
GimpMotionBuffer *buffer = GIMP_MOTION_BUFFER (object);
135
if (buffer->event_delay_timeout)
137
g_source_remove (buffer->event_delay_timeout);
138
buffer->event_delay_timeout = 0;
141
G_OBJECT_CLASS (parent_class)->dispose (object);
145
gimp_motion_buffer_finalize (GObject *object)
147
GimpMotionBuffer *buffer = GIMP_MOTION_BUFFER (object);
149
if (buffer->event_history)
151
g_array_free (buffer->event_history, TRUE);
152
buffer->event_history = NULL;
155
if (buffer->event_queue)
157
g_array_free (buffer->event_queue, TRUE);
158
buffer->event_queue = NULL;
161
G_OBJECT_CLASS (parent_class)->finalize (object);
165
gimp_motion_buffer_set_property (GObject *object,
173
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
179
gimp_motion_buffer_get_property (GObject *object,
187
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
193
/* public functions */
196
gimp_motion_buffer_new (void)
198
return g_object_new (GIMP_TYPE_MOTION_BUFFER,
203
gimp_motion_buffer_begin_stroke (GimpMotionBuffer *buffer,
205
GimpCoords *last_motion)
207
g_return_if_fail (GIMP_IS_MOTION_BUFFER (buffer));
208
g_return_if_fail (last_motion != NULL);
210
buffer->last_read_motion_time = time;
212
*last_motion = buffer->last_coords;
216
gimp_motion_buffer_end_stroke (GimpMotionBuffer *buffer)
218
g_return_if_fail (GIMP_IS_MOTION_BUFFER (buffer));
220
if (buffer->event_delay_timeout)
222
g_source_remove (buffer->event_delay_timeout);
223
buffer->event_delay_timeout = 0;
226
gimp_motion_buffer_event_queue_timeout (buffer);
230
* gimp_motion_buffer_motion_event:
238
* This function evaluates the event to decide if the change is big
239
* enough to need handling and returns FALSE, if change is less than
240
* set filter level taking a whole lot of load off any draw tools that
241
* have no use for these events anyway. If the event is seen fit at
242
* first look, it is evaluated for speed and smoothed. Due to lousy
243
* time resolution of events pretty strong smoothing is applied to
244
* timestamps for sensible speed result. This function is also ideal
245
* for other event adjustment like pressure curve or calculating other
246
* derived dynamics factors like angular velocity calculation from
247
* tilt values, to allow for even more dynamic brushes. Calculated
248
* distance to last event is stored in GimpCoords because its a
249
* sideproduct of velocity calculation and is currently calculated in
250
* each tool. If they were to use this distance, more resouces on
251
* recalculating the same value would be saved.
253
* Return value: %TRUE if the motion was significant enough to be
254
* processed, %FALSE otherwise.
257
gimp_motion_buffer_motion_event (GimpMotionBuffer *buffer,
264
gdouble delta_time = 0.001;
265
gdouble delta_x = 0.0;
266
gdouble delta_y = 0.0;
267
gdouble dir_delta_x = 0.0;
268
gdouble dir_delta_y = 0.0;
269
gdouble distance = 1.0;
271
g_return_val_if_fail (GIMP_IS_MOTION_BUFFER (buffer), FALSE);
272
g_return_val_if_fail (coords != NULL, FALSE);
274
/* the last_read_motion_time most be set unconditionally, so set
277
buffer->last_read_motion_time = time;
279
delta_time = (buffer->last_motion_delta_time * (1 - SMOOTH_FACTOR) +
280
(time - buffer->last_motion_time) * SMOOTH_FACTOR);
282
if (buffer->last_motion_time == 0)
284
/* First pair is invalid to do any velocity calculation, so we
285
* apply a constant value.
287
coords->velocity = 1.0;
291
GimpCoords last_dir_event = buffer->last_coords;
296
delta_x = last_dir_event.x - coords->x;
297
delta_y = last_dir_event.y - coords->y;
299
/* Events with distances less than the screen resolution are
300
* not worth handling.
302
filter = MIN (1.0 / scale_x, 1.0 / scale_y) / 2.0;
304
if (fabs (delta_x) < filter &&
305
fabs (delta_y) < filter)
310
distance = dist = sqrt (SQR (delta_x) + SQR (delta_y));
312
/* If even smoothed time resolution does not allow to guess for
313
* speed, use last velocity.
317
coords->velocity = buffer->last_coords.velocity;
321
/* We need to calculate the velocity in screen coordinates
322
* for human interaction
324
gdouble screen_distance = (distance * MIN (scale_x, scale_y));
326
/* Calculate raw valocity */
327
coords->velocity = ((screen_distance / delta_time) / VELOCITY_UNIT);
329
/* Adding velocity dependent smoothing, feels better in tools. */
330
coords->velocity = (buffer->last_coords.velocity *
331
(1 - MIN (SMOOTH_FACTOR, coords->velocity)) +
333
MIN (SMOOTH_FACTOR, coords->velocity));
335
/* Speed needs upper limit */
336
coords->velocity = MIN (coords->velocity, 1.0);
339
if (((fabs (delta_x) > DIRECTION_RADIUS) &&
340
(fabs (delta_y) > DIRECTION_RADIUS)) ||
341
(buffer->event_history->len < 4))
343
dir_delta_x = delta_x;
344
dir_delta_y = delta_y;
350
while (((fabs (dir_delta_x) < DIRECTION_RADIUS) ||
351
(fabs (dir_delta_y) < DIRECTION_RADIUS)) &&
354
last_dir_event = g_array_index (buffer->event_history,
357
dir_delta_x = last_dir_event.x - coords->x;
358
dir_delta_y = last_dir_event.y - coords->y;
364
if ((fabs (dir_delta_x) < DIRECTION_RADIUS) ||
365
(fabs (dir_delta_y) < DIRECTION_RADIUS))
367
coords->direction = buffer->last_coords.direction;
371
coords->direction = gimp_coords_direction (&last_dir_event, coords);
374
coords->direction = coords->direction - floor (coords->direction);
376
delta_dir = coords->direction - buffer->last_coords.direction;
378
if (delta_dir < -0.5)
380
coords->direction = (0.5 * coords->direction +
381
0.5 * (buffer->last_coords.direction - 1.0));
383
else if (delta_dir > 0.5)
385
coords->direction = (0.5 * coords->direction +
386
0.5 * (buffer->last_coords.direction + 1.0));
390
coords->direction = (0.5 * coords->direction +
391
0.5 * buffer->last_coords.direction);
394
coords->direction = coords->direction - floor (coords->direction);
396
/* do event fill for devices that do not provide enough events */
397
if (distance >= EVENT_FILL_PRECISION &&
399
buffer->event_history->len >= 2)
401
if (buffer->event_delay)
403
gimp_motion_buffer_interpolate_stroke (buffer, coords);
407
buffer->event_delay = TRUE;
408
gimp_motion_buffer_push_event_history (buffer, coords);
413
if (buffer->event_delay)
414
buffer->event_delay = FALSE;
416
gimp_motion_buffer_push_event_history (buffer, coords);
420
g_printerr ("DIST: %f, DT:%f, Vel:%f, Press:%f,smooth_dd:%f, POS: (%f, %f)\n",
423
buffer->last_coords.velocity,
431
g_array_append_val (buffer->event_queue, *coords);
433
buffer->last_coords = *coords;
434
buffer->last_motion_time = time;
435
buffer->last_motion_delta_time = delta_time;
436
buffer->last_motion_delta_x = delta_x;
437
buffer->last_motion_delta_y = delta_y;
438
buffer->last_motion_distance = distance;
444
gimp_motion_buffer_get_last_motion_time (GimpMotionBuffer *buffer)
446
g_return_val_if_fail (GIMP_IS_MOTION_BUFFER (buffer), 0);
448
return buffer->last_read_motion_time;
452
gimp_motion_buffer_request_stroke (GimpMotionBuffer *buffer,
453
GdkModifierType state,
456
GdkModifierType event_state;
459
g_return_if_fail (GIMP_IS_MOTION_BUFFER (buffer));
461
if (buffer->event_delay)
463
/* If we are in delay we use LAST state, not current */
464
event_state = buffer->last_active_state;
466
keep = 1; /* Holding one event in buf */
474
if (buffer->event_delay_timeout)
476
g_source_remove (buffer->event_delay_timeout);
477
buffer->event_delay_timeout = 0;
480
buffer->last_active_state = state;
482
while (buffer->event_queue->len > keep)
484
GimpCoords buf_coords;
486
gimp_motion_buffer_pop_event_queue (buffer, &buf_coords);
488
g_signal_emit (buffer, motion_buffer_signals[STROKE], 0,
489
&buf_coords, time, event_state);
492
if (buffer->event_delay)
494
buffer->event_delay_timeout =
496
(GSourceFunc) gimp_motion_buffer_event_queue_timeout,
502
gimp_motion_buffer_request_hover (GimpMotionBuffer *buffer,
503
GdkModifierType state,
506
g_return_if_fail (GIMP_IS_MOTION_BUFFER (buffer));
508
if (buffer->event_queue->len > 0)
510
GimpCoords buf_coords = g_array_index (buffer->event_queue,
512
buffer->event_queue->len - 1);
514
g_signal_emit (buffer, motion_buffer_signals[HOVER], 0,
515
&buf_coords, state, proximity);
517
g_array_set_size (buffer->event_queue, 0);
522
/* private functions */
525
gimp_motion_buffer_push_event_history (GimpMotionBuffer *buffer,
526
const GimpCoords *coords)
528
if (buffer->event_history->len == 4)
529
g_array_remove_index (buffer->event_history, 0);
531
g_array_append_val (buffer->event_history, *coords);
535
gimp_motion_buffer_pop_event_queue (GimpMotionBuffer *buffer,
538
*coords = g_array_index (buffer->event_queue, GimpCoords, 0);
540
g_array_remove_index (buffer->event_queue, 0);
544
gimp_motion_buffer_interpolate_stroke (GimpMotionBuffer *buffer,
548
gint i = buffer->event_history->len - 1;
550
/* Note that there must be exactly one event in buffer or bad things
551
* can happen. This must never get called under other circumstances.
553
ret_coords = g_array_new (FALSE, FALSE, sizeof (GimpCoords));
555
gimp_coords_interpolate_catmull (g_array_index (buffer->event_history,
557
g_array_index (buffer->event_history,
559
g_array_index (buffer->event_queue,
562
EVENT_FILL_PRECISION / 2,
566
/* Push the last actual event in history */
567
gimp_motion_buffer_push_event_history (buffer,
568
&g_array_index (buffer->event_queue,
571
g_array_set_size (buffer->event_queue, 0);
573
g_array_append_vals (buffer->event_queue,
574
&g_array_index (ret_coords, GimpCoords, 0),
577
g_array_free (ret_coords, TRUE);
581
gimp_motion_buffer_event_queue_timeout (GimpMotionBuffer *buffer)
583
buffer->event_delay = FALSE;
584
buffer->event_delay_timeout = 0;
586
if (buffer->event_queue->len > 0)
588
GimpCoords last_coords = g_array_index (buffer->event_queue,
590
buffer->event_queue->len - 1);
592
gimp_motion_buffer_push_event_history (buffer, &last_coords);
594
gimp_motion_buffer_request_stroke (buffer,
595
buffer->last_active_state,
596
buffer->last_read_motion_time);