Merge branch 'wip/path-constraint'

* wip/path-constraint:
  docs: Add PathConstraint
  tests: Add a PathConstraint interactive test
  Add ClutterPathConstraint
  actor-box: Add setters for origin and size
This commit is contained in:
Emmanuele Bassi 2010-10-25 17:09:46 +01:00
commit ef6b46d826
9 changed files with 562 additions and 1 deletions

View File

@ -114,6 +114,7 @@ source_h = \
$(srcdir)/clutter-model.h \
$(srcdir)/clutter-offscreen-effect.h \
$(srcdir)/clutter-page-turn-effect.h \
$(srcdir)/clutter-path-constraint.h \
$(srcdir)/clutter-path.h \
$(srcdir)/clutter-rectangle.h \
$(srcdir)/clutter-score.h \
@ -193,6 +194,7 @@ source_c = \
$(srcdir)/clutter-model.c \
$(srcdir)/clutter-offscreen-effect.c \
$(srcdir)/clutter-page-turn-effect.c \
$(srcdir)/clutter-path-constraint.c \
$(srcdir)/clutter-path.c \
$(srcdir)/clutter-rectangle.c \
$(srcdir)/clutter-score.c \

View File

@ -420,6 +420,55 @@ clutter_actor_box_progress (const GValue *a,
return TRUE;
}
/**
* clutter_actor_box_set_origin:
* @box: a #ClutterActorBox
* @x: the X coordinate of the new origin
* @y: the Y coordinate of the new origin
*
* Changes the origin of @box, maintaining the size of the #ClutterActorBox.
*
* Since: 1.6
*/
void
clutter_actor_box_set_origin (ClutterActorBox *box,
gfloat x,
gfloat y)
{
gfloat width, height;
g_return_if_fail (box != NULL);
width = box->x2 - box->x1;
height = box->y2 - box->y1;
box->x1 = x;
box->y1 = y;
box->x2 = box->x1 + width;
box->y2 = box->y1 + height;
}
/**
* clutter_actor_box_set_size:
* @box: a #ClutterActorBox
* @width: the new width
* @height: the new height
*
* Sets the size of @box, maintaining the origin of the #ClutterActorBox.
*
* Since: 1.6
*/
void
clutter_actor_box_set_size (ClutterActorBox *box,
gfloat width,
gfloat height)
{
g_return_if_fail (box != NULL);
box->x2 = box->x1 + width;
box->y2 = box->y1 + height;
}
G_DEFINE_BOXED_TYPE_WITH_CODE (ClutterActorBox, clutter_actor_box,
clutter_actor_box_copy,
clutter_actor_box_free,

View File

@ -0,0 +1,355 @@
/*
* Clutter.
*
* An OpenGL based 'interactive canvas' library.
*
* Copyright (C) 2010 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Author:
* Emmanuele Bassi <ebassi@linux.intel.com>
*/
/**
* SECTION:clutter-path-constraint
* @Title: ClutterPathConstraint
* @Short_Description: A constraint that follows a path
*
* #ClutterPathConstraint is a simple constraint that modifies the allocation
* of the #ClutterActor to which it has been applied using a #ClutterPath.
*
* By setting the #ClutterPathConstraint:offset property it is possible to
* control how far along the path the #ClutterActor should be.
*
* ClutterPathConstraint is available since Clutter 1.6.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "clutter-path-constraint.h"
#include "clutter-debug.h"
#include "clutter-private.h"
#define CLUTTER_PATH_CONSTRAINT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_PATH_CONSTRAINT, ClutterPathConstraintClass))
#define CLUTTER_IS_PATH_CONSTRAINT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_PATH_CONSTRAINT))
#define CLUTTER_PATH_CONSTRAINT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_PATH_CONSTRAINT, ClutterPathConstraintClass))
typedef struct _ClutterPathConstraintClass ClutterPathConstraintClass;
struct _ClutterPathConstraint
{
ClutterConstraint parent_instance;
ClutterPath *path;
gfloat offset;
ClutterActor *actor;
};
struct _ClutterPathConstraintClass
{
ClutterConstraintClass parent_class;
};
enum
{
PROP_0,
PROP_PATH,
PROP_OFFSET,
LAST_PROPERTY
};
G_DEFINE_TYPE (ClutterPathConstraint, clutter_path_constraint, CLUTTER_TYPE_CONSTRAINT);
static GParamSpec *path_properties[LAST_PROPERTY] = { NULL, };
static void
clutter_path_constraint_update_allocation (ClutterConstraint *constraint,
ClutterActor *actor,
ClutterActorBox *allocation)
{
ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (constraint);
gfloat width, height;
ClutterKnot position;
guint knot_id;
if (self->path == NULL)
return;
knot_id = clutter_path_get_position (self->path, self->offset, &position);
clutter_actor_box_get_size (allocation, &width, &height);
allocation->x1 = position.x;
allocation->y1 = position.y;
allocation->x2 = allocation->x1 + width;
allocation->y2 = allocation->y1 + height;
}
static void
clutter_path_constraint_set_actor (ClutterActorMeta *meta,
ClutterActor *new_actor)
{
ClutterPathConstraint *path = CLUTTER_PATH_CONSTRAINT (meta);
ClutterActorMetaClass *parent;
/* store the pointer to the actor, for later use */
path->actor = new_actor;
parent = CLUTTER_ACTOR_META_CLASS (clutter_path_constraint_parent_class);
parent->set_actor (meta, new_actor);
}
static void
clutter_path_constraint_dispose (GObject *gobject)
{
ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (gobject);
if (self->path != NULL)
{
g_object_unref (self->path);
self->path = NULL;
}
G_OBJECT_CLASS (clutter_path_constraint_parent_class)->dispose (gobject);
}
static void
clutter_path_constraint_set_property (GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (gobject);
switch (prop_id)
{
case PROP_PATH:
clutter_path_constraint_set_path (self, g_value_get_object (value));
break;
case PROP_OFFSET:
clutter_path_constraint_set_offset (self, g_value_get_float (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
}
}
static void
clutter_path_constraint_get_property (GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
ClutterPathConstraint *self = CLUTTER_PATH_CONSTRAINT (gobject);
switch (prop_id)
{
case PROP_PATH:
g_value_set_object (value, self->path);
break;
case PROP_OFFSET:
g_value_set_float (value, self->offset);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
}
}
static void
clutter_path_constraint_class_init (ClutterPathConstraintClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
ClutterActorMetaClass *meta_class = CLUTTER_ACTOR_META_CLASS (klass);
ClutterConstraintClass *constraint_class = CLUTTER_CONSTRAINT_CLASS (klass);
/**
* ClutterPathConstraint:path:
*
* The #ClutterPath used to constrain the position of an actor.
*
* Since: 1.6
*/
path_properties[PROP_PATH] =
g_param_spec_object ("path",
P_("Path"),
P_("The path used to constrain an actor"),
CLUTTER_TYPE_PATH,
CLUTTER_PARAM_READWRITE);
/**
* ClutterPathConstraint:offset:
*
* The offset along the #ClutterPathConstraint:path, between -1.0 and 2.0.
*
* Since: 1.6
*/
path_properties[PROP_OFFSET] =
g_param_spec_float ("offset",
P_("Offset"),
P_("The offset along the path, between -1.0 and 2.0"),
-1.0, 2.0,
0.0,
CLUTTER_PARAM_READWRITE);
gobject_class->set_property = clutter_path_constraint_set_property;
gobject_class->get_property = clutter_path_constraint_get_property;
gobject_class->dispose = clutter_path_constraint_dispose;
_clutter_object_class_install_properties (gobject_class,
LAST_PROPERTY,
path_properties);
meta_class->set_actor = clutter_path_constraint_set_actor;
constraint_class->update_allocation = clutter_path_constraint_update_allocation;
}
static void
clutter_path_constraint_init (ClutterPathConstraint *self)
{
self->offset = 0.0f;
}
/**
* clutter_path_constraint_new:
* @path: (allow-none): a #ClutterPath, or %NULL
* @offset: the offset along the #ClutterPath
*
* Creates a new #ClutterPathConstraint with the given @path and @offset
*
* Return value: (transfer full): the newly created #ClutterPathConstraint
*
* Since: 1.6
*/
ClutterConstraint *
clutter_path_constraint_new (ClutterPath *path,
gfloat offset)
{
g_return_val_if_fail (path == NULL || CLUTTER_IS_PATH (path), NULL);
return g_object_new (CLUTTER_TYPE_PATH_CONSTRAINT,
"path", path,
"offset", offset,
NULL);
}
/**
* clutter_path_constraint_set_path:
* @constraint: a #ClutterPathConstraint
* @path: (allow-none): a #ClutterPath
*
* Sets the @path to be followed by the #ClutterPathConstraint.
*
* The @constraint will take ownership of the #ClutterPath passed to this
* function.
*
* Since: 1.6
*/
void
clutter_path_constraint_set_path (ClutterPathConstraint *constraint,
ClutterPath *path)
{
g_return_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint));
g_return_if_fail (path == NULL || CLUTTER_IS_PATH (path));
if (constraint->path == path)
return;
if (constraint->path != NULL)
{
g_object_unref (constraint->path);
constraint->path = NULL;
}
if (path != NULL)
constraint->path = g_object_ref_sink (path);
if (constraint->actor != NULL)
clutter_actor_queue_relayout (constraint->actor);
_clutter_notify_by_pspec (G_OBJECT (constraint), path_properties[PROP_PATH]);
}
/**
* clutter_path_constraint_get_path:
* @constraint: a #ClutterPathConstraint
*
* Retrieves a pointer to the #ClutterPath used by @constraint.
*
* Return value: (transfer none): the #ClutterPath used by the
* #ClutterPathConstraint, or %NULL. The returned #ClutterPath is owned
* by the constraint and it should not be unreferenced
*
* Since: 1.6
*/
ClutterPath *
clutter_path_constraint_get_path (ClutterPathConstraint *constraint)
{
g_return_val_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint), NULL);
return constraint->path;
}
/**
* clutter_path_constraint_set_offset:
* @constraint: a #ClutterPathConstraint
* @offset: the offset along the path
*
* Sets the offset along the #ClutterPath used by @constraint.
*
* Since: 1.6
*/
void
clutter_path_constraint_set_offset (ClutterPathConstraint *constraint,
gfloat offset)
{
g_return_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint));
if (constraint->offset == offset)
return;
constraint->offset = offset;
if (constraint->actor != NULL)
clutter_actor_queue_relayout (constraint->actor);
_clutter_notify_by_pspec (G_OBJECT (constraint), path_properties[PROP_OFFSET]);
}
/**
* clutter_path_constraint_get_offset:
* @constraint: a #ClutterPathConstraint
*
* Retrieves the offset along the #ClutterPath used by @constraint.
*
* Return value: the offset
*
* Since: 1.6
*/
gfloat
clutter_path_constraint_get_offset (ClutterPathConstraint *constraint)
{
g_return_val_if_fail (CLUTTER_IS_PATH_CONSTRAINT (constraint), 0.0);
return constraint->offset;
}

