gnome-games r7823 - in trunk: aisleriot blackjack/src libgames-support



Author: chpe
Date: Fri Aug 15 10:14:40 2008
New Revision: 7823
URL: http://svn.gnome.org/viewvc/gnome-games?rev=7823&view=rev

Log:
Add "changed" signal to GamesCardTheme, and emit it when the theme has changed in a way that necessitates clearing the card cache.
Use this in GamesCardImages, and remove the methods that just forwarded to the theme.
Adapt aisleriot and blackjack to these API changes.

Modified:
   trunk/aisleriot/board.c
   trunk/blackjack/src/Makefile.am
   trunk/blackjack/src/card.cpp
   trunk/libgames-support/games-card-images.c
   trunk/libgames-support/games-card-images.h
   trunk/libgames-support/games-card-theme.c
   trunk/libgames-support/games-card-theme.h

Modified: trunk/aisleriot/board.c
==============================================================================
--- trunk/aisleriot/board.c	(original)
+++ trunk/aisleriot/board.c	Fri Aug 15 10:14:40 2008
@@ -108,6 +108,7 @@
   GdkGC *slot_gc;
   GdkCursor *cursor[LAST_CURSOR];
 
+  GamesCardTheme *theme;
   char *card_theme;
   CardSize card_size;
 
@@ -1427,10 +1428,11 @@
     g_free (xft_rgba);
 
     if (antialias_set) {
-      games_card_images_set_antialias (priv->images,
-                                       antialias_mode,
-                                       subpixel_order);
+      games_card_theme_set_antialias (priv->theme,
+                                      antialias_mode,
+                                      subpixel_order);
 
+      /* FIXMEchpe: clear the cached cards in the slots! */
       if (GTK_WIDGET_REALIZED (widget)) {
         gtk_widget_queue_draw (widget);
       }
@@ -3360,7 +3362,8 @@
   g_assert (priv->game != NULL);
 
   /* Create this down here since we need to have the scalable_cards value */
-  priv->images = games_card_images_new (priv->scalable_cards);
+  priv->theme = games_card_theme_new (NULL, priv->scalable_cards);
+  priv->images = games_card_images_new (priv->theme);
 
   return object;
 }
@@ -3381,6 +3384,7 @@
   g_byte_array_free (priv->moving_cards, TRUE);
 
   g_object_unref (priv->images);
+  g_object_unref (priv->theme);
 
 #if 0
   screen = gtk_widget_get_settings (widget);
@@ -3656,7 +3660,7 @@
   priv->geometry_set = FALSE;
   priv->slot_image = NULL;
 
