-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
FeatureHandling.h
235 lines (204 loc) · 7.34 KB
/
FeatureHandling.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Copyright (C) 2014-2017 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this Program; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "input/joysticks/JoystickTypes.h"
#include <memory>
namespace KODI
{
namespace JOYSTICK
{
class CDriverPrimitive;
class IInputHandler;
class IButtonMap;
class CJoystickFeature;
using FeaturePtr = std::shared_ptr<CJoystickFeature>;
/*!
* \ingroup joystick
* \brief Base class for joystick features
*
* See list of feature types in JoystickTypes.h.
*/
class CJoystickFeature
{
public:
CJoystickFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
virtual ~CJoystickFeature() = default;
/*!
* \brief A digital motion has occured
*
* \param source The source of the motion. Must be digital (button or hat)
* \param bPressed True for press motion, false for release motion
*
* \return true if the motion was handled, false otherwise
*/
virtual bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) = 0;
/*!
* \brief An analog motion has occured
*
* \param source The source of the motion. Must be a semiaxis
* \param magnitude The magnitude of the press or motion in the interval [0.0, 1.0]
*
* For semiaxes, the magnitude is the force or travel distance in the
* direction of the semiaxis. If the value is in the opposite direction,
* the magnitude is 0.0.
*
* For example, if the analog stick goes left, the negative semiaxis will
* have a value of 1.0 and the positive semiaxis will have a value of 0.0.
*/
virtual bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) = 0;
/*!
* \brief Process the motions that have occured since the last invocation
*
* This allows features with motion on multiple driver primitives to call
* their handler once all driver primitives are accounted for.
*/
virtual void ProcessMotions(void) = 0;
/*!
* \brief Check if the input handler is accepting input
*
* \param bActivation True if the motion is activating (true or positive),
* false if the motion is deactivating (false or zero)
*
* \return True if input should be sent to the input handler, false otherwise
*/
bool AcceptsInput(bool bActivation);
protected:
const FeatureName m_name;
IInputHandler* const m_handler;
IButtonMap* const m_buttonMap;
const bool m_bEnabled;
};
class CScalarFeature : public CJoystickFeature
{
public:
CScalarFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
virtual ~CScalarFeature() = default;
// implementation of CJoystickFeature
virtual bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
virtual bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
virtual void ProcessMotions(void) override;
private:
bool OnDigitalMotion(bool bPressed);
bool OnAnalogMotion(float magnitude);
void ProcessDigitalMotion();
void ProcessAnalogMotion();
// State variables
INPUT_TYPE m_inputType = INPUT_TYPE::UNKNOWN;
bool m_bDigitalState;
unsigned int m_motionStartTimeMs;
// Analog state variables
float m_analogState; // The current magnitude
float m_bActivated; // Set to true when first activated (magnitude > 0.0)
bool m_bDiscrete; // Set to false when a non-discrete axis is detected
};
/*!
* \ingroup joystick
* \brief Axis of a feature (analog stick, accelerometer, etc)
*
* Axes are composed of two driver primitives, one for the positive semiaxis
* and one for the negative semiaxis.
*
* This effectively means that an axis is two-dimensional, with each dimension
* either:
*
* - a digital value (0.0 or 1.0)
* - an analog value (continuous in the interval [0.0, 1.0])
*/
class CFeatureAxis
{
public:
CFeatureAxis(void) { Reset(); }
/*!
* \brief Set value of positive axis
*/
void SetPositiveDistance(float distance) { m_positiveDistance = distance; }
/*!
* \brief Set value of negative axis
*/
void SetNegativeDistance(float distance) { m_negativeDistance = distance; }
/*!
* \brief Get the final value of this axis.
*
* This axis is two-dimensional, so we need to compress these into a single
* dimension. This is done by subtracting the negative from the positive.
* Some examples:
*
* Positive axis: 1.0 (User presses right or analog stick moves right)
* Negative axis: 0.0
* -------------------
* Pos - Neg: 1.0 (Emulated analog stick moves right)
*
*
* Positive axis: 0.0
* Negative axis: 1.0 (User presses left or analog stick moves left)
* -------------------
* Pos - Neg: -1.0 (Emulated analog stick moves left)
*
*
* Positive axis: 1.0 (User presses both buttons)
* Negative axis: 1.0
* -------------------
* Pos - Neg: 0.0 (Emulated analog stick is centered)
*
*/
float GetPosition(void) const { return m_positiveDistance - m_negativeDistance; }
/*!
* \brief Reset both positive and negative values to zero
*/
void Reset(void) { m_positiveDistance = m_negativeDistance = 0.0f; }
protected:
float m_positiveDistance;
float m_negativeDistance;
};
class CAnalogStick : public CJoystickFeature
{
public:
CAnalogStick(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
virtual ~CAnalogStick() = default;
// implementation of CJoystickFeature
virtual bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
virtual bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
virtual void ProcessMotions(void) override;
protected:
CFeatureAxis m_vertAxis;
CFeatureAxis m_horizAxis;
float m_vertState;
float m_horizState;
unsigned int m_motionStartTimeMs;
};
class CAccelerometer : public CJoystickFeature
{
public:
CAccelerometer(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
virtual ~CAccelerometer() = default;
// implementation of CJoystickFeature
virtual bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
virtual bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
virtual void ProcessMotions(void) override;
protected:
CFeatureAxis m_xAxis;
CFeatureAxis m_yAxis;
CFeatureAxis m_zAxis;
float m_xAxisState;
float m_yAxisState;
float m_zAxisState;
};
}
}