diff options
author | bunnei <bunneidev@gmail.com> | 2021-03-11 11:00:44 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-11 11:00:44 -0800 |
commit | 827dcad26ea09fdf1926984cb6f8a0a4976996c6 (patch) | |
tree | 99955e9f688581d6850c2b6c968d1ccff9fc9672 /src/input_common/keyboard.cpp | |
parent | daf5c5060b4b2e4aa985fbfe9724eb99c51bbd71 (diff) | |
parent | 41e94b7b99f83a45633d555160b31b50f021c350 (diff) |
Merge pull request #6040 from german77/toggleKeyboard
Enable toggle buttons for keyboard and mouse
Diffstat (limited to 'src/input_common/keyboard.cpp')
-rw-r--r-- | src/input_common/keyboard.cpp | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/src/input_common/keyboard.cpp b/src/input_common/keyboard.cpp index 24a6f7a337..c467ff4c5f 100644 --- a/src/input_common/keyboard.cpp +++ b/src/input_common/keyboard.cpp @@ -12,20 +12,39 @@ namespace InputCommon { class KeyButton final : public Input::ButtonDevice { public: - explicit KeyButton(std::shared_ptr<KeyButtonList> key_button_list_) - : key_button_list(std::move(key_button_list_)) {} + explicit KeyButton(std::shared_ptr<KeyButtonList> 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(std::memory_order_relaxed); + } return status.load(); } + void ToggleButton() { + if (lock) { + return; + } + lock = true; + const bool old_toggle_status = toggled_status.load(); + toggled_status.store(!old_toggle_status); + } + + void UnlockButton() { + lock = false; + } + friend class KeyButtonList; private: std::shared_ptr<KeyButtonList> key_button_list; std::atomic<bool> status{false}; + std::atomic<bool> toggled_status{false}; + bool lock{false}; + const bool toggle; }; struct KeyButtonPair { @@ -51,6 +70,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 +99,8 @@ KeyButton::~KeyButton() { std::unique_ptr<Input::ButtonDevice> Keyboard::Create(const Common::ParamPackage& params) { const int key_code = params.Get("code", 0); - std::unique_ptr<KeyButton> button = std::make_unique<KeyButton>(key_button_list); + const bool toggle = params.Get("toggle", false); + std::unique_ptr<KeyButton> button = std::make_unique<KeyButton>(key_button_list, toggle); key_button_list->AddKeyButton(key_code, button.get()); return button; } |