View File

@ -0,0 +1,65 @@
/*
* Clutter.
*
* An OpenGL based 'interactive canvas' library.
*
* Copyright (C) 2010 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Author:
* Emmanuele Bassi <ebassi@linux.intel.com>
*/
#if !defined(__CLUTTER_H_INSIDE__) && !defined(CLUTTER_COMPILATION)
#error "Only <clutter/clutter.h> can be included directly."
#endif
#ifndef __CLUTTER_PATH_CONSTRAINT_H__
#define __CLUTTER_PATH_CONSTRAINT_H__
#include <clutter/clutter-constraint.h>
#include <clutter/clutter-path.h>
G_BEGIN_DECLS
#define CLUTTER_TYPE_PATH_CONSTRAINT (clutter_path_constraint_get_type ())
#define CLUTTER_PATH_CONSTRAINT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLUTTER_TYPE_PATH_CONSTRAINT, ClutterPathConstraint))
#define CLUTTER_IS_PATH_CONSTRAINT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLUTTER_TYPE_PATH_CONSTRAINT))
/**
* ClutterPathConstraint:
*
* <structname>ClutterPathConstraint</structname> is an opaque structure
* whose members cannot be directly accessed
*
* Since: 1.6
*/
typedef struct _ClutterPathConstraint ClutterPathConstraint;
GType clutter_path_constraint_get_type (void) G_GNUC_CONST;
ClutterConstraint *clutter_path_constraint_new (ClutterPath *path,
gfloat offset);
void clutter_path_constraint_set_path (ClutterPathConstraint *constraint,
ClutterPath *path);
ClutterPath * clutter_path_constraint_get_path (ClutterPathConstraint *constraint);
void clutter_path_constraint_set_offset (ClutterPathConstraint *constraint,
gfloat offset);
gfloat clutter_path_constraint_get_offset (ClutterPathConstraint *constraint);
G_END_DECLS
#endif /* __CLUTTER_PATH_CONSTRAINT_H__ */

