gnome-games r7162 - trunk/aisleriot



Author: chpe
Date: Sun Jan 13 22:07:17 2008
New Revision: 7162
URL: http://svn.gnome.org/viewvc/gnome-games?rev=7162&view=rev

Log:
Factor moving the selected cards to the new slot out into a function
since it's used from 2 places.
Add select-all signal and keybinding (unimplemented so far).


Modified:
   trunk/aisleriot/board.c
   trunk/aisleriot/board.h

Modified: trunk/aisleriot/board.c
==============================================================================
--- trunk/aisleriot/board.c	(original)
+++ trunk/aisleriot/board.c	Sun Jan 13 22:07:17 2008
@@ -197,15 +197,18 @@
   PROP_THEME
 };
 
+#ifdef ENABLE_KEYNAV
 enum
 {
   ACTIVATE,
   MOVE_CURSOR,
   TOGGLE_SELECTION,
+  SELECT_ALL,
   LAST_SIGNAL
 };
 
 static guint signals[LAST_SIGNAL];
+#endif /* ENABLE_KEYNAV */
 
 static void get_slot_and_card_from_point (AisleriotBoard *board,
                                           int x,
@@ -895,6 +898,8 @@
 
 /* helper functions */
 
+#ifdef ENABLE_KEYNAV
+
 static void
 aisleriot_board_error_bell (AisleriotBoard *board)
 {
@@ -903,6 +908,8 @@
 #endif
 }
 
+#endif /* ENABLE_KEYNAV */
+
 /* Work out new sizes and spacings for the cards. */
 static void
 aisleriot_board_setup_geometry (AisleriotBoard *board)
@@ -1379,6 +1386,71 @@
   priv->last_clicked_card_id = -1;
 }
 
+/* Note: this unsets the selection! */
+static gboolean
+aisleriot_board_move_selected_cards_to_slot (AisleriotBoard *board,
+                                             Slot *hslot)
+{
+  AisleriotBoardPrivate *priv = board->priv;
+  Slot *selection_slot = priv->selection_slot;
+  int selection_start_card_id = priv->selection_start_card_id;
+  gboolean moved;
+  guint8 *cards;
+  guint n_cards;
+
+  if (!selection_slot ||
+      priv->selection_start_card_id < 0)
+    return FALSE;
+
+  /* NOTE: We cannot use aisleriot_game_drop_valid here since the
+    * game may not support the "droppable" feature.
+    */
+
+  set_selection (board, NULL, -1, FALSE);
+
+  priv->click_status = STATUS_NONE;
+
+  aisleriot_game_record_move (priv->game,
+                              selection_slot->id,
+                              selection_slot->cards->data,
+                              selection_slot->cards->len);
+
+  /* Store the cards, since the move could alter slot->cards! */
+  g_assert (selection_slot->cards->len >= selection_start_card_id);
+  n_cards = selection_slot->cards->len - selection_start_card_id;
+
+  cards = g_alloca (n_cards);
+  memcpy (cards,
+          selection_slot->cards->data + selection_start_card_id,
+          n_cards);
+
+  /* Now take the cards off of the origin slot. We'll update the slot geometry later */
+  g_byte_array_set_size (selection_slot->cards, selection_start_card_id);
+  selection_slot->needs_update = TRUE;
+
+  moved = aisleriot_game_drop_cards (priv->game,
+                                      selection_slot->id,
+                                      hslot->id,
+                                      cards,
+                                      n_cards);
+  if (moved) {
+    aisleriot_game_end_move (priv->game);
+
+    if (selection_slot->needs_update)
+      g_signal_emit_by_name (priv->game, "slot-changed", selection_slot); /* FIXMEchpe! */
+
+    aisleriot_game_test_end_of_game (priv->game);
+  } else {
+    /* Not moved; discard the move add the cards back to the origin slot */
+    aisleriot_game_discard_move (priv->game);
+    aisleriot_game_slot_add_cards (priv->game, selection_slot, cards, n_cards);
+
+    /* FIXMEchpe: maybe beep? */
+  }
+
+  return moved;
+}
+
 /* Keynav */
 
 #ifdef ENABLE_KEYNAV
