mutter/tests/interactive/test-project.c
Emmanuele Bassi d6d208da7d Remove Units from the public API
With the recent change to internal floating point values, ClutterUnit
has become a redundant type, defined to be a float. All integer entry
points are being internally converted to floating point values to be
passed to the GL pipeline with the least amount of conversion.

ClutterUnit is thus exposed as just a "pixel with fractionary bits",
and not -- as users might think -- as generic, resolution and device
independent units. not that it was the case, but a definitive amount
of people was convinced it did provide this "feature", and was flummoxed
about the mere existence of this type.

So, having ClutterUnit exposed in the public API doubles the entry
points and has the following disadvantages:

  - we have to maintain twice the amount of entry points in ClutterActor
  - we still do an integer-to-float implicit conversion
  - we introduce a weird impedance between pixels and "pixels with
    fractionary bits"
  - language bindings will have to choose what to bind, and resort
    to manually overriding the API
    + *except* for language bindings based on GObject-Introspection, as
      they cannot do manual overrides, thus will replicate the entire
      set of entry points

For these reason, we should coalesces every Actor entry point for
pixels and for ClutterUnit into a single entry point taking a float,
like:

  void clutter_actor_set_x (ClutterActor *self,
                            gfloat        x);
  void clutter_actor_get_size (ClutterActor *self,
                               gfloat       *width,
                               gfloat       *height);
  gfloat clutter_actor_get_height (ClutterActor *self);

etc.

The issues I have identified are:

  - we'll have a two cases of compiler warnings:
    - printf() format of the return values from %d to %f
    - clutter_actor_get_size() taking floats instead of unsigned ints
  - we'll have a problem with varargs when passing an integer instead
    of a floating point value, except on 64bit platforms where the
    size of a float is the same as the size of an int

To be clear: the *intent* of the API should not change -- we still use
pixels everywhere -- but:

  - we remove ambiguity in the API with regard to pixels and units
  - we remove entry points we get to maintain for the whole 1.0
    version of the API
  - we make things simpler to bind for both manual language bindings
    and automatic (gobject-introspection based) ones
  - we have the simplest API possible while still exposing the
    capabilities of the underlying GL implementation
2009-05-06 16:44:47 +01:00

236 lines
5.3 KiB
C

