[gnome-games/wip/abhinavsingh/gamepad-config: 10/14] ui: Add GamepadMapper



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

    ui: Add GamepadMapper
    
    This will use GamepadView to get the user input & GamepadMappingBuilder
    to create the sdl string from those inputs.

 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         |  180 ++++++++++++++++++++++++++++++++++++
 5 files changed, 240 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 a94cfbe..ef3f705 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -125,6 +125,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..2a6a172
--- /dev/null
+++ b/src/ui/gamepad-mapper.vala
@@ -0,0 +1,180 @@
+// 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 State {
+               BEGIN,
+               CONFIGURE,
+               FINISH,
+       }
+
+       // TODO: Add skip button
+       private State? _state;
+       private State? state {
+               set {
+                       if (value == _state)
+                               return;
+
+                       switch (value) {
+                       case State.BEGIN:
+                               set_response_sensitive (Gtk.ResponseType.APPLY, false);
+                               header_bar.set_title (_("Configuring ") + gamepad.name);
+                               connect_to_gamepad ();
+
+                               break;
+                       case State.CONFIGURE:
+                               break;
+                       case State.FINISH:
+                               set_response_sensitive (Gtk.ResponseType.APPLY, true);
+                               header_bar.set_title (_("Finished configuration"));
+                               disconnect_from_gamepad ();
+
+                               break;
+                       }
+
+                       error_message.label = "";
+                       _state = value;
+               }
+               get { return _state; }
+       }
+
+       private GamepadInput? _input;
+       private GamepadInput? input {
+               set {
+                       _input = value;
+                       if (_input == null) {
+                               info_message.label = "";
+
+                               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 GamepadMappingBuilder mapping_builder;
+       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;
+               mapping_builder = new GamepadMappingBuilder (gamepad);
+       }
+
+       public void start () {
+               state = State.BEGIN;
+
+               next_input ();
+
+               var response = run ();
+               if (response == Gtk.ResponseType.APPLY)
+                       save_sdl_string ();
+       }
+
+       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 (!mapping_builder.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 (!mapping_builder.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 (!mapping_builder.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) {
+                       state = State.FINISH;
+
+                       return;
+               }
+
+               state = State.CONFIGURE;
+       }
+
+       private void save_sdl_string () {
+               var sdl_string = mapping_builder.build_sdl_string ();
+               message (sdl_string);
+               // TODO: Save using GamepadManager
+       }
+}


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