[gnome-mahjongg/arnaudb/wip/gtk4: 2/23] Use GestureMultiPress.



commit a6555b105b69b1a67cd564fbd966a7fbdf0634ff
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Apr 25 11:11:16 2020 +0200

    Use GestureMultiPress.

 meson.build             |  2 +-
 src/game-view.vala      | 60 ++++++++++++++++++++++++-------------------------
 src/gnome-mahjongg.vala | 11 ++++-----
 3 files changed, 35 insertions(+), 38 deletions(-)
---
diff --git a/meson.build b/meson.build
index c40e78b..975660c 100644
--- a/meson.build
+++ b/meson.build
@@ -19,7 +19,7 @@ pkgdatadir = datadir / 'gnome-mahjongg'
 # Dependencies
 glib_dep    = dependency ('glib-2.0', version: '>= 2.40.0')
 gio_dep     = dependency ('gio-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 1160947..484585a 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -32,6 +32,7 @@ public class GameView : Gtk.DrawingArea
     private uint   theme_resize_timer;
     private uint   theme_timer_id;
 
+    private Gtk.GestureMultiPress click_controller;     // for keeping in memory
 
     private Game? _game;
     public Game? game
@@ -94,11 +95,12 @@ public class GameView : Gtk.DrawingArea
         }
     }
 
-    public GameView ()
+    construct
     {
         can_focus = true;
         theme_timer_id = 0;
         add_events (Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK);
+        init_mouse ();
         size_allocate.connect(() => {
             /* Recalculate dimensions */
             update_dimensions ();
@@ -311,49 +313,47 @@ public class GameView : Gtk.DrawingArea
         return true;
     }
 
-    public override bool button_press_event (Gdk.EventButton event)
+    private inline void init_mouse ()
     {
-        if (game == null || game.paused)
-            return false;
+        click_controller = new Gtk.GestureMultiPress (this);    // only reacts to Gdk.BUTTON_PRIMARY
+        click_controller.pressed.connect (on_click);
+    }
 
-        /* Ignore the 2BUTTON and 3BUTTON events. */
-        if (event.type != Gdk.EventType.BUTTON_PRESS)
-            return false;
+    private inline void on_click (Gtk.GestureMultiPress _click_controller, int n_press, double event_x, 
double event_y)
+    {
+        if (game == null || game.paused)
+            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;
-            }
+            game.selected_tile = tile;
+        }
 
-            /* Unselect tile by clicking on it again */
-            else if (tile == game.selected_tile)
-            {
-                game.selected_tile = null;
-            }
+        /* Unselect tile by clicking on it again */
+        else if (tile == game.selected_tile)
+        {
+            game.selected_tile = null;
+        }
 
-            /* Attempt to match second tile to the selected one */
-            else if (game.selected_tile.matches (tile))
-            {
-                game.remove_pair (game.selected_tile, tile);
-            }
-            else
-            {
-                game.selected_tile = tile;
-            }
+        /* Attempt to match second tile to the selected one */
+        else if (game.selected_tile.matches (tile))
+        {
+            game.remove_pair (game.selected_tile, tile);
+        }
+        else
+        {
+            game.selected_tile = tile;
         }
 
         queue_draw();
-        return false;
     }
 
     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]