[four-in-a-row: 10/13] Disallow clicking outside the board.



commit 2ceaa4482991bba772373ebca308f499a4a1b7e1
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Thu Dec 19 04:00:39 2019 +0100

    Disallow clicking outside the board.
    
    That is clearly more often a
    bad click than a wanted one.

 src/game-board-view.vala | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
---
diff --git a/src/game-board-view.vala b/src/game-board-view.vala
index 59d80e1..ff4c543 100644
--- a/src/game-board-view.vala
+++ b/src/game-board-view.vala
@@ -42,17 +42,6 @@ private class GameBoardView : Gtk.DrawingArea {
         this.game_board = game_board;
     }
 
-    private inline int get_column(int xpos) {
-        /* Derive column from pixel position */
-        int c = (xpos - board_x) / tile_size;
-        if (c > 6)
-            c = 6;
-        if (c < 0)
-            c = 0;
-
-        return c;
-    }
-
     internal inline void draw_tile(int r, int c) {
         queue_draw_area(c*tile_size + board_x, r*tile_size + board_y, tile_size, tile_size);
     }
@@ -278,10 +267,27 @@ private class GameBoardView : Gtk.DrawingArea {
 
     protected override bool button_press_event(Gdk.EventButton e) {
         int x;
+        int y;
         Gdk.Window? window = get_window();
         if (window == null)
             assert_not_reached ();
-        ((!) window).get_device_position(e.device, out x, null, null);
-        return column_clicked(get_column(x));
+        ((!) window).get_device_position(e.device, out x, out y, null);
+
+        int col;
+        if (get_column(x, y, out col))
+            return column_clicked(col);
+        else
+            return false;
+    }
+    private inline bool get_column(int x, int y, out int col) {
+        col = (x - board_x) / tile_size;
+        if (x < board_x || y < board_y || col < 0 || col > 6)
+            return false;
+
+        int row = (y - board_y) / tile_size;
+        if (row < 0 || row > 6)
+            return false;
+
+        return true;
     }
 }


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