xbmc
MouseStat.h
1 /*
2  * Copyright (C) 2005-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 
11 #include "windowing/XBMC_events.h"
12 
13 #define XBMC_BUTTON(X) (1 << ((X)-1))
14 #define XBMC_BUTTON_LEFT 1
15 #define XBMC_BUTTON_MIDDLE 2
16 #define XBMC_BUTTON_RIGHT 3
17 #define XBMC_BUTTON_WHEELUP 4
18 #define XBMC_BUTTON_WHEELDOWN 5
19 #define XBMC_BUTTON_X1 6
20 #define XBMC_BUTTON_X2 7
21 #define XBMC_BUTTON_X3 8
22 #define XBMC_BUTTON_X4 9
23 #define XBMC_BUTTON_LMASK XBMC_BUTTON(XBMC_BUTTON_LEFT)
24 #define XBMC_BUTTON_MMASK XBMC_BUTTON(XBMC_BUTTON_MIDDLE)
25 #define XBMC_BUTTON_RMASK XBMC_BUTTON(XBMC_BUTTON_RIGHT)
26 #define XBMC_BUTTON_X1MASK XBMC_BUTTON(XBMC_BUTTON_X1)
27 #define XBMC_BUTTON_X2MASK XBMC_BUTTON(XBMC_BUTTON_X2)
28 #define XBMC_BUTTON_X3MASK XBMC_BUTTON(XBMC_BUTTON_X3)
29 #define XBMC_BUTTON_X4MASK XBMC_BUTTON(XBMC_BUTTON_X4)
30 
31 #define MOUSE_MINIMUM_MOVEMENT 2
32 #define MOUSE_DOUBLE_CLICK_LENGTH 500L
33 #define MOUSE_ACTIVE_LENGTH 5000L
34 
35 #define MOUSE_MAX_BUTTON 7
36 
37 enum MOUSE_STATE
38 {
40  MOUSE_STATE_NORMAL = 1,
42  MOUSE_STATE_FOCUS,
44  MOUSE_STATE_DRAG,
46  MOUSE_STATE_CLICK
47 };
48 
49 enum MOUSE_BUTTON
50 {
51  MOUSE_LEFT_BUTTON = 0,
52  MOUSE_RIGHT_BUTTON,
53  MOUSE_MIDDLE_BUTTON,
54  MOUSE_EXTRA_BUTTON1,
55  MOUSE_EXTRA_BUTTON2,
56  MOUSE_EXTRA_BUTTON3,
57  MOUSE_EXTRA_BUTTON4
58 };
59 
60 enum class HoldAction
61 {
63  NONE,
65  DRAG_START,
67  DRAG,
69  DRAG_END
70 };
71 
73 struct MouseState
74 {
76  int x;
78  int y;
80  int16_t dx;
82  int16_t dy;
84  int8_t dz;
86  bool button[MOUSE_MAX_BUTTON];
88  bool active;
89 };
90 
92 {
93  int x;
94  int y;
95 };
96 
97 class CAction;
98 
100 {
101 public:
102  CMouseStat();
103  virtual ~CMouseStat();
104 
105  void Initialize();
106  void HandleEvent(const XBMC_Event& newEvent);
107  void SetResolution(int maxX, int maxY, float speedX, float speedY);
108  bool IsActive();
109  bool IsEnabled() const;
110 
111  void SetActive(bool active = true);
112  void SetState(MOUSE_STATE state) { m_pointerState = state; }
113  void SetEnabled(bool enabled = true);
114  MOUSE_STATE GetState() const { return m_pointerState; }
115  uint32_t GetKey() const;
116 
117  HoldAction GetHold(int ButtonID) const;
118  inline int GetX(void) const { return m_mouseState.x; }
119  inline int GetY(void) const { return m_mouseState.y; }
120  inline int GetDX(void) const { return m_mouseState.dx; }
121  inline int GetDY(void) const { return m_mouseState.dy; }
122  MousePosition GetPosition() { return MousePosition{m_mouseState.x, m_mouseState.y}; }
123 
124 private:
142  class CButtonState
143  {
144  public:
147  enum BUTTON_ACTION
148  {
149  MB_NONE = 0,
150  MB_SHORT_CLICK,
151  MB_LONG_CLICK,
152  MB_DOUBLE_CLICK,
153  MB_DRAG_START,
154  MB_DRAG,
155  MB_DRAG_END
156  };
157 
158  CButtonState();
159 
169  BUTTON_ACTION Update(unsigned int time, int x, int y, bool down);
170 
171  private:
174  static const unsigned int click_confines = 5;
175 
177  static const unsigned int short_click_time = 1000;
178 
180  static const unsigned int double_click_time = 500;
181 
182  bool InClickRange(int x, int y) const;
183 
184  enum BUTTON_STATE
185  {
186  STATE_RELEASED = 0,
187  STATE_IN_CLICK,
188  STATE_IN_DOUBLE_CLICK,
189  STATE_IN_DOUBLE_IGNORE,
190  STATE_IN_DRAG
191  };
192 
193  BUTTON_STATE m_state;
194  unsigned int m_time;
195  int m_x;
196  int m_y;
197  };
198 
205  bool MovedPastThreshold() const;
206 
207  // state of the mouse
208  MOUSE_STATE m_pointerState{MOUSE_STATE_NORMAL};
209  MouseState m_mouseState{};
210  bool m_mouseEnabled;
211  CButtonState m_buttonState[MOUSE_MAX_BUTTON];
212 
213  int m_maxX{0};
214  int m_maxY{0};
215  float m_speedX{0.0f};
216  float m_speedY{0.0f};
217 
218  // active/click timers
219  unsigned int m_lastActiveTime;
220 
221  bool bClick[MOUSE_MAX_BUTTON]{};
222  bool bDoubleClick[MOUSE_MAX_BUTTON]{};
223  HoldAction m_hold[MOUSE_MAX_BUTTON]{};
224  bool bLongClick[MOUSE_MAX_BUTTON]{};
225 
226  uint32_t m_Key;
227 };
int16_t dx
Definition: MouseStat.h:80
int x
Definition: MouseStat.h:76
Definition: MouseStat.h:91
class encapsulating information regarding a particular user action to be sent to windows and controls...
Definition: Action.h:22
int16_t dy
Definition: MouseStat.h:82
int y
Definition: MouseStat.h:78
Definition: XBMC_events.h:109
bool active
Definition: MouseStat.h:88
Definition: MouseStat.h:99
Holds everything we know about the current state of the mouse.
Definition: MouseStat.h:73
int8_t dz
Definition: MouseStat.h:84