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.cpp127
1 files changed, 127 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..d0ee64ad7
--- /dev/null
+++ b/src/input_common/mouse/mouse_input.cpp
@@ -0,0 +1,127 @@
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 const auto button_index = static_cast<std::size_t>(button_);
58 buttons |= static_cast<u16>(button);
59 last_button = static_cast<MouseButton>(button_);
60
61 mouse_info[button_index].mouse_origin = Common::MakeVec(x, y);
62 mouse_info[button_index].last_mouse_position = Common::MakeVec(x, y);
63 mouse_info[button_index].data.pressed = true;
64}
65
66void Mouse::MouseMove(int x, int y) {
67 for (MouseInfo& info : mouse_info) {
68 if (info.data.pressed) {
69 auto mouse_move = Common::MakeVec(x, y) - info.mouse_origin;
70 auto mouse_change = Common::MakeVec(x, y) - info.last_mouse_position;
71 info.last_mouse_position = Common::MakeVec(x, y);
72 info.data.axis = {mouse_move.x, -mouse_move.y};
73
74 if (mouse_change.x == 0 && mouse_change.y == 0) {
75 info.tilt_speed = 0;
76 } else {
77 info.tilt_direction = mouse_change.Cast<float>();
78 info.tilt_speed = info.tilt_direction.Normalize() * info.sensitivity;
79 }
80 }
81 }
82}
83
84void Mouse::ReleaseButton(int button_) {
85 if (button_ >= static_cast<int>(mouse_info.size())) {
86 return;
87 }
88
89 int button = 1 << button_;
90 const auto button_index = static_cast<std::size_t>(button_);
91 buttons &= static_cast<u16>(0xFF - button);
92
93 mouse_info[button_index].tilt_speed = 0;
94 mouse_info[button_index].data.pressed = false;
95 mouse_info[button_index].data.axis = {0, 0};
96}
97
98void Mouse::BeginConfiguration() {
99 buttons = 0;
100 last_button = MouseButton::Undefined;
101 mouse_queue.Clear();
102 configuring = true;
103}
104
105void Mouse::EndConfiguration() {
106 buttons = 0;
107 last_button = MouseButton::Undefined;
108 mouse_queue.Clear();
109 configuring = false;
110}
111
112Common::SPSCQueue<MouseStatus>& Mouse::GetMouseQueue() {
113 return mouse_queue;
114}
115
116const Common::SPSCQueue<MouseStatus>& Mouse::GetMouseQueue() const {
117 return mouse_queue;
118}
119
120MouseData& Mouse::GetMouseState(std::size_t button) {
121 return mouse_info[button].data;
122}
123
124const MouseData& Mouse::GetMouseState(std::size_t button) const {
125 return mouse_info[button].data;
126}
127} // namespace MouseInput