-  retval = games_card_images_set_theme (priv->images, card_theme);
+  retval = games_card_theme_set_theme (priv->theme, card_theme);
 
   /* NOTE! We need to do this even if setting the theme failed, since
    * the attempt will have wiped out the old theme data!
@@ -3679,7 +3683,7 @@
   AisleriotBoardPrivate *priv = board->priv;
   const char *theme;
 
-  theme = games_card_images_get_theme (priv->images);
+  theme = games_card_theme_get_theme (priv->theme);
 
   return theme != NULL ? theme : GAMES_CARD_THEME_DEFAULT;
 }

Modified: trunk/blackjack/src/Makefile.am
==============================================================================
--- trunk/blackjack/src/Makefile.am	(original)
+++ trunk/blackjack/src/Makefile.am	Fri Aug 15 10:14:40 2008
@@ -1,4 +1,5 @@
 INCLUDES =  \
+	-I$(top_srcdir) \
 	-I$(top_srcdir)/libgames-support	\
 	-DPIXMAPDIR=\""$(pkgdatadir)/blackjack/pixmaps"\"	\
 	-DDATADIR=\""$(pkgdatadir)"\"		\

Modified: trunk/blackjack/src/card.cpp
==============================================================================
--- trunk/blackjack/src/card.cpp	(original)
+++ trunk/blackjack/src/card.cpp	Fri Aug 15 10:14:40 2008
@@ -25,7 +25,8 @@
 
 #include "blackjack.h"
 #include <gdk-pixbuf/gdk-pixbuf.h>
-#include <games-card-images.h>
+#include <libgames-support/games-card-theme.h>
+#include <libgames-support/games-card-images.h>
 #include "card.h"
 #include "chips.h"
 #include "draw.h"
@@ -38,6 +39,7 @@
 
 GdkBitmap *mask;
 
+GamesCardTheme *theme = NULL;
 GamesCardImages *images = NULL;
 
 GdkPixmap *
@@ -117,26 +119,30 @@
         GdkPixbuf *scaled = NULL;
         CardSize card_size;
 
-        if (!images) {
+        if (!theme) {
                 const char *env;
-                char *theme;
+                char *card_theme;
                 gboolean scalable, success;
 
                 env = g_getenv ("BLACKJACK_CARDS_SCALABLE");
                 scalable = env == NULL || g_ascii_strtoll (env, NULL, 10) != 0;
 
-                theme = bj_get_card_style ();
-                images = games_card_images_new (scalable);
+                theme = games_card_theme_new (NULL, scalable);
+
+                images = games_card_images_new (theme);
+                g_object_unref (theme);
+
                 games_card_images_set_cache_mode (images, CACHE_PIXMAPS);
                 games_card_images_set_drawable (images, playing_area->window);
 
-                if (!games_card_images_set_theme (images, theme)) {
-                        g_warning ("Failed to load theme %s!", theme);
+                card_theme = bj_get_card_style ();
+                if (!games_card_theme_set_theme (theme, card_theme)) {
+                        g_warning ("Failed to load theme %s!", card_theme);
                 }
-                g_free (theme);
+                g_free (card_theme);
         }
 
-        games_card_images_set_size (images, width, height, 1.0);
+        games_card_theme_set_size (theme, width, height, 1.0);
         card_size = games_card_images_get_size (images);
 
         bj_slot_set_size (card_size.width, card_size.height);
@@ -147,9 +153,9 @@
 }
 
 void
-bj_card_set_theme (gchar *theme)
+bj_card_set_theme (gchar *card_theme)
 {
-        games_card_images_set_theme (images, theme);
+        games_card_theme_set_theme (theme, card_theme);
 
         bj_draw_rescale_cards ();
         mask = games_card_images_get_card_mask (images);

Modified: trunk/libgames-support/games-card-images.c
==============================================================================
--- trunk/libgames-support/games-card-images.c	(original)
+++ trunk/libgames-support/games-card-images.c	Fri Aug 15 10:14:40 2008
@@ -35,7 +35,7 @@
 
 enum {
   PROP_0,
-  PROP_SCALABLE
+  PROP_THEME
 };
 
 /* MARK_IS_TRANSFORMED must be the same value as GAMES_CARD_IMAGES_HIGHLIGHTED ! */
@@ -83,6 +83,13 @@
   }
 }
 
+static void
+games_card_images_theme_changed_cb (GamesCardTheme * theme,
+                                    GamesCardImages * images)
+{
+  games_card_images_clear_cache (images);
+}
+
 static inline guint
 card_to_index (Card card)
 {
@@ -197,23 +204,6 @@
   images->cache_mode = CACHE_PIXMAPS;
 }
 
-static GObject *
-games_card_images_constructor (GType type,
-                               guint n_construct_properties,
-                               GObjectConstructParam * construct_params)
-{
-  GObject *object;
-  GamesCardImages *images;
-
-  object = G_OBJECT_CLASS (games_card_images_parent_class)->constructor
-    (type, n_construct_properties, construct_params);
-  images = GAMES_CARD_IMAGES (object);
-
-  images->theme = games_card_theme_new (NULL, images->scalable);
-
-  return object;
-}
-
 static void
 games_card_images_finalize (GObject * object)
 {
@@ -222,6 +212,8 @@
   games_card_images_clear_cache (images);
   g_free (images->cache);
 
+  g_signal_handlers_disconnect_by_func
+    (images->theme, G_CALLBACK (games_card_images_theme_changed_cb), images);
   g_object_unref (images->theme);
 
   G_OBJECT_CLASS (games_card_images_parent_class)->finalize (object);
@@ -235,8 +227,12 @@
   GamesCardImages *images = GAMES_CARD_IMAGES (object);
 
   switch (prop_id) {
-  case PROP_SCALABLE:
-    images->scalable = g_value_get_boolean (value) != FALSE;
+  case PROP_THEME:
+    images->theme = g_value_dup_object (value);
+    g_assert (images->theme);
+
+    g_signal_connect (images->theme, "changed",
+                      G_CALLBACK (games_card_images_theme_changed_cb), images);
     break;
   }
 }
@@ -246,18 +242,19 @@
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
 
-  gobject_class->constructor = games_card_images_constructor;
   gobject_class->set_property = games_card_images_set_property;
   gobject_class->finalize = games_card_images_finalize;
 
   g_object_class_install_property
     (gobject_class,
-     PROP_SCALABLE,
-     g_param_spec_boolean ("scalable", NULL, NULL,
-                           TRUE,
-                           G_PARAM_WRITABLE | G_PARAM_STATIC_NAME |
-                           G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB |
-                           G_PARAM_CONSTRUCT_ONLY));
+     PROP_THEME,
+     g_param_spec_object ("theme", NULL, NULL,
+                          GAMES_TYPE_CARD_THEME,
+                          G_PARAM_WRITABLE |
+                          G_PARAM_STATIC_NAME |
+                          G_PARAM_STATIC_NICK |
+                          G_PARAM_STATIC_BLURB |
+                          G_PARAM_CONSTRUCT_ONLY));
 }
 
 /* public API */
