[gnome-games/wip/abhinavsingh/gamepad-config: 18/23] ui: Add GamepadMapper



commit e238d125101cc2f8ff3a5048bc0bd77b6fd34df1
Author: theawless <theawless gmail com>
Date:   Sun May 28 14:55:59 2017 +0530

    ui: Add GamepadMapper
    
    Use GamepadView to get the user input and GamepadMappingBuilder to
    build the sdl string from those inputs.

 data/Makefile.am                   |    1 +
 data/org.gnome.Games.gresource.xml |    1 +
 data/ui/gamepad-mapper.ui          |   37 +++++++++
 src/Makefile.am                    |    1 +
 src/ui/gamepad-mapper.vala         |  148 ++++++++++++++++++++++++++++++++++++
 5 files changed, 188 insertions(+), 0 deletions(-)
---
diff --git a/data/Makefile.am b/data/Makefile.am
index cf1b58b..5f3d675 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -37,6 +37,7 @@ EXTRA_DIST = \
        ui/empty-collection.ui \
        ui/error-display.ui \
        ui/error-info-bar.ui \
+       ui/gamepad-mapper.ui \
        ui/game-icon-view.ui \
        ui/media-menu-button.ui \
        ui/media-selector.ui \
diff --git a/data/org.gnome.Games.gresource.xml b/data/org.gnome.Games.gresource.xml
index 9e14d6f..49a7af4 100644
--- a/data/org.gnome.Games.gresource.xml
+++ b/data/org.gnome.Games.gresource.xml
@@ -16,6 +16,7 @@
     <file preprocess="xml-stripblanks">ui/empty-collection.ui</file>
     <file preprocess="xml-stripblanks">ui/error-display.ui</file>
     <file preprocess="xml-stripblanks">ui/error-info-bar.ui</file>
+    <file preprocess="xml-stripblanks">ui/gamepad-mapper.ui</file>
     <file preprocess="xml-stripblanks">ui/game-icon-view.ui</file>
     <file preprocess="xml-stripblanks">ui/media-menu-button.ui</file>
     <file preprocess="xml-stripblanks">ui/media-selector.ui</file>
diff --git a/data/ui/gamepad-mapper.ui b/data/ui/gamepad-mapper.ui
new file mode 100644
index 0000000..ed0e5fa
--- /dev/null
+++ b/data/ui/gamepad-mapper.ui
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="GamesGamepadMapper" parent="GtkBox">
+    <property name="visible">True</property>
+    <property name="can_focus">False</property>
+    <property name="orientation">vertical</property>
+    <child>
+      <object class="GamesGamepadView" id="gamepad_view">
+        <property name="visible">True</property>
+        <property name="halign">fill</property>
+        <property name="valign">fill</property>
+        <property name="hexpand">True</property>
+        <property name="vexpand">True</property>
+      </object>
+    </child>
+    <child>
+      <object class="GtkActionBar" id="action_bar">
+        <property name="visible">True</property>
+        <child>
+          <object class="GtkButton" id="skip_button">
+            <property name="visible">True</property>
+            <property name="label" translatable="yes">Skip</property>
+            <signal name="clicked" handler="on_skip_clicked"/>
+          </object>
+          <packing>
+            <property name="pack-type">start</property>
+          </packing>
+        </child>
+        <child type="center">
+          <object class="GtkLabel" id="info_message">
+            <property name="visible">True</property>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/Makefile.am b/src/Makefile.am
index 7c8aa4a..d566dda 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -146,6 +146,7 @@ gnome_games_SOURCES = \
        ui/empty-collection.vala \
        ui/error-display.vala \
        ui/error-info-bar.vala \
+       ui/gamepad-mapper.vala \
        ui/gamepad-view.vala \
        ui/game-icon-view.vala \
        ui/game-thumbnail.vala \
