[gnome-mahjongg/arnaudb/modernize-code: 2/3] Use GestureMultiPress.



commit 7d405d5a0caa2e0f2541d5ad96d4e0176b6120ff
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri Mar 6 19:00:21 2020 +0100

    Use GestureMultiPress.
    
    Bump Gtk required version to 3.14.

 meson.build             |  2 +-
 src/game-view.vala      | 70 +++++++++++++++++++++++++++----------------------
 src/gnome-mahjongg.vala | 11 +++-----
 3 files changed, 43 insertions(+), 40 deletions(-)
---
diff --git a/meson.build b/meson.build
index 4fdd221..2805235 100644
--- a/meson.build
+++ b/meson.build
@@ -18,7 +18,7 @@ pkgdatadir = join_paths (datadir, 'gnome-mahjongg')
 
 # Dependencies
 glib_dep = dependency ('glib-2.0', version: '>= 2.40.0')
-gtk_dep = dependency ('gtk+-3.0', version: '>= 3.13.2')
+gtk_dep = dependency ('gtk+-3.0', version: '>= 3.14.0')
 librsvg_dep = dependency ('librsvg-2.0', version: '>= 2.32.0')
 
 subdir ('po')
diff --git a/src/game-view.vala b/src/game-view.vala
index 2024525..c40ff3e 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -23,6 +23,8 @@ public class GameView : Gtk.DrawingArea
     private int tile_layer_offset_x;
     private int tile_layer_offset_y;
 
+    private Gtk.GestureMultiPress click_controller; // for keeping in memory
+
     private Game? _game;
     public Game? game
     {
@@ -47,6 +49,8 @@ public class GameView : Gtk.DrawingArea
     {
         can_focus = true;
         add_events (Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK);
+
+        init_mouse ();
     }
 
     public void set_background (string? colour)
@@ -275,52 +279,54 @@ public class GameView : Gtk.DrawingArea
         return true;
     }
 
-    public override bool button_press_event (Gdk.EventButton event)
+    private void init_mouse ()
+    {
+        click_controller = new Gtk.GestureMultiPress (this);
+        click_controller.pressed.connect (on_click);
+    }
+
+    private inline void on_click (Gtk.GestureMultiPress _click_controller, int n_press, double event_x, 
double event_y)
     {
         if (game == null || game.paused)
-            return false;
+            return;
 
         /* Ignore the 2BUTTON and 3BUTTON events. */
-        if (event.type != Gdk.EventType.BUTTON_PRESS)
-            return false;
+        uint button = _click_controller.get_button ();
+        if (button != Gdk.BUTTON_PRIMARY)
+            return;
 
         /* Get the tile under the square */
-        var tile = find_tile ((uint) event.x, (uint) event.y);
+        var tile = find_tile ((uint) event_x, (uint) event_y);
 
         /* If not a valid tile then ignore the event */
         if (tile == null || !game.tile_can_move (tile))
-            return true;
+            return;
 
-        if (event.button == 1)
+        /* Select first tile */
+        if (game.selected_tile == null)
         {
-            /* Select first tile */
-            if (game.selected_tile == null)
-            {
-                game.selected_tile = tile;
-                return true;
-            }
-
-            /* Unselect tile by clicking on it again */
-            if (tile == game.selected_tile)
-            {
-                game.selected_tile = null;
-                return true;
-            }
+            game.selected_tile = tile;
+            return;
+        }
 
-            /* Attempt to match second tile to the selected one */
-            if (game.selected_tile.matches (tile))
-            {
-                game.remove_pair (game.selected_tile, tile);
-                return true;
-            }
-            else
-            {
-                game.selected_tile = tile;
-                return true;
-            }
+        /* Unselect tile by clicking on it again */
+        if (tile == game.selected_tile)
+        {
+            game.selected_tile = null;
+            return;
         }
 
-        return false;
+        /* Attempt to match second tile to the selected one */
+        if (game.selected_tile.matches (tile))
+        {
+            game.remove_pair (game.selected_tile, tile);
+            return;
+        }
+        else
+        {
+            game.selected_tile = tile;
+            return;
+        }
     }
 
     private Tile? find_tile (uint x, uint y)
diff --git a/src/gnome-mahjongg.vala b/src/gnome-mahjongg.vala
index db5a8ce..a0918c7 100644
--- a/src/gnome-mahjongg.vala
+++ b/src/gnome-mahjongg.vala
@@ -101,7 +101,8 @@ public class Mahjongg : Gtk.Application
         var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
 
         game_view = new GameView ();
-        game_view.button_press_event.connect (view_button_press_event);
+        view_click_controller = new Gtk.GestureMultiPress (game_view);
+        view_click_controller.pressed.connect (on_click);
         game_view.set_size_request (600, 400);
 
         title = new Gtk.Label ("");
@@ -297,16 +298,12 @@ public class Mahjongg : Gtk.Application
         }
     }
 
-    private bool view_button_press_event (Gtk.Widget widget, Gdk.EventButton event)
+    private Gtk.GestureMultiPress view_click_controller;    // for keeping in memory
+    private inline void on_click (Gtk.GestureMultiPress _view_click_controller, int n_press, double event_x, 
double event_y)
     {
         /* Cancel pause on click */
         if (game_view.game.paused)
-        {
             pause_cb ();
-            return true;
-        }
-
-        return false;
     }
 
     private void background_changed_cb (Gtk.ColorButton widget)


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