[gnome-2048] Add gestures.



commit 165550a7efa7959802ff489ea31df4e1ad522440
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Jan 30 07:21:44 2019 +0100

    Add gestures.
    
    That allows playing on touchscreens.
    It should probably evolve a bit, but
    I cannot test this on real hardware.
    
    Closes #5.

 data/mainwindow.ui   | 12 -----------
 src/application.vala | 59 +++++++++++++++++++++++++++++++++++++++++++---------
 src/game.vala        | 21 ++++++++++---------
 3 files changed, 60 insertions(+), 32 deletions(-)
---
diff --git a/data/mainwindow.ui b/data/mainwindow.ui
index 7a6e888..d525ed7 100644
--- a/data/mainwindow.ui
+++ b/data/mainwindow.ui
@@ -85,24 +85,12 @@
             <property name="can-focus">False</property>
             <property name="label-xalign">0</property>
             <property name="shadow-type">none</property>
-            <child>
-              <placeholder/>
-            </child>
           </object>
           <packing>
             <property name="left-attach">1</property>
             <property name="top-attach">1</property>
           </packing>
         </child>
-        <child>
-          <placeholder/>
-        </child>
-        <child>
-          <placeholder/>
-        </child>
-        <child>
-          <placeholder/>
-        </child>
       </object>
     </child>
   </object>
diff --git a/src/application.vala b/src/application.vala
index 4b38ae4..ecd67a3 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -38,8 +38,7 @@ public class Application : Gtk.Application
     private Label _score;
     private MenuButton _new_game_button;
     private MenuButton _hamburger_button;
-
-    private GtkClutter.Embed embed;
+    private GtkClutter.Embed _embed;
 
     private bool _game_restored;
 
@@ -138,8 +137,9 @@ public class Application : Gtk.Application
         set_accels_for_action ("app.toggle-hamburger",  {                 "F10",
                                                                           "Menu"    });
 
-        _window.notify ["has-toplevel-focus"].connect (() => embed.grab_focus ());
+        _window.notify ["has-toplevel-focus"].connect (() => _embed.grab_focus ());
         _window.show_all ();
+        _init_gesture ();
 
         _game_restored = _game.restore_game ();
         if (!_game_restored)
@@ -231,7 +231,7 @@ public class Application : Gtk.Application
         _hamburger_button = (MenuButton) builder.get_object ("hamburger-button");
         _hamburger_button.notify ["active"].connect (() => {
                 if (!_hamburger_button.active)
-                    embed.grab_focus ();
+                    _embed.grab_focus ();
             });
         _settings.changed ["allow-undo"].connect (_update_hamburger_menu);
         _update_hamburger_menu ();
@@ -239,10 +239,10 @@ public class Application : Gtk.Application
 
     private void _create_game_view (Builder builder)
     {
-        embed = new GtkClutter.Embed ();
-        AspectFrame frame = (AspectFrame) builder.get_object ("aspectframe");
-        frame.add (embed);
-        _game.view = embed.get_stage ();
+        _embed = new GtkClutter.Embed ();
+        AspectFrame _frame = (AspectFrame) builder.get_object ("aspectframe");
+        _frame.add (_embed);
+        _game.view = _embed.get_stage ();
     }
 
     /*\
@@ -319,7 +319,7 @@ public class Application : Gtk.Application
 
         _game.new_game ();
 
-        embed.grab_focus ();
+        _embed.grab_focus ();
     }
 
     private void toggle_new_game_cb (/* SimpleAction action, Variant? variant */)
@@ -421,7 +421,7 @@ public class Application : Gtk.Application
     {
         _game_restored = false;
 
-        if (_hamburger_button.active || (_window.focus_visible && !embed.is_focus))
+        if (_hamburger_button.active || (_window.focus_visible && !_embed.is_focus))
             return false;
 
         return _game.key_pressed (event);
@@ -622,4 +622,43 @@ public class Application : Gtk.Application
                 debug ("score added");
             });
     }
+
+    /*\
+    * * gesture
+    \*/
+
+    private GestureSwipe gesture;
+    private inline void _init_gesture ()
+    {
+        gesture = new GestureSwipe (_embed); // _window works, but problems with headerbar; the main grid or 
the aspectframe do as _embed
+        gesture.set_propagation_phase (PropagationPhase.CAPTURE);
+        gesture.set_button (/* all events */ 0);
+        gesture.swipe.connect (_on_swipe);
+    }
+
+    private inline void _on_swipe (GestureSwipe gesture, double velocity_x, double velocity_y)
+    {
+        double abs_x = velocity_x.abs ();
+        double abs_y = velocity_y.abs ();
+        if (abs_x * abs_x + abs_y * abs_y < 400.0)
+            return;
+        bool left_or_right = abs_y * 4.0 < abs_x;
+        bool up_or_down = abs_x * 4.0 < abs_y;
+        if (left_or_right)
+        {
+            if (velocity_x < -10.0)
+                _game.move_left ();
+            else if (velocity_x > 10.0)
+                _game.move_right ();
+        }
+        else if (up_or_down)
+        {
+            if (velocity_y < -10.0)
+                _game.move_up ();
+            else if (velocity_y > 10.0)
+                _game.move_down ();
+        }
+        else
+            return;
+    }
 }
diff --git a/src/game.vala b/src/game.vala
index dbae0c9..9553a78 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -200,12 +200,13 @@ public class Game : Object
 
         uint keyval = _upper_key (event.keyval);
 
-        if (keyval == Gdk.Key.Down)       _move_down ();
-        else if (keyval == Gdk.Key.Up)    _move_up ();
-        else if (keyval == Gdk.Key.Left)  _move_left ();
-        else if (keyval == Gdk.Key.Right) _move_right ();
-
-        return false;
+        if      (keyval == Gdk.Key.Down)  move_down ();
+        else if (keyval == Gdk.Key.Up)    move_up ();
+        else if (keyval == Gdk.Key.Left)  move_left ();
+        else if (keyval == Gdk.Key.Right) move_right ();
+        else
+            return false;
+        return true;
     }
 
     public void reload_settings ()
@@ -388,7 +389,7 @@ public class Game : Object
         _foreground_nxt[pos.row,pos.col] = view;
     }
 
-    private void _move_down ()
+    internal void move_down ()
     {
         debug ("move down");
 
@@ -414,7 +415,7 @@ public class Game : Object
         }
     }
 
-    private void _move_up ()
+    internal void move_up ()
     {
         debug ("move up");
 
@@ -440,7 +441,7 @@ public class Game : Object
         }
     }
 
-    private void _move_left ()
+    internal void move_left ()
     {
         debug ("move left");
 
@@ -466,7 +467,7 @@ public class Game : Object
         }
     }
 
-    private void _move_right ()
+    internal void move_right ()
     {
         debug ("move right");
 


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