Commit bd16ffe9 authored by Abhinav Singh's avatar Abhinav Singh Committed by Adrien Plazas
Browse files

ui: Add GamepadMapper

Use GamepadView to get the user input and GamepadMappingBuilder to
build the sdl string from those inputs.

https://bugzilla.gnome.org/show_bug.cgi?id=780754
parent 8d5d0cbd
......@@ -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 \
......
......@@ -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>
......
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="GamesGamepadMapper" 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>
......@@ -146,6 +146,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 \
......
// 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.Box {
public signal void finished (string sdl_string);
[GtkChild]
private GamepadView gamepad_view;
[GtkChild]
private Gtk.Label info_message;
private Gamepad gamepad;
private GamepadMappingBuilder mapping_builder;
private GamepadInput[] mapping_inputs;
private GamepadInput input;
private uint current_input_index;
private ulong gamepad_event_handler_id;
public GamepadMapper (Gamepad gamepad, GamepadViewConfiguration configuration, GamepadInput[] mapping_inputs) {
this.gamepad = gamepad;
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 () {
mapping_builder = new GamepadMappingBuilder ();
current_input_index = 0;
connect_to_gamepad ();
next_input ();
}
public void stop () {
disconnect_from_gamepad ();
}
[GtkCallback]
private void on_skip_clicked () {
next_input ();
}
private void connect_to_gamepad () {
gamepad_event_handler_id = 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 () {
if (gamepad_event_handler_id != 0) {
gamepad.disconnect (gamepad_event_handler_id);
gamepad_event_handler_id = 0;
}
}
private void on_button_event (EventGamepadButton event) {
if (input.type == EventCode.EV_ABS)
return;
var success = mapping_builder.set_button_mapping (event.gamepad_button.hardware_index,
input);
if (!success)
return;
next_input ();
}
private void on_axis_event (EventGamepadAxis event) {
if (input.type == EventCode.EV_KEY)
return;
if (-0.8 < event.gamepad_axis.value < 0.8)
return;
var success = mapping_builder.set_axis_mapping (event.gamepad_axis.hardware_index,
input);
if (!success)
return;
next_input ();
}
private void on_hat_event (EventGamepadHat event) {
if (event.gamepad_hat.value == 0)
return;
var success = mapping_builder.set_hat_mapping (event.gamepad_hat.hardware_index,
event.gamepad_hat.value,
input);
if (!success)
return;
next_input ();
}
private void next_input () {
if (current_input_index == mapping_inputs.length) {
var sdl_string = mapping_builder.build_sdl_string ();
finished (sdl_string);
return;
}
gamepad_view.reset ();
input = mapping_inputs[current_input_index++];
gamepad_view.highlight (input, true);
update_info_message ();
}
private void update_info_message () {
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;
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment