[gnome-games/wip/abhinavsingh/gamepad-config: 39/40] ui: Add GamepadMapper



commit 6ed5c0a136c97ec65b9e55176b4734de44a24a4b
Author: theawless <theawless gmail com>
Date:   Sun May 28 06:09:20 2017 +0530

    ui: Add GamepadMapper
    
    This will use GamepadView to get the user input and
    GamepadInverseMapping to create the sdl string from those mappings.

 data/Makefile.am                   |    1 +
 data/org.gnome.Games.gresource.xml |    1 +
 data/ui/gamepad-mapper.ui          |   57 ++++++++++++
 src/Makefile.am                    |    1 +
 src/ui/gamepad-mapper.vala         |  168 ++++++++++++++++++++++++++++++++++++
 5 files changed, 228 insertions(+), 0 deletions(-)
---
diff --git a/data/Makefile.am b/data/Makefile.am
index 88d5264..a331981 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 d43fbab..40578bb 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..596493a
--- /dev/null
+++ b/data/ui/gamepad-mapper.ui
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="GamesGamepadMapper" parent="GtkDialog">
+    <property name="destroy-with-parent">True</property>
+    <property name="type_hint">dialog</property>
+    <child internal-child="vbox">
+      <object class="GtkBox">
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">6</property>
+        <property name="margin-top">12</property>
+        <property name="margin-bottom">12</property>
+        <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">
+            <property name="halign">center</property>
+            <property name="visible">True</property>
+          </object>
+        </child>
+      </object>
+    </child>
+    <child type="action">
+      <object class="GtkButton" id="button_apply">
+        <property name="visible">True</property>
+        <property name="can-default">True</property>
+        <property name="label" translatable="yes">Apply</property>
+        <style>
+          <class name="suggested-action"/>
+        </style>
+      </object>
+    </child>
+    <child type="action">
+      <object class="GtkButton" id="button_cancel">
+        <property name="visible">True</property>
+        <property name="label" translatable="yes">Cancel</property>
+      </object>
+    </child>
+    <action-widgets>
+      <action-widget response="apply" default="true">button_apply</action-widget>
+      <action-widget response="cancel">button_cancel</action-widget>
+    </action-widgets>
+  </template>
+</interface>
diff --git a/src/Makefile.am b/src/Makefile.am
index b285f3c..72dc990 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -124,6 +124,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..136eb60
--- /dev/null
+++ b/src/ui/gamepad-mapper.vala
@@ -0,0 +1,168 @@
+// 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.Dialog {
+       private enum UiState {
+               BEGIN,
+               CONFIGURE,
+               FINISH,
+       }
+
+       // TODO: Add skip button
+       private UiState _ui_state;
+       private UiState ui_state {
+               set {
+                       switch (value) {
+                       case UiState.BEGIN:
+                               set_response_sensitive (Gtk.ResponseType.APPLY, false);
+                               header_bar.set_title (_("Configuring ") + gamepad.name);
+
+                               break;
+                       case UiState.CONFIGURE:
+                               break;
+                       case UiState.FINISH:
+                               set_response_sensitive (Gtk.ResponseType.APPLY, true);
+                               header_bar.set_title (_("Finished configuration"));
+
+                               break;
+                       }
+                       error_message.label = "";
+                       _ui_state = value;
+               }
+               get { return _ui_state; }
+       }
+
+       private GamepadInput? _input;
+       private GamepadInput? input {
+               set {
+                       _input = value;
+                       if (_input == null)
+                               return;
+                       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;
+                       }
+               }
+               get { return _input; }
+
+       }
+
+       [GtkChild]
+       private Gtk.Label info_message;
+       [GtkChild]
+       private Gtk.Label error_message;
+       [GtkChild]
+       private GamepadView gamepad_view;
+       private Gtk.HeaderBar header_bar;
+
+       private Gamepad gamepad;
+       private GamepadInverseMapping inverse_mapping;
+       private ulong gamepad_event_handler_id;
+
+       construct {
+               use_header_bar = 1;
+               header_bar = (Gtk.HeaderBar) get_header_bar ();
+       }
+
+       public GamepadMapper (Gamepad gamepad) {
+               this.gamepad = gamepad;
+               inverse_mapping = new GamepadInverseMapping (gamepad);
+       }
+
+       public void start () {
+               ui_state = UiState.BEGIN;
+               connect_to_gamepad ();
+
+               next_input ();
+
+               var response = run ();
+       }
+
+       private void connect_to_gamepad () {
+               gamepad_event_handler_id = this.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 () {
+               gamepad.disconnect (gamepad_event_handler_id);
+       }
+
+       private void on_button_event (EventGamepadButton event) {
+               if (!inverse_mapping.set_button_mapping (event.gamepad_button.hardware_index, input)) {
+                       error_message.label = _("This button is already in use");
+
+                       return;
+               }
+
+               next_input ();
+       }
+
+       private void on_axis_event (EventGamepadAxis event) {
+               if (-0.8 < event.gamepad_axis.value && event.gamepad_axis.value < 0.8)
+                       return;
+
+               if (!inverse_mapping.set_axis_mapping (event.gamepad_axis.hardware_index, input)) {
+                       error_message.label = _("This axis is already in use");
+
+                       return;
+               }
+
+               next_input ();
+       }
+
+       private void on_hat_event (EventGamepadHat event) {
+               if (event.gamepad_hat.value == 0)
+                       return;
+
+               if (!inverse_mapping.set_hat_mapping (event.gamepad_hat.hardware_index, 
event.gamepad_hat.value, input)) {
+                       error_message.label = _("This hat is already in use");
+
+                       return;
+               }
+
+               next_input ();
+       }
+
+       private void next_input () {
+               input = gamepad_view.highlight_next ();
+               if (input == null)
+                       finish ();
+
+               ui_state = UiState.CONFIGURE;
+       }
+
+       private void finish () {
+               ui_state = UiState.FINISH;
+               disconnect_from_gamepad ();
+
+               var sdl_string = inverse_mapping.get_sdl_string ();
+               // TODO: save sdl_string
+       }
+}


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