[gnome-klotski] Use GestureMultiPress.



commit 0d22e515ae4ed9a2af950440978906f8fe659610
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Thu Mar 5 17:08:08 2020 +0100

    Use GestureMultiPress.

 meson.build          |  2 +-
 src/puzzle-view.vala | 82 +++++++++++++++++++++++++++++++---------------------
 2 files changed, 50 insertions(+), 34 deletions(-)
---
diff --git a/meson.build b/meson.build
index 49ba800..83d9947 100644
--- a/meson.build
+++ b/meson.build
@@ -13,7 +13,7 @@ python3 = import('python3')
 gee_dependency = dependency('gee-0.8')
 glib_dependency = dependency('glib-2.0', version: '>= 2.42.0')
 gnome_games_support_dependency = dependency('libgnome-games-support-1')
-gtk_dependency = dependency('gtk+-3.0', version: '>= 3.22.23')
+gtk_dependency = dependency('gtk+-3.0', version: '>= 3.24.0')
 rsvg_dependency = dependency('librsvg-2.0', version: '>= 2.32.0')
 posix_dependency = meson.get_compiler('vala').find_library('posix')
 
diff --git a/src/puzzle-view.vala b/src/puzzle-view.vala
index a3bbef7..147e1a0 100644
--- a/src/puzzle-view.vala
+++ b/src/puzzle-view.vala
@@ -73,6 +73,8 @@ private class PuzzleView : Gtk.DrawingArea
     Gtk.StyleContext style_context;
     construct
     {
+        init_mouse ();
+
         style_context = get_style_context ();
 
         set_size_request (250, 250);    // probably too small, but window requests 600x400 anyway
@@ -223,53 +225,67 @@ private class PuzzleView : Gtk.DrawingArea
         }
     }
 
-    protected override bool button_press_event (Gdk.EventButton event)
-    {
-        if (event.button == Gdk.BUTTON_PRIMARY)
-        {
-            if (puzzle.game_over ())
-                return false;
+    /*\
+    * * mouse user actions
+    \*/
 
-            int new_piece_x = (int) (event.x - kx) / tile_size;
-            int new_piece_y = (int) (event.y - ky) / tile_size;
-            if (new_piece_x < 0 || new_piece_x >= (int) puzzle.width
-             || new_piece_y < 0 || new_piece_y >= (int) puzzle.height)
-                return false;
-            piece_x = (uint8) new_piece_x;
-            piece_y = (uint8) new_piece_y;
-            char new_piece_id = puzzle.get_piece_id (puzzle.map, piece_x, piece_y);
+    private Gtk.GestureMultiPress click_controller;         // for keeping in memory
+
+    private void init_mouse ()  // called on construct
+    {
+        click_controller = new Gtk.GestureMultiPress (this);
+        click_controller.pressed.connect (on_click);
+        click_controller.released.connect (on_release);
+    }
 
-            bool already_moving = piece_id != '\0';
-            if (already_moving && piece_unmoved)
+    private static inline void on_click (Gtk.GestureMultiPress _click_controller, int n_press, double 
event_x, double event_y)
+    {
+        uint button = _click_controller.get_button ();
+        if (button == Gdk.BUTTON_PRIMARY)
+        {
+            PuzzleView _this = (PuzzleView) _click_controller.get_widget ();
+            if (_this.puzzle.game_over ())
+                return;
+
+            int new_piece_x = (int) (event_x - _this.kx) / _this.tile_size;
+            int new_piece_y = (int) (event_y - _this.ky) / _this.tile_size;
+            if (new_piece_x < 0 || new_piece_x >= (int) _this.puzzle.width
+             || new_piece_y < 0 || new_piece_y >= (int) _this.puzzle.height)
+                return;
+            _this.piece_x = (uint8) new_piece_x;
+            _this.piece_y = (uint8) new_piece_y;
+            char new_piece_id = _this.puzzle.get_piece_id (_this.puzzle.map, _this.piece_x, _this.piece_y);
+
+            bool already_moving = _this.piece_id != '\0';
+            if (already_moving && _this.piece_unmoved)
             {
-                piece_id = '\0';
-                return false;
+                _this.piece_id = '\0';
+                return;
             }
-            if (Puzzle.is_static_tile (new_piece_id) || new_piece_id == piece_id)
-                return false;
+            if (Puzzle.is_static_tile (new_piece_id) || new_piece_id == _this.piece_id)
+                return;
 
             if (already_moving)
             {
-                validate_move ();
-                if (!puzzle.can_be_moved (new_piece_id))
-                    return false;
+                _this.validate_move ();
+                if (!_this.puzzle.can_be_moved (new_piece_id))
+                    return;
             }
 
-            piece_unmoved = true;
-            piece_id = new_piece_id;
-            puzzle.move_map = puzzle.map;
+            _this.piece_unmoved = true;
+            _this.piece_id = new_piece_id;
+            _this.puzzle.move_map = _this.puzzle.map;
         }
-
-        return false;
     }
 
-    protected override bool button_release_event (Gdk.EventButton event)
+    private static inline void on_release (Gtk.GestureMultiPress _click_controller, int n_press, double 
event_x, double event_y)
     {
-        if (event.button == Gdk.BUTTON_PRIMARY && piece_id != '\0')
-            validate_move ();
-
-        return false;
+        PuzzleView _this = (PuzzleView) _click_controller.get_widget ();
+        uint button = _click_controller.get_button ();
+        if (button == Gdk.BUTTON_PRIMARY && _this.piece_id != '\0')
+            _this.validate_move ();
     }
+
     private void validate_move ()
     {
         if (piece_unmoved)


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