[gnome-games/wip/abhinavsingh/gamepad-reassign: 1/3] ui: Add ControllerReassigner



commit a06765751bef2d3563545317977b7645547bcb97
Author: theawless <theawless gmail com>
Date:   Sat Aug 12 00:22:32 2017 +0530

    ui: Add ControllerReassigner
    
    Allows the user to reassign controllers based on the orders in which
    they pressed the buttons on their controllers.

 data/Makefile.am                   |    1 +
 data/org.gnome.Games.gresource.xml |    1 +
 data/ui/controller-reassigner.ui   |   63 ++++++++++++++++++++++++++++++
 src/Makefile.am                    |    1 +
 src/ui/controller-reassigner.vala  |   75 ++++++++++++++++++++++++++++++++++++
 5 files changed, 141 insertions(+), 0 deletions(-)
---
diff --git a/data/Makefile.am b/data/Makefile.am
index fff2a95..1e1fc40 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/controller-reassigner.ui \
        ui/media-menu-button.ui \
        ui/media-selector.ui \
        ui/preferences-page-controllers.ui \
diff --git a/data/org.gnome.Games.gresource.xml b/data/org.gnome.Games.gresource.xml
index b8ba7b5..dfd9e2d 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/controller-reassigner.ui</file>
     <file preprocess="xml-stripblanks">ui/media-menu-button.ui</file>
     <file preprocess="xml-stripblanks">ui/media-selector.ui</file>
     <file preprocess="xml-stripblanks">ui/preferences-page-controllers.ui</file>
diff --git a/data/ui/controller-reassigner.ui b/data/ui/controller-reassigner.ui
new file mode 100644
index 0000000..0aaed19
--- /dev/null
+++ b/data/ui/controller-reassigner.ui
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="GamesControllerReassigner" parent="GtkDialog">
+    <property name="destroy-with-parent">True</property>
+    <property name="type_hint">dialog</property>
+    <property name="title" translatable="yes">Reassign controllers</property>
+    <child internal-child="vbox">
+      <object class="GtkBox">
+        <property name="orientation">vertical</property>
+        <property name="visible">True</property>
+        <property name="spacing">6</property>
+        <property name="margin-bottom">6</property>
+        <child>
+          <object class="GtkInfoBar" id="info_bar">
+            <property name="visible">True</property>
+            <child internal-child="content_area">
+              <object class="GtkBox">
+                <child>
+                  <object class="GtkLabel">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="label" translatable="yes">Reassign controllers by pressing buttons on 
your controllers in the desired order</property>
+                    <attributes>
+                      <attribute name="weight" value="bold"/>
+                    </attributes>
+                  </object>
+                </child>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkListBox" id="controllers_list_box">
+            <property name="visible">True</property>
+            <property name="selection_mode">none</property>
+            <property name="margin-start">12</property>
+            <property name="margin-end">12</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 e9cd162..f77d066 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -154,6 +154,7 @@ gnome_games_SOURCES = \
        ui/game-icon-view.vala \
        ui/game-thumbnail.vala \
        ui/gamepad-view-configuration.vala \
+       ui/controller-reassigner.vala \
        ui/media-selector.vala \
        ui/media-menu-button.vala \
        ui/preferences-page.vala \
diff --git a/src/ui/controller-reassigner.vala b/src/ui/controller-reassigner.vala
new file mode 100644
index 0000000..2ba9b79
--- /dev/null
+++ b/src/ui/controller-reassigner.vala
@@ -0,0 +1,75 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+[GtkTemplate (ui = "/org/gnome/Games/ui/controller-reassigner.ui")]
+private class Games.ControllerReassigner : Gtk.Dialog {
+       [GtkChild]
+       private Gtk.ListBox controllers_list_box;
+
+       public ControllerSet controller_set { private set; get; }
+
+       private uint current_port;
+       private GamepadMonitor gamepad_monitor;
+
+       construct {
+               use_header_bar = 1;
+
+               controller_set = new ControllerSet ();
+               controller_set.changed.connect (reset_controllers);
+               current_port = 0;
+               gamepad_monitor = GamepadMonitor.get_instance ();
+
+               events |= Gdk.EventMask.KEY_RELEASE_MASK;
+       }
+
+       public ControllerReassigner () {
+               key_release_event.connect (keyboard_event);
+               gamepad_monitor.foreach_gamepad ((gamepad) => {
+                       gamepad.button_press_event.connect (gamepad_event);
+               });
+       }
+
+       private void reset_controllers () {
+               remove_controllers ();
+               update_controllers ();
+       }
+
+       private void update_controllers () {
+               if (controller_set == null)
+                       return;
+
+               if (controller_set.has_gamepads) {
+                       controller_set.gamepads.foreach ((port, gamepad) => {
+                               add_controller (gamepad.name, port);
+                       });
+               }
+               if (controller_set.has_keyboard)
+                       add_controller (_("Keyboard"), controller_set.keyboard_port);
+       }
+
+       private void add_controller (string label, uint port) {
+               var position = (int) port;
+               var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
+               box.pack_start (new Gtk.Label (position.to_string ()), false, false);
+               box.pack_end (new Gtk.Label (label), false, false);
+               box.spacing = 6;
+               box.margin = 6;
+               box.show_all ();
+               controllers_list_box.insert (box, position);
+       }
+
+       private void remove_controllers () {
+               controllers_list_box.foreach ((child) => child.destroy ());
+       }
+
+       private bool keyboard_event () {
+               key_release_event.disconnect (keyboard_event);
+               controller_set.keyboard_port = current_port++;
+
+               return false;
+       }
+
+       private void gamepad_event (Gamepad gamepad, Event _) {
+               gamepad.button_press_event.disconnect (gamepad_event);
+               controller_set.add_gamepad (current_port++, gamepad);
+       }
+}


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