diff options
author | bunnei <bunneidev@gmail.com> | 2021-03-11 20:00:44 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-11 20:00:44 +0100 |
commit | 827dcad26ea09fdf1926984cb6f8a0a4976996c6 (patch) | |
tree | 99955e9f688581d6850c2b6c968d1ccff9fc9672 /src/input_common | |
parent | Merge pull request #5891 from ameerj/bgra-ogl (diff) | |
parent | Enable mouse toggle buttons (diff) | |
download | yuzu-827dcad26ea09fdf1926984cb6f8a0a4976996c6.tar yuzu-827dcad26ea09fdf1926984cb6f8a0a4976996c6.tar.gz yuzu-827dcad26ea09fdf1926984cb6f8a0a4976996c6.tar.bz2 yuzu-827dcad26ea09fdf1926984cb6f8a0a4976996c6.tar.lz yuzu-827dcad26ea09fdf1926984cb6f8a0a4976996c6.tar.xz yuzu-827dcad26ea09fdf1926984cb6f8a0a4976996c6.tar.zst yuzu-827dcad26ea09fdf1926984cb6f8a0a4976996c6.zip |
Diffstat (limited to 'src/input_common')
-rw-r--r-- | src/input_common/keyboard.cpp | 31 | ||||
-rw-r--r-- | src/input_common/mouse/mouse_input.cpp | 36 | ||||
-rw-r--r-- | src/input_common/mouse/mouse_input.h | 5 | ||||
-rw-r--r-- | src/input_common/mouse/mouse_poller.cpp | 20 |
4 files changed, 84 insertions, 8 deletions
diff --git a/src/input_common/keyboard.cpp b/src/input_common/keyboard.cpp index 24a6f7a33..c467ff4c5 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; } diff --git a/src/input_common/mouse/mouse_input.cpp b/src/input_common/mouse/mouse_input.cpp index d81e790ee..329e416c7 100644 --- a/src/input_common/mouse/mouse_input.cpp +++ b/src/input_common/mouse/mouse_input.cpp @@ -162,6 +162,42 @@ void Mouse::EndConfiguration() { configuring = false; } +bool Mouse::ToggleButton(std::size_t button_) { + if (button_ >= mouse_info.size()) { + return false; + } + const auto button = 1U << button_; + const bool button_state = (toggle_buttons & button) != 0; + const bool button_lock = (lock_buttons & button) != 0; + + if (button_lock) { + return button_state; + } + + lock_buttons |= static_cast<u16>(button); + + if (button_state) { + toggle_buttons &= static_cast<u16>(0xFF - button); + } else { + toggle_buttons |= static_cast<u16>(button); + } + + return !button_state; +} + +bool Mouse::UnlockButton(std::size_t button_) { + if (button_ >= mouse_info.size()) { + return false; + } + + const auto button = 1U << button_; + const bool button_state = (toggle_buttons & button) != 0; + + lock_buttons &= static_cast<u16>(0xFF - button); + + return button_state; +} + Common::SPSCQueue<MouseStatus>& Mouse::GetMouseQueue() { return mouse_queue; } diff --git a/src/input_common/mouse/mouse_input.h b/src/input_common/mouse/mouse_input.h index 3622fe080..750d9b011 100644 --- a/src/input_common/mouse/mouse_input.h +++ b/src/input_common/mouse/mouse_input.h @@ -69,6 +69,9 @@ public: */ void ReleaseButton(MouseButton button_); + [[nodiscard]] bool ToggleButton(std::size_t button_); + [[nodiscard]] bool UnlockButton(std::size_t button_); + [[nodiscard]] Common::SPSCQueue<MouseStatus>& GetMouseQueue(); [[nodiscard]] const Common::SPSCQueue<MouseStatus>& GetMouseQueue() const; @@ -94,6 +97,8 @@ private: }; u16 buttons{}; + u16 toggle_buttons{}; + u16 lock_buttons{}; std::thread update_thread; MouseButton last_button{MouseButton::Undefined}; std::array<MouseInfo, 7> mouse_info; diff --git a/src/input_common/mouse/mouse_poller.cpp b/src/input_common/mouse/mouse_poller.cpp index bb56787ee..0e1db54fb 100644 --- a/src/input_common/mouse/mouse_poller.cpp +++ b/src/input_common/mouse/mouse_poller.cpp @@ -14,16 +14,25 @@ namespace InputCommon { class MouseButton final : public Input::ButtonDevice { public: - explicit MouseButton(u32 button_, const MouseInput::Mouse* mouse_input_) - : button(button_), mouse_input(mouse_input_) {} + explicit MouseButton(u32 button_, bool toggle_, MouseInput::Mouse* mouse_input_) + : button(button_), toggle(toggle_), mouse_input(mouse_input_) {} bool GetStatus() const override { - return mouse_input->GetMouseState(button).pressed; + const bool button_state = mouse_input->GetMouseState(button).pressed; + if (!toggle) { + return button_state; + } + + if (button_state) { + return mouse_input->ToggleButton(button); + } + return mouse_input->UnlockButton(button); } private: const u32 button; - const MouseInput::Mouse* mouse_input; + const bool toggle; + MouseInput::Mouse* mouse_input; }; MouseButtonFactory::MouseButtonFactory(std::shared_ptr<MouseInput::Mouse> mouse_input_) @@ -32,8 +41,9 @@ MouseButtonFactory::MouseButtonFactory(std::shared_ptr<MouseInput::Mouse> mouse_ std::unique_ptr<Input::ButtonDevice> MouseButtonFactory::Create( const Common::ParamPackage& params) { const auto button_id = params.Get("button", 0); + const auto toggle = params.Get("toggle", false); - return std::make_unique<MouseButton>(button_id, mouse_input.get()); + return std::make_unique<MouseButton>(button_id, toggle, mouse_input.get()); } Common::ParamPackage MouseButtonFactory::GetNextInput() const { |