Debian Codegraph @

xbmc/games/controllers/input/ControllerActivity.cpp

raw
1 /*
2 * Copyright (C) 2023 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 "ControllerActivity.h"
10
11 #include "application/Application.h"
12 #include "input/InputTranslator.h"
13
14 using namespace KODI;
15 using namespace GAME;
16
17 #include <algorithm>
18 #include <cstdlib>
19
20 void CControllerActivity::ClearButtonState()
21 {
22 m_lastActivation = 0.0f;
23 m_currentActivation = 0.0f;
24 m_activeKey.clear();
25 m_activePointers.clear();
26 m_activeButtons.clear();
27 }
28
29 void CControllerActivity::OnButtonPress(bool pressed)
30 {
31 if (pressed)
32 m_currentActivation = 1.0f;
33 }
34
35 void CControllerActivity::OnButtonMotion(float magnitude)
36 {
37 m_currentActivation = std::max(magnitude, m_currentActivation);
38 }
39
40 void CControllerActivity::OnAnalogStickMotion(float x, float y)
41 {
42 m_currentActivation = std::max(std::abs(x), m_currentActivation);
43 m_currentActivation = std::max(std::abs(y), m_currentActivation);
44 }
45
46 void CControllerActivity::OnWheelMotion(float position)
47 {
48 m_currentActivation = std::max(std::abs(position), m_currentActivation);
49 }
50
51 void CControllerActivity::OnThrottleMotion(float position)
52 {
53 m_currentActivation = std::max(std::abs(position), m_currentActivation);
54 }
55
56 void CControllerActivity::OnKeyPress(const KEYBOARD::KeyName& key)
57 {
58 // Skip the first key press, as it is usually a modifier key
59 if (!m_bKeyPressed)
60 {
61 m_bKeyPressed = true;
62 return;
63 }
64
65 // We only store a single key to avoid "stuck" keys, as any key release will
66 // clear the current activation
67 m_activeKey = key;
68 }
69
70 void CControllerActivity::OnKeyRelease(const KEYBOARD::KeyName& key)
71 {
72 // Clear the current activation to avoid "stuck" keys
73 m_activeKey.clear();
74 }
75
76 void CControllerActivity::OnMouseMotion(const MOUSE::PointerName& relpointer,
77 int differenceX,
78 int differenceY)
79 {
80 //! @todo Fix mouse pointer handling
81 return;
82
83 //! @todo Handle multiple pointers
84 //m_activePointers.insert(relpointer);
85
86 INPUT::INTERCARDINAL_DIRECTION dir = GetPointerDirection(differenceX, differenceY);
87
88 // Check if direction is valid
89 if (dir != INPUT::INTERCARDINAL_DIRECTION::NONE)
90 m_currentActivation = 1.0f;
91 }
92
93 void CControllerActivity::OnMouseButtonPress(const MOUSE::ButtonName& button)
94 {
95 m_activeButtons.insert(button);
96 }
97
98 void CControllerActivity::OnMouseButtonRelease(const MOUSE::ButtonName& button)
99 {
100 m_activeButtons.erase(button);
101 }
102
103 void CControllerActivity::OnInputFrame()
104 {
105 if (g_application.IsAppFocused())
106 {
107 // Process pressed keys
108 if (!m_activeKey.empty())
109 m_currentActivation = 1.0f;
110
111 // Process pressed mouse buttons
112 if (!m_activeButtons.empty())
113 m_currentActivation = 1.0f;
114 }
115
116 // Process activation
117 m_lastActivation = m_currentActivation;
118 m_currentActivation = 0.0f;
119 }
120
121 KODI::INPUT::INTERCARDINAL_DIRECTION CControllerActivity::GetPointerDirection(int differenceX,
122 int differenceY)
123 {
124 using namespace INPUT;
125
126 // Translate from left-handed coordinate system to right-handed coordinate system
127 differenceY *= -1;
128
129 return CInputTranslator::VectorToIntercardinalDirection(static_cast<float>(differenceX),
130 static_cast<float>(differenceY));
131 }