[gtk+/xi2: 452/1239] Add helper functions to know data relative to the coordinates of two events.
- From: Carlos Garnacho <carlosg src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gtk+/xi2: 452/1239] Add helper functions to know data relative to the coordinates of two events.
- Date: Tue, 29 Sep 2009 10:48:00 +0000 (UTC)
commit edf58dcd6380d9968fa6574d84f018b0d2172f4d
Author: Carlos Garnacho <carlos lanedo com>
Date: Sun Jul 5 15:28:55 2009 +0100
Add helper functions to know data relative to the coordinates of two events.
There are functions to return the distance, angle and center between two
GdkEvents with x/y info.
gdk/gdkevents.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
gdk/gdkevents.h | 12 +++++++
2 files changed, 102 insertions(+), 0 deletions(-)
---
diff --git a/gdk/gdkevents.c b/gdk/gdkevents.c
index 24a29a1..079da43 100644
--- a/gdk/gdkevents.c
+++ b/gdk/gdkevents.c
@@ -26,6 +26,7 @@
#include "config.h"
#include <string.h> /* For memset() */
+#include <math.h>
#include "gdk.h"
#include "gdkinternals.h"
@@ -889,6 +890,95 @@ gdk_event_request_motions (const GdkEventMotion *event)
gdk_device_get_state (event->device, event->window, NULL, NULL);
}
+static gboolean
+gdk_events_get_axis_distances (GdkEvent *event1,
+ GdkEvent *event2,
+ gdouble *x_distance,
+ gdouble *y_distance,
+ gdouble *distance)
+{
+ gdouble x1, x2, y1, y2;
+ gdouble xd, yd;
+
+ if (!gdk_event_get_coords (event1, &x1, &y1) ||
+ !gdk_event_get_coords (event2, &x2, &y2))
+ return FALSE;
+
+ xd = x2 - x1;
+ yd = y2 - y1;
+
+ if (x_distance)
+ *x_distance = xd;
+
+ if (y_distance)
+ *y_distance = yd;
+
+ if (distance)
+ *distance = sqrt ((xd * xd) + (yd * yd));
+
+ return TRUE;
+}
+
+gboolean
+gdk_events_get_distance (GdkEvent *event1,
+ GdkEvent *event2,
+ gdouble *distance)
+{
+ return gdk_events_get_axis_distances (event1, event2,
+ NULL, NULL,
+ distance);
+}
+
+gboolean
+gdk_events_get_angle (GdkEvent *event1,
+ GdkEvent *event2,
+ gdouble *angle)
+{
+ gdouble x_distance, y_distance, distance;
+
+ if (!gdk_events_get_axis_distances (event1, event2,
+ &x_distance, &y_distance,
+ &distance))
+ return FALSE;
+
+ if (angle)
+ {
+ *angle = atan2 (x_distance, y_distance);
+
+ /* Invert angle */
+ *angle = (2 * G_PI) - *angle;
+
+ /* Shift it 90° */
+ *angle += G_PI / 2;
+
+ /* And constraint it to 0°-360° */
+ *angle = fmod (*angle, 2 * G_PI);
+ }
+
+ return TRUE;
+}
+
+gboolean
+gdk_events_get_center (GdkEvent *event1,
+ GdkEvent *event2,
+ gdouble *x,
+ gdouble *y)
+{
+ gdouble x1, x2, y1, y2;
+
+ if (!gdk_event_get_coords (event1, &x1, &y1) ||
+ !gdk_event_get_coords (event2, &x2, &y2))
+ return FALSE;
+
+ if (x)
+ *x = (x2 + x1) / 2;
+
+ if (y)
+ *y = (y2 + y1) / 2;
+
+ return TRUE;
+}
+
/**
* gdk_event_set_screen:
* @event: a #GdkEvent
diff --git a/gdk/gdkevents.h b/gdk/gdkevents.h
index b3a05af..21b5a2d 100644
--- a/gdk/gdkevents.h
+++ b/gdk/gdkevents.h
@@ -539,6 +539,18 @@ gboolean gdk_event_get_axis (const GdkEvent *event,
GdkAxisUse axis_use,
gdouble *value);
void gdk_event_request_motions (const GdkEventMotion *event);
+
+gboolean gdk_events_get_distance (GdkEvent *event1,
+ GdkEvent *event2,
+ gdouble *distance);
+gboolean gdk_events_get_angle (GdkEvent *event1,
+ GdkEvent *event2,
+ gdouble *angle);
+gboolean gdk_events_get_center (GdkEvent *event1,
+ GdkEvent *event2,
+ gdouble *x,
+ gdouble *y);
+
void gdk_event_handler_set (GdkEventFunc func,
gpointer data,
GDestroyNotify notify);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]