[gnome-games/wip/abhinavsingh/gamepad-config: 18/22] ui: Add GamepadTester



commit f8105f5982bfd0417321694bb810e5457f2683e0
Author: theawless <theawless gmail com>
Date:   Fri Jul 21 11:45:32 2017 +0530

    ui: Add GamepadTester
    
    This allows the user to test their gamepads by highlighting GamepadView
    widget on user inputs.

 data/Makefile.am                   |    1 +
 data/org.gnome.Games.gresource.xml |    1 +
 data/ui/gamepad-tester.ui          |   18 ++++++++
 src/Makefile.am                    |    1 +
 src/ui/gamepad-tester.vala         |   81 ++++++++++++++++++++++++++++++++++++
 5 files changed, 102 insertions(+), 0 deletions(-)
---
diff --git a/data/Makefile.am b/data/Makefile.am
index 5f3d675..0b9e21f 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -38,6 +38,7 @@ EXTRA_DIST = \
        ui/error-display.ui \
        ui/error-info-bar.ui \
        ui/gamepad-mapper.ui \
+       ui/gamepad-tester.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 49a7af4..61d23af 100644
--- a/data/org.gnome.Games.gresource.xml
+++ b/data/org.gnome.Games.gresource.xml
@@ -17,6 +17,7 @@
     <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/gamepad-tester.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-tester.ui b/data/ui/gamepad-tester.ui
new file mode 100644
index 0000000..ab43ea1
--- /dev/null
+++ b/data/ui/gamepad-tester.ui
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="GamesGamepadTester" parent="GtkBox">
+    <property name="visible">True</property>
+    <property name="can_focus">False</property>
+    <property name="orientation">vertical</property>
+    <child>
+      <object class="GtkBox" id="gamepad_view_holder">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="halign">fill</property>
+        <property name="valign">fill</property>
+        <property name="hexpand">True</property>
+        <property name="vexpand">True</property>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/Makefile.am b/src/Makefile.am
index d566dda..2632f73 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -147,6 +147,7 @@ gnome_games_SOURCES = \
        ui/error-display.vala \
        ui/error-info-bar.vala \
        ui/gamepad-mapper.vala \
+       ui/gamepad-tester.vala \
        ui/gamepad-view.vala \
        ui/game-icon-view.vala \
        ui/game-thumbnail.vala \
diff --git a/src/ui/gamepad-tester.vala b/src/ui/gamepad-tester.vala
new file mode 100644
index 0000000..65ac327
--- /dev/null
+++ b/src/ui/gamepad-tester.vala
@@ -0,0 +1,81 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+[GtkTemplate (ui = "/org/gnome/Games/ui/gamepad-tester.ui")]
+private class Games.GamepadTester : Gtk.Box {
+       [GtkChild]
+       private Gtk.Box gamepad_view_holder;
+
+       private Gamepad gamepad;
+       private GamepadView gamepad_view;
+
+       private ulong gamepad_button_press_event_handler_id;
+       private ulong gamepad_button_release_event_handler_id;
+       private ulong gamepad_axis_event_handler_id;
+       private ulong gamepad_hat_event_handler_id;
+
+       public GamepadTester (Gamepad gamepad, GamepadViewConfiguration configuration) {
+               this.gamepad = gamepad;
+
+               gamepad_view = new GamepadView (configuration);
+               gamepad_view_holder.pack_start (gamepad_view);
+               gamepad_view.show ();
+       }
+
+       public void start () {
+               gamepad_view.reset ();
+               connect_to_gamepad ();
+       }
+
+       public void stop () {
+               disconnect_from_gamepad ();
+       }
+
+       private void connect_to_gamepad () {
+               gamepad_button_press_event_handler_id = gamepad.button_press_event.connect ((event) => {
+                       on_button_event (event.gamepad_button, true);
+               });
+               gamepad_button_release_event_handler_id = gamepad.button_release_event.connect ((event) => {
+                       on_button_event (event.gamepad_button, false);
+               });
+               gamepad_axis_event_handler_id = gamepad.axis_event.connect ((event) => {
+                       on_axis_event (event.gamepad_axis);
+               });
+               gamepad_hat_event_handler_id = gamepad.hat_event.connect ((event) => {
+                       on_hat_event (event.gamepad_hat);
+               });
+       }
+
+       private void disconnect_from_gamepad () {
+               if (gamepad_button_press_event_handler_id != 0) {
+                       gamepad.disconnect (gamepad_button_press_event_handler_id);
+                       gamepad_button_press_event_handler_id = 0;
+               }
+               if (gamepad_button_release_event_handler_id != 0) {
+                       gamepad.disconnect (gamepad_button_release_event_handler_id);
+                       gamepad_button_release_event_handler_id = 0;
+               }
+               if (gamepad_axis_event_handler_id != 0) {
+                       gamepad.disconnect (gamepad_axis_event_handler_id);
+                       gamepad_axis_event_handler_id = 0;
+               }
+               if (gamepad_hat_event_handler_id != 0) {
+                       gamepad.disconnect (gamepad_hat_event_handler_id);
+                       gamepad_axis_event_handler_id = 0;
+               }
+       }
+
+       private void on_button_event (EventGamepadButton event, bool pressed) {
+               var highlight = pressed;
+               gamepad_view.highlight ({EventCode.EV_KEY, event.button}, highlight);
+       }
+
+       private void on_axis_event (EventGamepadAxis event) {
+               var highlight = !(-0.8 < event.gamepad_axis.value < 0.8);
+               gamepad_view.highlight ({EventCode.EV_ABS, event.axis}, highlight);
+       }
+
+       private void on_hat_event (EventGamepadHat event) {
+               var highlight = !(event.gamepad_hat.value == 0);
+               gamepad_view.highlight ({EventCode.EV_ABS, event.axis}, highlight);
+       }
+}


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