[gnome-games/wip/abhinavsingh/gamepad-config2: 20/20] temp patch



commit 650d0c624f5c07bbaa6e9c96e1656edd23a1212d
Author: theawless <theawless gmail com>
Date:   Mon Jul 17 15:03:10 2017 +0530

    temp patch

 data/ui/gamepad-mapper.ui                |   15 +-----
 src/Makefile.am                          |    2 +-
 src/gamepad/gamepad-mapping-builder.vala |   78 ++++++++++++++++++++---------
 src/gamepad/standard-gamepad-inputs.c    |   17 ++++++
 src/gamepad/standard-gamepad-inputs.vala |   28 -----------
 src/ui/gamepad-mapper.vala               |   79 +++++++++---------------------
 src/ui/gamepad-view-configuration.vala   |   49 ++++++++++++++++++
 src/ui/gamepad-view.vala                 |   59 +++++++++++-----------
 src/ui/preferences-window.vala           |    1 +
 9 files changed, 176 insertions(+), 152 deletions(-)
---
diff --git a/data/ui/gamepad-mapper.ui b/data/ui/gamepad-mapper.ui
index 673f8c6..f1efcc8 100644
--- a/data/ui/gamepad-mapper.ui
+++ b/data/ui/gamepad-mapper.ui
@@ -10,20 +10,7 @@
     <property name="margin-start">48</property>
     <property name="margin-end">48</property>
     <child>
-      <object class="GtkLabel" id="info_message">
-        <property name="visible">True</property>
-      </object>
-    </child>
-    <child>
-      <object class="GtkLabel" id="error_message">
-        <property name="visible">True</property>
-        <attributes>
-          <attribute name="foreground" value="red"/>
-        </attributes>
-      </object>
-    </child>
-    <child>
-      <object class="GamesGamepadView" id="gamepad_view">
+      <object class="GtkBox" id="gamepad_view_holder">
         <property name="halign">center</property>
         <property name="visible">True</property>
         <property name="halign">fill</property>
diff --git a/src/Makefile.am b/src/Makefile.am
index 98f6923..a8ceb49 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -90,7 +90,6 @@ gnome_games_SOURCES = \
        gamepad/gamepad-monitor.c \
        gamepad/raw-gamepad.c \
        gamepad/raw-gamepad-monitor.c \
-       gamepad/standard-gamepad-inputs.vala \
        \
        generic/generic-game.vala \
        generic/generic-game-uri-adapter.vala \
@@ -131,6 +130,7 @@ gnome_games_SOURCES = \
        ui/error-info-bar.vala \
        ui/gamepad-mapper.vala \
        ui/gamepad-view.vala \
+       ui/gamepad-view-configuration.vala \
        ui/game-icon-view.vala \
        ui/game-thumbnail.vala \
        ui/media-selector.vala \
diff --git a/src/gamepad/gamepad-mapping-builder.vala b/src/gamepad/gamepad-mapping-builder.vala
index d3f53e9..ee8678f 100644
--- a/src/gamepad/gamepad-mapping-builder.vala
+++ b/src/gamepad/gamepad-mapping-builder.vala
@@ -1,26 +1,53 @@
 // 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 struct GamepadInputSource {
+               GamepadInput input;
+               string source;
+       }
+
+       private const GamepadInputSource[] input_sources = {
+               { { EventCode.EV_ABS, EventCode.ABS_X }, "leftx" },
+               { { EventCode.EV_ABS, EventCode.ABS_Y }, "lefty" },
+               { { EventCode.EV_ABS, EventCode.ABS_RX }, "rightx" },
+               { { EventCode.EV_ABS, EventCode.ABS_RY }, "righty" },
+               { { EventCode.EV_KEY, EventCode.BTN_A }, "a" },
+               { { EventCode.EV_KEY, EventCode.BTN_B }, "b" },
+               { { EventCode.EV_KEY, EventCode.BTN_DPAD_DOWN }, "dpdown" },
+               { { EventCode.EV_KEY, EventCode.BTN_DPAD_LEFT }, "dpleft" },
+               { { EventCode.EV_KEY, EventCode.BTN_DPAD_RIGHT }, "dpright" },
+               { { EventCode.EV_KEY, EventCode.BTN_DPAD_UP }, "dpup" },
+               { { EventCode.EV_KEY, EventCode.BTN_MODE }, "guide" },
+               { { EventCode.EV_KEY, EventCode.BTN_SELECT }, "back" },
+               { { EventCode.EV_KEY, EventCode.BTN_TL }, "leftshoulder" },
+               { { EventCode.EV_KEY, EventCode.BTN_TR }, "rightshoulder" },
+               { { EventCode.EV_KEY, EventCode.BTN_START }, "start" },
+               { { EventCode.EV_KEY, EventCode.BTN_THUMBL }, "leftstick" },
+               { { EventCode.EV_KEY, EventCode.BTN_THUMBR }, "rightstick" },
+               { { EventCode.EV_KEY, EventCode.BTN_TL2 }, "lefttrigger" },
+               { { EventCode.EV_KEY, EventCode.BTN_TR2 }, "righttrigger" },
+               { { EventCode.EV_KEY, EventCode.BTN_Y }, "x" },
+               { { EventCode.EV_KEY, EventCode.BTN_X }, "y" },
+       };
+
+       private struct GamepadInputMapping {
+               string source_string;
+               string destination_string;
+       }
+
+       private GamepadInputMapping[] mappings;
        private GamepadDPad[] dpads;
 
        construct {
-               destinations = new string[sources.length];
+               mappings = new GamepadInputMapping[]{};
                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] + ",";
