[hitori: 2/3] Paint (and tag) cells with the keyboard




commit 640a1202d53e8d27a9a98e545219fe281385e639
Author: Toni Ruža <toni ruza gmail com>
Date:   Mon Nov 1 21:25:41 2021 +0100

    Paint (and tag) cells with the keyboard

 src/interface.c | 83 ++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 50 insertions(+), 33 deletions(-)
---
diff --git a/src/interface.c b/src/interface.c
index d5d90aa..14915ee 100644
--- a/src/interface.c
+++ b/src/interface.c
@@ -414,56 +414,28 @@ hitori_draw_cb (GtkWidget *drawing_area, cairo_t *cr, Hitori *hitori)
        return FALSE;
 }
 
-gboolean
-hitori_button_release_cb (GtkWidget *drawing_area, GdkEventButton *event, Hitori *hitori)
+static void
+hitori_update_cell_state (Hitori *hitori, HitoriVector pos, gboolean tag1, gboolean tag2)
 {
-       gint width, height;
-       gdouble cell_size;
-       HitoriVector pos;
        HitoriUndo *undo;
        gboolean recheck = FALSE;
 
-       if (hitori->processing_events == FALSE)
-               return FALSE;
-
-       width = gdk_window_get_width (gtk_widget_get_window (hitori->drawing_area));
-       height = gdk_window_get_height (gtk_widget_get_window (hitori->drawing_area));
-
-       /* Clamp the width/height to the minimum */
-       if (height < width)
-               width = height;
-
-       cell_size = (gdouble) width / (gdouble) hitori->board_size;
-
-       /* Determine the cell in which the button was released */
-       pos.x = (guchar) ((event->x - hitori->drawing_area_x_offset) / cell_size);
-       pos.y = (guchar) ((event->y - hitori->drawing_area_y_offset) / cell_size);
-
-       if (pos.x >= hitori->board_size || pos.y >= hitori->board_size)
-               return FALSE;
-
-       /* Move the cursor to the clicked cell and deactivate it
-        * (assuming player will use the mouse for the next move) */
-       hitori->cursor_position.x = pos.x;
-       hitori->cursor_position.y = pos.y;
-       hitori->cursor_active = FALSE;
-
        /* Update the undo stack */
        undo = g_new (HitoriUndo, 1);
        undo->cell = pos;
        undo->undo = hitori->undo_stack;
        undo->redo = NULL;
 
-       if (event->state & GDK_SHIFT_MASK && event->state & GDK_CONTROL_MASK) {
+       if (tag1 && tag2) {
                /* Update both tags' state */
                hitori->board[pos.x][pos.y].status ^= CELL_TAG1;
                hitori->board[pos.x][pos.y].status ^= CELL_TAG2;
                undo->type = UNDO_TAGS;
-       } else if (event->state & GDK_SHIFT_MASK) {
+       } else if (tag1) {
                /* Update tag 1's state */
                hitori->board[pos.x][pos.y].status ^= CELL_TAG1;
                undo->type = UNDO_TAG1;
-       } else if (event->state & GDK_CONTROL_MASK) {
+       } else if (tag2) {
                /* Update tag 2's state */
                hitori->board[pos.x][pos.y].status ^= CELL_TAG2;
                undo->type = UNDO_TAG2;
@@ -500,6 +472,43 @@ hitori_button_release_cb (GtkWidget *drawing_area, GdkEventButton *event, Hitori
        /* Check to see if the player's won */
        if (recheck == TRUE)
                hitori_check_win (hitori);
+}
+
+gboolean
+hitori_button_release_cb (GtkWidget *drawing_area, GdkEventButton *event, Hitori *hitori)
+{
+       gint width, height;
+       gdouble cell_size;
+       HitoriVector pos;
+
+       if (hitori->processing_events == FALSE)
+               return FALSE;
+
+       width = gdk_window_get_width (gtk_widget_get_window (hitori->drawing_area));
+       height = gdk_window_get_height (gtk_widget_get_window (hitori->drawing_area));
+
+       /* Clamp the width/height to the minimum */
+       if (height < width)
+               width = height;
+
+       cell_size = (gdouble) width / (gdouble) hitori->board_size;
+
+       /* Determine the cell in which the button was released */
+       pos.x = (guchar) ((event->x - hitori->drawing_area_x_offset) / cell_size);
+       pos.y = (guchar) ((event->y - hitori->drawing_area_y_offset) / cell_size);
+
+       if (pos.x >= hitori->board_size || pos.y >= hitori->board_size)
+               return FALSE;
+
+       /* Move the cursor to the clicked cell and deactivate it
+        * (assuming player will use the mouse for the next move) */
+       hitori->cursor_position.x = pos.x;
+       hitori->cursor_position.y = pos.y;
+       hitori->cursor_active = FALSE;
+
+       hitori_update_cell_state (hitori, pos,
+                                 event->state & GDK_SHIFT_MASK,
+                                 event->state & GDK_CONTROL_MASK);
 
        return FALSE;
 }
@@ -535,6 +544,12 @@ hitori_key_press_cb (GtkWidget *drawing_area, GdkEventKey *event, Hitori *hitori
                                        hitori->cursor_position.y += 1;
                                }
                                break;
+                       case GDK_KEY_space:
+                       case GDK_KEY_Return:
+                               hitori_update_cell_state (hitori, hitori->cursor_position,
+                                                         event->state & GDK_SHIFT_MASK,
+                                                         event->state & GDK_CONTROL_MASK);
+                               break;
                        default:
                                did_something = FALSE;
                }
@@ -732,6 +747,7 @@ undo_cb (GSimpleAction *action, GVariant *parameter, gpointer user_data)
                        break;
        }
 
+       self->cursor_position = self->undo_stack->cell;
        self->undo_stack = self->undo_stack->undo;
 
        g_simple_action_set_enabled (self->redo_action, TRUE);
@@ -754,6 +770,7 @@ redo_cb (GSimpleAction *action, GVariant *parameter, gpointer user_data)
                return;
 
        self->undo_stack = self->undo_stack->redo;
+       self->cursor_position = self->undo_stack->cell;
 
        switch (self->undo_stack->type) {
                case UNDO_PAINT:


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