summaryrefslogtreecommitdiff
path: root/src/input_common/mouse/mouse_input.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/mouse/mouse_input.cpp')
-rw-r--r--src/input_common/mouse/mouse_input.cpp129
1 files changed, 129 insertions, 0 deletions
diff --git a/src/input_common/mouse/mouse_input.cpp b/src/input_common/mouse/mouse_input.cpp
new file mode 100644
index 000000000..10786a541
--- /dev/null
+++ b/src/input_common/mouse/mouse_input.cpp
@@ -0,0 +1,129 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4
5#include "input_common/mouse/mouse_input.h"
6
7namespace MouseInput {
8
9Mouse::Mouse() {
10 update_thread = std::thread(&Mouse::UpdateThread, this);
11}
12
13Mouse::~Mouse() {
14 update_thread_running = false;
15 if (update_thread.joinable()) {
16 update_thread.join();
17 }
18}
19
20void Mouse::UpdateThread() {
21 constexpr int update_time = 10;
22 while (update_thread_running) {
23 for (MouseInfo& info : mouse_info) {
24 const Common::Vec3f angular_direction{
25 -info.tilt_direction.y,
26 0.0f,
27 -info.tilt_direction.x,
28 };
29
30 info.motion.SetGyroscope(angular_direction * info.tilt_speed);
31 info.motion.UpdateRotation(update_time * 1000);
32 info.motion.UpdateOrientation(update_time * 1000);
33 info.tilt_speed = 0;
34 info.data.motion = info.motion.GetMotion();
35 }
36 if (configuring) {
37 UpdateYuzuSettings();
38 }
39 std::this_thread::sleep_for(std::chrono::milliseconds(update_time));
40 }
41}
42
43void Mouse::UpdateYuzuSettings() {
44 if (buttons == 0) {
45 return;
46 }
47
48 mouse_queue.Push(MouseStatus{
49 .button = last_button,
50 });
51}
52
53void Mouse::PressButton(int x, int y, int button_) {
54 const auto button_index = static_cast<std::size_t>(button_);
55 if (button_index >= mouse_info.size()) {
56 return;
57 }
58
59 const auto button = 1U << button_index;
60 buttons |= static_cast<u16>(button);
61 last_button = static_cast<MouseButton>(button_index);
62
63 mouse_info[button_index].mouse_origin = Common::MakeVec(x, y);
64 mouse_info[button_index].last_mouse_position = Common::MakeVec(x, y);
65 mouse_info[button_index].data.pressed = true;
66}
67
68void Mouse::MouseMove(int x, int y) {
69 for (MouseInfo& info : mouse_info) {
70 if (info.data.pressed) {
71 const auto mouse_move = Common::MakeVec(x, y) - info.mouse_origin;
72 const auto mouse_change = Common::MakeVec(x, y) - info.last_mouse_position;
73 info.last_mouse_position = Common::MakeVec(x, y);
74 info.data.axis = {mouse_move.x, -mouse_move.y};
75
76 if (mouse_change.x == 0 && mouse_change.y == 0) {
77 info.tilt_speed = 0;
78 } else {
79 info.tilt_direction = mouse_change.Cast<float>();
80 info.tilt_speed = info.tilt_direction.Normalize() * info.sensitivity;
81 }
82 }
83 }
84}
85
86void Mouse::ReleaseButton(int button_) {
87 const auto button_index = static_cast<std::size_t>(button_);
88 if (button_index >= mouse_info.size()) {
89 return;
90 }
91
92 const auto button = 1U << button_index;
93 buttons &= static_cast<u16>(0xFF - button);
94
95 mouse_info[button_index].tilt_speed = 0;
96 mouse_info[button_index].data.pressed = false;
97 mouse_info[button_index].data.axis = {0, 0};
98}
99
100void Mouse::BeginConfiguration() {
101 buttons = 0;
102 last_button = MouseButton::Undefined;
103 mouse_queue.Clear();
104 configuring = true;
105}
106
107void Mouse::EndConfiguration() {
108 buttons = 0;
109 last_button = MouseButton::Undefined;
110 mouse_queue.Clear();
111 configuring = false;
112}
113
114Common::SPSCQueue<MouseStatus>& Mouse::GetMouseQueue() {
115 return mouse_queue;
116}
117
118const Common::SPSCQueue<MouseStatus>& Mouse::GetMouseQueue() const {
119 return mouse_queue;
120}
121
122MouseData& Mouse::GetMouseState(std::size_t button) {
123 return mouse_info[button].data;
124}
125
126const MouseData& Mouse::GetMouseState(std::size_t button) const {
127 return mouse_info[button].data;
128}
129} // namespace MouseInput