diff options
| author | 2017-08-29 11:29:10 -0400 | |
|---|---|---|
| committer | 2017-08-29 11:29:10 -0400 | |
| commit | 75cd28a7cc1c73e1be1fc72d4b86b267cbb79081 (patch) | |
| tree | 18f3e7e5b864511da54d847e645dd23a69033924 /src/core/frontend/emu_window.cpp | |
| parent | Merge pull request #2905 from danzel/fix-2902 (diff) | |
| parent | EmuWindow: refactor touch input into a TouchDevice (diff) | |
| download | yuzu-75cd28a7cc1c73e1be1fc72d4b86b267cbb79081.tar.gz yuzu-75cd28a7cc1c73e1be1fc72d4b86b267cbb79081.tar.xz yuzu-75cd28a7cc1c73e1be1fc72d4b86b267cbb79081.zip | |
Merge pull request #2899 from wwylele/touch-refactor
Refactor touch input into a TouchDevice
Diffstat (limited to 'src/core/frontend/emu_window.cpp')
| -rw-r--r-- | src/core/frontend/emu_window.cpp | 71 |
1 files changed, 58 insertions, 13 deletions
diff --git a/src/core/frontend/emu_window.cpp b/src/core/frontend/emu_window.cpp index 54fa5c7fa..e67394177 100644 --- a/src/core/frontend/emu_window.cpp +++ b/src/core/frontend/emu_window.cpp | |||
| @@ -2,14 +2,55 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | 5 | #include <cmath> |
| 7 | #include "common/assert.h" | 6 | #include <mutex> |
| 8 | #include "core/3ds.h" | ||
| 9 | #include "core/core.h" | ||
| 10 | #include "core/frontend/emu_window.h" | 7 | #include "core/frontend/emu_window.h" |
| 8 | #include "core/frontend/input.h" | ||
| 11 | #include "core/settings.h" | 9 | #include "core/settings.h" |
| 12 | 10 | ||
| 11 | class EmuWindow::TouchState : public Input::Factory<Input::TouchDevice>, | ||
| 12 | public std::enable_shared_from_this<TouchState> { | ||
| 13 | public: | ||
| 14 | std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage&) override { | ||
| 15 | return std::make_unique<Device>(shared_from_this()); | ||
| 16 | } | ||
| 17 | |||
| 18 | std::mutex mutex; | ||
| 19 | |||
| 20 | bool touch_pressed = false; ///< True if touchpad area is currently pressed, otherwise false | ||
| 21 | |||
| 22 | float touch_x = 0.0f; ///< Touchpad X-position | ||
| 23 | float touch_y = 0.0f; ///< Touchpad Y-position | ||
| 24 | |||
| 25 | private: | ||
| 26 | class Device : public Input::TouchDevice { | ||
| 27 | public: | ||
| 28 | explicit Device(std::weak_ptr<TouchState>&& touch_state) : touch_state(touch_state) {} | ||
| 29 | std::tuple<float, float, bool> GetStatus() const override { | ||
| 30 | if (auto state = touch_state.lock()) { | ||
| 31 | std::lock_guard<std::mutex> guard(state->mutex); | ||
| 32 | return std::make_tuple(state->touch_x, state->touch_y, state->touch_pressed); | ||
| 33 | } | ||
| 34 | return std::make_tuple(0.0f, 0.0f, false); | ||
| 35 | } | ||
| 36 | |||
| 37 | private: | ||
| 38 | std::weak_ptr<TouchState> touch_state; | ||
| 39 | }; | ||
| 40 | }; | ||
| 41 | |||
| 42 | EmuWindow::EmuWindow() { | ||
| 43 | // TODO: Find a better place to set this. | ||
| 44 | config.min_client_area_size = std::make_pair(400u, 480u); | ||
| 45 | active_config = config; | ||
| 46 | touch_state = std::make_shared<TouchState>(); | ||
| 47 | Input::RegisterFactory<Input::TouchDevice>("emu_window", touch_state); | ||
| 48 | } | ||
| 49 | |||
| 50 | EmuWindow::~EmuWindow() { | ||
| 51 | Input::UnregisterFactory<Input::TouchDevice>("emu_window"); | ||
| 52 | } | ||
| 53 | |||
| 13 | /** | 54 | /** |
| 14 | * Check if the given x/y coordinates are within the touchpad specified by the framebuffer layout | 55 | * Check if the given x/y coordinates are within the touchpad specified by the framebuffer layout |
| 15 | * @param layout FramebufferLayout object describing the framebuffer size and screen positions | 56 | * @param layout FramebufferLayout object describing the framebuffer size and screen positions |
| @@ -38,22 +79,26 @@ void EmuWindow::TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y) { | |||
| 38 | if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y)) | 79 | if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y)) |
| 39 | return; | 80 | return; |
| 40 | 81 | ||
| 41 | touch_x = Core::kScreenBottomWidth * (framebuffer_x - framebuffer_layout.bottom_screen.left) / | 82 | std::lock_guard<std::mutex> guard(touch_state->mutex); |
| 42 | (framebuffer_layout.bottom_screen.right - framebuffer_layout.bottom_screen.left); | 83 | touch_state->touch_x = |
| 43 | touch_y = Core::kScreenBottomHeight * (framebuffer_y - framebuffer_layout.bottom_screen.top) / | 84 | static_cast<float>(framebuffer_x - framebuffer_layout.bottom_screen.left) / |
| 44 | (framebuffer_layout.bottom_screen.bottom - framebuffer_layout.bottom_screen.top); | 85 | (framebuffer_layout.bottom_screen.right - framebuffer_layout.bottom_screen.left); |
| 86 | touch_state->touch_y = | ||
| 87 | static_cast<float>(framebuffer_y - framebuffer_layout.bottom_screen.top) / | ||
| 88 | (framebuffer_layout.bottom_screen.bottom - framebuffer_layout.bottom_screen.top); | ||
| 45 | 89 | ||
| 46 | touch_pressed = true; | 90 | touch_state->touch_pressed = true; |
| 47 | } | 91 | } |
| 48 | 92 | ||
| 49 | void EmuWindow::TouchReleased() { | 93 | void EmuWindow::TouchReleased() { |
| 50 | touch_pressed = false; | 94 | std::lock_guard<std::mutex> guard(touch_state->mutex); |
| 51 | touch_x = 0; | 95 | touch_state->touch_pressed = false; |
| 52 | touch_y = 0; | 96 | touch_state->touch_x = 0; |
| 97 | touch_state->touch_y = 0; | ||
| 53 | } | 98 | } |
| 54 | 99 | ||
| 55 | void EmuWindow::TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y) { | 100 | void EmuWindow::TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y) { |
| 56 | if (!touch_pressed) | 101 | if (!touch_state->touch_pressed) |
| 57 | return; | 102 | return; |
| 58 | 103 | ||
| 59 | if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y)) | 104 | if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y)) |