[gnome-games/wip/aplazas/gamepad-navigation: 4/7] collection-icon-view: Use a GamepadBrowse



commit 8ddf2b0e432ecc4dfff00e37bc5fb1ea474cc8ef
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Sun Aug 5 10:19:19 2018 +0200

    collection-icon-view: Use a GamepadBrowse
    
    This makes the code simpler.

 data/ui/collection-icon-view.ui  |   4 +
 src/ui/collection-icon-view.vala | 178 +++------------------------------------
 2 files changed, 18 insertions(+), 164 deletions(-)
---
diff --git a/data/ui/collection-icon-view.ui b/data/ui/collection-icon-view.ui
index e0bdee3b..3912798b 100644
--- a/data/ui/collection-icon-view.ui
+++ b/data/ui/collection-icon-view.ui
@@ -43,4 +43,8 @@
       </packing>
     </child>
   </template>
+  <object class="GamesGamepadBrowse" id="gamepad_browse">
+    <signal name="browse" handler="on_gamepad_browse"/>
+    <signal name="accept" handler="on_gamepad_accept"/>
+  </object>
 </interface>
diff --git a/src/ui/collection-icon-view.vala b/src/ui/collection-icon-view.vala
index 8f435e7f..93fd7cb0 100644
--- a/src/ui/collection-icon-view.vala
+++ b/src/ui/collection-icon-view.vala
@@ -2,14 +2,6 @@
 
 [GtkTemplate (ui = "/org/gnome/Games/ui/collection-icon-view.ui")]
 private class Games.CollectionIconView : Gtk.Stack {
-       private enum CursorMovementSource {
-               UNKNOWN,
-               DIRECTIONAL_PAD,
-               ANALOG_STICK,
-       }
-
-       private const double DEAD_ZONE = 0.3;
-
        public signal void game_activated (Game game);
 
        private string[] filtering_terms;
@@ -68,15 +60,12 @@ private class Games.CollectionIconView : Gtk.Stack {
        [GtkChild]
        private Gtk.FlowBox flow_box;
 
+       [GtkChild]
+       private GamepadBrowse gamepad_browse;
+
        // Current size used by the thumbnails.
        private int game_view_size;
 
-       private Gtk.DirectionType cursor_direction;
-       private CursorMovementSource cursor_movement_source;
-       private Manette.Device? cursor_movement_device;
-       private double cursor_speed;
-       private uint cursor_timeout;
-
        static construct {
                set_css_name ("gamescollectioniconview");
        }
@@ -85,118 +74,28 @@ private class Games.CollectionIconView : Gtk.Stack {
                flow_box.max_children_per_line = uint.MAX;
                flow_box.set_filter_func (filter_box);
                flow_box.set_sort_func (sort_boxes);
-               unmap.connect (cancel_cursor_movement);
+               unmap.connect (() => gamepad_browse.cancel_cursor_movement);
        }
 
        public bool gamepad_button_press_event (Manette.Event event) {
                if (!get_mapped ())
                        return false;
 
-               uint16 button;
-               if (!event.get_button (out button))
-                       return false;
-
-               switch (button) {
-               case EventCode.BTN_A:
-                       flow_box.activate_cursor_child ();
-
-                       return true;
-               case EventCode.BTN_START:
-                       flow_box.activate_cursor_child ();
-
-                       return true;
-               case EventCode.BTN_DPAD_UP:
-                       return move_cursor (Gtk.DirectionType.UP, CursorMovementSource.DIRECTIONAL_PAD, 
event.get_device (), 1.0);
-               case EventCode.BTN_DPAD_DOWN:
-                       return move_cursor (Gtk.DirectionType.DOWN, CursorMovementSource.DIRECTIONAL_PAD, 
event.get_device (), 1.0);
-               case EventCode.BTN_DPAD_LEFT:
-                       return move_cursor (Gtk.DirectionType.LEFT, CursorMovementSource.DIRECTIONAL_PAD, 
event.get_device (), 1.0);
-               case EventCode.BTN_DPAD_RIGHT:
-                       return move_cursor (Gtk.DirectionType.RIGHT, CursorMovementSource.DIRECTIONAL_PAD, 
event.get_device (), 1.0);
-               default:
-                       return false;
-               }
+               return gamepad_browse.gamepad_button_press_event (event);
        }
 
        public bool gamepad_button_release_event (Manette.Event event) {
                if (!get_mapped ())
                        return false;
 
-               uint16 button;
-               if (!event.get_button (out button))
-                       return false;
-
-               switch (button) {
-               case EventCode.BTN_DPAD_UP:
-                       if (cursor_movement_source != CursorMovementSource.DIRECTIONAL_PAD ||
-                           cursor_movement_device != event.get_device ())
-                               return false;
-
-                       return cancel_cursor_movement_for_direction (Gtk.DirectionType.UP);
-               case EventCode.BTN_DPAD_DOWN:
-                       if (cursor_movement_source != CursorMovementSource.DIRECTIONAL_PAD ||
-                           cursor_movement_device != event.get_device ())
-                               return false;
-
-                       return cancel_cursor_movement_for_direction (Gtk.DirectionType.DOWN);
-               case EventCode.BTN_DPAD_LEFT:
-                       if (cursor_movement_source != CursorMovementSource.DIRECTIONAL_PAD ||
-                           cursor_movement_device != event.get_device ())
-                               return false;
-
-                       return cancel_cursor_movement_for_direction (Gtk.DirectionType.LEFT);
-               case EventCode.BTN_DPAD_RIGHT:
-                       if (cursor_movement_source != CursorMovementSource.DIRECTIONAL_PAD ||
-                           cursor_movement_device != event.get_device ())
-                               return false;
-
-                       return cancel_cursor_movement_for_direction (Gtk.DirectionType.RIGHT);
-               default:
-                       return false;
-               }
+               return gamepad_browse.gamepad_button_release_event (event);
        }
 
        public bool gamepad_absolute_axis_event (Manette.Event event) {
                if (!get_mapped ())
                        return false;
 
-               uint16 axis;
-               double value;
-               if (!event.get_absolute (out axis, out value))
-                       return false;
-
-               // We quare the value to get the speed so the progression is
-               // exponential. No need to compute the absolute value if we square it.
-               switch (axis) {
-               case EventCode.ABS_X:
-                       if (value > DEAD_ZONE)
-                               return move_cursor (Gtk.DirectionType.RIGHT, 
CursorMovementSource.ANALOG_STICK, event.get_device (), value * value);
-                       else if (value < -DEAD_ZONE)
-                               return move_cursor (Gtk.DirectionType.LEFT, 
CursorMovementSource.ANALOG_STICK, event.get_device (), value * value);
-                       else if (cursor_movement_source == CursorMovementSource.ANALOG_STICK &&
-                                cursor_movement_device == event.get_device () &&
-                                (cursor_direction == Gtk.DirectionType.LEFT || cursor_direction == 
Gtk.DirectionType.RIGHT))
-                               cancel_cursor_movement ();
-
-                       return false;
-               case EventCode.ABS_Y:
-                       if (value > DEAD_ZONE)
-                               return move_cursor (Gtk.DirectionType.DOWN, 
CursorMovementSource.ANALOG_STICK, event.get_device (), value * value);
-                       else if (value < -DEAD_ZONE)
-                               return move_cursor (Gtk.DirectionType.UP, CursorMovementSource.ANALOG_STICK, 
event.get_device (), value * value);
-                       else if (cursor_movement_source == CursorMovementSource.ANALOG_STICK &&
-                                cursor_movement_device == event.get_device () &&
-                                (cursor_direction == Gtk.DirectionType.UP || cursor_direction == 
Gtk.DirectionType.DOWN))
-                               cancel_cursor_movement ();
-
-                       return false;
-               case EventCode.ABS_RX:
-                       return false;
-               case EventCode.ABS_RY:
-                       return false;
-               default:
-                       return false;
-               }
+               return gamepad_browse.gamepad_absolute_axis_event (event);
        }
 
        public void reset_scroll_position () {
@@ -204,7 +103,8 @@ private class Games.CollectionIconView : Gtk.Stack {
                adjustment.set_value (0);
        }
 
-       private bool apply_cursor_movement () {
+       [GtkCallback]
+       private bool on_gamepad_browse (Gtk.DirectionType direction) {
                if (flow_box.get_selected_children ().length () == 0) {
                        var first_child = flow_box.get_child_at_index (0);
                        if (first_child == null)
@@ -212,12 +112,12 @@ private class Games.CollectionIconView : Gtk.Stack {
 
                        flow_box.select_child (first_child);
                        // This is needed to start moving the cursor with the gamepad only.
-                       first_child.focus (cursor_direction);
+                       first_child.focus (direction);
 
                        return true;
                }
 
-               switch (cursor_direction) {
+               switch (direction) {
                case Gtk.DirectionType.UP:
                        return flow_box.move_cursor (Gtk.MovementStep.DISPLAY_LINES, -1);
                case Gtk.DirectionType.DOWN:
@@ -231,59 +131,9 @@ private class Games.CollectionIconView : Gtk.Stack {
                }
        }
 
-       private bool move_cursor (Gtk.DirectionType direction, CursorMovementSource source, Manette.Device 
device, double speed) {
-               cursor_movement_source = source;
-               cursor_movement_device = device;
-               cursor_speed = speed;
-
-               if (cursor_timeout != 0 && cursor_direction == direction)
-                       return true;
-
-               if (cursor_timeout != 0)
-                       Source.remove (cursor_timeout);
-
-               cursor_timeout = 0;
-               cursor_direction = direction;
-
-               if (!apply_cursor_movement ())
-                       return false;
-
-               cursor_timeout = Timeout.add (500, setup_cursor_cb);
-
-               return true;
-       }
-
-       private bool setup_cursor_cb () {
-               if (cursor_speed == 0) {
-                       cancel_cursor_movement ();
-
-                       return false;
-               }
-
-               if (!apply_cursor_movement ())
-                       return false;
-
-               cursor_timeout = Timeout.add ((uint) (30 / cursor_speed), setup_cursor_cb);
-
-               return false;
-       }
-
-       private void cancel_cursor_movement () {
-               if (cursor_timeout != 0)
-                       Source.remove (cursor_timeout);
-
-               cursor_movement_source = CursorMovementSource.UNKNOWN;
-               cursor_movement_device = null;
-               cursor_timeout = 0;
-
-               return;
-       }
-
-       private bool cancel_cursor_movement_for_direction (Gtk.DirectionType direction) {
-               if (cursor_direction != direction)
-                       return false;
-
-               cancel_cursor_movement ();
+       [GtkCallback]
+       private bool on_gamepad_accept () {
+               flow_box.activate_cursor_child ();
 
                return true;
        }


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