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.cpp125
1 files changed, 125 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..3f4264caa
--- /dev/null
+++ b/src/input_common/mouse/mouse_input.cpp
@@ -0,0 +1,125 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4
5#include "common/logging/log.h"
6#include "common/math_util.h"
7#include "common/param_package.h"
8#include "input_common/mouse/mouse_input.h"
9
10namespace MouseInput {
11
12Mouse::Mouse() {
13 update_thread = std::thread(&Mouse::UpdateThread, this);
14}
15
16Mouse::~Mouse() {
17 update_thread_running = false;
18 if (update_thread.joinable()) {
19 update_thread.join();
20 }
21}
22
23void Mouse::UpdateThread() {
24 constexpr int update_time = 10;
25 while (update_thread_running) {
26 for (MouseInfo& info : mouse_info) {
27 Common::Vec3f angular_direction = {-info.tilt_direction.y, 0.0f,
28 -info.tilt_direction.x};
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 MouseStatus pad_status{};
45 if (buttons != 0) {
46 pad_status.button = last_button;
47 mouse_queue.Push(pad_status);
48 }
49}
50
51void Mouse::PressButton(int x, int y, int button_) {
52 if (button_ >= static_cast<int>(mouse_info.size())) {
53 return;
54 }
55
56 int button = 1 << button_;
57 buttons |= static_cast<u16>(button);
58 last_button = static_cast<MouseButton>(button_);
59
60 mouse_info[button_].mouse_origin = Common::MakeVec(x, y);
61 mouse_info[button_].last_mouse_position = Common::MakeVec(x, y);
62 mouse_info[button_].data.pressed = true;
63}
64
65void Mouse::MouseMove(int x, int y) {
66 for (MouseInfo& info : mouse_info) {
67 if (info.data.pressed) {
68 auto mouse_move = Common::MakeVec(x, y) - info.mouse_origin;
69 auto mouse_change = Common::MakeVec(x, y) - info.last_mouse_position;
70 info.last_mouse_position = Common::MakeVec(x, y);
71 info.data.axis = {mouse_move.x, -mouse_move.y};
72
73 if (mouse_change.x == 0 && mouse_change.y == 0) {
74 info.tilt_speed = 0;
75 } else {
76 info.tilt_direction = mouse_change.Cast<float>();
77 info.tilt_speed = info.tilt_direction.Normalize() * info.sensitivity;
78 }
79 }
80 }
81}
82
83void Mouse::ReleaseButton(int button_) {
84 if (button_ >= static_cast<int>(mouse_info.size())) {
85 return;
86 }
87
88 int button = 1 << button_;
89 buttons &= static_cast<u16>(0xFF - button);
90
91 mouse_info[button_].tilt_speed = 0;
92 mouse_info[button_].data.pressed = false;
93 mouse_info[button_].data.axis = {0, 0};
94}
95
96void Mouse::BeginConfiguration() {
97 buttons = 0;
98 last_button = MouseButton::Undefined;
99 mouse_queue.Clear();
100 configuring = true;
101}
102
103void Mouse::EndConfiguration() {
104 buttons = 0;
105 last_button = MouseButton::Undefined;
106 mouse_queue.Clear();
107 configuring = false;
108}
109
110Common::SPSCQueue<MouseStatus>& Mouse::GetMouseQueue() {
111 return mouse_queue;
112}
113
114const Common::SPSCQueue<MouseStatus>& Mouse::GetMouseQueue() const {
115 return mouse_queue;
116}
117
118MouseData& Mouse::GetMouseState(std::size_t button) {
119 return mouse_info[button].data;
120}
121
122const MouseData& Mouse::GetMouseState(std::size_t button) const {
123 return mouse_info[button].data;
124}
125} // namespace MouseInput