Debian Codegraph @

xbmc/input/mouse/generic/MouseInputHandling.cpp

raw
1 /*
2 * Copyright (C) 2016-2024 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "MouseInputHandling.h"
10
11 #include "input/InputTranslator.h"
12 #include "input/joysticks/interfaces/IButtonMap.h"
13 #include "input/mouse/interfaces/IMouseInputHandler.h"
14
15 using namespace KODI;
16 using namespace MOUSE;
17
18 CMouseInputHandling::CMouseInputHandling(IMouseInputHandler* handler,
19 JOYSTICK::IButtonMap* buttonMap)
20 : m_handler(handler), m_buttonMap(buttonMap)
21 {
22 }
23
24 bool CMouseInputHandling::OnPosition(int x, int y)
25 {
26 using namespace JOYSTICK;
27
28 if (!m_bHasPosition)
29 {
30 m_bHasPosition = true;
31 m_x = x;
32 m_y = y;
33 return true;
34 }
35
36 int dx = x - m_x;
37 int dy = y - m_y;
38
39 bool bHandled = false;
40
41 // Get direction of motion
42 POINTER_DIRECTION dir = GetPointerDirection(dx, dy);
43
44 CDriverPrimitive source(dir);
45 if (source.IsValid())
46 {
47 // Get pointer in direction of motion
48 PointerName pointerName;
49 if (m_buttonMap->GetFeature(source, pointerName))
50 {
51 // Get orthogonal direction of motion
52 POINTER_DIRECTION dirCCW = GetOrthogonalDirectionCCW(dir);
53
54 // Get mapped directions of motion for rotation and reflection
55 CDriverPrimitive target;
56 CDriverPrimitive targetCCW;
57
58 if (m_buttonMap->GetRelativePointer(pointerName, dir, target))
59 m_buttonMap->GetRelativePointer(pointerName, dirCCW, targetCCW);
60
61 if (target.IsValid())
62 {
63 // Invert y to right-handed cartesian system
64 dy *= -1;
65
66 // Perform rotation
67 int rotation[2][2] = {{1, 0}, {0, 1}};
68
69 GetRotation(dir, target.PointerDirection(), rotation);
70
71 dx = rotation[0][0] * dx + rotation[0][1] * dy;
72 dy = rotation[1][0] * dx + rotation[1][1] * dy;
73
74 if (targetCCW.IsValid())
75 {
76 // Perform reflection
77 int reflection[2][2] = {{1, 0}, {0, 1}};
78
79 GetReflectionCCW(target.PointerDirection(), targetCCW.PointerDirection(), reflection);
80
81 dx = reflection[0][0] * dx + reflection[0][1] * dy;
82 dy = reflection[1][0] * dx + reflection[1][1] * dy;
83 }
84
85 // Invert y back to left-handed coordinate system
86 dy *= -1;
87 }
88
89 bHandled = m_handler->OnMotion(pointerName, dx, dy);
90 }
91 }
92 else
93 {
94 // Don't fall through - might disrupt the game
95 bHandled = true;
96 }
97
98 m_x = x;
99 m_y = y;
100
101 m_handler->OnInputFrame();
102
103 return bHandled;
104 }
105
106 bool CMouseInputHandling::OnButtonPress(BUTTON_ID button)
107 {
108 bool bHandled = false;
109
110 JOYSTICK::CDriverPrimitive source(button);
111
112 ButtonName buttonName;
113 if (m_buttonMap->GetFeature(source, buttonName))
114 bHandled = m_handler->OnButtonPress(buttonName);
115
116 m_handler->OnInputFrame();
117
118 return bHandled;
119 }
120
121 void CMouseInputHandling::OnButtonRelease(BUTTON_ID button)
122 {
123 JOYSTICK::CDriverPrimitive source(button);
124
125 ButtonName buttonName;
126 if (m_buttonMap->GetFeature(source, buttonName))
127 m_handler->OnButtonRelease(buttonName);
128
129 m_handler->OnInputFrame();
130 }
131
132 POINTER_DIRECTION CMouseInputHandling::GetPointerDirection(int x, int y)
133 {
134 using namespace INPUT;
135
136 return CInputTranslator::VectorToCardinalDirection(static_cast<float>(x), static_cast<float>(-y));
137 }
138
139 POINTER_DIRECTION CMouseInputHandling::GetOrthogonalDirectionCCW(POINTER_DIRECTION direction)
140 {
141 switch (direction)
142 {
143 case POINTER_DIRECTION::RIGHT:
144 return POINTER_DIRECTION::UP;
145 case POINTER_DIRECTION::UP:
146 return POINTER_DIRECTION::LEFT;
147 case POINTER_DIRECTION::LEFT:
148 return POINTER_DIRECTION::DOWN;
149 case POINTER_DIRECTION::DOWN:
150 return POINTER_DIRECTION::RIGHT;
151 default:
152 break;
153 }
154
155 return POINTER_DIRECTION::NONE;
156 }
157
158 void CMouseInputHandling::GetRotation(POINTER_DIRECTION source,
159 POINTER_DIRECTION target,
160 int (&rotation)[2][2])
161 {
162 switch (source)
163 {
164 case POINTER_DIRECTION::RIGHT:
165 {
166 switch (target)
167 {
168 case POINTER_DIRECTION::UP:
169 GetRotation(90, rotation);
170 break;
171 case POINTER_DIRECTION::LEFT:
172 GetRotation(180, rotation);
173 break;
174 case POINTER_DIRECTION::DOWN:
175 GetRotation(270, rotation);
176 break;
177 default:
178 break;
179 }
180 break;
181 }
182 case POINTER_DIRECTION::UP:
183 {
184 switch (target)
185 {
186 case POINTER_DIRECTION::LEFT:
187 GetRotation(90, rotation);
188 break;
189 case POINTER_DIRECTION::DOWN:
190 GetRotation(180, rotation);
191 break;
192 case POINTER_DIRECTION::RIGHT:
193 GetRotation(270, rotation);
194 break;
195 default:
196 break;
197 }
198 break;
199 }
200 case POINTER_DIRECTION::LEFT:
201 {
202 switch (target)
203 {
204 case POINTER_DIRECTION::DOWN:
205 GetRotation(90, rotation);
206 break;
207 case POINTER_DIRECTION::RIGHT:
208 GetRotation(180, rotation);
209 break;
210 case POINTER_DIRECTION::UP:
211 GetRotation(270, rotation);
212 break;
213 default:
214 break;
215 }
216 break;
217 }
218 case POINTER_DIRECTION::DOWN:
219 {
220 switch (target)
221 {
222 case POINTER_DIRECTION::RIGHT:
223 GetRotation(90, rotation);
224 break;
225 case POINTER_DIRECTION::UP:
226 GetRotation(180, rotation);
227 break;
228 case POINTER_DIRECTION::LEFT:
229 GetRotation(270, rotation);
230 break;
231 default:
232 break;
233 }
234 break;
235 }
236 default:
237 break;
238 }
239 }
240
241 void CMouseInputHandling::GetRotation(int deg, int (&rotation)[2][2])
242 {
243 switch (deg)
244 {
245 case 90:
246 {
247 rotation[0][0] = 0;
248 rotation[0][1] = -1;
249 rotation[1][0] = 1;
250 rotation[1][1] = 0;
251 break;
252 }
253 case 180:
254 {
255 rotation[0][0] = -1;
256 rotation[0][1] = 0;
257 rotation[1][0] = 0;
258 rotation[1][1] = -1;
259 break;
260 }
261 case 270:
262 {
263 rotation[0][0] = 0;
264 rotation[0][1] = 1;
265 rotation[1][0] = -1;
266 rotation[1][1] = 0;
267 break;
268 }
269 default:
270 break;
271 }
272 }
273
274 void CMouseInputHandling::GetReflectionCCW(POINTER_DIRECTION source,
275 POINTER_DIRECTION target,
276 int (&rotation)[2][2])
277 {
278 switch (source)
279 {
280 case POINTER_DIRECTION::RIGHT:
281 {
282 switch (target)
283 {
284 case POINTER_DIRECTION::DOWN:
285 GetReflection(0, rotation);
286 break;
287 default:
288 break;
289 }
290 break;
291 }
292 case POINTER_DIRECTION::UP:
293 {
294 switch (target)
295 {
296 case POINTER_DIRECTION::RIGHT:
297 GetReflection(90, rotation);
298 break;
299 default:
300 break;
301 }
302 break;
303 }
304 case POINTER_DIRECTION::LEFT:
305 {
306 switch (target)
307 {
308 case POINTER_DIRECTION::UP:
309 GetReflection(180, rotation);
310 break;
311 default:
312 break;
313 }
314 break;
315 }
316 case POINTER_DIRECTION::DOWN:
317 {
318 switch (target)
319 {
320 case POINTER_DIRECTION::LEFT:
321 GetReflection(270, rotation);
322 break;
323 default:
324 break;
325 }
326 break;
327 }
328 default:
329 break;
330 }
331 }
332
333 void CMouseInputHandling::GetReflection(int deg, int (&reflection)[2][2])
334 {
335 switch (deg)
336 {
337 case 0:
338 case 180:
339 {
340 reflection[0][0] = 1;
341 reflection[0][1] = 0;
342 reflection[1][0] = 0;
343 reflection[1][1] = -1;
344 break;
345 }
346 case 90:
347 case 270:
348 {
349 reflection[0][0] = -1;
350 reflection[0][1] = 0;
351 reflection[1][0] = 0;
352 reflection[1][1] = 1;
353 break;
354 }
355 default:
356 break;
357 }
358 }