event: Add distance and angle accessors

Let's start using ClutterPoint in the API.
This commit is contained in:
Emmanuele Bassi 2012-04-19 12:16:54 +01:00
parent d021cc7c02
commit 695621f5ea
3 changed files with 112 additions and 19 deletions

View File

@ -33,6 +33,8 @@
#include "clutter-keysyms.h" #include "clutter-keysyms.h"
#include "clutter-private.h" #include "clutter-private.h"
#include <math.h>
/** /**
* SECTION:clutter-event * SECTION:clutter-event
* @short_description: User and window system events * @short_description: User and window system events
@ -278,11 +280,34 @@ clutter_event_get_coords (const ClutterEvent *event,
gfloat *x, gfloat *x,
gfloat *y) gfloat *y)
{ {
gfloat event_x, event_y; ClutterPoint coords;
g_return_if_fail (event != NULL); g_return_if_fail (event != NULL);
event_x = event_y = 0; clutter_event_get_position (event, &coords);
if (x != NULL)
*x = coords.x;
if (y != NULL)
*y = coords.y;
}
/**
* clutter_event_get_position:
* @event: a #ClutterEvent
* @position: a #ClutterPoint
*
* Retrieves the event coordinates as a #ClutterPoint.
*
* Since: 1.12
*/
void
clutter_event_get_position (const ClutterEvent *event,
ClutterPoint *position)
{
g_return_if_fail (event != NULL);
g_return_if_fail (position != NULL);
switch (event->type) switch (event->type)
{ {
@ -294,44 +319,35 @@ clutter_event_get_coords (const ClutterEvent *event,
case CLUTTER_CLIENT_MESSAGE: case CLUTTER_CLIENT_MESSAGE:
case CLUTTER_DELETE: case CLUTTER_DELETE:
case CLUTTER_EVENT_LAST: case CLUTTER_EVENT_LAST:
clutter_point_init (position, 0.f, 0.f);
break; break;
case CLUTTER_ENTER: case CLUTTER_ENTER:
case CLUTTER_LEAVE: case CLUTTER_LEAVE:
event_x = event->crossing.x; clutter_point_init (position, event->crossing.x, event->crossing.y);
event_y = event->crossing.y;
break; break;
case CLUTTER_BUTTON_PRESS: case CLUTTER_BUTTON_PRESS:
case CLUTTER_BUTTON_RELEASE: case CLUTTER_BUTTON_RELEASE:
event_x = event->button.x; clutter_point_init (position, event->button.x, event->button.y);
event_y = event->button.y;
break; break;
case CLUTTER_MOTION: case CLUTTER_MOTION:
event_x = event->motion.x; clutter_point_init (position, event->motion.x, event->motion.y);
event_y = event->motion.y;
break; break;
case CLUTTER_TOUCH_BEGIN: case CLUTTER_TOUCH_BEGIN:
case CLUTTER_TOUCH_UPDATE: case CLUTTER_TOUCH_UPDATE:
case CLUTTER_TOUCH_END: case CLUTTER_TOUCH_END:
case CLUTTER_TOUCH_CANCEL: case CLUTTER_TOUCH_CANCEL:
event_x = event->touch.x; clutter_point_init (position, event->touch.x, event->touch.y);
event_y = event->touch.y;
break; break;
case CLUTTER_SCROLL: case CLUTTER_SCROLL:
event_x = event->scroll.x; clutter_point_init (position, event->scroll.x, event->scroll.y);
event_y = event->scroll.y;
break; break;
} }
if (x)
*x = event_x;
if (y)
*y = event_y;
} }
/** /**
@ -1513,3 +1529,68 @@ clutter_event_get_axes (const ClutterEvent *event,
return retval; return retval;
} }
/**
* clutter_event_get_distance:
* @source: a #ClutterEvent
* @target: a #ClutterEvent
*
* Retrieves the distance between two events, a @source and a @target.
*
* Return value: the distance between two #ClutterEvent
*
* Since: 1.12
*/
float
clutter_event_get_distance (const ClutterEvent *source,
const ClutterEvent *target)
{
ClutterPoint p0, p1;
clutter_event_get_position (source, &p0);
clutter_event_get_position (source, &p1);
return clutter_point_distance (&p0, &p1, NULL, NULL);
}
/**
* clutter_event_get_angle:
* @source: a #ClutterEvent
* @target: a #ClutterEvent
*
* Retrieves the angle relative from @source to @target.
*
* The direction of the angle is from the position X axis towards
* the positive Y axis.
*
* Return value: the angle between two #ClutterEvent
*
* Since: 1.12
*/
double
clutter_event_get_angle (const ClutterEvent *source,
const ClutterEvent *target)
{
ClutterPoint p0, p1;
float x_distance, y_distance;
double angle;
clutter_event_get_position (source, &p0);
clutter_event_get_position (target, &p1);
if (clutter_point_equals (&p0, &p1))
return 0;
clutter_point_distance (&p0, &p1, &x_distance, &y_distance);
angle = atan2 (x_distance, y_distance);
/* invert the angle, and shift it by 90 degrees */
angle = (2.0 * G_PI) - angle;
angle += G_PI / 2.0;
/* keep the angle within the [ 0, 360 ] interval */
angle = fmod (angle, 2.0 * G_PI);
return angle;
}

