[gnome-games/wip/abhinavsingh/keyboard-config: 4/6] ui: Add KeyboardMapper



commit 314652c7845f73d8509c0f9c5b58aa583d077958
Author: theawless <theawless gmail com>
Date:   Mon Aug 28 04:36:30 2017 +0530

    ui: Add KeyboardMapper
    
    Use GamepadView to get the user input and KeyboardMappingBuilder to
    build the configuration from those inputs.

 data/Makefile.am                   |    1 +
 data/org.gnome.Games.gresource.xml |    1 +
 data/ui/keyboard-mapper.ui         |   37 +++++++++++++++++
 src/Makefile.am                    |    1 +
 src/ui/keyboard-mapper.vala        |   76 ++++++++++++++++++++++++++++++++++++
 5 files changed, 116 insertions(+), 0 deletions(-)
---
diff --git a/data/Makefile.am b/data/Makefile.am
index 73459a4..51c9d5a 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -41,6 +41,7 @@ EXTRA_DIST = \
        ui/gamepad-mapper.ui \
        ui/gamepad-tester.ui \
        ui/game-icon-view.ui \
+       ui/keyboard-mapper.ui \
        ui/keyboard-tester.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 680d4c4..aa4f495 100644
--- a/data/org.gnome.Games.gresource.xml
+++ b/data/org.gnome.Games.gresource.xml
@@ -20,6 +20,7 @@
     <file preprocess="xml-stripblanks">ui/gamepad-mapper.ui</file>
     <file preprocess="xml-stripblanks">ui/gamepad-tester.ui</file>
     <file preprocess="xml-stripblanks">ui/game-icon-view.ui</file>
+    <file preprocess="xml-stripblanks">ui/keyboard-mapper.ui</file>
     <file preprocess="xml-stripblanks">ui/keyboard-tester.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/keyboard-mapper.ui b/data/ui/keyboard-mapper.ui
new file mode 100644
index 0000000..9f83161
--- /dev/null
+++ b/data/ui/keyboard-mapper.ui
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="GamesKeyboardMapper" 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 cc75910..89a4894 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -156,6 +156,7 @@ gnome_games_SOURCES = \
        ui/game-icon-view.vala \
        ui/game-thumbnail.vala \
        ui/gamepad-view-configuration.vala \
+       ui/keyboard-mapper.vala \
        ui/keyboard-tester.vala \
        ui/media-selector.vala \
        ui/media-menu-button.vala \
diff --git a/src/ui/keyboard-mapper.vala b/src/ui/keyboard-mapper.vala
new file mode 100644
index 0000000..68d5d55
--- /dev/null
+++ b/src/ui/keyboard-mapper.vala
@@ -0,0 +1,76 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+[GtkTemplate (ui = "/org/gnome/Games/ui/keyboard-mapper.ui")]
+private class Games.KeyboardMapper : Gtk.Box {
+       public signal void finished (Retro.GamepadConfiguration mapping);
+
+       [GtkChild]
+       private GamepadView gamepad_view;
+       [GtkChild]
+       private Gtk.Label info_message;
+
+       public Retro.VirtualGamepad keyboard { get; set; }
+       private KeyboardMappingBuilder mapping_builder;
+       private GamepadInput[] mapping_inputs;
+       private GamepadInput input;
+       private uint current_input_index;
+
+       construct {
+               info_message.label = _("Press suitable key on your keyboard");
+               mapping_builder = new KeyboardMappingBuilder ();
+       }
+
+       public KeyboardMapper (GamepadViewConfiguration configuration, GamepadInput[] mapping_inputs) {
+               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 () {
+               current_input_index = 0;
+               connect_to_keyboard ();
+
+               next_input ();
+       }
+
+       public void stop () {
+               disconnect_from_keyboard ();
+       }
+
+       [GtkCallback]
+       private void on_skip_clicked () {
+               next_input ();
+       }
+
+       private void connect_to_keyboard () {
+               keyboard.widget.key_release_event.connect (on_keyboard_event);
+       }
+
+       private void disconnect_from_keyboard () {
+               keyboard.widget.key_release_event.disconnect (on_keyboard_event);
+       }
+
+       private bool on_keyboard_event (Gdk.EventKey key) {
+               var success = mapping_builder.set_input_mapping (input, key.hardware_keycode);
+               if (success)
+                       next_input ();
+
+               return true;
+       }
+
+       private void next_input () {
+               if (current_input_index == mapping_inputs.length) {
+                       finished (mapping_builder.mapping);
+
+                       return;
+               }
+
+               gamepad_view.reset ();
+               input = mapping_inputs[current_input_index++];
+               gamepad_view.highlight (input, true);
+       }
+}


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