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



commit ad26dbb2bd2567033d714ecf92b6976220e493ae
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   |   42 ++++++++++++++++++++
 src/ui/collection-box.vala       |   12 ++++++
 src/ui/collection-icon-view.vala |   80 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 134 insertions(+), 0 deletions(-)
---
diff --git a/src/ui/application-window.vala b/src/ui/application-window.vala
index 4c157ef..2d76947 100644
--- a/src/ui/application-window.vala
+++ b/src/ui/application-window.vala
@@ -127,6 +127,10 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
                inhibit_flags = 0;
 
                set_show_menubar (false); // Essential, see bug #771683
+
+               var gamepad_monitor = GamepadMonitor.get_instance ();
+               gamepad_monitor.foreach_gamepad (connect_gamepad);
+               gamepad_monitor.gamepad_plugged.connect (connect_gamepad);
        }
 
        public void run_game (Game game) {
@@ -223,6 +227,44 @@ private class Games.ApplicationWindow : Gtk.ApplicationWindow {
                return false;
        }
 
+       private void on_gamepad_button_press_event (Event event) {
+               switch (ui_state) {
+               case UiState.COLLECTION:
+                       collection_box.gamepad_button_press_event (event);
+
+                       break;
+               }
+       }
+
+       private void on_gamepad_button_release_event (Event event) {
+               switch (ui_state) {
+               case UiState.COLLECTION:
+                       collection_box.gamepad_button_release_event (event);
+
+                       break;
+               }
+       }
+
+       private void on_gamepad_axis_event (Event event) {
+               switch (ui_state) {
+               case UiState.COLLECTION:
+                       collection_box.gamepad_axis_event (event);
+
+                       break;
+               }
+       }
+
+       private void connect_gamepad (Gamepad gamepad) {
+               gamepad.button_press_event.connect (on_gamepad_button_press_event);
+               gamepad.button_release_event.connect (on_gamepad_button_release_event);
+               gamepad.axis_event.connect (on_gamepad_axis_event);
+               gamepad.unplugged.connect (disconnect_gamepad);
+       }
+
+       private void disconnect_gamepad (Gamepad gamepad) {
+               // TODO Disconnect the gamepad?
+       }
+
        [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..7dc0e5a 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 (Event event) {
+               return icon_view.gamepad_button_press_event (event);
+       }
+
+       public bool gamepad_button_release_event (Event event) {
+               return icon_view.gamepad_button_release_event (event);
+       }
+
+       public bool gamepad_axis_event (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..acc1571 100644
--- a/src/ui/collection-icon-view.vala
+++ b/src/ui/collection-icon-view.vala
@@ -52,6 +52,86 @@ private class Games.CollectionIconView : Gtk.Stack {
                flow_box.set_sort_func (sort_boxes);
        }
 
+       public bool gamepad_button_press_event (Event event) {
+               if (!visible)
+                       return false;
+
+               switch (event.gamepad_button.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 (Event event) {
+               return false;
+       }
+
+       public bool gamepad_axis_event (Event event) {
+               if (!visible)
+                       return false;
+
+               switch (event.gamepad_axis.axis) {
+               case EventCode.ABS_X:
+                       if (event.gamepad_axis.value > 0.8)
+                               return flow_box.move_cursor (Gtk.MovementStep.VISUAL_POSITIONS, 1);
+                       else if (event.gamepad_axis.value < -0.8)
+                               return flow_box.move_cursor (Gtk.MovementStep.VISUAL_POSITIONS, -1);
+
+                       return false;
+               case EventCode.ABS_Y:
+                       if (event.gamepad_axis.value > 0.8)
+                               return flow_box.move_cursor (Gtk.MovementStep.DISPLAY_LINES, 1);
+                       else if (event.gamepad_axis.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]