View File

@ -445,6 +445,15 @@ void clutter_event_set_coords (ClutterEvent
void clutter_event_get_coords (const ClutterEvent *event, void clutter_event_get_coords (const ClutterEvent *event,
gfloat *x, gfloat *x,
gfloat *y); gfloat *y);
CLUTTER_AVAILABLE_IN_1_12
void clutter_event_get_position (const ClutterEvent *event,
ClutterPoint *position);
CLUTTER_AVAILABLE_IN_1_12
float clutter_event_get_distance (const ClutterEvent *source,
const ClutterEvent *target);
CLUTTER_AVAILABLE_IN_1_12
double clutter_event_get_angle (const ClutterEvent *source,
const ClutterEvent *target);
gdouble * clutter_event_get_axes (const ClutterEvent *event, gdouble * clutter_event_get_axes (const ClutterEvent *event,
guint *n_axes); guint *n_axes);

View File

@ -619,6 +619,8 @@ clutter_effect_queue_repaint
clutter_events_pending clutter_events_pending
clutter_event_copy clutter_event_copy
clutter_event_flags_get_type clutter_event_flags_get_type
clutter_event_free
clutter_event_get_angle
clutter_event_get_axes clutter_event_get_axes
clutter_event_get_button clutter_event_get_button
clutter_event_get_click_count clutter_event_get_click_count
@ -626,13 +628,13 @@ clutter_event_get_coords
clutter_event_get_device clutter_event_get_device
clutter_event_get_device_id clutter_event_get_device_id
clutter_event_get_device_type clutter_event_get_device_type
clutter_event_get_distance
clutter_event_get_event_sequence clutter_event_get_event_sequence
clutter_event_get_flags clutter_event_get_flags
clutter_event_free
clutter_event_get
clutter_event_get_key_code clutter_event_get_key_code
clutter_event_get_key_symbol clutter_event_get_key_symbol
clutter_event_get_key_unicode clutter_event_get_key_unicode
clutter_event_get_position
clutter_event_get_related clutter_event_get_related
clutter_event_get_scroll_delta clutter_event_get_scroll_delta
clutter_event_get_scroll_direction clutter_event_get_scroll_direction
@ -642,6 +644,7 @@ clutter_event_get_stage
clutter_event_get_state clutter_event_get_state
clutter_event_get_type clutter_event_get_type
clutter_event_get_time clutter_event_get_time
clutter_event_get
clutter_event_new clutter_event_new
clutter_event_peek clutter_event_peek
clutter_event_put clutter_event_put