@@ -266,15 +263,15 @@
  * games_card_images_new:
  * @theme_dir: the directory to load the theme data from, or %NULL to use
  * the default directory
- * @scalable: whether to use scalable themes, or prerendered themes
+ * @theme: the #GamesCardTheme to cache images from
  *
  * Returns: a new #GamesCardImages
  */
 GamesCardImages *
-games_card_images_new (gboolean scalable)
+games_card_images_new (GamesCardTheme * theme)
 {
   return g_object_new (GAMES_TYPE_CARD_IMAGES,
-                       "scalable", scalable,
+                       "theme", theme,
                        NULL);
 }
 
@@ -334,75 +331,6 @@
 }
 
 /**
- * games_card_images_set_antialias:
- * @images:
- * @antialias: the antialiasing mode to use (see @cairo_antialias_t)
- * @subpixel_order: the subpixel order to use (see @cairo_subpixel_order_t)
- * if @antialias is %CAIRO_ANTIALIAS_SUBPIXEL 
- *
- * Turns on antialising of cards, if using a scalable theme.
- * Changing the antialias settings invalidates all cached pixbufs and pixmaps.
- */
-void
-games_card_images_set_antialias (GamesCardImages * images,
-                                 guint antialias, guint subpixel_order)
-{
-  g_return_if_fail (GAMES_IS_CARD_IMAGES (images));
-
-  games_card_images_clear_cache (images);
-
-  games_card_theme_set_antialias (images->theme, antialias, subpixel_order);
-}
-
-/**
- * games_card_images_set_theme:
- * @images:
- * @theme_name: the name of the theme to load
- *
- * Loads the card theme @theme_name. If the card theme cannot be loaded,
- * it falls back to the default card theme, if present.
- * Changing the card theme invalidates all cache pixbufs and pixmaps.
- * After changing the theme, the card size will be undefined; you need
- * to call games_card_images_set_size() to set it before getting a
- * card from @images again.
- * 
- * Returns: %TRUE iff loading the new card theme succeeded
- */
-gboolean
-games_card_images_set_theme (GamesCardImages * images,
-                             const gchar * theme_name)
-{
-  const char *old_theme;
-
-  g_return_val_if_fail (GAMES_IS_CARD_IMAGES (images), FALSE);
-  g_return_val_if_fail (theme_name != NULL && theme_name[0] != '\0', FALSE);
-
-  old_theme = games_card_theme_get_theme (images->theme);
-  if (old_theme != NULL && strcmp (old_theme, theme_name) == 0)
-    return TRUE;
-
-  /* We need to clear the cache even if changing the theme fails! */
-  games_card_images_clear_cache (images);
-
-  return games_card_theme_set_theme (images->theme, theme_name);
-}
-
-/**
- * games_card_images_get_theme:
- * @images:
- *
- * Returns: the name of the currently loaded card theme, or %NULL if no theme
- * is loaded
- */
-const gchar *
-games_card_images_get_theme (GamesCardImages * images)
-{
-  g_return_val_if_fail (GAMES_IS_CARD_IMAGES (images), NULL);
-
-  return games_card_theme_get_theme (images->theme);
-}
-
-/**
  * games_card_images_set_size:
  * @images:
  * @width: the maximum width
@@ -447,20 +375,6 @@
 }
 
 /**
- * games_card_images_get_aspect:
- * @images:
- *
- * Returns: the aspect ratio of the cards in the currently loaded theme
- */
-double
-games_card_images_get_aspect (GamesCardImages * images)
-{
-  g_return_val_if_fail (GAMES_IS_CARD_IMAGES (images), 1.0);
-
-  return games_card_theme_get_aspect (images->theme);
-}
-
-/**
  * games_card_images_set_background_color:
  * @images:
  * @colour:

Modified: trunk/libgames-support/games-card-images.h
==============================================================================
--- trunk/libgames-support/games-card-images.h	(original)
+++ trunk/libgames-support/games-card-images.h	Fri Aug 15 10:14:40 2008
@@ -56,15 +56,14 @@
   GdkColor background_colour;
   GdkColor selection_colour;
 
-  guint scalable : 1;
-  guint cache_mode : 1;
+  guint cache_mode;
 } GamesCardImages;
 
 typedef GObjectClass GamesCardImagesClass;
 
 GType games_card_images_get_type (void);
 
-GamesCardImages *games_card_images_new (gboolean scalable);
+GamesCardImages *games_card_images_new (GamesCardTheme * theme);
 
 void games_card_images_set_cache_mode (GamesCardImages * images,
                                        GamesCardImagesCacheMode mode);
@@ -72,22 +71,12 @@
 void games_card_images_set_drawable (GamesCardImages * images,
                                      GdkWindow * drawable);
 
-void games_card_images_set_antialias (GamesCardImages * images,
-                                      guint antialias, guint subpixel_order);
-
-gboolean games_card_images_set_theme (GamesCardImages * images,
-                                      const gchar * name);
-
-const gchar *games_card_images_get_theme (GamesCardImages * images);
-
 gboolean games_card_images_set_size (GamesCardImages * images,
                                      gint width,
                                      gint height, gdouble proportion);
 
 CardSize games_card_images_get_size (GamesCardImages * images);
 
-double games_card_images_get_aspect (GamesCardImages * images);
-
 void games_card_images_set_background_color (GamesCardImages * images,
                                              const GdkColor * color);
 

Modified: trunk/libgames-support/games-card-theme.c
==============================================================================
--- trunk/libgames-support/games-card-theme.c	(original)
+++ trunk/libgames-support/games-card-theme.c	Fri Aug 15 10:14:40 2008
@@ -30,6 +30,10 @@
 
 #include "games-card-theme.h"
 
+struct _GamesCardThemeClass {
+  GObjectClass parent_class;
+};
+
 struct _GamesCardTheme {
   GObject parent;
 
@@ -72,6 +76,13 @@
   PROP_THEME_DIRECTORY
 };
 
+enum {
+  CHANGED,
+  LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL];
+
 /* FIXMEchpe: use uninstalled data dir for rendering the card theme! */
 #define SLOTDIR  PKGDATADIR "/pixmaps"
 
