[gnome-games/wip/exalm/dpad: 41/43] gamepad-mapping-builder: Support binding half axes



commit 780a17ebffb9162de6047f9e8f98182727704074
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Fri Oct 12 02:06:10 2018 +0500

    gamepad-mapping-builder: Support binding half axes
    
    Add a 'range' parameter to set_axis_mapping() that allows to only bind
    positive or negative half.

 src/gamepad/gamepad-mapping-builder.vala | 36 ++++++++++++++++++++++++++++++--
 src/ui/gamepad-mapper.vala               |  2 +-
 2 files changed, 35 insertions(+), 3 deletions(-)
---
diff --git a/src/gamepad/gamepad-mapping-builder.vala b/src/gamepad/gamepad-mapping-builder.vala
index ddd25724..a552b58d 100644
--- a/src/gamepad/gamepad-mapping-builder.vala
+++ b/src/gamepad/gamepad-mapping-builder.vala
@@ -35,12 +35,19 @@ private class Games.GamepadMappingBuilder : Object {
                string destination_string;
        }
 
+       private struct GamepadAxisMapping {
+               bool positive_used;
+               bool negative_used;
+       }
+
        private GamepadInputMapping[] mappings;
        private GamepadDPad[] dpads;
+       private HashTable<string, GamepadAxisMapping?> axes;
 
        construct {
                mappings = new GamepadInputMapping[]{};
                dpads = new GamepadDPad[]{};
+               axes = new HashTable<string, GamepadAxisMapping?> (str_hash, str_equal);
        }
 
        public string build_sdl_string () {
@@ -56,8 +63,33 @@ private class Games.GamepadMappingBuilder : Object {
                return add_destination (@"b$(hardware_index)", source);
        }
 
-       public bool set_axis_mapping (uint8 hardware_index, GamepadInput source) {
-               return add_destination (@"a$(hardware_index)", source);
+       public bool set_axis_mapping (uint8 hardware_index, int range, GamepadInput source) {
+               var destination = @"a$(hardware_index)";
+
+               var positive = range >= 0;
+               var negative = range <= 0;
+
+               if (!(destination in axes))
+                       axes[destination] = { false, false };
+
+               var mapping = axes[destination];
+
+               if ((positive && mapping.positive_used) ||
+                   (negative && mapping.negative_used))
+                       return false;
+
+               axes[destination] = {
+                       mapping.positive_used || positive,
+                       mapping.negative_used || negative
+               };
+
+               if (range > 0)
+                       return add_destination (@"+$(destination)", source);
+
+               if (range < 0)
+                       return add_destination (@"-$(destination)", source);
+
+               return add_destination (destination, source);
        }
 
        public bool set_hat_mapping (uint8 hardware_index, int32 value, GamepadInput source) {
diff --git a/src/ui/gamepad-mapper.vala b/src/ui/gamepad-mapper.vala
index 93eb3bdc..2af64125 100644
--- a/src/ui/gamepad-mapper.vala
+++ b/src/ui/gamepad-mapper.vala
@@ -77,7 +77,7 @@ private class Games.GamepadMapper : Gtk.Bin {
                                return;
 
                        if (!mapping_builder.set_axis_mapping ((uint8) event.get_hardware_index (),
-                                                              input))
+                                                              0, input))
                                return;
 
                        break;


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