xbmc
FeatureHandling.h
1 /*
2  * Copyright (C) 2014-2018 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 #pragma once
10 
12 
13 #include <chrono>
14 #include <memory>
15 
16 namespace KODI
17 {
18 namespace JOYSTICK
19 {
20 class CDriverPrimitive;
21 class IInputHandler;
22 class IButtonMap;
23 
24 class CJoystickFeature;
25 using FeaturePtr = std::shared_ptr<CJoystickFeature>;
26 
34 {
35 public:
36  CJoystickFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
37  virtual ~CJoystickFeature() = default;
38 
47  virtual bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) = 0;
48 
62  virtual bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) = 0;
63 
70  virtual void ProcessMotions(void) = 0;
71 
80  bool AcceptsInput(bool bActivation);
81 
82 protected:
86  void ResetMotion();
87 
91  void StartMotion();
92 
96  bool InMotion() const;
97 
101  unsigned int MotionTimeMs() const;
102 
103  const FeatureName m_name;
104  IInputHandler* const m_handler;
105  IButtonMap* const m_buttonMap;
106  const bool m_bEnabled;
107 
108 private:
109  std::chrono::time_point<std::chrono::steady_clock> m_motionStartTimeMs;
110 };
111 
113 {
114 public:
115  CScalarFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
116  ~CScalarFeature() override = default;
117 
118  // implementation of CJoystickFeature
119  bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
120  bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
121  void ProcessMotions() override;
122 
123 private:
124  bool OnDigitalMotion(bool bPressed);
125  bool OnAnalogMotion(float magnitude);
126 
127  void ProcessDigitalMotion();
128  void ProcessAnalogMotion();
129 
130  // State variables
131  INPUT_TYPE m_inputType = INPUT_TYPE::UNKNOWN;
132  bool m_bDigitalState;
133  bool m_bInitialPressHandled = false;
134 
135  // Analog state variables
136  float m_analogState; // The current magnitude
137  float m_bActivated; // Set to true when first activated (magnitude > 0.0)
138  bool m_bDiscrete; // Set to false when a non-discrete axis is detected
139 };
140 
155 {
156 public:
157  CFeatureAxis(void) { Reset(); }
158 
162  void SetPositiveDistance(float distance) { m_positiveDistance = distance; }
163 
167  void SetNegativeDistance(float distance) { m_negativeDistance = distance; }
168 
194  float GetPosition(void) const { return m_positiveDistance - m_negativeDistance; }
195 
199  void Reset(void) { m_positiveDistance = m_negativeDistance = 0.0f; }
200 
201 protected:
202  float m_positiveDistance;
203  float m_negativeDistance;
204 };
205 
207 {
208 public:
209  CAxisFeature(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
210  ~CAxisFeature() override = default;
211 
212  // partial implementation of CJoystickFeature
213  bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
214  void ProcessMotions() override;
215 
216 protected:
217  CFeatureAxis m_axis;
218 
219  float m_state;
220 };
221 
222 class CWheel : public CAxisFeature
223 {
224 public:
225  CWheel(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
226  ~CWheel() override = default;
227 
228  // partial implementation of CJoystickFeature
229  bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
230 };
231 
232 class CThrottle : public CAxisFeature
233 {
234 public:
235  CThrottle(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
236  ~CThrottle() override = default;
237 
238  // partial implementation of CJoystickFeature
239  bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
240 };
241 
243 {
244 public:
245  CAnalogStick(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
246  ~CAnalogStick() override = default;
247 
248  // implementation of CJoystickFeature
249  bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
250  bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
251  void ProcessMotions() override;
252 
253 protected:
254  CFeatureAxis m_vertAxis;
255  CFeatureAxis m_horizAxis;
256 
257  float m_vertState;
258  float m_horizState;
259 };
260 
262 {
263 public:
264  CAccelerometer(const FeatureName& name, IInputHandler* handler, IButtonMap* buttonMap);
265  ~CAccelerometer() override = default;
266 
267  // implementation of CJoystickFeature
268  bool OnDigitalMotion(const CDriverPrimitive& source, bool bPressed) override;
269  bool OnAnalogMotion(const CDriverPrimitive& source, float magnitude) override;
270  void ProcessMotions() override;
271 
272 protected:
273  CFeatureAxis m_xAxis;
274  CFeatureAxis m_yAxis;
275  CFeatureAxis m_zAxis;
276 
277  float m_xAxisState;
278  float m_yAxisState;
279  float m_zAxisState;
280 };
281 } // namespace JOYSTICK
282 } // namespace KODI
INPUT_TYPE
Types of input available for scalar features.
Definition: JoystickTypes.h:140
virtual bool OnAnalogMotion(const CDriverPrimitive &source, float magnitude)=0
An analog motion has occurred.
Definition: FeatureHandling.h:242
Definition: IInputHandler.h:16
float GetPosition(void) const
Get the final value of this axis.
Definition: FeatureHandling.h:194
Button map interface to translate between the driver&#39;s raw button/hat/axis elements and physical joys...
Definition: IButtonMap.h:28
virtual void ProcessMotions(void)=0
Process the motions that have occurred since the last invocation.
void SetNegativeDistance(float distance)
Set value of negative axis.
Definition: FeatureHandling.h:167
Definition: FeatureHandling.h:222
Definition: FeatureHandling.h:232
void Reset(void)
Reset both positive and negative values to zero.
Definition: FeatureHandling.h:199
void StartMotion()
Start the motion timer.
Definition: FeatureHandling.cpp:58
bool AcceptsInput(bool bActivation)
Check if the input handler is accepting input.
Definition: FeatureHandling.cpp:40
Controller configuration window.
Definition: AudioDecoder.h:18
Interface for handling input events for game controllers.
Definition: IInputHandler.h:25
void SetPositiveDistance(float distance)
Set value of positive axis.
Definition: FeatureHandling.h:162
unsigned int MotionTimeMs() const
Get the time for which the feature has been in motion.
Definition: FeatureHandling.cpp:68
Basic driver element associated with input events.
Definition: DriverPrimitive.h:69
void ResetMotion()
Reset motion timer.
Definition: FeatureHandling.cpp:53
std::string FeatureName
Name of a physical feature belonging to the joystick.
Definition: JoystickTypes.h:28
Definition: FeatureHandling.h:206
Definition: FeatureHandling.h:112
Axis of a feature (analog stick, accelerometer, etc)
Definition: FeatureHandling.h:154
Definition: FeatureHandling.h:261
Base class for joystick features.
Definition: FeatureHandling.h:33
bool InMotion() const
Check if the feature is in motion.
Definition: FeatureHandling.cpp:63
virtual bool OnDigitalMotion(const CDriverPrimitive &source, bool bPressed)=0
A digital motion has occurred.