@@ -1914,9 +1986,6 @@
   Slot *selection_slot = priv->selection_slot;
   int selection_start_card_id = priv->selection_start_card_id;
   guint state = 0;
-  gboolean moved;
-  guint8 *cards;
-  guint n_cards;
 
   if (!GTK_WIDGET_HAS_FOCUS (widget))
     return;
@@ -1972,56 +2041,14 @@
   if (selection_slot != NULL &&
       selection_start_card_id >= 0 &&
       priv->focus_card_id == ((int) focus_slot->cards->len) - 1) {
-    /* Remove the old selection. If the move doesn't succeed,
-     * we'll re-select them later.
-     */
-    set_selection (board, NULL, -1, FALSE);
-
-    priv->click_status = STATUS_NONE;
-
-    aisleriot_game_record_move (priv->game,
-                                selection_slot->id,
-                                selection_slot->cards->data,
-                                selection_slot->cards->len);
-
-    /* Store the cards, since the move could alter slot->cards! */
-    g_assert (selection_slot->cards->len >= selection_start_card_id);
-    n_cards = selection_slot->cards->len - selection_start_card_id;
-
-    cards = g_alloca (n_cards);
-    memcpy (cards,
-            selection_slot->cards->data + selection_start_card_id,
-            n_cards);
-
-    /* Now take the cards off of the origin slot. We'll update the slot geometry later */
-    g_byte_array_set_size (selection_slot->cards, selection_start_card_id);
-    selection_slot->needs_update = TRUE;
-
-    moved = aisleriot_game_drop_cards (priv->game,
-                                       selection_slot->id,
-                                       focus_slot->id,
-                                       cards,
-                                       n_cards);
-    if (moved) {
-      aisleriot_game_end_move (priv->game);
-
-      games_sound_play ("click");
-
+    if (aisleriot_board_move_selected_cards_to_slot (board, focus_slot)) {
       /* Select the new topmost card */
       set_focus (board, focus_slot, ((int) focus_slot->cards->len - 1), TRUE);
 
-      if (selection_slot->needs_update)
-        g_signal_emit_by_name (priv->game, "slot-changed", selection_slot); /* FIXMEchpe! */
-
-      aisleriot_game_test_end_of_game (priv->game);
-
       return;
     }
 
-    aisleriot_game_discard_move (priv->game);
-    aisleriot_game_slot_add_cards (priv->game, selection_slot, cards, n_cards);
-
-    g_print ("set-selection start %d len %d\n", selection_start_card_id, selection_slot->cards->len);
+    /* Trying to move the cards has unset the selection; re-select them */
     set_selection (board, selection_slot, selection_start_card_id, TRUE);
   }
 
@@ -2108,6 +2135,16 @@
 }
 
 static void
