mirror of
https://github.com/brl/mutter.git
synced 2024-11-22 16:10:41 -05:00
Add SnapConstraint
A SnapConstraint is a constraint that "snaps" the edges of two actors together.
This commit is contained in:
parent
b3f5a6e2ba
commit
3c15c0c9bb
@ -126,6 +126,7 @@ source_h = \
|
||||
$(srcdir)/clutter-shader.h \
|
||||
$(srcdir)/clutter-shader-effect.h \
|
||||
$(srcdir)/clutter-shader-types.h \
|
||||
$(srcdir)/clutter-snap-constraint.h \
|
||||
$(srcdir)/clutter-stage.h \
|
||||
$(srcdir)/clutter-stage-manager.h \
|
||||
$(srcdir)/clutter-stage-window.h \
|
||||
@ -207,6 +208,7 @@ source_c = \
|
||||
$(srcdir)/clutter-shader.c \
|
||||
$(srcdir)/clutter-shader-effect.c \
|
||||
$(srcdir)/clutter-shader-types.c \
|
||||
$(srcdir)/clutter-snap-constraint.c \
|
||||
$(srcdir)/clutter-stage.c \
|
||||
$(srcdir)/clutter-stage-manager.c \
|
||||
$(srcdir)/clutter-stage-window.c \
|
||||
|
589
clutter/clutter-snap-constraint.c
Normal file
589
clutter/clutter-snap-constraint.c
Normal file
@ -0,0 +1,589 @@
|
||||
/*
|
||||
* 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-snap-constraint
|
||||
* @Title: ClutterSnapConstraint
|
||||
* @Short_Description: A constraint snapping two actors together
|
||||
*
|
||||
* #ClutterSnapConstraint is a constraint the snaps the edges of two
|
||||
* actors together, expanding the actor's allocation if necessary.
|
||||
*
|
||||
* An offset can be applied to the constraint, to provide spacing.
|
||||
*
|
||||
* #ClutterSnapConstraint is available since Clutter 1.6
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "clutter-snap-constraint.h"
|
||||
|
||||
#include "clutter-actor-private.h"
|
||||
#include "clutter-constraint.h"
|
||||
#include "clutter-debug.h"
|
||||
#include "clutter-enum-types.h"
|
||||
#include "clutter-private.h"
|
||||
|
||||
#define CLUTTER_SNAP_CONSTRAINT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CLUTTER_TYPE_SNAP_CONSTRAINT, ClutterSnapConstraintClass))
|
||||
#define CLUTTER_IS_SNAP_CONSTRAINT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CLUTTER_TYPE_SNAP_CONSTRAINT))
|
||||
#define CLUTTER_SNAP_CONSTRAINT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CLUTTER_TYPE_SNAP_CONSTRAINT, ClutterSnapConstraintClass))
|
||||
|
||||
typedef struct _ClutterSnapConstraintClass ClutterSnapConstraintClass;
|
||||
|
||||
struct _ClutterSnapConstraint
|
||||
{
|
||||
ClutterConstraint parent_instance;
|
||||
|
||||
ClutterActor *actor;
|
||||
ClutterActor *source;
|
||||
|
||||
ClutterSnapEdge from_edge;
|
||||
ClutterSnapEdge to_edge;
|
||||
|
||||
gfloat offset;
|
||||
};
|
||||
|
||||
struct _ClutterSnapConstraintClass
|
||||
{
|
||||
ClutterConstraintClass parent_class;
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
PROP_0,
|
||||
|
||||
PROP_SOURCE,
|
||||
PROP_FROM_EDGE,
|
||||
PROP_TO_EDGE,
|
||||
PROP_OFFSET,
|
||||
|
||||
PROP_LAST
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE (ClutterSnapConstraint,
|
||||
clutter_snap_constraint,
|
||||
CLUTTER_TYPE_CONSTRAINT);
|
||||
|
||||
static GParamSpec *obj_props[PROP_LAST] = { NULL, };
|
||||
|
||||
static void
|
||||
source_queue_relayout (ClutterActor *source,
|
||||
ClutterSnapConstraint *constraint)
|
||||
{
|
||||
if (constraint->actor != NULL)
|
||||
clutter_actor_queue_relayout (constraint->actor);
|
||||
}
|
||||
|
||||
static void
|
||||
source_destroyed (ClutterActor *actor,
|
||||
ClutterSnapConstraint *constraint)
|
||||
{
|
||||
constraint->source = NULL;
|
||||
}
|
||||
|
||||
static inline void
|
||||
warn_horizontal_edge (const gchar *edge,
|
||||
ClutterActor *actor,
|
||||
ClutterActor *source)
|
||||
{
|
||||
g_warning (G_STRLOC ": the %s edge of actor '%s' can only be snapped "
|
||||
"to either the right or the left edge of actor '%s'",
|
||||
edge,
|
||||
_clutter_actor_get_debug_name (actor),
|
||||
_clutter_actor_get_debug_name (source));
|
||||
}
|
||||
|
||||
static inline void
|
||||
warn_vertical_edge (const gchar *edge,
|
||||
ClutterActor *actor,
|
||||
ClutterActor *source)
|
||||
{
|
||||
g_warning (G_STRLOC ": the %s edge of actor '%s' can only "
|
||||
"be snapped to the top or bottom edge of actor '%s'",
|
||||
edge,
|
||||
_clutter_actor_get_debug_name (actor),
|
||||
_clutter_actor_get_debug_name (source));
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_snap_constraint_update_allocation (ClutterConstraint *constraint,
|
||||
ClutterActor *actor,
|
||||
ClutterActorBox *allocation)
|
||||
{
|
||||
ClutterSnapConstraint *self = CLUTTER_SNAP_CONSTRAINT (constraint);
|
||||
gfloat source_width, source_height;
|
||||
gfloat source_x, source_y;
|
||||
gfloat actor_width, actor_height;
|
||||
|
||||
if (self->source == NULL)
|
||||
return;
|
||||
|
||||
clutter_actor_get_position (self->source, &source_x, &source_y);
|
||||
clutter_actor_get_size (self->source, &source_width, &source_height);
|
||||
|
||||
clutter_actor_box_get_size (allocation, &actor_width, &actor_height);
|
||||
|
||||
switch (self->to_edge)
|
||||
{
|
||||
case CLUTTER_SNAP_EDGE_LEFT:
|
||||
if (self->from_edge == CLUTTER_SNAP_EDGE_LEFT)
|
||||
allocation->x1 = source_x + self->offset;
|
||||
else if (self->from_edge == CLUTTER_SNAP_EDGE_RIGHT)
|
||||
allocation->x2 = source_x + self->offset;
|
||||
else
|
||||
warn_horizontal_edge ("left", self->actor, self->source);
|
||||
break;
|
||||
|
||||
case CLUTTER_SNAP_EDGE_RIGHT:
|
||||
if (self->from_edge == CLUTTER_SNAP_EDGE_RIGHT)
|
||||
allocation->x2 = source_x + source_height + self->offset;
|
||||
else if (self->from_edge == CLUTTER_SNAP_EDGE_LEFT)
|
||||
allocation->x1 = source_x + source_height + self->offset;
|
||||
else
|
||||
warn_horizontal_edge ("right", self->actor, self->source);
|
||||
break;
|
||||
|
||||
break;
|
||||
|
||||
case CLUTTER_SNAP_EDGE_TOP:
|
||||
if (self->from_edge == CLUTTER_SNAP_EDGE_TOP)
|
||||
allocation->y1 = source_y + self->offset;
|
||||
else if (self->from_edge == CLUTTER_SNAP_EDGE_BOTTOM)
|
||||
allocation->y2 = source_y + self->offset;
|
||||
else
|
||||
warn_vertical_edge ("top", self->actor, self->source);
|
||||
break;
|
||||
|
||||
case CLUTTER_SNAP_EDGE_BOTTOM:
|
||||
if (self->from_edge == CLUTTER_SNAP_EDGE_BOTTOM)
|
||||
allocation->y2 = source_y + source_height + self->offset;
|
||||
else if (self->from_edge == CLUTTER_SNAP_EDGE_TOP)
|
||||
allocation->y1 = source_y + source_height + self->offset;
|
||||
else
|
||||
warn_vertical_edge ("bottom", self->actor, self->source);
|
||||
break;
|
||||
|
||||
default:
|
||||
g_assert_not_reached ();
|
||||
break;
|
||||
}
|
||||
|
||||
if (allocation->x2 - allocation->x1 < 0)
|
||||
allocation->x2 = allocation->x1;
|
||||
|
||||
if (allocation->y2 - allocation->y1 < 0)
|
||||
allocation->y2 = allocation->y1;
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_snap_constraint_set_actor (ClutterActorMeta *meta,
|
||||
ClutterActor *new_actor)
|
||||
{
|
||||
ClutterSnapConstraint *self = CLUTTER_SNAP_CONSTRAINT (meta);
|
||||
ClutterActorMetaClass *parent;
|
||||
|
||||
/* store the pointer to the actor, for later use */
|
||||
self->actor = new_actor;
|
||||
|
||||
parent = CLUTTER_ACTOR_META_CLASS (clutter_snap_constraint_parent_class);
|
||||
parent->set_actor (meta, new_actor);
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_snap_constraint_set_property (GObject *gobject,
|
||||
guint prop_id,
|
||||
const GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
ClutterSnapConstraint *self = CLUTTER_SNAP_CONSTRAINT (gobject);
|
||||
|
||||
switch (prop_id)
|
||||
{
|
||||
case PROP_SOURCE:
|
||||
clutter_snap_constraint_set_source (self, g_value_get_object (value));
|
||||
break;
|
||||
|
||||
case PROP_FROM_EDGE:
|
||||
clutter_snap_constraint_set_edges (self,
|
||||
g_value_get_enum (value),
|
||||
self->to_edge);
|
||||
break;
|
||||
|
||||
case PROP_TO_EDGE:
|
||||
clutter_snap_constraint_set_edges (self,
|
||||
self->from_edge,
|
||||
g_value_get_enum (value));
|
||||
break;
|
||||
|
||||
case PROP_OFFSET:
|
||||
clutter_snap_constraint_set_offset (self, g_value_get_float (value));
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_snap_constraint_get_property (GObject *gobject,
|
||||
guint prop_id,
|
||||
GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
ClutterSnapConstraint *self = CLUTTER_SNAP_CONSTRAINT (gobject);
|
||||
|
||||
switch (prop_id)
|
||||
{
|
||||
case PROP_SOURCE:
|
||||
g_value_set_object (value, self->source);
|
||||
break;
|
||||
|
||||
case PROP_FROM_EDGE:
|
||||
g_value_set_enum (value, self->from_edge);
|
||||
break;
|
||||
|
||||
case PROP_TO_EDGE:
|
||||
g_value_set_enum (value, self->to_edge);
|
||||
break;
|
||||
|
||||
case PROP_OFFSET:
|
||||
g_value_set_float (value, self->offset);
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_snap_constraint_class_init (ClutterSnapConstraintClass *klass)
|
||||
{
|
||||
ClutterActorMetaClass *meta_class = CLUTTER_ACTOR_META_CLASS (klass);
|
||||
ClutterConstraintClass *constraint_class = CLUTTER_CONSTRAINT_CLASS (klass);
|
||||
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
||||
|
||||
gobject_class->set_property = clutter_snap_constraint_set_property;
|
||||
gobject_class->get_property = clutter_snap_constraint_get_property;
|
||||
|
||||
meta_class->set_actor = clutter_snap_constraint_set_actor;
|
||||
|
||||
constraint_class->update_allocation = clutter_snap_constraint_update_allocation;
|
||||
/**
|
||||
* ClutterSnapConstraint:source:
|
||||
*
|
||||
* The #ClutterActor used as the source for the constraint
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
obj_props[PROP_SOURCE] =
|
||||
g_param_spec_object ("source",
|
||||
P_("Source"),
|
||||
P_("The source of the constraint"),
|
||||
CLUTTER_TYPE_ACTOR,
|
||||
CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
|
||||
|
||||
/**
|
||||
* ClutterSnapConstraint:from-edge:
|
||||
*
|
||||
* The edge of the #ClutterActor that should be snapped
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
obj_props[PROP_FROM_EDGE] =
|
||||
g_param_spec_enum ("from-edge",
|
||||
P_("From Edge"),
|
||||
P_("The edge of the actor that should be snapped"),
|
||||
CLUTTER_TYPE_SNAP_EDGE,
|
||||
CLUTTER_SNAP_EDGE_RIGHT,
|
||||
CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
|
||||
|
||||
/**
|
||||
* ClutterSnapConstraint:to-edge:
|
||||
*
|
||||
* The edge of the #ClutterSnapConstraint:source that should be snapped
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
obj_props[PROP_TO_EDGE] =
|
||||
g_param_spec_enum ("to-edge",
|
||||
P_("To Edge"),
|
||||
P_("The edge of the source that should be snapped"),
|
||||
CLUTTER_TYPE_SNAP_EDGE,
|
||||
CLUTTER_SNAP_EDGE_RIGHT,
|
||||
CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
|
||||
|
||||
/**
|
||||
* ClutterSnapConstraint:offset:
|
||||
*
|
||||
* The offset, in pixels, between #ClutterSnapConstraint:from-edge
|
||||
* and #ClutterSnapConstraint:to-edge
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
obj_props[PROP_OFFSET] =
|
||||
g_param_spec_float ("offset",
|
||||
P_("Offset"),
|
||||
P_("The offset in pixels to apply to the constraint"),
|
||||
-G_MAXFLOAT, G_MAXFLOAT,
|
||||
0.0f,
|
||||
CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT);
|
||||
|
||||
_clutter_object_class_install_properties (gobject_class,
|
||||
PROP_LAST,
|
||||
obj_props);
|
||||
}
|
||||
|
||||
static void
|
||||
clutter_snap_constraint_init (ClutterSnapConstraint *self)
|
||||
{
|
||||
self->actor = NULL;
|
||||
self->source = NULL;
|
||||
|
||||
self->from_edge = CLUTTER_SNAP_EDGE_RIGHT;
|
||||
self->to_edge = CLUTTER_SNAP_EDGE_RIGHT;
|
||||
|
||||
self->offset = 0.0f;
|
||||
}
|
||||
|
||||
/**
|
||||
* clutter_snap_constraint_new:
|
||||
* @source: (allow-none): the #ClutterActor to use as the source of
|
||||
* the constraint, or %NULL
|
||||
* @from_edge: the edge of the actor to use in the constraint
|
||||
* @to_edge: the edge of @source to use in the constraint
|
||||
* @offset: the offset to apply to the constraint, in pixels
|
||||
*
|
||||
* Creates a new #ClutterSnapConstraint that will snap a #ClutterActor
|
||||
* to the @edge of @source, with the given @offset.
|
||||
*
|
||||
* Return value: the newly created #ClutterSnapConstraint
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
ClutterConstraint *
|
||||
clutter_snap_constraint_new (ClutterActor *source,
|
||||
ClutterSnapEdge from_edge,
|
||||
ClutterSnapEdge to_edge,
|
||||
gfloat offset)
|
||||
{
|
||||
g_return_val_if_fail (source == NULL || CLUTTER_IS_ACTOR (source), NULL);
|
||||
|
||||
return g_object_new (CLUTTER_TYPE_SNAP_CONSTRAINT,
|
||||
"source", source,
|
||||
"from-edge", from_edge,
|
||||
"to-edge", to_edge,
|
||||
"offset", offset,
|
||||
NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* clutter_snap_constraint_set_source:
|
||||
* @constraint: a #ClutterSnapConstraint
|
||||
* @source: (allow-none): a #ClutterActor, or %NULL to unset the source
|
||||
*
|
||||
* Sets the source #ClutterActor for the constraint
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
void
|
||||
clutter_snap_constraint_set_source (ClutterSnapConstraint *constraint,
|
||||
ClutterActor *source)
|
||||
{
|
||||
ClutterActor *old_source;
|
||||
|
||||
g_return_if_fail (CLUTTER_IS_SNAP_CONSTRAINT (constraint));
|
||||
g_return_if_fail (source == NULL || CLUTTER_IS_ACTOR (source));
|
||||
|
||||
if (constraint->source == source)
|
||||
return;
|
||||
|
||||
old_source = constraint->source;
|
||||
if (old_source != NULL)
|
||||
{
|
||||
g_signal_handlers_disconnect_by_func (old_source,
|
||||
G_CALLBACK (source_destroyed),
|
||||
constraint);
|
||||
g_signal_handlers_disconnect_by_func (old_source,
|
||||
G_CALLBACK (source_queue_relayout),
|
||||
constraint);
|
||||
}
|
||||
|
||||
constraint->source = source;
|
||||
if (constraint->source != NULL)
|
||||
{
|
||||
g_signal_connect (constraint->source, "queue-relayout",
|
||||
G_CALLBACK (source_queue_relayout),
|
||||
constraint);
|
||||
g_signal_connect (constraint->source, "destroy",
|
||||
G_CALLBACK (source_destroyed),
|
||||
constraint);
|
||||
|
||||
if (constraint->actor != NULL)
|
||||
clutter_actor_queue_relayout (constraint->actor);
|
||||
}
|
||||
|
||||
_clutter_notify_by_pspec (G_OBJECT (constraint), obj_props[PROP_SOURCE]);
|
||||
}
|
||||
|
||||
/**
|
||||
* clutter_snap_constraint_get_source:
|
||||
* @constraint: a #ClutterSnapConstraint
|
||||
*
|
||||
* Retrieves the #ClutterActor set using clutter_snap_constraint_set_source()
|
||||
*
|
||||
* Return value: (transfer none): a pointer to the source actor
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
ClutterActor *
|
||||
clutter_snap_constraint_get_source (ClutterSnapConstraint *constraint)
|
||||
{
|
||||
g_return_val_if_fail (CLUTTER_IS_SNAP_CONSTRAINT (constraint), NULL);
|
||||
|
||||
return constraint->source;
|
||||
}
|
||||
|
||||
/**
|
||||
* clutter_snap_constraint_set_edges:
|
||||
* @constraint: a #ClutterSnapConstraint
|
||||
* @from_edge: the edge on the actor
|
||||
* @to_edge: the edge on the source
|
||||
*
|
||||
* Sets the edges to be used by the @constraint
|
||||
*
|
||||
* The @from_edge is the edge on the #ClutterActor to which @constraint
|
||||
* has been added. The @to_edge is the edge of the #ClutterActor inside
|
||||
* the #ClutterSnapConstraint:source property.
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
void
|
||||
clutter_snap_constraint_set_edges (ClutterSnapConstraint *constraint,
|
||||
ClutterSnapEdge from_edge,
|
||||
ClutterSnapEdge to_edge)
|
||||
{
|
||||
gboolean from_changed = FALSE, to_changed = FALSE;
|
||||
|
||||
g_return_if_fail (CLUTTER_IS_SNAP_CONSTRAINT (constraint));
|
||||
|
||||
g_object_freeze_notify (G_OBJECT (constraint));
|
||||
|
||||
if (constraint->from_edge != from_edge)
|
||||
{
|
||||
constraint->from_edge = from_edge;
|
||||
_clutter_notify_by_pspec (G_OBJECT (constraint),
|
||||
obj_props[PROP_FROM_EDGE]);
|
||||
from_changed = TRUE;
|
||||
}
|
||||
|
||||
if (constraint->to_edge != to_edge)
|
||||
{
|
||||
constraint->to_edge = to_edge;
|
||||
_clutter_notify_by_pspec (G_OBJECT (constraint),
|
||||
obj_props[PROP_TO_EDGE]);
|
||||
to_changed = TRUE;
|
||||
}
|
||||
|
||||
if ((from_changed || to_changed) &&
|
||||
constraint->actor != NULL)
|
||||
{
|
||||
clutter_actor_queue_relayout (constraint->actor);
|
||||
}
|
||||
|
||||
g_object_thaw_notify (G_OBJECT (constraint));
|
||||
}
|
||||
|
||||
/**
|
||||
* clutter_snap_constraint_get_edges:
|
||||
* @constraint: a #ClutterSnapConstraint
|
||||
* @from_edge: (out): return location for the actor's edge, or %NULL
|
||||
* @to_edge: (out): return location for the source's edge, or %NULL
|
||||
*
|
||||
* Retrieves the edges used by the @constraint
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
void
|
||||
clutter_snap_constraint_get_edge (ClutterSnapConstraint *constraint,
|
||||
ClutterSnapEdge *from_edge,
|
||||
ClutterSnapEdge *to_edge)
|
||||
{
|
||||
g_return_if_fail (CLUTTER_IS_SNAP_CONSTRAINT (constraint));
|
||||
|
||||
if (from_edge)
|
||||
*from_edge = constraint->from_edge;
|
||||
|
||||
if (to_edge)
|
||||
*to_edge = constraint->to_edge;
|
||||
}
|
||||
|
||||
/**
|
||||
* clutter_snap_constraint_set_offset:
|
||||
* @constraint: a #ClutterSnapConstraint
|
||||
* @offset: the offset to apply, in pixels
|
||||
*
|
||||
* Sets the offset to be applied to the constraint
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
void
|
||||
clutter_snap_constraint_set_offset (ClutterSnapConstraint *constraint,
|
||||
gfloat offset)
|
||||
{
|
||||
g_return_if_fail (CLUTTER_IS_SNAP_CONSTRAINT (constraint));
|
||||
|
||||
if (fabs (constraint->offset - offset) < 0.00001f)
|
||||
return;
|
||||
|
||||
constraint->offset = offset;
|
||||
|
||||
if (constraint->actor != NULL)
|
||||
clutter_actor_queue_relayout (constraint->actor);
|
||||
|
||||
_clutter_notify_by_pspec (G_OBJECT (constraint), obj_props[PROP_OFFSET]);
|
||||
}
|
||||
|
||||
/**
|
||||
* clutter_snap_constraint_get_offset:
|
||||
* @constraint: a #ClutterSnapConstraint
|
||||
*
|
||||
* Retrieves the offset set using clutter_snap_constraint_set_offset()
|
||||
*
|
||||
* Return value: the offset, in pixels
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
gfloat
|
||||
clutter_snap_constraint_get_offset (ClutterSnapConstraint *constraint)
|
||||
{
|
||||
g_return_val_if_fail (CLUTTER_IS_SNAP_CONSTRAINT (constraint), 0.0);
|
||||
|
||||
return constraint->offset;
|
||||
}
|
90
clutter/clutter-snap-constraint.h
Normal file
90
clutter/clutter-snap-constraint.h
Normal file
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* 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_SNAP_CONSTRAINT_H__
|
||||
#define __CLUTTER_SNAP_CONSTRAINT_H__
|
||||
|
||||
#include <clutter/clutter-constraint.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define CLUTTER_TYPE_SNAP_CONSTRAINT (clutter_snap_constraint_get_type ())
|
||||
#define CLUTTER_SNAP_CONSTRAINT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CLUTTER_TYPE_SNAP_CONSTRAINT, ClutterSnapConstraint))
|
||||
#define CLUTTER_IS_SNAP_CONSTRAINT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLUTTER_TYPE_SNAP_CONSTRAINT))
|
||||
|
||||
/**
|
||||
* ClutterSnapConstraint:
|
||||
*
|
||||
* <structname>ClutterSnapConstraint</structname> is an opaque structure
|
||||
* whose members cannot be directly accesses
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
typedef struct _ClutterSnapConstraint ClutterSnapConstraint;
|
||||
|
||||
/**
|
||||
* ClutterSnapEdge:
|
||||
* @CLUTTER_SNAP_EDGE_TOP: the top edge
|
||||
* @CLUTTER_SNAP_EDGE_RIGHT: the right edge
|
||||
* @CLUTTER_SNAP_EDGE_BOTTOM: the bottom edge
|
||||
* @CLUTTER_SNAP_EDGE_LEFT: the left edge
|
||||
*
|
||||
* The edge to snap
|
||||
*
|
||||
* Since: 1.6
|
||||
*/
|
||||
typedef enum {
|
||||
CLUTTER_SNAP_EDGE_TOP,
|
||||
CLUTTER_SNAP_EDGE_RIGHT,
|
||||
CLUTTER_SNAP_EDGE_BOTTOM,
|
||||
CLUTTER_SNAP_EDGE_LEFT
|
||||
} ClutterSnapEdge;
|
||||
|
||||
GType clutter_snap_constraint_get_type (void) G_GNUC_CONST;
|
||||
|
||||
ClutterConstraint * clutter_snap_constraint_new (ClutterActor *source,
|
||||
ClutterSnapEdge from_edge,
|
||||
ClutterSnapEdge to_edge,
|
||||
gfloat offset);
|
||||
|
||||
void clutter_snap_constraint_set_source (ClutterSnapConstraint *constraint,
|
||||
ClutterActor *source);
|
||||
ClutterActor * clutter_snap_constraint_get_source (ClutterSnapConstraint *constraint);
|
||||
void clutter_snap_constraint_set_edges (ClutterSnapConstraint *constraint,
|
||||
ClutterSnapEdge from_edge,
|
||||
ClutterSnapEdge to_edge);
|
||||
void clutter_snap_constraint_get_edges (ClutterSnapConstraint *constraint,
|
||||
ClutterSnapEdge *from_edge,
|
||||
ClutterSnapEdge *to_edge);
|
||||
void clutter_snap_constraint_set_offset (ClutterSnapConstraint *constraint,
|
||||
gfloat offset);
|
||||
gfloat clutter_snap_constraint_get_offset (ClutterSnapConstraint *constraint);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __CLUTTER_SNAP_CONSTRAINT_H__ */
|
@ -93,6 +93,7 @@
|
||||
#include "clutter-shader.h"
|
||||
#include "clutter-shader-effect.h"
|
||||
#include "clutter-shader-types.h"
|
||||
#include "clutter-snap-constraint.h"
|
||||
#include "clutter-stage.h"
|
||||
#include "clutter-stage-manager.h"
|
||||
#include "clutter-stage-window.h"
|
||||
|
@ -94,21 +94,31 @@
|
||||
</chapter>
|
||||
|
||||
<chapter>
|
||||
<title>Actions, Constraints and Effects</title>
|
||||
<title>Actions</title>
|
||||
|
||||
<xi:include href="xml/clutter-click-action.xml"/>
|
||||
<xi:include href="xml/clutter-drag-action.xml"/>
|
||||
</chapter>
|
||||
|
||||
<chapter>
|
||||
<title>Constraints</title>
|
||||
|
||||
<xi:include href="xml/clutter-bind-constraint.xml"/>
|
||||
<xi:include href="xml/clutter-align-constraint.xml"/>
|
||||
<xi:include href="xml/clutter-bind-constraint.xml"/>
|
||||
<xi:include href="xml/clutter-path-constraint.xml"/>
|
||||
<xi:include href="xml/clutter-snap-constraint.xml"/>
|
||||
</chapter>
|
||||
|
||||
<chapter>
|
||||
<title>Effects</title>
|
||||
|
||||
<xi:include href="xml/clutter-offscreen-effect.xml"/>
|
||||
<xi:include href="xml/clutter-shader-effect.xml"/>
|
||||
<xi:include href="xml/clutter-deform-effect.xml"/>
|
||||
|
||||
<xi:include href="xml/clutter-blur-effect.xml"/>
|
||||
<xi:include href="xml/clutter-colorize-effect.xml"/>
|
||||
<xi:include href="xml/clutter-desaturate-effect.xml"/>
|
||||
<xi:include href="xml/clutter-deform-effect.xml"/>
|
||||
<xi:include href="xml/clutter-page-turn-effect.xml"/>
|
||||
</chapter>
|
||||
|
||||
|
@ -2582,3 +2582,22 @@ CLUTTER_TYPE_PATH_CONSTRAINT
|
||||
<SUBSECTION Private>
|
||||
clutter_path_constraint_get_type
|
||||
</SECTION>
|
||||
|
||||
<SECTION>
|
||||
<FILE>clutter-snap-constraint</FILE>
|
||||
ClutterSnapEdge
|
||||
ClutterSnapConstraint
|
||||
clutter_snap_constraint_new
|
||||
clutter_snap_constraint_set_source
|
||||
clutter_snap_constraint_get_source
|
||||
clutter_snap_constraint_set_edges
|
||||
clutter_snap_constraint_get_edges
|
||||
clutter_snap_constraint_set_offset
|
||||
clutter_snap_constraint_get_offset
|
||||
<SUBSECTION Standard>
|
||||
CLUTTER_SNAP_CONSTRAINT
|
||||
CLUTTER_IS_SNAP_CONSTRAINT
|
||||
CLUTTER_TYPE_SNAP_CONSTRAINT
|
||||
<SUBSECTION Private>
|
||||
clutter_snap_constraint_get_type
|
||||
</SECTION>
|
||||
|
@ -54,6 +54,7 @@ clutter_script_get_type
|
||||
clutter_settings_get_type
|
||||
clutter_shader_get_type
|
||||
clutter_shader_effect_get_type
|
||||
clutter_snap_constraint_get_type
|
||||
clutter_stage_get_type
|
||||
clutter_stage_manager_get_type
|
||||
clutter_state_get_type
|
||||
|
@ -58,11 +58,11 @@ UNIT_TESTS = \
|
||||
test-bind.c \
|
||||
test-cogl-point-sprites.c \
|
||||
test-table-layout.c \
|
||||
test-path-constraint.c
|
||||
test-path-constraint.c \
|
||||
test-snap-constraint.c
|
||||
|
||||
if X11_TESTS
|
||||
UNIT_TESTS += test-pixmap.c
|
||||
UNIT_TESTS += test-devices.c
|
||||
UNIT_TESTS += test-pixmap.c test-devices.c
|
||||
endif
|
||||
|
||||
# For convenience, this provides a way to easily run individual unit tests:
|
||||
|
90
tests/interactive/test-snap-constraint.c
Normal file
90
tests/interactive/test-snap-constraint.c
Normal file
@ -0,0 +1,90 @@
|
||||
#include <stdlib.h>
|
||||
#include <gmodule.h>
|
||||
|
||||
#include <clutter/clutter.h>
|
||||
|
||||
G_MODULE_EXPORT int
|
||||
test_snap_constraint_main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
ClutterActor *stage, *layer_a, *layer_b, *layer_c;
|
||||
|
||||
clutter_init (&argc, &argv);
|
||||
|
||||
/* the main container */
|
||||
stage = clutter_stage_new ();
|
||||
clutter_actor_set_name (stage, "stage");
|
||||
clutter_stage_set_title (CLUTTER_STAGE (stage), "Snap Constraint");
|
||||
clutter_stage_set_color (CLUTTER_STAGE (stage), CLUTTER_COLOR_Aluminium1);
|
||||
clutter_stage_set_user_resizable (CLUTTER_STAGE (stage), TRUE);
|
||||
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
|
||||
|
||||
/* first layer, with a fixed (100, 25) size */
|
||||
layer_a = clutter_rectangle_new_with_color (CLUTTER_COLOR_ScarletRed);
|
||||
clutter_actor_set_name (layer_a, "layerA");
|
||||
clutter_container_add_actor (CLUTTER_CONTAINER (stage), layer_a);
|
||||
clutter_actor_set_size (layer_a, 100.0, 25.0);
|
||||
|
||||
/* the first layer is anchored to the middle of the stage */
|
||||
clutter_actor_add_constraint (layer_a, clutter_align_constraint_new (stage, CLUTTER_ALIGN_X_AXIS, 0.5));
|
||||
clutter_actor_add_constraint (layer_a, clutter_align_constraint_new (stage, CLUTTER_ALIGN_Y_AXIS, 0.5));
|
||||
|
||||
/* second layer, with no implicit size */
|
||||
layer_b = clutter_rectangle_new_with_color (CLUTTER_COLOR_DarkButter);
|
||||
clutter_actor_set_name (layer_b, "layerB");
|
||||
clutter_container_add_actor (CLUTTER_CONTAINER (stage), layer_b);
|
||||
|
||||
/* the second layer tracks the X coordinate and the width of
|
||||
* the first layer
|
||||
*/
|
||||
clutter_actor_add_constraint (layer_b, clutter_bind_constraint_new (layer_a, CLUTTER_BIND_X, 0.0));
|
||||
clutter_actor_add_constraint (layer_b, clutter_bind_constraint_new (layer_a, CLUTTER_BIND_WIDTH, 0.0));
|
||||
|
||||
/* the second layer is snapped between the bottom edge of
|
||||
* the first layer, and the bottom edge of the stage; a
|
||||
* spacing of 10 pixels in each direction is added for padding
|
||||
*/
|
||||
clutter_actor_add_constraint (layer_b,
|
||||
clutter_snap_constraint_new (layer_a,
|
||||
CLUTTER_SNAP_EDGE_TOP,
|
||||
CLUTTER_SNAP_EDGE_BOTTOM,
|
||||
10.0));
|
||||
|
||||
clutter_actor_add_constraint (layer_b,
|
||||
clutter_snap_constraint_new (stage,
|
||||
CLUTTER_SNAP_EDGE_BOTTOM,
|
||||
CLUTTER_SNAP_EDGE_BOTTOM,
|
||||
-10.0));
|
||||
|
||||
/* the third layer, with no implicit size */
|
||||
layer_c = clutter_rectangle_new_with_color (CLUTTER_COLOR_LightChameleon);
|
||||
clutter_actor_set_name (layer_c, "layerC");
|
||||
clutter_container_add_actor (CLUTTER_CONTAINER (stage), layer_c);
|
||||
|
||||
/* as for the second layer, the third layer tracks the X
|
||||
* coordinate and width of the first layer
|
||||
*/
|
||||
clutter_actor_add_constraint (layer_c, clutter_bind_constraint_new (layer_a, CLUTTER_BIND_X, 0.0));
|
||||
clutter_actor_add_constraint (layer_c, clutter_bind_constraint_new (layer_a, CLUTTER_BIND_WIDTH, 0.0));
|
||||
|
||||
/* the third layer is snapped between the top edge of the stage
|
||||
* and the top edge of the first layer; again, a spacing of
|
||||
* 10 pixels in each direction is added for padding
|
||||
*/
|
||||
clutter_actor_add_constraint (layer_c,
|
||||
clutter_snap_constraint_new (layer_a,
|
||||
CLUTTER_SNAP_EDGE_BOTTOM,
|
||||
CLUTTER_SNAP_EDGE_TOP,
|
||||
-10.0));
|
||||
clutter_actor_add_constraint (layer_c,
|
||||
clutter_snap_constraint_new (stage,
|
||||
CLUTTER_SNAP_EDGE_TOP,
|
||||
CLUTTER_SNAP_EDGE_TOP,
|
||||
10.0));
|
||||
|
||||
clutter_actor_show (stage);
|
||||
|
||||
clutter_main ();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user