Save gamepad mapping only if buttons/combo changes

This commit is contained in:
rock88 2020-06-17 21:52:17 +03:00
parent cc39487ad0
commit a0ceb44757
4 changed files with 23 additions and 10 deletions

View file

@ -89,7 +89,7 @@ void GamepadMapper::set_mapped_button(GamepadButtons origin_button, GamepadButto
m_gamepad_map[origin_button] = new_button;
}
GamepadButtons GamepadMapper::mapped_button(int button) {
GamepadButtons GamepadMapper::mapped_button(GamepadButtons button) {
return m_gamepad_map[button];
}

View file

@ -55,7 +55,7 @@ public:
GamepadButtons convert_nanogui_analog_axis(int axis);
void set_mapped_button(GamepadButtons origin_button, GamepadButtons new_button);
GamepadButtons mapped_button(int origin_button);
GamepadButtons mapped_button(GamepadButtons origin_button);
void set_combo_buttons(std::array<GamepadButtons, 3> buttons, GamepadCombo combo);
std::array<GamepadButtons, 3> combo_buttons(GamepadCombo combo);

View file

@ -173,7 +173,7 @@ void InputSettingsWindow::reload_gamepad_input_settings() {
}
button_container->add<Label>(GamepadMapper::mapper()->button_label((GamepadButtons)i, false));
auto button = button_container->add<Button>(GamepadMapper::mapper()->button_label(GamepadMapper::mapper()->mapped_button(i), true));
auto button = button_container->add<Button>(GamepadMapper::mapper()->button_label(GamepadMapper::mapper()->mapped_button((GamepadButtons)i), true));
button->set_callback([this, i] {
assign_button((GamepadButtons)i);
});
@ -260,27 +260,39 @@ bool InputSettingsWindow::gamepad_analog_event(int jid, int axis, float value) {
}
void InputSettingsWindow::window_disappear() {
GamepadMapper::mapper()->save_gamepad_map(m_app_id);
if (m_has_changes) {
GamepadMapper::mapper()->save_gamepad_map(m_app_id);
}
}
void InputSettingsWindow::assign_button(GamepadButtons button) {
GamepadButtons current = GamepadMapper::mapper()->mapped_button(button);
overlay = screen()->add<GamepadInputOverlay>("Press button for reassign \"" + GamepadMapper::mapper()->button_label(button, false) + "\"");
overlay->set_completion([this, button](auto result) {
overlay->set_completion([this, button, current](auto result) {
overlay->dispose();
overlay = NULL;
GamepadMapper::mapper()->set_mapped_button(button, result[0]);
reload_gamepad_input_settings();
if (current != result[0]) {
m_has_changes = true;
GamepadMapper::mapper()->set_mapped_button(button, result[0]);
reload_gamepad_input_settings();
}
});
}
void InputSettingsWindow::assign_combo(GamepadCombo combo, int buttons_count) {
auto current = GamepadMapper::mapper()->combo_buttons(combo);
overlay = screen()->add<GamepadInputOverlay>("Enter combo for reassign \"" + GamepadMapper::mapper()->combo_label(combo) + "\"", buttons_count);
overlay->set_completion([this, combo](auto result) {
overlay->set_completion([this, combo, current](auto result) {
overlay->dispose();
overlay = NULL;
GamepadMapper::mapper()->set_combo_buttons(result, combo);
reload_combo_settings();
if (current != result) {
m_has_changes = true;
GamepadMapper::mapper()->set_combo_buttons(result, combo);
reload_combo_settings();
}
});
}

View file

@ -24,4 +24,5 @@ private:
void assign_combo(GamepadCombo combo, int buttons_count);
int m_app_id;
bool m_has_changes = false;
};