[gnome-games/wip/aplazas/gamepad-ui: 65/65] ui: Navigate the collection with gamepads



commit 06ac818fb6950ca38f1c33fdc531b0353f111708
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Sun Sep 10 21:05:38 2017 +0200

    ui: Navigate the collection with gamepads

 src/ui/application-window.vala   |   86 ++++++++++++++++++++++++++++++++++++
 src/ui/collection-box.vala       |   12 +++++
 src/ui/collection-icon-view.vala |   89 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+), 0 deletions(-)
---
diff --git a/src/ui/application-window.vala b/src/ui/application-window.vala
index 4c157ef..8d2fd2b 100644
--- a/src/ui/application-window.vala
+++ b/src/ui/application-window.vala
@@ -90,6 +90,8 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
        private uint inhibit_cookie;
        private Gtk.ApplicationInhibitFlags inhibit_flags;
 
+       private Manette.Monitor manette_monitor;
+
        public ApplicationWindow (ListModel collection) {
                collection_box.collection = collection;
        }
@@ -127,6 +129,12 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
                inhibit_flags = 0;
 
                set_show_menubar (false); // Essential, see bug #771683
+
+               manette_monitor = new Manette.Monitor ();
+               var manette_iterator = manette_monitor.iterate ();
+               Manette.Device manette_device = null;
+               while (manette_iterator.next (out manette_device))
+                       on_device_connected (manette_device);
        }
 
        public void run_game (Game game) {
@@ -223,6 +231,84 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
                return false;
        }
 
+       private void on_gamepad_button_press_event (Manette.Event event) {
+               switch (ui_state) {
+               case UiState.COLLECTION:
+                       collection_box.gamepad_button_press_event (event);
+
+                       break;
+               }
+       }
+
+       private void on_gamepad_button_release_event (Manette.Event event) {
+               switch (ui_state) {
+               case UiState.COLLECTION:
+                       collection_box.gamepad_button_release_event (event);
+
+                       break;
+               }
+       }
+
+       private void on_gamepad_absolute_axis_event (Manette.Event event) {
+               switch (ui_state) {
+               case UiState.COLLECTION:
+                       collection_box.gamepad_axis_event (event);
+
+                       break;
+               }
+       }
+
+
+       private void on_device_connected (Manette.Device device) {
+               // Plug this device to the port where the CoreView's joypad was
+               // connected as a last resort.
+               // var port = core_view_joypad_port;
+               // var retro_gamepad = new RetroGamepad (device, present_analog_sticks);
+               // controllers[port] = retro_gamepad;
+               // core.set_controller (port, retro_gamepad);
+               // device.disconnected.connect (() => on_device_disconnected (port));
+
+               // Assign the CoreView's joypad to another disconnected port if it
+               // exists and return.
+               // for (var i = core_view_joypad_port; i < controllers.length; i++) {
+               //      if (controllers[i] == null) {
+                               // Found an disconnected port and so assigning core_view_joypad to it
+               //              core_view_joypad_port = i;
+               //              controllers[core_view_joypad_port] = core_view_joypad;
+               //              core.set_controller (core_view_joypad_port, core_view_joypad);
+
+               //              return;
+               //      }
+               // }
+
+               // Now it means that there is no disconnected port so append the
+               // CoreView's joypad to ports.
+               // core_view_joypad_port = controllers.length;
+               // controllers += core_view_joypad;
+               // core.set_controller (core_view_joypad_port, core_view_joypad);
+               device.button_press_event.connect (on_gamepad_button_press_event);
+               device.button_release_event.connect (on_gamepad_button_release_event);
+               device.absolute_axis_event.connect (on_gamepad_absolute_axis_event);
+               device.disconnected.connect (on_device_disconnected);
+       }
+
+       private void on_device_disconnected () {
+               // if (core_view_joypad_port > port) {
+                       // Remove the controller and shift the CoreView's joypad to
+                       // "lesser" port.
+               //      controllers[core_view_joypad_port] = null;
+               //      core_view_joypad_port = port;
+               //      controllers[core_view_joypad_port] = core_view_joypad;
+               //      core.set_controller (core_view_joypad_port, core_view_joypad);
+               // }
+               // else {
+                       // Just remove the controller as no need to shift the
+                       // CoreView's joypad.
+               //      controllers[port] = null;
+               //      core.set_controller (port, null);
+               // }
+       }
+
        [GtkCallback]
        private void on_game_activated (Game game) {
                run_game (game);
diff --git a/src/ui/collection-box.vala b/src/ui/collection-box.vala
index c54f480..3339548 100644
--- a/src/ui/collection-box.vala
+++ b/src/ui/collection-box.vala
@@ -32,6 +32,18 @@ private class Games.CollectionBox : Gtk.Box {
                                                              BindingFlags.DEFAULT);
        }
 
+       public bool gamepad_button_press_event (Manette.Event event) {
+               return icon_view.gamepad_button_press_event (event);
+       }
+
+       public bool gamepad_button_release_event (Manette.Event event) {
+               return icon_view.gamepad_button_release_event (event);
+       }
+
+       public bool gamepad_axis_event (Manette.Event event) {
+               return icon_view.gamepad_axis_event (event);
+       }
+
        [GtkCallback]
        private void on_loading_notification_closed () {
                loading_notification_revealer.set_reveal_child (false);
diff --git a/src/ui/collection-icon-view.vala b/src/ui/collection-icon-view.vala
index 59cc50f..8f60eca 100644
--- a/src/ui/collection-icon-view.vala
+++ b/src/ui/collection-icon-view.vala
@@ -52,6 +52,95 @@ private class Games.CollectionIconView : Gtk.Stack {
                flow_box.set_sort_func (sort_boxes);
        }
 
+       public bool gamepad_button_press_event (Manette.Event event) {
+               if (!visible)
+                       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_B:
+                       return false;
+               case EventCode.BTN_X:
+                       return false;
+               case EventCode.BTN_Y:
+                       return false;
+               case EventCode.BTN_TL:
+                       return false;
+               case EventCode.BTN_TR:
+                       return false;
+               case EventCode.BTN_TL2:
+                       return false;
+               case EventCode.BTN_TR2:
+                       return false;
+               case EventCode.BTN_SELECT:
+                       return false;
+               case EventCode.BTN_START:
+                       flow_box.activate_cursor_child ();
+
+                       return true;
+               case EventCode.BTN_MODE:
+                       return false;
+               case EventCode.BTN_THUMBL:
+                       return false;
+               case EventCode.BTN_THUMBR:
+                       return false;
+               case EventCode.BTN_DPAD_UP:
+                       return flow_box.move_cursor (Gtk.MovementStep.DISPLAY_LINES, -1);
+               case EventCode.BTN_DPAD_DOWN:
+                       return flow_box.move_cursor (Gtk.MovementStep.DISPLAY_LINES, 1);
+               case EventCode.BTN_DPAD_LEFT:
+                       return flow_box.move_cursor (Gtk.MovementStep.VISUAL_POSITIONS, -1);
+               case EventCode.BTN_DPAD_RIGHT:
+                       return flow_box.move_cursor (Gtk.MovementStep.VISUAL_POSITIONS, 1);
+               default:
+                       return false;
+               }
+       }
+
+       public bool gamepad_button_release_event (Manette.Event event) {
+               return false;
+       }
+
+       public bool gamepad_axis_event (Manette.Event event) {
+               if (!visible)
+                       return false;
+
+               uint16 axis;
+               double value;
+               if (event.get_absolute (out axis, out value))
+                       return false;
+
+               switch (axis) {
+               case EventCode.ABS_X:
+                       if (value > 0.8)
+                               return flow_box.move_cursor (Gtk.MovementStep.VISUAL_POSITIONS, 1);
+                       else if (value < -0.8)
+                               return flow_box.move_cursor (Gtk.MovementStep.VISUAL_POSITIONS, -1);
+
+                       return false;
+               case EventCode.ABS_Y:
+                       if (value > 0.8)
+                               return flow_box.move_cursor (Gtk.MovementStep.DISPLAY_LINES, 1);
+                       else if (value < -0.8)
+                               return flow_box.move_cursor (Gtk.MovementStep.DISPLAY_LINES, -1);
+
+                       return false;
+               case EventCode.ABS_RX:
+                       return false;
+               case EventCode.ABS_RY:
+                       return false;
+               default:
+                       return false;
+               }
+       }
+
        [GtkCallback]
        private void on_child_activated (Gtk.FlowBoxChild child) {
                if (child.get_child () is GameIconView)


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