[gnome-games/wip/aplazas/gamepad: 70/70] retro: Use the new Gamepad class from retro-gtk



commit 15176f77a0d2c79e653e6ed7469db10e0f0a949a
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Tue Jul 25 14:39:43 2017 +0200

    retro: Use the new Gamepad class from retro-gtk
    
    RetroGamepad has partially been moved to the new Gamepad class of
    retro-gtk, using it makes the code simpler.

 flatpak/org.gnome.Games.json       |    3 +-
 src/retro/retro-gamepad.vala       |  104 +++---------------------------------
 src/retro/retro-input-manager.vala |    6 +-
 3 files changed, 12 insertions(+), 101 deletions(-)
---
diff --git a/flatpak/org.gnome.Games.json b/flatpak/org.gnome.Games.json
index fd6d6e6..beeef8e 100644
--- a/flatpak/org.gnome.Games.json
+++ b/flatpak/org.gnome.Games.json
@@ -128,7 +128,8 @@
             "sources": [
                 {
                     "type": "git",
-                    "url": "https://git.gnome.org/browse/retro-gtk";
+                    "url": "https://git.gnome.org/browse/retro-gtk";,
+                    "branch": "wip/aplazas/gamepad"
                 }
             ]
         },
diff --git a/src/retro/retro-gamepad.vala b/src/retro/retro-gamepad.vala
index 9c619bf..6cb60a6 100644
--- a/src/retro/retro-gamepad.vala
+++ b/src/retro/retro-gamepad.vala
@@ -1,110 +1,20 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
-private class Games.RetroGamepad: Object, Retro.InputDevice {
+private class Games.RetroGamepad: Object {
        public Gamepad gamepad { get; construct; }
        public bool present_analog_sticks { get; construct; }
-
-       private bool[] buttons;
-       private int16[] axes;
+       public Retro.Gamepad retro_input_device { get; construct; }
 
        public RetroGamepad (Gamepad gamepad, bool present_analog_sticks) {
                Object (gamepad: gamepad, present_analog_sticks: present_analog_sticks);
        }
 
        construct {
-               buttons = new bool[EventCode.KEY_MAX + 1];
-               axes = new int16[EventCode.ABS_MAX + 1];
-
-               gamepad.button_press_event.connect ((event) => buttons[event.gamepad_button.button] = true);
-               gamepad.button_release_event.connect ((event) => buttons[event.gamepad_button.button] = 
false);
-               gamepad.axis_event.connect ((event) => axes[event.gamepad_axis.axis] = (int16) 
(event.gamepad_axis.value * int16.MAX));
-       }
-
-       public void poll () {}
-
-       public int16 get_input_state (Retro.DeviceType device, uint index, uint id) {
-               switch (device) {
-               case Retro.DeviceType.JOYPAD:
-                       return get_button_pressed ((Retro.JoypadId) id) ? int16.MAX : 0;
-               case Retro.DeviceType.ANALOG:
-                       return get_analog_value ((Retro.AnalogIndex) index, (Retro.AnalogId) id);
-               default:
-                       return 0;
-               }
-       }
-
-       public Retro.DeviceType get_device_type () {
-               if (present_analog_sticks)
-                       return Retro.DeviceType.ANALOG;
-
-               return Retro.DeviceType.JOYPAD;
-       }
-
-       public uint64 get_device_capabilities () {
-               return (1 << Retro.DeviceType.JOYPAD) | (1 << Retro.DeviceType.ANALOG);
-       }
-
-       public bool get_button_pressed (Retro.JoypadId button) {
-               switch (button) {
-               case Retro.JoypadId.B:
-                       return buttons[EventCode.BTN_A];
-               case Retro.JoypadId.Y:
-                       return buttons[EventCode.BTN_Y];
-               case Retro.JoypadId.SELECT:
-                       return buttons[EventCode.BTN_SELECT];
-               case Retro.JoypadId.START:
-                       return buttons[EventCode.BTN_START];
-               case Retro.JoypadId.UP:
-                       return buttons[EventCode.BTN_DPAD_UP];
-               case Retro.JoypadId.DOWN:
-                       return buttons[EventCode.BTN_DPAD_DOWN];
-               case Retro.JoypadId.LEFT:
-                       return buttons[EventCode.BTN_DPAD_LEFT];
-               case Retro.JoypadId.RIGHT:
-                       return buttons[EventCode.BTN_DPAD_RIGHT];
-               case Retro.JoypadId.A:
-                       return buttons[EventCode.BTN_B];
-               case Retro.JoypadId.X:
-                       return buttons[EventCode.BTN_X];
-               case Retro.JoypadId.L:
-                       return buttons[EventCode.BTN_TL];
-               case Retro.JoypadId.R:
-                       return buttons[EventCode.BTN_TR];
-               case Retro.JoypadId.L2:
-                       return buttons[EventCode.BTN_TL2];
-               case Retro.JoypadId.R2:
-                       return buttons[EventCode.BTN_TR2];
-               case Retro.JoypadId.L3:
-                       return buttons[EventCode.BTN_THUMBL];
-               case Retro.JoypadId.R3:
-                       return buttons[EventCode.BTN_THUMBR];
-               default:
-                       return false;
-               }
-       }
+               var retro_gamepad = new Retro.Gamepad (present_analog_sticks);
+               retro_input_device = retro_gamepad;
 
-       public int16 get_analog_value (Retro.AnalogIndex index, Retro.AnalogId id) {
-               switch (index) {
-               case Retro.AnalogIndex.LEFT:
-                       switch (id) {
-                       case Retro.AnalogId.X:
-                               return axes[EventCode.ABS_X];
-                       case Retro.AnalogId.Y:
-                               return axes[EventCode.ABS_Y];
-                       default:
-                               return 0;
-                       }
-               case Retro.AnalogIndex.RIGHT:
-                       switch (id) {
-                       case Retro.AnalogId.X:
-                               return axes[EventCode.ABS_RX];
-                       case Retro.AnalogId.Y:
-                               return axes[EventCode.ABS_RY];
-                       default:
-                               return 0;
-                       }
-               default:
-                       return 0;
-               }
+               gamepad.button_press_event.connect ((event) => retro_gamepad.button_press_event 
(event.gamepad_button.button));
+               gamepad.button_release_event.connect ((event) => retro_gamepad.button_release_event 
(event.gamepad_button.button));
+               gamepad.axis_event.connect ((event) => retro_gamepad.axis_event (event.gamepad_axis.axis, 
event.gamepad_axis.value));
        }
 }
diff --git a/src/retro/retro-input-manager.vala b/src/retro/retro-input-manager.vala
index 26fdc52..09ce2cc 100644
--- a/src/retro/retro-input-manager.vala
+++ b/src/retro/retro-input-manager.vala
@@ -3,7 +3,7 @@
 private class Games.RetroInputManager : Retro.InputDeviceManager, Retro.Rumble {
        private Retro.VirtualGamepad keyboard;
        private GamepadMonitor gamepad_monitor;
-       private Retro.InputDevice?[] input_devices;
+       private Object?[] input_devices;
        private int keyboard_port;
        private bool present_analog_sticks;
 
@@ -18,7 +18,7 @@ private class Games.RetroInputManager : Retro.InputDeviceManager, Retro.Rumble {
                        var port = input_devices.length;
                        var retro_gamepad = new RetroGamepad (gamepad, present_analog_sticks);
                        input_devices += retro_gamepad;
-                       set_controller_device (port, retro_gamepad);
+                       set_controller_device (port, retro_gamepad.retro_input_device);
                        gamepad.unplugged.connect (() => handle_gamepad_unplugged (port));
                });
 
@@ -33,7 +33,7 @@ private class Games.RetroInputManager : Retro.InputDeviceManager, Retro.Rumble {
                var port = keyboard_port;
                var retro_gamepad = new RetroGamepad (gamepad, present_analog_sticks);
                input_devices[port] = retro_gamepad;
-               set_controller_device (port, retro_gamepad);
+               set_controller_device (port, retro_gamepad.retro_input_device);
                gamepad.unplugged.connect (() => handle_gamepad_unplugged (port));
 
                // Assign keyboard to another unplugged port if exists and return


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