+               foreach (var mapping in mappings)
+                       sdl_string += mapping.source_string + ":" + mapping.destination_string + ",";
 
                return sdl_string;
        }
@@ -56,33 +83,34 @@ private class Games.GamepadMappingBuilder : Object {
        }
 
        private bool add_destination (string destination_string, GamepadInput source) {
-               if (index_in_destinations (destination_string) != -1)
+               if (is_mapping_present (destination_string))
                        return false;
 
-               var i = index_in_inputs (source);
-               if (i == -1) {
+               var source_string = get_mapping_source (source);
+               if (source_string == null) {
                        warning ("Invalid input");
 
                        return false;
                }
-               destinations[i] = destination_string;
+               GamepadInputMapping mapping = {source_string, destination_string};
+               mappings += mapping;
 
                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;
+       private bool is_mapping_present (string destination_string) {
+               foreach (var mapping in mappings)
+                       if (destination_string == mapping.destination_string)
+                               return true;
 
-               return -1;
+               return false;
        }
 
-       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;
+       private string? get_mapping_source (GamepadInput input) {
+               foreach (var input_source in input_sources)
+                       if (input == input_source.input)
+                               return input_source.source;
 
-               return -1;
+               return null;
        }
 }
diff --git a/src/gamepad/standard-gamepad-inputs.c b/src/gamepad/standard-gamepad-inputs.c
new file mode 100644
index 0000000..df46f48
--- /dev/null
+++ b/src/gamepad/standard-gamepad-inputs.c
@@ -0,0 +1,17 @@
+/* standard-gamepad-inputs.c generated by valac 0.37.1.4-3591e, the Vala compiler
+ * generated from standard-gamepad-inputs.vala, do not modify */
+
+/* This file is part of GNOME Games. License: GPL-3.0+.*/
+
+#include <glib.h>
+#include <glib-object.h>
+#include <gamepad-input.h>
+#include <linux/input-event-codes.h>
+
+
+
+
+
+const GamesGamepadInput GAMES_STANDARD_GAMEPAD_INPUTS[21] = {{EV_KEY, BTN_A}, {EV_KEY, BTN_B}, {EV_KEY, 
BTN_X}, {EV_KEY, BTN_Y}, {EV_KEY, BTN_THUMBL}, {EV_KEY, BTN_THUMBR}, {EV_KEY, BTN_TL}, {EV_KEY, BTN_TR}, 
{EV_KEY, BTN_TL2}, {EV_KEY, BTN_TR2}, {EV_KEY, BTN_DPAD_UP}, {EV_KEY, BTN_DPAD_LEFT}, {EV_KEY, 
BTN_DPAD_DOWN}, {EV_KEY, BTN_DPAD_RIGHT}, {EV_KEY, BTN_SELECT}, {EV_KEY, BTN_MODE}, {EV_KEY, BTN_START}, 
{EV_ABS, ABS_X}, {EV_ABS, ABS_RX}, {EV_ABS, ABS_Y}, {EV_ABS, ABS_RY}};
+
+
diff --git a/src/ui/gamepad-mapper.vala b/src/ui/gamepad-mapper.vala
index ef58916..19a0d64 100644
--- a/src/ui/gamepad-mapper.vala
+++ b/src/ui/gamepad-mapper.vala
@@ -13,9 +13,9 @@ private class Games.GamepadMapper : Gtk.Box {
        private State? _state;
        private State? state {
                set {
-                       error_message.label = "";
                        if (value == _state)
                                return;
+                       gamepad_view.reset ();
 
                        switch (value) {
                        case State.BEGIN:
@@ -45,21 +45,21 @@ private class Games.GamepadMapper : Gtk.Box {
                set {
                        _input = value;
                        if (_input == null) {
-                               info_message.label = "";
+                               subtitle = "";
 
                                return;
                        }
 
                        switch (input.type) {
                        case EventCode.EV_KEY:
-                               info_message.label = _("Press suitable button on your gamepad");
+                               subtitle = _("Press suitable button on your gamepad");
 
                                break;
                        case EventCode.EV_ABS:
                                if (input.code == EventCode.ABS_X || input.code == EventCode.ABS_RX)
-                                       info_message.label = _("Move suitable axis left/right on your 
gamepad");
+                                       subtitle = _("Move suitable axis left/right on your gamepad");
                                else if (input.code == EventCode.ABS_Y || input.code == EventCode.ABS_RY)
-                                       info_message.label = _("Move suitable axis up/down on your gamepad");
+                                       subtitle = _("Move suitable axis up/down on your gamepad");
 
                                break;
                        default:
@@ -67,15 +67,10 @@ private class Games.GamepadMapper : Gtk.Box {
                        }
                }
                get { return _input; }
-
        }
 
        [GtkChild]
-       private Gtk.Label info_message;
-       [GtkChild]
-       private Gtk.Label error_message;
-       [GtkChild]
-       private GamepadView gamepad_view;
+       private Gtk.Box gamepad_view_holder;
        [GtkChild]
        public Gtk.Box header_content;
        [GtkChild]
@@ -84,36 +79,23 @@ private class Games.GamepadMapper : Gtk.Box {
        private Gtk.Button skip_button;
        [GtkChild]
        private Gtk.Label title;
+       private string subtitle;
 
        private Gamepad gamepad;
+       private GamepadView gamepad_view;
        private GamepadMappingBuilder mapping_builder;
        private ulong gamepad_event_handler_id;
-       private uint error_message_hider_id;
-
-       construct {
-               error_message.notify["label"].connect (() => {
-                       if (error_message.label != "")
-                               error_message_hide ();
-               });
-       }
+       private GamepadInputPath[] input_paths;
+       private uint current_input_index;
 
        public GamepadMapper (Gamepad gamepad) {
                this.gamepad = gamepad;
+               gamepad_view = new GamepadView (STANDARD_GAMEPAD_VIEW_CONFIGURATION);
+               input_paths = STANDARD_GAMEPAD_INPUT_PATHS;
+
                mapping_builder = new GamepadMappingBuilder ();
                state = State.BEGIN;
-       }
-
-       private void error_message_hide () {
-               if (error_message_hider_id > 0) {
-                       GLib.Source.remove (error_message_hider_id);
-               }
-               error_message_hider_id = GLib.Timeout.add (2000, () => {
-                       error_message_hider_id = 0;
-                       if (error_message != null)
-                               error_message.label = "";
-
-                       return false;
-               });
+               current_input_index = 0;
        }
 
        public void start () {
@@ -168,39 +150,28 @@ private class Games.GamepadMapper : Gtk.Box {
        }
 
        private void on_button_event (EventGamepadButton event) {
-               if (input.type == EventCode.EV_ABS) {
-                       error_message.label = _("This combination is not valid");
-
+               if (input.type == EventCode.EV_ABS)
                        return;
-               }
 
                var success = mapping_builder.set_button_mapping (event.gamepad_button.hardware_index,
                                                                  input);
-               if (!success) {
-                       error_message.label = _("This button is already in use");
-
+               if (!success)
                        return;
-               }
 
                next_input ();
        }
 
        private void on_axis_event (EventGamepadAxis event) {
-               if (input.type == EventCode.EV_KEY) {
-                       error_message.label = _("This combination is not valid");
-
+               if (input.type == EventCode.EV_KEY)
                        return;
-               }
+
                if (-0.8 < event.gamepad_axis.value && event.gamepad_axis.value < 0.8)
                        return;
 
                var success = mapping_builder.set_axis_mapping (event.gamepad_axis.hardware_index,
                                                                input);
-               if (!success) {
-                       error_message.label = _("This axis is already in use");
-
+               if (!success)
                        return;
-               }
 
                next_input ();
        }
@@ -213,24 +184,22 @@ private class Games.GamepadMapper : Gtk.Box {
                                                               event.gamepad_hat.value,
                                                               event.gamepad_hat.axis,
                                                               input);
-               if (!success) {
-                       error_message.label = _("This hat is already in use");
-
+               if (!success)
                        return;
-               }
 
                next_input ();
        }
 
        private void next_input () {
-               input = gamepad_view.highlight_next ();
-               if (input == null) {
+               if (current_input_index >= input_paths.length) {
                        state = State.FINISH;
 
                        return;
                }
-
                state = State.CONFIGURE;
+
+               input = input_paths[current_input_index++].input;
+               gamepad_view.highlight (input, true);
        }
 
        private void save_mapping () {
diff --git a/src/ui/gamepad-view-configuration.vala b/src/ui/gamepad-view-configuration.vala
new file mode 100644
index 0000000..533986a
--- /dev/null
+++ b/src/ui/gamepad-view-configuration.vala
@@ -0,0 +1,49 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private struct Games.GamepadInputPath {
+       GamepadInput input;
+       string path;
+}
+
+private struct Games.GamepadViewConfiguration {
+       string svg_path;
+       GamepadInputPath[] input_paths;
+
+       public string? get_input_path (GamepadInput input) {
+               foreach (var input_path in input_paths)
+                       if (input == input_path.input)
+                               return input_path.path;
+
+               return null;
+       }
+}
+
+namespace Games {
+       private const GamepadInputPath[] STANDARD_GAMEPAD_INPUT_PATHS = {
+               { { EventCode.EV_ABS, EventCode.ABS_X }, "leftx" },
+               { { EventCode.EV_ABS, EventCode.ABS_Y }, "lefty" },
+               { { EventCode.EV_ABS, EventCode.ABS_RX }, "rightx" },
+               { { EventCode.EV_ABS, EventCode.ABS_RY }, "righty" },
+               { { EventCode.EV_KEY, EventCode.BTN_A }, "a" },
+               { { EventCode.EV_KEY, EventCode.BTN_B }, "b" },
+               { { EventCode.EV_KEY, EventCode.BTN_DPAD_DOWN }, "dpdown" },
+               { { EventCode.EV_KEY, EventCode.BTN_DPAD_LEFT }, "dpleft" },
+               { { EventCode.EV_KEY, EventCode.BTN_DPAD_RIGHT }, "dpright" },
+               { { EventCode.EV_KEY, EventCode.BTN_DPAD_UP }, "dpup" },
+               { { EventCode.EV_KEY, EventCode.BTN_MODE }, "guide" },
+               { { EventCode.EV_KEY, EventCode.BTN_SELECT }, "back" },
+               { { EventCode.EV_KEY, EventCode.BTN_TL }, "leftshoulder" },
+               { { EventCode.EV_KEY, EventCode.BTN_TR }, "rightshoulder" },
+               { { EventCode.EV_KEY, EventCode.BTN_START }, "start" },
+               { { EventCode.EV_KEY, EventCode.BTN_THUMBL }, "leftstick" },
+               { { EventCode.EV_KEY, EventCode.BTN_THUMBR }, "rightstick" },
+               { { EventCode.EV_KEY, EventCode.BTN_TL2 }, "lefttrigger" },
+               { { EventCode.EV_KEY, EventCode.BTN_TR2 }, "righttrigger" },
+               { { EventCode.EV_KEY, EventCode.BTN_Y }, "x" },
+               { { EventCode.EV_KEY, EventCode.BTN_X }, "y" },
+       };
+
+       private const GamepadViewConfiguration STANDARD_GAMEPAD_VIEW_CONFIGURATION = {
+               "resource:///org/gnome/Games/gamepads/standard-gamepad.svg", STANDARD_GAMEPAD_INPUT_PATHS
+       };
+}
diff --git a/src/ui/gamepad-view.vala b/src/ui/gamepad-view.vala
index a06f6cb..3d6e5da 100644
--- a/src/ui/gamepad-view.vala
+++ b/src/ui/gamepad-view.vala
@@ -1,41 +1,42 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 private class Games.GamepadView : Gtk.DrawingArea {
-       private const string STANDARD_GAMEPAD_SVG_PATH = 
"resource:///org/gnome/Games/gamepads/standard-gamepad.svg";
        private const Gtk.StateFlags DEFAULT_STATE = Gtk.StateFlags.NORMAL;
        private const Gtk.StateFlags HIGHLIGHT_STATE = Gtk.StateFlags.LINK;
 
+       private struct GamepadInputDisplay {
+               GamepadInput input;
+               string path;
+               bool highlight;
+       }
+
        private Rsvg.Handle handle;
-       private string[] highlight_paths = { "a", "b", "x", "y",
-                                            "leftstick", "rightstick",
-                                            "leftshoulder", "rightshoulder", "lefttrigger", "righttrigger",
-                                            "dpup", "dpleft", "dpdown", "dpright",
-                                            "back", "guide", "start",
-                                            "leftx", "rightx", "lefty", "righty" };
-       private GamepadInput[] inputs;
-       private int current_input_index;
-
-       construct {
+       private GamepadInputDisplay[] input_displays;
+
+       public GamepadView (GamepadViewConfiguration configuration) {
+               foreach (var input_path in configuration.input_paths) {
+                       GamepadInputDisplay input_display = {input_path.input, input_path.path, false};
+                       input_displays += input_display;
+               }
+
                try {
-                       handle = new Rsvg.Handle.from_file (STANDARD_GAMEPAD_SVG_PATH);
+                       handle = new Rsvg.Handle.from_file (configuration.svg_path);
                        set_size_request (handle.width, handle.height);
                }
                catch (Error e) {
                        warning (e.message);
                }
-
-               inputs = STANDARD_GAMEPAD_INPUTS;
-               current_input_index = -1;
        }
 
-       public GamepadInput? highlight_next () {
-               current_input_index += 1;
-               queue_draw ();
-
-               if (current_input_index >= inputs.length)
-                       return null;
+       public void reset () {
+               foreach (var input_display in input_displays)
+                       input_display.highlight = false;
+       }
 
-               return inputs[current_input_index];
+       public void highlight (GamepadInput input, bool highlight) {
+               foreach (var input_display in input_displays)
+                       if (input == input_display.input)
+                               input_display.highlight = highlight;
        }
 
        public override bool draw (Cairo.Context context) {
@@ -43,11 +44,7 @@ private class Games.GamepadView : Gtk.DrawingArea {
                calculate_image_dimensions (out x, out y, out scale);
 
                color_gamepad (context, x, y, scale);
-               if (!(0 <= current_input_index && current_input_index < inputs.length))
-                       return false;
-
-               var highlight_path = "#" + highlight_paths[current_input_index];
-               highlight_gamepad (context, highlight_path, x, y, scale);
+               highlight_gamepad (context, x, y, scale);
 
                return false;
        }
@@ -57,6 +54,7 @@ private class Games.GamepadView : Gtk.DrawingArea {
                var color_suface = color_context.get_target ();
                color_context.translate (x, y);
                color_context.scale (scale, scale);
+
                handle.render_cairo (color_context);
 
                var color = get_style_context ().get_color (DEFAULT_STATE);
@@ -64,12 +62,15 @@ private class Games.GamepadView : Gtk.DrawingArea {
                gamepad_context.mask_surface (color_suface, 0, 0);
        }
 
-       private void highlight_gamepad (Cairo.Context gamepad_context, string highlight_path, double x, 
double y, double scale) {
+       private void highlight_gamepad (Cairo.Context gamepad_context, double x, double y, double scale) {
                var highlight_context = create_similar_context (gamepad_context);
                var highlight_suface = highlight_context.get_target ();
                highlight_context.translate (x, y);
                highlight_context.scale (scale, scale);
-               handle.render_cairo_sub (highlight_context, highlight_path);
+               
+               foreach (var input_display in input_displays)
+                       if (input_display.highlight)
+                               handle.render_cairo_sub (highlight_context, input_display.path);
 
                var color = get_style_context ().get_color (HIGHLIGHT_STATE);
                gamepad_context.set_source_rgba (color.red, color.green, color.blue, color.alpha);
diff --git a/src/ui/preferences-window.vala b/src/ui/preferences-window.vala
index f925ea8..69be97f 100644
--- a/src/ui/preferences-window.vala
+++ b/src/ui/preferences-window.vala
@@ -19,5 +19,6 @@ private class Games.PreferencesWindow : Gtk.Window {
        private void update_right_header () {
                var page = (PreferencesPage) stack.get_visible_child ();
                right_header_bar.custom_title = (page == null) ? null : page.header_content;
+               right_header_bar.subtitle = "what is this";
        }
 }


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