#include <clutter/clutter.h>
#include <stdio.h>
#include <stdlib.h>
#include <gmodule.h>
static ClutterActor *main_stage, *rect, *p[5];
static void
init_handles ()
{
gint i;
ClutterVertex v[4];
ClutterVertex v1, v2;
ClutterColor blue = { 0, 0, 0xff, 0xff };
clutter_actor_get_abs_allocation_vertices (rect, v);
for (i = 0; i < 4; ++i)
{
p[i] = clutter_rectangle_new_with_color (&blue);
clutter_actor_set_size (p[i], 5, 5);
clutter_actor_set_position (p[i], 0, 0);
clutter_group_add (CLUTTER_GROUP (main_stage), p[i]);
clutter_actor_set_position (p[i],
v[i].x - clutter_actor_get_width (p[i]) / 2,
v[i].y - clutter_actor_get_height (p[i]) / 2);
clutter_actor_raise_top (p[i]);
clutter_actor_show (p[i]);
}
v1.x = clutter_actor_get_width (rect) / 2;
v1.y = clutter_actor_get_height (rect) / 2;
v1.z = 0;
clutter_actor_apply_transform_to_point (rect, &v1, &v2);
p[4] = clutter_rectangle_new_with_color (&blue);
clutter_actor_set_size (p[4], 5, 5);
clutter_actor_set_position (p[4], 0, 0);
clutter_group_add (CLUTTER_GROUP (main_stage), p[4]);
clutter_actor_set_position (p[4],
v2.x - clutter_actor_get_width (p[4]) / 2,
v2.y - clutter_actor_get_height (p[4]) / 2);
clutter_actor_raise_top (p[4]);
clutter_actor_show (p[4]);
}
static void
place_handles ()
{
gint i;
ClutterVertex v[4];
ClutterVertex v1, v2;
clutter_actor_get_abs_allocation_vertices (rect, v);
for (i = 0; i < 4; ++i)
{
clutter_actor_set_position (p[i],
v[i].x - clutter_actor_get_width (p[i])/2,
v[i].y - clutter_actor_get_height (p[i])/2);
}
v1.x = clutter_actor_get_width (rect) / 2;
v1.y = clutter_actor_get_height (rect) / 2;
v1.z = 0;
clutter_actor_apply_transform_to_point (rect, &v1, &v2);
clutter_actor_set_position (p[4],
v2.x - clutter_actor_get_width (p[4])/2,
v2.y - clutter_actor_get_height (p[4])/2);
}
#define M(m,row,col) (m)[col*4+row]
static gint
find_handle_index (ClutterActor * a)
{
gint i;
for (i = 0; i < sizeof(p)/sizeof(p[0]); ++i)
if (p[i] == a)
return i;
return -1;
}
static gboolean
on_event (ClutterStage *stage,
ClutterEvent *event,
gpointer user_data)
{
static ClutterActor * dragging = NULL;
switch (event->type)
{
case CLUTTER_BUTTON_PRESS:
{
ClutterActor *actor;
gfloat x, y;
clutter_event_get_coords (event, &x, &y);
actor = clutter_stage_get_actor_at_pos (stage,
CLUTTER_PICK_ALL,
x, y);
if (actor != CLUTTER_ACTOR (stage))
{
if (actor != rect)
dragging = actor;
}
}
break;
case CLUTTER_MOTION:
{
if (dragging)
{
gfloat x, y;
gint i;
ClutterActorBox box1, box2;
ClutterUnit xp, yp;
i = find_handle_index (dragging);
if (i < 0)
break;
clutter_event_get_coords (event, &x, &y);
clutter_actor_get_allocation_box (dragging, &box1);
clutter_actor_get_allocation_box (rect, &box2);
xp = CLUTTER_UNITS_FROM_DEVICE (x - 3) - box1.x1;
yp = CLUTTER_UNITS_FROM_DEVICE (y - 3) - box1.y1;
if (i == 4)
{
g_debug ("moving box by %f, %f",
CLUTTER_UNITS_TO_FLOAT (xp),
CLUTTER_UNITS_TO_FLOAT (yp));
clutter_actor_move_by (rect, xp, yp);
}
else
{
g_debug ("adjusting box by %f, %f, handle %d",
CLUTTER_UNITS_TO_FLOAT (xp),
CLUTTER_UNITS_TO_FLOAT (yp),
i);
switch (i)
{
case 0:
box2.x1 += xp;
box2.y1 += yp;
break;
case 1:
box2.x2 += xp;
box2.y1 += yp;
break;
case 2:
box2.x1 += xp;
box2.y2 += yp;
break;
case 3:
box2.x2 += xp;
box2.y2 += yp;
break;
}
/* FIXME this is just plain wrong, to allocate directly
* like this
*/
clutter_actor_allocate (rect, &box2, TRUE);
}
place_handles ();
}
}
break;
case CLUTTER_BUTTON_RELEASE:
{
dragging = NULL;
}
break;
default:
break;
}
return FALSE;
}
G_MODULE_EXPORT int
test_project_main (int argc, char *argv[])
{
ClutterActor *label;
ClutterColor stage_color = { 0x0, 0x0, 0x0, 0xff },
white = { 0xff, 0xff, 0xff, 0xff };
clutter_init (&argc, &argv);
main_stage = clutter_stage_get_default ();
clutter_stage_set_color (CLUTTER_STAGE (main_stage), &stage_color);
clutter_actor_set_size (main_stage, 640, 480);
rect = clutter_rectangle_new_with_color (&white);
clutter_actor_set_size (rect, 320, 240);
clutter_actor_set_position (rect, 180, 120);
clutter_actor_set_rotation (rect, CLUTTER_Y_AXIS, 60, 0, 0, 0);
clutter_group_add (CLUTTER_GROUP (main_stage), rect);
label = clutter_text_new_with_text ("Mono 8pt", "Drag the blue rectangles");
clutter_text_set_color (CLUTTER_TEXT (label), &white);
clutter_actor_set_position (label, 10, 10);
clutter_group_add (CLUTTER_GROUP (main_stage), label);
clutter_actor_show_all (main_stage);
g_signal_connect (main_stage, "event", G_CALLBACK (on_event), NULL);
init_handles ();
clutter_main();
return EXIT_SUCCESS;
}