[gnome-games/wip/aplazas/gamepad-config: 9/26] gamepad: Add GamepadMappingBuilder



commit 91e172361c7a9289e6d85f539fb9b330b8139ab7
Author: theawless <theawless gmail com>
Date:   Wed Jun 21 02:55:48 2017 +0530

    gamepad: Add GamepadMappingBuilder
    
    This will be used to build the sdl string from user's inputs.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=780754

 src/Makefile.am                          |    1 +
 src/gamepad/gamepad-mapping-builder.vala |   93 ++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 9b23d47..4812431 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -80,6 +80,7 @@ gnome_games_SOURCES = \
        gamepad/gamepad.c \
        gamepad/gamepad-dpad.c \
        gamepad/gamepad-input.c \
+       gamepad/gamepad-mapping-builder.vala \
        gamepad/gamepad-mapping.c \
        gamepad/gamepad-mapping-error.c \
        gamepad/gamepad-mappings-manager.c \
diff --git a/src/gamepad/gamepad-mapping-builder.vala b/src/gamepad/gamepad-mapping-builder.vala
new file mode 100644
index 0000000..d9a7a40
--- /dev/null
+++ b/src/gamepad/gamepad-mapping-builder.vala
@@ -0,0 +1,93 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.GamepadMappingBuilder : Object {
+       private GamepadInput[] inputs;
+       private string[] sources = { "a", "b", "x", "y",
+                                    "leftstick", "rightstick",
+                                    "leftshoulder", "rightshoulder", "lefttrigger", "righttrigger",
+                                    "dpup", "dpleft", "dpdown", "dpright",
+                                    "back", "guide", "start",
+                                    "leftx", "rightx", "lefty", "righty" };
+       private string[] destinations;
+
+       construct {
+               destinations = new string[sources.length];
+               inputs = GamepadInput.get_standard_inputs ();
+       }
+
+       public string build_sdl_string () {
+               var sdl_string = "platform:Linux" + ",";
+
+               for (var i = 0; i < destinations.length; ++i)
+                       if (destinations[i] != null)
+                               sdl_string += sources[i] + ":" + destinations[i] + ",";
+
+               return sdl_string;
+       }
+
+       public bool set_button_mapping (uint8 hardware_index, GamepadInput source) {
+               var destination_string = "b" + hardware_index.to_string ();
+
+               return add_destination (destination_string, source);
+       }
+
+       public bool set_axis_mapping (uint8 hardware_index, GamepadInput source) {
+               var destination_string = "a" + hardware_index.to_string ();
+
+               return add_destination (destination_string, source);
+       }
+
+       public bool set_hat_mapping (uint8 hardware_index, int32 hardware_value, GamepadInput source) {
+               var destination_string = "h" + hardware_index.to_string ();
+               destination_string += ".";
+
+               // doesn't work for many cases
+               switch (source.code) {
+               case EventCode.BTN_DPAD_UP:
+               case EventCode.BTN_DPAD_DOWN:
+                       destination_string += (hardware_value == 1) ? "1" : "3";
+
+                       break;
+               case EventCode.BTN_DPAD_LEFT:
+               case EventCode.BTN_DPAD_RIGHT:
+                       destination_string += (hardware_value == 1) ? "4" : "2";
+
+                       break;
+               default:
+                       break;
+               }
+
+               return add_destination (destination_string, source);
+       }
+
+       private bool add_destination (string destination_string, GamepadInput source) {
+               if (index_in_destinations (destination_string) != -1)
+                       return false;
+
+               var i = index_in_inputs (source);
+               if (i == -1) {
+                       warning ("Invalid input");
+
+                       return false;
+               }
+               destinations[i] = destination_string;
+
+               return true;
+       }
+
+       private int index_in_destinations (string destination_string) {
+               for (var i = 0; i < destinations.length ; ++i)
+                       if (destination_string == destinations[i])
+                               return i;
+
+               return -1;
+       }
+
+       private int index_in_inputs (GamepadInput input) {
+               for (var i = 0; i < inputs.length ; ++i)
+                       if (input == inputs[i])
+                               return i;
+
+               return -1;
+       }
+}


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