@@ -571,13 +582,31 @@
 }
 
 static void
-games_card_theme_class_init (GamesCardThemeClass * class)
+games_card_theme_class_init (GamesCardThemeClass * klass)
 {
-  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
   gobject_class->set_property = games_card_theme_set_property;
   gobject_class->finalize = games_card_theme_finalize;
 
+  /**
+   * GamesCardTheme:changed:
+   * @theme: the object on which the signal is emitted
+   *
+   * The ::changed signal is emitted when the card theme has
+   * changed in any way that makes it necessary to re-render
+   * any displayed or cached images.
+   */
+  signals[CHANGED] =
+    g_signal_newv ("changed",
+                   G_TYPE_FROM_CLASS (klass),
+                   (GSignalFlags) (G_SIGNAL_RUN_LAST),
+                   NULL,
+                   NULL, NULL,
+                   g_cclosure_marshal_VOID__VOID,
+                   G_TYPE_NONE,
+                   0, NULL);
+
   g_object_class_install_property
     (gobject_class,
      PROP_SCALABLE,
@@ -642,6 +671,7 @@
   theme->subpixel_order = subpixel_order;
 
   games_card_theme_clear_source_pixbuf (theme);
+  g_signal_emit (theme, signals[CHANGED], 0);
 }
 
 /**
@@ -669,6 +699,7 @@
 
   games_card_theme_clear_source_pixbuf (theme);
   games_card_theme_clear_theme_data (theme);
+  g_signal_emit (theme, signals[CHANGED], 0);
 
   theme->card_size.width = theme->card_size.height = theme->slot_size.width =
     theme->slot_size.width = -1;
@@ -715,8 +746,8 @@
     return FALSE;
   }
 
-  if ((width == theme->slot_size.width)
-      && (height == theme->slot_size.height))
+  if ((width == theme->slot_size.width) &&
+      (height == theme->slot_size.height))
     return FALSE;
 
   theme->slot_size.width = width;
@@ -810,6 +841,7 @@
   }
 
   games_card_theme_clear_source_pixbuf (theme);
+  g_signal_emit (theme, signals[CHANGED], 0);
 
   return TRUE;
 }

Modified: trunk/libgames-support/games-card-theme.h
==============================================================================
--- trunk/libgames-support/games-card-theme.h	(original)
+++ trunk/libgames-support/games-card-theme.h	Fri Aug 15 10:14:40 2008
@@ -40,8 +40,8 @@
   gint height;
 } CardSize;
 
-typedef struct _GamesCardTheme GamesCardTheme;
-typedef GObjectClass GamesCardThemeClass;
+typedef struct _GamesCardThemeClass GamesCardThemeClass;
+typedef struct _GamesCardTheme      GamesCardTheme;
 
 GType games_card_theme_get_type (void);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]