View File

@ -189,6 +189,13 @@ void clutter_actor_box_union (const ClutterActorBox *a,
const ClutterActorBox *b,
ClutterActorBox *result);
void clutter_actor_box_set_origin (ClutterActorBox *box,
gfloat x,
gfloat y);
void clutter_actor_box_set_size (ClutterActorBox *box,
gfloat width,
gfloat height);
/**
* ClutterGeometry:
* @x: X coordinate of the top left corner of an actor

View File

@ -82,6 +82,7 @@
#include "clutter-model.h"
#include "clutter-offscreen-effect.h"
#include "clutter-page-turn-effect.h"
#include "clutter-path-constraint.h"
#include "clutter-path.h"
#include "clutter-rectangle.h"
#include "clutter-score.h"

View File

@ -451,7 +451,9 @@ clutter_actor_box_get_x
clutter_actor_box_get_y
clutter_actor_box_get_width
clutter_actor_box_get_height
clutter_actor_box_set_origin
clutter_actor_box_get_origin
clutter_actor_box_set_size
clutter_actor_box_get_size
clutter_actor_box_get_area
clutter_actor_box_contains
@ -2555,3 +2557,19 @@ CLUTTER_IS_SETTINGS
<SUBSECTION Private>
clutter_settings_get_type
</SECTION>
<SECTION>
<FILE>clutter-path-constraint</FILE>
ClutterPathConstraint
clutter_path_constraint_new
clutter_path_constraint_set_path
clutter_path_constraint_get_path
clutter_path_constraint_set_offset
clutter_path_constraint_get_offset
<SUBSECTION Standard>
CLUTTER_PATH_CONSTRAINT
CLUTTER_IS_PATH_CONSTRAINT
CLUTTER_TYPE_PATH_CONSTRAINT
<SUBSECTION Private>
clutter_path_constraint_get_type
</SECTION>

View File

@ -57,7 +57,8 @@ UNIT_TESTS = \
test-scrolling.c \
test-bind.c \
test-cogl-point-sprites.c \
test-table-layout.c
test-table-layout.c \
test-path-constraint.c
if X11_TESTS
UNIT_TESTS += test-pixmap.c

View File

@ -0,0 +1,63 @@
#include <stdlib.h>
#include <clutter/clutter.h>
#define PATH_DESCRIPTION \
"M 0, 0 " \
"L 0, 300 " \
"L 300, 300 " \
"L 300, 0 " \
"L 0, 0"
static gboolean toggled = FALSE;
static gboolean
on_button_press (ClutterActor *actor,
const ClutterEvent *event,
gpointer dummy G_GNUC_UNUSED)
{
if (!toggled)
clutter_actor_animate (actor, CLUTTER_EASE_OUT_CUBIC, 500,
"@constraints.path.offset", 1.0,
NULL);
else
clutter_actor_animate (actor, CLUTTER_EASE_OUT_CUBIC, 500,
"@constraints.path.offset", 0.0,
NULL);
toggled = !toggled;
return TRUE;
}
int
test_path_constraint_main (int argc,
char *argv[])
{
ClutterActor *stage, *rect;
ClutterPath *path;
ClutterColor rect_color = { 0xcc, 0x00, 0x00, 0xff };
clutter_init (&argc, &argv);
stage = clutter_stage_new ();
clutter_stage_set_title (CLUTTER_STAGE (stage), "Path Constraint");
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
path = clutter_path_new ();
clutter_path_set_description (path, PATH_DESCRIPTION);
rect = clutter_rectangle_new ();
clutter_rectangle_set_color (CLUTTER_RECTANGLE (rect), &rect_color);
clutter_actor_set_size (rect, 128, 128);
clutter_actor_set_reactive (rect, TRUE);
clutter_actor_add_constraint_with_name (rect, "path", clutter_path_constraint_new (path, 0.0));
clutter_container_add_actor (CLUTTER_CONTAINER (stage), rect);
g_signal_connect (rect, "button-press-event", G_CALLBACK (on_button_press), NULL);
clutter_actor_show (stage);
clutter_main ();
return EXIT_SUCCESS;
}