+aisleriot_board_select_all (AisleriotBoard *board)
+{
+/*  AisleriotBoardPrivate *priv = board->priv;
+  Slot *focus_slot;
+  int focus_card_id,new_selection_start_card_id;*/
+
+  aisleriot_board_error_bell (board);
+}
+
+static void
 aisleriot_board_toggle_selection (AisleriotBoard *board)
 {
   AisleriotBoardPrivate *priv = board->priv;
@@ -2497,67 +2534,20 @@
    * (in click-to-select mode only), or we set the selection.
    */
   if (hslot != priv->selection_slot) {
-    Slot *selection_slot = priv->selection_slot;
-    int selection_start_card_id = priv->selection_start_card_id;
-    gboolean moved;
-    guint8 *cards;
-    guint n_cards;
 
-    /* NOTE: We cannot use aisleriot_game_drop_valid here since the
-     * game may not support the "droppable" feature.
-     */
     if (!priv->click_to_move ||
-        selection_start_card_id < 0)
+        priv->selection_start_card_id < 0)
       goto set_selection;
 
-    /* Remove the old selection. If the move doesn't succeed, we'll select
-     * the clicked-on cards instead.
-     */
-    set_selection (board, NULL, -1, FALSE);
-
-    priv->click_status = STATUS_NONE;
-
-    aisleriot_game_record_move (priv->game,
-                                selection_slot->id,
-                                selection_slot->cards->data,
-                                selection_slot->cards->len);
-
-    /* Store the cards, since the move could alter slot->cards! */
-    g_assert (selection_slot->cards->len >= selection_start_card_id);
-    n_cards = selection_slot->cards->len - selection_start_card_id;
-
-    cards = g_alloca (n_cards);
-    memcpy (cards,
-            selection_slot->cards->data + selection_start_card_id,
-            n_cards);
-
-    /* Now take the cards off of the origin slot. We'll update the slot geometry later */
-    g_byte_array_set_size (selection_slot->cards, selection_start_card_id);
-    selection_slot->needs_update = TRUE;
-
-    moved = aisleriot_game_drop_cards (priv->game,
-                                       selection_slot->id,
-                                       hslot->id,
-                                       cards,
-                                       n_cards);
-    if (moved) {
-      aisleriot_game_end_move (priv->game);
-
-      if (selection_slot->needs_update)
-        g_signal_emit_by_name (priv->game, "slot-changed", selection_slot); /* FIXMEchpe! */
-
-      aisleriot_game_test_end_of_game (priv->game);
-
+    /* Try to move the selected cards to the clicked slot */
+    if (aisleriot_board_move_selected_cards_to_slot (board, hslot))
       return TRUE;
-    }
-
-    /* Not moved; discard the move and select the new cards */
-    aisleriot_game_discard_move (priv->game);
-
-    aisleriot_game_slot_add_cards (priv->game, selection_slot, cards, n_cards);
 
     /* FIXMEchpe: maybe beep? */
 
+    /* Trying to move the cards has unset the selection; select
+     * the clicked-on cards instead.
+     */
     goto set_selection;
   }
 
@@ -3182,6 +3172,7 @@
 #ifdef ENABLE_KEYNAV
   klass->activate = aisleriot_board_activate;
   klass->move_cursor = aisleriot_board_move_cursor;
+  klass->select_all = aisleriot_board_select_all;
   klass->toggle_selection = aisleriot_board_toggle_selection;
 
   /* Keybinding signals */
@@ -3216,6 +3207,16 @@
                   g_cclosure_marshal_VOID__VOID,
                   G_TYPE_NONE,
                   0);
+
+  signals[SELECT_ALL] =
+    g_signal_new (I_("select-all"),
+                  G_TYPE_FROM_CLASS (gobject_class),
+                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                  G_STRUCT_OFFSET (AisleriotBoardClass, select_all),
+                  NULL, NULL,
+                  g_cclosure_marshal_VOID__VOID,
+                  G_TYPE_NONE,
+                  0);
 #endif /* ENABLE_KEYNAV */
 
   /* Properties */
@@ -3294,6 +3295,8 @@
   /* Selection */
   gtk_binding_entry_add_signal (binding_set, GDK_space, 0,
                                 "toggle-selection", 0);
+  gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
+                                "select-all", 0);
 
   /* Activate */
   gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,

Modified: trunk/aisleriot/board.h
==============================================================================
--- trunk/aisleriot/board.h	(original)
+++ trunk/aisleriot/board.h	Sun Jan 13 22:07:17 2008
@@ -47,11 +47,12 @@
   GtkDrawingAreaClass parent_class;
 
   /* keybinding signals */
-  gboolean (* move_cursor) (AisleriotBoard *board,
-                            GtkMovementStep step,
-                            int count);
-  void (* activate) (AisleriotBoard *board);
+  gboolean (* move_cursor)  (AisleriotBoard *board,
+                             GtkMovementStep step,
+                             int count);
+  void (* activate)         (AisleriotBoard *board);
   void (* toggle_selection) (AisleriotBoard *board);
+  void (* select_all)       (AisleriotBoard *board);
 };
 
 GType aisleriot_board_get_type (void);



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