From 9006de848b9d44fa6eaade31aef6f3b73002f6a9 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Mon, 23 Jun 2008 15:31:12 +0000 Subject: [PATCH] 2008-06-23 Emmanuele Bassi * clutter/clutter-texture.c: (clutter_texture_get_preferred_width), (clutter_texture_get_preferred_height), (clutter_texture_set_property), (clutter_texture_get_property), (clutter_texture_class_init), (clutter_texture_init): Add the new :keep-aspect-ratio property to ClutterTexture; when set to TRUE the texture will return a preferred width maintaining the aspect ratio with the given height and a preferred height maintaining the aspect ratio with the given width. This allows to set the width or the height and have the texture automatically request the height or the width respectively while maintaining the aspect ratio of the original image. * tests/test-script.json: Update to test the new :keep-aspect-ratio property. --- ChangeLog | 19 ++++++++++ clutter/clutter-texture.c | 80 +++++++++++++++++++++++++++------------ tests/test-script.json | 2 +- 3 files changed, 76 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0cef9cdd1..2c9a71a54 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,22 @@ +2008-06-23 Emmanuele Bassi + + * clutter/clutter-texture.c: + (clutter_texture_get_preferred_width), + (clutter_texture_get_preferred_height), + (clutter_texture_set_property), + (clutter_texture_get_property), + (clutter_texture_class_init), + (clutter_texture_init): Add the new :keep-aspect-ratio property + to ClutterTexture; when set to TRUE the texture will return a + preferred width maintaining the aspect ratio with the given height + and a preferred height maintaining the aspect ratio with the + given width. This allows to set the width or the height and have + the texture automatically request the height or the width respectively + while maintaining the aspect ratio of the original image. + + * tests/test-script.json: Update to test the new :keep-aspect-ratio + property. + 2008-06-23 Neil Roberts Bug 918 - Group doesn't clip if it's children are clipped diff --git a/clutter/clutter-texture.c b/clutter/clutter-texture.c index 491c06632..0817a06dd 100644 --- a/clutter/clutter-texture.c +++ b/clutter/clutter-texture.c @@ -95,6 +95,7 @@ struct _ClutterTexturePrivate guchar *local_data; guint in_dispose : 1; + guint keep_aspect_ratio : 1; }; enum @@ -108,7 +109,8 @@ enum PROP_REPEAT_X, PROP_FILTER_QUALITY, PROP_COGL_TEXTURE, - PROP_FILENAME + PROP_FILENAME, + PROP_KEEP_ASPECT_RATIO }; enum @@ -338,17 +340,24 @@ clutter_texture_get_preferred_width (ClutterActor *self, { if (natural_width_p) { - if ((for_height < 0) || (priv->height <= 0)) - *natural_width_p = CLUTTER_UNITS_FROM_DEVICE (priv->width); + if (!priv->keep_aspect_ratio || + for_height < 0 || + priv->height <= 0) + { + *natural_width_p = CLUTTER_UNITS_FROM_DEVICE (priv->width); + } else { /* Set the natural width so as to preserve the aspect ratio */ - ClutterFixed ratio = - clutter_qdivx (CLUTTER_INT_TO_FIXED (priv->width), - CLUTTER_INT_TO_FIXED (priv->height)); - - *natural_width_p = CLUTTER_UNITS_FROM_FIXED ( - clutter_qmulx (ratio, CLUTTER_UNITS_TO_FIXED (for_height))); + ClutterFixed ratio, height; + + ratio = clutter_qdivx (CLUTTER_INT_TO_FIXED (priv->width), + CLUTTER_INT_TO_FIXED (priv->height)); + + height = CLUTTER_UNITS_TO_FIXED (for_height); + + *natural_width_p = + CLUTTER_UNITS_FROM_FIXED (clutter_qmulx (ratio, height)); } } } @@ -376,17 +385,24 @@ clutter_texture_get_preferred_height (ClutterActor *self, { if (natural_height_p) { - if ((for_width < 0) || (priv->width <= 0)) - *natural_height_p = CLUTTER_UNITS_FROM_DEVICE (priv->height); + if (!priv->keep_aspect_ratio || + for_width < 0 || + priv->width <= 0) + { + *natural_height_p = CLUTTER_UNITS_FROM_DEVICE (priv->height); + } else { /* Set the natural height so as to preserve the aspect ratio */ - ClutterFixed ratio = - clutter_qdivx (CLUTTER_INT_TO_FIXED (priv->height), - CLUTTER_INT_TO_FIXED (priv->width)); + ClutterFixed ratio, width; - *natural_height_p = CLUTTER_UNITS_FROM_FIXED ( - clutter_qmulx (ratio, CLUTTER_UNITS_TO_FIXED (for_width))); + ratio = clutter_qdivx (CLUTTER_INT_TO_FIXED (priv->height), + CLUTTER_INT_TO_FIXED (priv->width)); + + width = CLUTTER_UNITS_TO_FIXED (for_width); + + *natural_height_p = + CLUTTER_UNITS_FROM_FIXED (clutter_qmulx (ratio, width)); } } } @@ -561,6 +577,9 @@ clutter_texture_set_property (GObject *object, case PROP_NO_SLICE: priv->no_slice = g_value_get_boolean (value); break; + case PROP_KEEP_ASPECT_RATIO: + priv->keep_aspect_ratio = g_value_get_boolean (value); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -608,6 +627,9 @@ clutter_texture_get_property (GObject *object, case PROP_NO_SLICE: g_value_set_boolean (value, priv->no_slice); break; + case PROP_KEEP_ASPECT_RATIO: + g_value_set_boolean (value, priv->keep_aspect_ratio); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -723,6 +745,15 @@ clutter_texture_class_init (ClutterTextureClass *klass) NULL, G_PARAM_WRITABLE)); + g_object_class_install_property + (gobject_class, PROP_KEEP_ASPECT_RATIO, + g_param_spec_boolean ("keep-aspect-ratio", + "Keep Aspect Ratio", + "Keep the aspect ratio of the texture when " + "requesting the preferred width or height", + FALSE, + CLUTTER_PARAM_READWRITE)); + /** * ClutterTexture::size-change: * @texture: the texture which received the signal @@ -841,14 +872,15 @@ clutter_texture_init (ClutterTexture *self) self->priv = priv = CLUTTER_TEXTURE_GET_PRIVATE (self); - priv->max_tile_waste = 64; - priv->filter_quality = CLUTTER_TEXTURE_QUALITY_MEDIUM; - priv->repeat_x = FALSE; - priv->repeat_y = FALSE; - priv->sync_actor_size = TRUE; - priv->texture = COGL_INVALID_HANDLE; - priv->fbo_handle = COGL_INVALID_HANDLE; - priv->local_data = NULL; + priv->max_tile_waste = 64; + priv->filter_quality = CLUTTER_TEXTURE_QUALITY_MEDIUM; + priv->repeat_x = FALSE; + priv->repeat_y = FALSE; + priv->sync_actor_size = TRUE; + priv->texture = COGL_INVALID_HANDLE; + priv->fbo_handle = COGL_INVALID_HANDLE; + priv->local_data = NULL; + priv->keep_aspect_ratio = FALSE; } static void diff --git a/tests/test-script.json b/tests/test-script.json index e17185beb..5992f1a10 100644 --- a/tests/test-script.json +++ b/tests/test-script.json @@ -42,7 +42,7 @@ "x" : 100, "y" : 100, "width" : "20mm", - "height" : "100px", + "keep-aspect-ratio" : true, "anchor-x" : "5mm", "anchor-y" : "5pt", "opacity" : 100,