From 1f228c51ca2c510622f4204937f90c7f2bbc7bf6 Mon Sep 17 00:00:00 2001 From: german Date: Fri, 5 Mar 2021 19:21:04 -0600 Subject: Enable button toggle for keyboard in the modifier button --- src/input_common/keyboard.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'src/input_common') diff --git a/src/input_common/keyboard.cpp b/src/input_common/keyboard.cpp index 24a6f7a33..fa0e60ac1 100644 --- a/src/input_common/keyboard.cpp +++ b/src/input_common/keyboard.cpp @@ -12,20 +12,37 @@ namespace InputCommon { class KeyButton final : public Input::ButtonDevice { public: - explicit KeyButton(std::shared_ptr key_button_list_) - : key_button_list(std::move(key_button_list_)) {} + explicit KeyButton(std::shared_ptr key_button_list_, bool toggle_) + : key_button_list(std::move(key_button_list_)), toggle(toggle_) {} ~KeyButton() override; bool GetStatus() const override { + if (toggle) { + return toggled_status.load(); + } return status.load(); } + void ToggleButton() { + if (!lock) { + lock = true; + toggled_status.store(!toggled_status.load()); + } + } + + void UnlockButton() { + lock = false; + } + friend class KeyButtonList; private: std::shared_ptr key_button_list; std::atomic status{false}; + std::atomic toggled_status{false}; + bool lock = {}; + const bool toggle; }; struct KeyButtonPair { @@ -51,6 +68,11 @@ public: for (const KeyButtonPair& pair : list) { if (pair.key_code == key_code) { pair.key_button->status.store(pressed); + if (pressed) { + pair.key_button->ToggleButton(); + } else { + pair.key_button->UnlockButton(); + } } } } @@ -75,7 +97,8 @@ KeyButton::~KeyButton() { std::unique_ptr Keyboard::Create(const Common::ParamPackage& params) { const int key_code = params.Get("code", 0); - std::unique_ptr button = std::make_unique(key_button_list); + const bool toggle = params.Get("toggle", false); + std::unique_ptr button = std::make_unique(key_button_list, toggle); key_button_list->AddKeyButton(key_code, button.get()); return button; } -- cgit v1.2.3