diff --git a/src/ui/gamepad-mapper.vala b/src/ui/gamepad-mapper.vala
new file mode 100644
index 0000000..38b3a0d
--- /dev/null
+++ b/src/ui/gamepad-mapper.vala
@@ -0,0 +1,148 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+[GtkTemplate (ui = "/org/gnome/Games/ui/gamepad-mapper.ui")]
+private class Games.GamepadMapper : Gtk.Box {
+       public signal void finished (string sdl_string);
+
+       [GtkChild]
+       private GamepadView gamepad_view;
+       [GtkChild]
+       private Gtk.Label info_message;
+
+       private Gamepad gamepad;
+       private GamepadMappingBuilder mapping_builder;
+       private GamepadInput[] mapping_inputs;
+       private GamepadInput input;
+       private uint current_input_index;
+
+       private ulong gamepad_event_handler_id;
+
+       public GamepadMapper (Gamepad gamepad, GamepadViewConfiguration configuration, GamepadInput[] 
mapping_inputs) {
+               this.gamepad = gamepad;
+               this.mapping_inputs = mapping_inputs;
+               try {
+                       gamepad_view.set_configuration (configuration);
+               }
+               catch (Error e) {
+                       critical ("Could not set up gamepad view: %s", e.message);
+               }
+       }
+
+       public void start () {
+               mapping_builder = new GamepadMappingBuilder ();
+               current_input_index = 0;
+               connect_to_gamepad ();
+
+               next_input ();
+       }
+
+       public void stop () {
+               disconnect_from_gamepad ();
+       }
+
+       [GtkCallback]
+       private void on_skip_clicked () {
+               next_input ();
+       }
+
+       private void connect_to_gamepad () {
+               gamepad_event_handler_id = gamepad.event.connect ((event) => {
+                       switch (event.type) {
+                       case EventType.EVENT_GAMEPAD_BUTTON_RELEASE:
+                               on_button_event (event.gamepad_button);
+
+                               break;
+                       case EventType.EVENT_GAMEPAD_AXIS:
+                               on_axis_event (event.gamepad_axis);
+
+                               break;
+                       case EventType.EVENT_GAMEPAD_HAT:
+                               on_hat_event (event.gamepad_hat);
+
+                               break;
+                       default:
+                               break;
+                       }
+               });
+       }
+
+       private void disconnect_from_gamepad () {
+               if (gamepad_event_handler_id != 0) {
+                       gamepad.disconnect (gamepad_event_handler_id);
+                       gamepad_event_handler_id = 0;
+               }
+       }
+
+       private void on_button_event (EventGamepadButton event) {
+               if (input.type == EventCode.EV_ABS)
+                       return;
+
+               var success = mapping_builder.set_button_mapping (event.gamepad_button.hardware_index,
+                                                                 input);
+               if (!success)
+                       return;
+
+               next_input ();
+       }
+
+       private void on_axis_event (EventGamepadAxis event) {
+               if (input.type == EventCode.EV_KEY)
+                       return;
+
+               if (-0.8 < event.gamepad_axis.value < 0.8)
+                       return;
+
+               var success = mapping_builder.set_axis_mapping (event.gamepad_axis.hardware_index,
+                                                               input);
+               if (!success)
+                       return;
+
+               next_input ();
+       }
+
+       private void on_hat_event (EventGamepadHat event) {
+               if (event.gamepad_hat.value == 0)
+                       return;
+
+               var success = mapping_builder.set_hat_mapping (event.gamepad_hat.hardware_index,
+                                                              event.gamepad_hat.value,
+                                                              input);
+               if (!success)
+                       return;
+
+               next_input ();
+       }
+
+       private void next_input () {
+               if (current_input_index == mapping_inputs.length) {
+                       var sdl_string = mapping_builder.build_sdl_string ();
+                       finished (sdl_string);
+
+                       return;
+               }
+
+               gamepad_view.reset ();
+               input = mapping_inputs[current_input_index++];
+               gamepad_view.highlight (input, true);
+
+               update_info_message ();
+       }
+
+       private void update_info_message () {
+               switch (input.type) {
+               case EventCode.EV_KEY:
+                       info_message.label = _("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");
+                       else if (input.code == EventCode.ABS_Y || input.code == EventCode.ABS_RY)
+                               info_message.label = _("Move suitable axis up/down on your gamepad");
+
+                       break;
+               default:
+                       break;
+               }
+       }
+}


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