[gnome-games/wip/abhinavsingh/gamepad-config: 11/23] gamepad: Add GamepadMappingBuilder
- From: Abhinav Singh <abhinavsingh src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games/wip/abhinavsingh/gamepad-config: 11/23] gamepad: Add GamepadMappingBuilder
- Date: Wed, 12 Jul 2017 12:32:54 +0000 (UTC)
commit 23b7b65fe8280f4caf513d4abf95555df5f23a12
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 | 88 ++++++++++++++++++++++++++++++
2 files changed, 89 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 71fcc08..6859e85 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -81,6 +81,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..d3f53e9
--- /dev/null
+++ b/src/gamepad/gamepad-mapping-builder.vala
@@ -0,0 +1,88 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.GamepadMappingBuilder : Object {
+ 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;
+ private GamepadDPad[] dpads;
+
+ construct {
+ destinations = new string[sources.length];
+ dpads = new GamepadDPad[]{};
+ }
+
+ 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, uint16 axis, GamepadInput
source) {
+ var destination_string = "h" + hardware_index.to_string ();
+ destination_string += ".";
+
+ while (dpads.length <= hardware_index)
+ dpads += GamepadDPad ();
+ var dpad = dpads[hardware_index];
+ var changed_value = hardware_value == 0 ? dpad.axis_values[axis] : hardware_value;
+ dpad.axis_values[axis] = hardware_value;
+
+ // add 4 so the remainder is positive
+ var dpad_position = (changed_value + axis + 4) % 4;
+ var dpad_position_2pow = Math.pow (2, dpad_position);
+ destination_string += dpad_position_2pow.to_string ();
+
+ 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 < STANDARD_GAMEPAD_INPUTS.length ; ++i)
+ if (input == STANDARD_GAMEPAD_INPUTS[i])
+ return i;
+
+ return -1;
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]