summaryrefslogtreecommitdiff
path: root/src/core/frontend/emu_window.cpp
diff options
context:
space:
mode:
authorGravatar Feng Chen2021-12-18 13:57:14 +0800
committerGravatar GitHub2021-12-18 13:57:14 +0800
commite49184e6069a9d791d2df3c1958f5c4b1187e124 (patch)
treeb776caf722e0be0e680f67b0ad0842628162ef1c /src/core/frontend/emu_window.cpp
parentImplement convert legacy to generic (diff)
parentMerge pull request #7570 from ameerj/favorites-expanded (diff)
downloadyuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.tar.gz
yuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.tar.xz
yuzu-e49184e6069a9d791d2df3c1958f5c4b1187e124.zip
Merge branch 'yuzu-emu:master' into convert_legacy
Diffstat (limited to 'src/core/frontend/emu_window.cpp')
-rw-r--r--src/core/frontend/emu_window.cpp98
1 files changed, 10 insertions, 88 deletions
diff --git a/src/core/frontend/emu_window.cpp b/src/core/frontend/emu_window.cpp
index e1f7e5886..57c6ffc43 100644
--- a/src/core/frontend/emu_window.cpp
+++ b/src/core/frontend/emu_window.cpp
@@ -3,87 +3,23 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <mutex> 5#include <mutex>
6#include "common/settings.h"
7#include "core/frontend/emu_window.h" 6#include "core/frontend/emu_window.h"
8#include "core/frontend/input.h"
9 7
10namespace Core::Frontend { 8namespace Core::Frontend {
11 9
12GraphicsContext::~GraphicsContext() = default; 10GraphicsContext::~GraphicsContext() = default;
13 11
14class EmuWindow::TouchState : public Input::Factory<Input::TouchDevice>,
15 public std::enable_shared_from_this<TouchState> {
16public:
17 std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage&) override {
18 return std::make_unique<Device>(shared_from_this());
19 }
20
21 std::mutex mutex;
22
23 Input::TouchStatus status;
24
25private:
26 class Device : public Input::TouchDevice {
27 public:
28 explicit Device(std::weak_ptr<TouchState>&& touch_state_) : touch_state(touch_state_) {}
29 Input::TouchStatus GetStatus() const override {
30 if (auto state = touch_state.lock()) {
31 std::lock_guard guard{state->mutex};
32 return state->status;
33 }
34 return {};
35 }
36
37 private:
38 std::weak_ptr<TouchState> touch_state;
39 };
40};
41
42EmuWindow::EmuWindow() { 12EmuWindow::EmuWindow() {
43 // TODO: Find a better place to set this. 13 // TODO: Find a better place to set this.
44 config.min_client_area_size = 14 config.min_client_area_size =
45 std::make_pair(Layout::MinimumSize::Width, Layout::MinimumSize::Height); 15 std::make_pair(Layout::MinimumSize::Width, Layout::MinimumSize::Height);
46 active_config = config; 16 active_config = config;
47 touch_state = std::make_shared<TouchState>();
48 Input::RegisterFactory<Input::TouchDevice>("emu_window", touch_state);
49}
50
51EmuWindow::~EmuWindow() {
52 Input::UnregisterFactory<Input::TouchDevice>("emu_window");
53}
54
55/**
56 * Check if the given x/y coordinates are within the touchpad specified by the framebuffer layout
57 * @param layout FramebufferLayout object describing the framebuffer size and screen positions
58 * @param framebuffer_x Framebuffer x-coordinate to check
59 * @param framebuffer_y Framebuffer y-coordinate to check
60 * @return True if the coordinates are within the touchpad, otherwise false
61 */
62static bool IsWithinTouchscreen(const Layout::FramebufferLayout& layout, u32 framebuffer_x,
63 u32 framebuffer_y) {
64 return (framebuffer_y >= layout.screen.top && framebuffer_y < layout.screen.bottom &&
65 framebuffer_x >= layout.screen.left && framebuffer_x < layout.screen.right);
66}
67
68std::pair<u32, u32> EmuWindow::ClipToTouchScreen(u32 new_x, u32 new_y) const {
69 new_x = std::max(new_x, framebuffer_layout.screen.left);
70 new_x = std::min(new_x, framebuffer_layout.screen.right - 1);
71
72 new_y = std::max(new_y, framebuffer_layout.screen.top);
73 new_y = std::min(new_y, framebuffer_layout.screen.bottom - 1);
74
75 return std::make_pair(new_x, new_y);
76} 17}
77 18
78void EmuWindow::TouchPressed(u32 framebuffer_x, u32 framebuffer_y, size_t id) { 19EmuWindow::~EmuWindow() {}
79 if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y)) {
80 return;
81 }
82 if (id >= touch_state->status.size()) {
83 return;
84 }
85 20
86 std::lock_guard guard{touch_state->mutex}; 21std::pair<f32, f32> EmuWindow::MapToTouchScreen(u32 framebuffer_x, u32 framebuffer_y) const {
22 std::tie(framebuffer_x, framebuffer_y) = ClipToTouchScreen(framebuffer_x, framebuffer_y);
87 const float x = 23 const float x =
88 static_cast<float>(framebuffer_x - framebuffer_layout.screen.left) / 24 static_cast<float>(framebuffer_x - framebuffer_layout.screen.left) /
89 static_cast<float>(framebuffer_layout.screen.right - framebuffer_layout.screen.left); 25 static_cast<float>(framebuffer_layout.screen.right - framebuffer_layout.screen.left);
@@ -91,31 +27,17 @@ void EmuWindow::TouchPressed(u32 framebuffer_x, u32 framebuffer_y, size_t id) {
91 static_cast<float>(framebuffer_y - framebuffer_layout.screen.top) / 27 static_cast<float>(framebuffer_y - framebuffer_layout.screen.top) /
92 static_cast<float>(framebuffer_layout.screen.bottom - framebuffer_layout.screen.top); 28 static_cast<float>(framebuffer_layout.screen.bottom - framebuffer_layout.screen.top);
93 29
94 touch_state->status[id] = std::make_tuple(x, y, true); 30 return std::make_pair(x, y);
95}
96
97void EmuWindow::TouchReleased(size_t id) {
98 if (id >= touch_state->status.size()) {
99 return;
100 }
101 std::lock_guard guard{touch_state->mutex};
102 touch_state->status[id] = std::make_tuple(0.0f, 0.0f, false);
103} 31}
104 32
105void EmuWindow::TouchMoved(u32 framebuffer_x, u32 framebuffer_y, size_t id) { 33std::pair<u32, u32> EmuWindow::ClipToTouchScreen(u32 new_x, u32 new_y) const {
106 if (id >= touch_state->status.size()) { 34 new_x = std::max(new_x, framebuffer_layout.screen.left);
107 return; 35 new_x = std::min(new_x, framebuffer_layout.screen.right - 1);
108 }
109
110 if (!std::get<2>(touch_state->status[id])) {
111 return;
112 }
113 36
114 if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y)) { 37 new_y = std::max(new_y, framebuffer_layout.screen.top);
115 std::tie(framebuffer_x, framebuffer_y) = ClipToTouchScreen(framebuffer_x, framebuffer_y); 38 new_y = std::min(new_y, framebuffer_layout.screen.bottom - 1);
116 }
117 39
118 TouchPressed(framebuffer_x, framebuffer_y, id); 40 return std::make_pair(new_x, new_y);
119} 41}
120 42
121void EmuWindow::UpdateCurrentFramebufferLayout(u32 width, u32 height) { 43void EmuWindow::UpdateCurrentFramebufferLayout(u32 width, u32 height) {