Debian Codegraph @

src/engine/localevent.h

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2019 - 2026 *
4 * *
5 * Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 *
6 * Copyright (C) 2008 by Josh Matthews <[email protected]> *
7 * Copyright (C) 2006 by Andrey Afletdinov <[email protected]> *
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, write to the *
21 * Free Software Foundation, Inc., *
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23 ***************************************************************************/
24
25 #pragma once
26
27 #include <cstddef>
28 #include <cstdint>
29 #include <functional>
30 #include <memory>
31 #include <optional>
32 #include <string>
33 #include <utility>
34
35 #include "math_base.h"
36 #include "timing.h"
37
38 namespace EventProcessing
39 {
40 class EventEngine;
41 }
42
43 namespace fheroes2
44 {
45 enum class Key : int32_t
46 {
47 NONE,
48 KEY_BACKSPACE,
49 KEY_ENTER, // This is for both Enter keys on keyboards. Check getKeyFromSDL() function for explanation.
50 KEY_ESCAPE,
51 KEY_SPACE,
52 KEY_EXCLAIM,
53 KEY_DOUBLE_QUOTE,
54 KEY_HASH,
55 KEY_DOLLAR,
56 KEY_AMPERSAND,
57 KEY_QUOTE,
58 KEY_BACKQUOTE,
59 KEY_LEFT_PARENTHESIS,
60 KEY_RIGHT_PARENTHESIS,
61 KEY_ASTERISK,
62 KEY_PLUS,
63 KEY_COMMA,
64 KEY_MINUS,
65 KEY_PERIOD,
66 KEY_SLASH,
67 KEY_COLON,
68 KEY_SEMICOLON,
69 KEY_LESS,
70 KEY_EQUALS,
71 KEY_GREATER,
72 KEY_QUESTION,
73 KEY_AT,
74 KEY_LEFT_BRACKET,
75 KEY_BACKSLASH,
76 KEY_RIGHT_BRACKET,
77 KEY_CARET,
78 KEY_UNDERSCORE,
79 KEY_LEFT_ALT,
80 KEY_RIGHT_ALT,
81 KEY_LEFT_CONTROL,
82 KEY_RIGHT_CONTROL,
83 KEY_LEFT_SHIFT,
84 KEY_RIGHT_SHIFT,
85 KEY_TAB,
86 KEY_DELETE,
87 KEY_PAGE_UP,
88 KEY_PAGE_DOWN,
89 KEY_F1,
90 KEY_F2,
91 KEY_F3,
92 KEY_F4,
93 KEY_F5,
94 KEY_F6,
95 KEY_F7,
96 KEY_F8,
97 KEY_F9,
98 KEY_F10,
99 KEY_F11,
100 KEY_F12,
101 KEY_LEFT,
102 KEY_RIGHT,
103 KEY_UP,
104 KEY_DOWN,
105 KEY_0,
106 KEY_1,
107 KEY_2,
108 KEY_3,
109 KEY_4,
110 KEY_5,
111 KEY_6,
112 KEY_7,
113 KEY_8,
114 KEY_9,
115 KEY_A,
116 KEY_B,
117 KEY_C,
118 KEY_D,
119 KEY_E,
120 KEY_F,
121 KEY_G,
122 KEY_H,
123 KEY_I,
124 KEY_J,
125 KEY_K,
126 KEY_L,
127 KEY_M,
128 KEY_N,
129 KEY_O,
130 KEY_P,
131 KEY_Q,
132 KEY_R,
133 KEY_S,
134 KEY_T,
135 KEY_U,
136 KEY_V,
137 KEY_W,
138 KEY_X,
139 KEY_Y,
140 KEY_Z,
141 KEY_PRINT,
142 KEY_KP_0,
143 KEY_KP_1,
144 KEY_KP_2,
145 KEY_KP_3,
146 KEY_KP_4,
147 KEY_KP_5,
148 KEY_KP_6,
149 KEY_KP_7,
150 KEY_KP_8,
151 KEY_KP_9,
152 KEY_KP_PERIOD,
153 KEY_KP_DIVIDE,
154 KEY_KP_MULTIPLY,
155 KEY_KP_MINUS,
156 KEY_KP_PLUS,
157 KEY_KP_ENTER,
158 KEY_KP_EQUALS,
159 KEY_HOME,
160 KEY_END,
161
162 // These keys are from mouse buttons.
163 KEY_MOUSE_BUTTON_BACKWARD,
164 KEY_MOUSE_BUTTON_FORWARD,
165
166 // Put all new keys before this line.
167 LAST_KEY
168 };
169
170 // Key modifier is used for key combination detection.
171 enum KeyModifier : int32_t
172 {
173 KEY_MODIFIER_NONE = 0,
174 KEY_MODIFIER_CTRL = ( 1 << 0 ),
175 KEY_MODIFIER_SHIFT = ( 1 << 1 ),
176 KEY_MODIFIER_ALT = ( 1 << 2 ),
177 KEY_MODIFIER_CAPS = ( 1 << 3 ),
178 KEY_MODIFIER_NUM = ( 1 << 4 )
179 };
180
181 const char * KeySymGetName( const Key key );
182
183 size_t InsertKeySym( std::string & res, size_t pos, const Key key, const int32_t mod );
184 }
185
186 class LocalEvent final
187 {
188 public:
189 friend class EventProcessing::EventEngine;
190
191 static LocalEvent & Get();
192
193 static void initEventEngine();
194
195 static int32_t getCurrentKeyModifiers();
196
197 // Only one instance of the class can exist.
198 LocalEvent( const LocalEvent & ) = delete;
199 LocalEvent & operator=( const LocalEvent & ) = delete;
200
201 // Reset all previous event statuses.
202 void reset()
203 {
204 _actionStates = 0;
205 }
206
207 void setGlobalMouseMotionEventHook( std::function<fheroes2::Rect( const int32_t, const int32_t )> hook )
208 {
209 _globalMouseMotionEventHook = std::move( hook );
210 }
211
212 void setGlobalKeyDownEventHook( std::function<void( const fheroes2::Key, const int32_t )> hook )
213 {
214 _globalKeyDownEventHook = std::move( hook );
215 }
216
217 // Return false when event handling should be stopped, true otherwise.
218 bool HandleEvents( const bool sleepAfterEventProcessing = true, const bool allowExit = false );
219
220 bool hasMouseMoved() const
221 {
222 return ( _actionStates & MOUSE_MOTION ) == MOUSE_MOTION;
223 }
224
225 const fheroes2::Point & getMouseCursorPos() const
226 {
227 return _mouseCursorPos;
228 }
229
230 const fheroes2::Point & getMouseLeftButtonPressedPos() const
231 {
232 return _mousePressLeftPos;
233 }
234
235 bool MouseClickLeft();
236 bool MouseClickMiddle();
237 bool MouseClickRight();
238
239 bool MouseClickLeft( const fheroes2::Rect & area );
240 bool MouseClickRight( const fheroes2::Rect & area );
241
242 // The long press event is triggered only once. If this event was triggered (i.e. this method was called
243 // and returned true), then after releasing the mouse button, the click event will not be triggered.
244 bool MouseLongPressLeft( const fheroes2::Rect & rt );
245
246 bool isMouseWheelUp() const
247 {
248 return ( _actionStates & MOUSE_WHEEL ) && _mouseWheelMovementOffset.y > 0;
249 }
250
251 bool isMouseWheelDown() const
252 {
253 return ( _actionStates & MOUSE_WHEEL ) && _mouseWheelMovementOffset.y < 0;
254 }
255
256 bool isMouseLeftButtonPressed() const
257 {
258 return ( _actionStates & MOUSE_PRESSED ) && _currentMouseButton == MouseButtonType::MOUSE_BUTTON_LEFT;
259 }
260
261 bool isMouseLeftButtonPressedInArea( const fheroes2::Rect & area ) const
262 {
263 return isMouseLeftButtonPressed() && ( area & _mousePressLeftPos );
264 }
265
266 bool isMouseLeftButtonPressedAndHeldInArea( const fheroes2::Rect & area ) const
267 {
268 return isMouseLeftButtonPressedInArea( area ) && isMouseCursorPosInArea( area );
269 }
270
271 bool isMouseRightButtonPressed() const
272 {
273 return ( _actionStates & MOUSE_PRESSED ) && _currentMouseButton == MouseButtonType::MOUSE_BUTTON_RIGHT;
274 }
275
276 bool isMouseRightButtonPressedInArea( const fheroes2::Rect & area ) const
277 {
278 return isMouseRightButtonPressed() && ( area & _mousePressRightPos );
279 }
280
281 bool isMouseLeftButtonReleased() const
282 {
283 return ( _actionStates & MOUSE_RELEASED ) && _currentMouseButton == MouseButtonType::MOUSE_BUTTON_LEFT;
284 }
285
286 bool isMouseLeftButtonReleasedInArea( const fheroes2::Rect & area ) const
287 {
288 return isMouseLeftButtonReleased() && ( area & _mouseReleaseLeftPos );
289 }
290
291 bool isMouseWheelUpInArea( const fheroes2::Rect & area ) const
292 {
293 return isMouseWheelUp() && ( area & _mouseCursorPos );
294 }
295
296 bool isMouseWheelDownInArea( const fheroes2::Rect & area ) const
297 {
298 return isMouseWheelDown() && ( area & _mouseCursorPos );
299 }
300
301 bool isMouseCursorPosInArea( const fheroes2::Rect & area ) const
302 {
303 return area & _mouseCursorPos;
304 }
305
306 // Returns true if the current mouse event is triggered by the touchpad and not the mouse (in other words,
307 // this event is emulated using the touchpad). It is assumed that there is only one mouse in the system,
308 // and touchpad events have priority in this sense - that is, as long as the touchpad emulates pressing any
309 // mouse button, it is assumed that all mouse events are triggered by the touchpad.
310 bool isMouseEventFromTouchpad() const
311 {
312 return _actionStates & MOUSE_TOUCH;
313 }
314
315 bool isAnyKeyPressed() const
316 {
317 return _actionStates & KEY_PRESSED;
318 }
319
320 bool isKeyPressed( const fheroes2::Key key ) const
321 {
322 return key == _currentKeyboardValue && ( _actionStates & KEY_PRESSED );
323 }
324
325 bool isKeyBeingHold() const
326 {
327 return ( _actionStates & KEY_HOLD ) != 0;
328 }
329
330 fheroes2::Key getPressedKeyValue() const
331 {
332 return _currentKeyboardValue;
333 }
334
335 void initController();
336 void CloseController();
337
338 void SetControllerPointerSpeed( const int newSpeed )
339 {
340 if ( newSpeed <= 0 ) {
341 return;
342 }
343
344 _controllerPointerSpeed = newSpeed / _controllerSpeedModifier;
345 }
346
347 bool isDragInProgress() const
348 {
349 return _actionStates & DRAG_ONGOING;
350 }
351
352 void registerDrag()
353 {
354 setStates( DRAG_ONGOING );
355 }
356
357 // Only for cases when a continuous pressed event is required.
358 void resetLongPress()
359 {
360 _mouseButtonLongPressDelay.reset();
361 }
362
363 private:
364 enum class MouseButtonType : uint8_t
365 {
366 MOUSE_BUTTON_UNKNOWN,
367 MOUSE_BUTTON_LEFT,
368 MOUSE_BUTTON_MIDDLE,
369 MOUSE_BUTTON_RIGHT
370 };
371
372 enum class KeyboardEventState : uint8_t
373 {
374 KEY_UNKNOWN,
375 KEY_DOWN,
376 KEY_UP
377 };
378
379 enum class ControllerAxisType : uint8_t
380 {
381 CONTROLLER_AXIS_UNKNOWN,
382 CONTROLLER_AXIS_LEFT_X,
383 CONTROLLER_AXIS_LEFT_Y,
384 CONTROLLER_AXIS_RIGHT_X,
385 CONTROLLER_AXIS_RIGHT_Y
386 };
387
388 enum class ControllerButtonType : uint8_t
389 {
390 CONTROLLER_BUTTON_UNKNOWN,
391 CONTROLLER_BUTTON_A,
392 CONTROLLER_BUTTON_B,
393 CONTROLLER_BUTTON_X,
394 CONTROLLER_BUTTON_Y,
395 CONTROLLER_BUTTON_RIGHT_SHOULDER,
396 CONTROLLER_BUTTON_LEFT_SHOULDER,
397 CONTROLLER_BUTTON_DPAD_UP,
398 CONTROLLER_BUTTON_DPAD_DOWN,
399 CONTROLLER_BUTTON_DPAD_RIGHT,
400 CONTROLLER_BUTTON_DPAD_LEFT,
401 CONTROLLER_BUTTON_BACK,
402 CONTROLLER_BUTTON_START,
403 CONTROLLER_BUTTON_GUIDE
404 };
405
406 enum class TouchFingerEventType : uint8_t
407 {
408 FINGER_EVENT_UNKNOWN,
409 FINGER_EVENT_DOWN,
410 FINGER_EVENT_UP,
411 FINGER_EVENT_MOTION
412 };
413
414 enum : uint32_t
415 {
416 NO_EVENT = 0,
417 // Key on the keyboard has been pressed
418 KEY_PRESSED = ( 1 << 0 ),
419 // Mouse cursor has been moved
420 MOUSE_MOTION = ( 1 << 1 ),
421 // Mouse button is currently pressed
422 MOUSE_PRESSED = ( 1 << 2 ),
423 // Mouse button has just been released
424 MOUSE_RELEASED = ( 1 << 3 ),
425 // Mouse wheel has been rotated
426 MOUSE_WHEEL = ( 1 << 4 ),
427 // Current mouse event has been triggered by the touchpad
428 MOUSE_TOUCH = ( 1 << 5 ),
429 // Key on the keyboard is currently being held down
430 KEY_HOLD = ( 1 << 6 ),
431 // Some UI component registered the start of drag motion
432 DRAG_ONGOING = ( 1 << 7 ),
433 };
434
435 enum : int16_t
436 {
437 CONTROLLER_L_DEADZONE = 4000,
438 CONTROLLER_R_DEADZONE = 25000
439 };
440
441 class LongPressDelay final : public fheroes2::TimeDelay
442 {
443 public:
444 using TimeDelay::TimeDelay;
445
446 void reset()
447 {
448 TimeDelay::reset();
449
450 _triggered = false;
451 }
452
453 bool isTriggered() const
454 {
455 return _triggered;
456 }
457
458 void setTriggered()
459 {
460 _triggered = true;
461 }
462
463 private:
464 bool _triggered{ false };
465 };
466
467 std::unique_ptr<EventProcessing::EventEngine> _engine;
468
469 uint32_t _actionStates{ NO_EVENT };
470 fheroes2::Key _currentKeyboardValue{ fheroes2::Key::NONE };
471 MouseButtonType _currentMouseButton{ MouseButtonType::MOUSE_BUTTON_UNKNOWN };
472
473 fheroes2::Point _mousePressLeftPos;
474 fheroes2::Point _mousePressMiddlePos;
475 fheroes2::Point _mousePressRightPos;
476
477 fheroes2::Point _mouseReleaseLeftPos;
478 fheroes2::Point _mouseReleaseMiddlePos;
479 fheroes2::Point _mouseReleaseRightPos;
480
481 fheroes2::Point _mouseCursorPos;
482
483 fheroes2::Point _mouseWheelMovementOffset;
484
485 LongPressDelay _mouseButtonLongPressDelay;
486
487 std::function<fheroes2::Rect( const int32_t, const int32_t )> _globalMouseMotionEventHook;
488 std::function<void( const fheroes2::Key, const int32_t )> _globalKeyDownEventHook;
489
490 fheroes2::Rect _mouseCursorRenderArea;
491
492 // used to convert user-friendly pointer speed values into more usable ones
493 const double _controllerSpeedModifier{ 2000000.0 };
494 double _controllerPointerSpeed{ 10.0 / _controllerSpeedModifier };
495 fheroes2::PointBase2D<double> _emulatedPointerPos;
496
497 // bigger value corresponds to faster pointer movement speed with bigger stick axis values
498 const double _controllerAxisSpeedup{ 1.03 };
499 const double _controllerTriggerCursorSpeedup{ 2.0 };
500
501 fheroes2::Time _controllerTimer;
502 int16_t _controllerLeftXAxis{ 0 };
503 int16_t _controllerLeftYAxis{ 0 };
504 int16_t _controllerRightXAxis{ 0 };
505 int16_t _controllerRightYAxis{ 0 };
506 bool _controllerScrollActive{ false };
507
508 // IDs of currently active (touching the touchpad) fingers, if any. These IDs consist of a touch device id and a finger id.
509 std::pair<std::optional<std::pair<int64_t, int64_t>>, std::optional<std::pair<int64_t, int64_t>>> _fingerIds;
510 // Is the two-finger gesture currently being processed
511 bool _isTwoFingerGestureInProgress = false;
512
513 static void StopSounds();
514 static void ResumeSounds();
515
516 static void onRenderDeviceResetEvent();
517
518 LocalEvent();
519
520 ~LocalEvent() = default;
521
522 void onMouseMotionEvent( fheroes2::Point position );
523 void onMouseButtonEvent( const bool isPressed, const MouseButtonType buttonType, fheroes2::Point position );
524 void onKeyboardEvent( const fheroes2::Key key, const int32_t keyModifier, const KeyboardEventState keyState );
525 void onMouseWheelEvent( fheroes2::Point position );
526 void onControllerAxisEvent( const ControllerAxisType axisType, const int16_t value );
527 void onControllerButtonEvent( const bool isPressed, const ControllerButtonType buttonType );
528 void onTouchFingerEvent( const TouchFingerEventType eventType, const int64_t touchId, const int64_t fingerId, fheroes2::PointBase2D<float> position );
529
530 void ProcessControllerAxisMotion();
531
532 void setStates( const uint32_t states )
533 {
534 _actionStates |= states;
535 }
536
537 void resetStates( const uint32_t states )
538 {
539 _actionStates &= ~states;
540 }
541 };