| 1 |
/* |
| 2 |
* Copyright (C) 2005-2024 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 |
/// \ingroup mouse |
| 14 |
/// \{ |
| 15 |
|
| 16 |
#define XBMC_BUTTON(X) (1 << ((X)-1)) |
| 17 |
#define XBMC_BUTTON_LEFT 1 |
| 18 |
#define XBMC_BUTTON_MIDDLE 2 |
| 19 |
#define XBMC_BUTTON_RIGHT 3 |
| 20 |
#define XBMC_BUTTON_WHEELUP 4 |
| 21 |
#define XBMC_BUTTON_WHEELDOWN 5 |
| 22 |
#define XBMC_BUTTON_X1 6 |
| 23 |
#define XBMC_BUTTON_X2 7 |
| 24 |
#define XBMC_BUTTON_X3 8 |
| 25 |
#define XBMC_BUTTON_X4 9 |
| 26 |
#define XBMC_BUTTON_LMASK XBMC_BUTTON(XBMC_BUTTON_LEFT) |
| 27 |
#define XBMC_BUTTON_MMASK XBMC_BUTTON(XBMC_BUTTON_MIDDLE) |
| 28 |
#define XBMC_BUTTON_RMASK XBMC_BUTTON(XBMC_BUTTON_RIGHT) |
| 29 |
#define XBMC_BUTTON_X1MASK XBMC_BUTTON(XBMC_BUTTON_X1) |
| 30 |
#define XBMC_BUTTON_X2MASK XBMC_BUTTON(XBMC_BUTTON_X2) |
| 31 |
#define XBMC_BUTTON_X3MASK XBMC_BUTTON(XBMC_BUTTON_X3) |
| 32 |
#define XBMC_BUTTON_X4MASK XBMC_BUTTON(XBMC_BUTTON_X4) |
| 33 |
|
| 34 |
#define MOUSE_MINIMUM_MOVEMENT 2 |
| 35 |
#define MOUSE_DOUBLE_CLICK_LENGTH 500L |
| 36 |
#define MOUSE_ACTIVE_LENGTH 5000L |
| 37 |
|
| 38 |
#define MOUSE_MAX_BUTTON 7 |
| 39 |
|
| 40 |
enum MOUSE_STATE |
| 41 |
{ |
| 42 |
/*! Normal state */ |
| 43 |
MOUSE_STATE_NORMAL = 1, |
| 44 |
/*! Control below the mouse is currently in focus */ |
| 45 |
MOUSE_STATE_FOCUS, |
| 46 |
/*! A drag operation is being performed */ |
| 47 |
MOUSE_STATE_DRAG, |
| 48 |
/*! A mousebutton is being clicked */ |
| 49 |
MOUSE_STATE_CLICK |
| 50 |
}; |
| 51 |
|
| 52 |
enum MOUSE_BUTTON |
| 53 |
{ |
| 54 |
MOUSE_LEFT_BUTTON = 0, |
| 55 |
MOUSE_RIGHT_BUTTON, |
| 56 |
MOUSE_MIDDLE_BUTTON, |
| 57 |
MOUSE_EXTRA_BUTTON1, |
| 58 |
MOUSE_EXTRA_BUTTON2, |
| 59 |
MOUSE_EXTRA_BUTTON3, |
| 60 |
MOUSE_EXTRA_BUTTON4 |
| 61 |
}; |
| 62 |
|
| 63 |
enum class HoldAction |
| 64 |
{ |
| 65 |
/*! No action should occur */ |
| 66 |
NONE, |
| 67 |
/*! A drag action has started */ |
| 68 |
DRAG_START, |
| 69 |
/*! A drag action is in progress */ |
| 70 |
DRAG, |
| 71 |
/*! A drag action has finished */ |
| 72 |
DRAG_END |
| 73 |
}; |
| 74 |
|
| 75 |
//! Holds everything we know about the current state of the mouse |
| 76 |
struct MouseState |
| 77 |
{ |
| 78 |
/*! X location */ |
| 79 |
int x; |
| 80 |
/*! Y location */ |
| 81 |
int y; |
| 82 |
/*! Change in x */ |
| 83 |
int16_t dx; |
| 84 |
/*! Change in y */ |
| 85 |
int16_t dy; |
| 86 |
/*! Change in z (wheel) */ |
| 87 |
int8_t dz; |
| 88 |
/*! Current state of the buttons */ |
| 89 |
bool button[MOUSE_MAX_BUTTON]; |
| 90 |
/*! True if the mouse is active */ |
| 91 |
bool active; |
| 92 |
}; |
| 93 |
|
| 94 |
struct MousePosition |
| 95 |
{ |
| 96 |
int x; |
| 97 |
int y; |
| 98 |
}; |
| 99 |
|
| 100 |
class CAction; |
| 101 |
|
| 102 |
class CMouseStat |
| 103 |
{ |
| 104 |
public: |
| 105 |
CMouseStat(); |
| 106 |
virtual ~CMouseStat(); |
| 107 |
|
| 108 |
void Initialize(); |
| 109 |
void HandleEvent(const XBMC_Event& newEvent); |
| 110 |
void SetResolution(int maxX, int maxY, float speedX, float speedY); |
| 111 |
bool IsActive(); |
| 112 |
bool IsEnabled() const; |
| 113 |
|
| 114 |
void SetActive(bool active = true); |
| 115 |
void SetState(MOUSE_STATE state) { m_pointerState = state; } |
| 116 |
void SetEnabled(bool enabled = true); |
| 117 |
MOUSE_STATE GetState() const { return m_pointerState; } |
| 118 |
uint32_t GetKey() const; |
| 119 |
|
| 120 |
HoldAction GetHold(int ButtonID) const; |
| 121 |
inline int GetX(void) const { return m_mouseState.x; } |
| 122 |
inline int GetY(void) const { return m_mouseState.y; } |
| 123 |
inline int GetDX(void) const { return m_mouseState.dx; } |
| 124 |
inline int GetDY(void) const { return m_mouseState.dy; } |
| 125 |
MousePosition GetPosition() { return MousePosition{m_mouseState.x, m_mouseState.y}; } |
| 126 |
|
| 127 |
private: |
| 128 |
/*! \brief Holds information regarding a particular mouse button state |
| 129 |
|
| 130 |
The CButtonState class is used to track where in a button event the mouse currently is. |
| 131 |
There is effectively 5 BUTTON_STATE's available, and transitioning between those states |
| 132 |
is handled by the Update() function. |
| 133 |
|
| 134 |
The actions we detect are: |
| 135 |
* short clicks - down/up press of the mouse within short_click_time ms, where the pointer stays |
| 136 |
within click_confines pixels |
| 137 |
* long clicks - down/up press of the mouse greater than short_click_time ms, where the pointers |
| 138 |
stays within click_confines pixels |
| 139 |
* double clicks - a further down press of the mouse within double_click_time of the up press of |
| 140 |
a short click, where the pointer stays within click_confines pixels |
| 141 |
* drag - the mouse is down and has been moved more than click_confines pixels |
| 142 |
|
| 143 |
\sa CMouseStat |
| 144 |
*/ |
| 145 |
class CButtonState |
| 146 |
{ |
| 147 |
public: |
| 148 |
/*! \brief enum for the actions to perform as a result of an Update function |
| 149 |
*/ |
| 150 |
enum BUTTON_ACTION |
| 151 |
{ |
| 152 |
MB_NONE = 0, ///< no action should occur |
| 153 |
MB_SHORT_CLICK, ///< a short click has occurred (a double click may be in process) |
| 154 |
MB_LONG_CLICK, ///< a long click has occurred |
| 155 |
MB_DOUBLE_CLICK, ///< a double click has occurred |
| 156 |
MB_DRAG_START, ///< a drag action has started |
| 157 |
MB_DRAG, ///< a drag action is in progress |
| 158 |
MB_DRAG_END |
| 159 |
}; ///< a drag action has finished |
| 160 |
|
| 161 |
CButtonState(); |
| 162 |
|
| 163 |
/*! \brief Update the button state, with where the mouse is, and whether the button is down or |
| 164 |
not |
| 165 |
|
| 166 |
\param time frame time in ms |
| 167 |
\param x horizontal coordinate of the mouse |
| 168 |
\param y vertical coordinate of the mouse |
| 169 |
\param down true if the button is down |
| 170 |
\return action that should be performed |
| 171 |
*/ |
| 172 |
BUTTON_ACTION Update(unsigned int time, int x, int y, bool down); |
| 173 |
|
| 174 |
private: |
| 175 |
//! number of pixels that the pointer may move while the button is down to |
| 176 |
//! trigger a click |
| 177 |
static const unsigned int click_confines = 5; |
| 178 |
|
| 179 |
//! Time for mouse down/up to trigger a short click rather than a long click |
| 180 |
static const unsigned int short_click_time = 1000; |
| 181 |
|
| 182 |
//! Time for mouse down following a short click to trigger a double click |
| 183 |
static const unsigned int double_click_time = 500; |
| 184 |
|
| 185 |
bool InClickRange(int x, int y) const; |
| 186 |
|
| 187 |
enum BUTTON_STATE |
| 188 |
{ |
| 189 |
STATE_RELEASED = 0, ///< mouse button is released, no events pending |
| 190 |
STATE_IN_CLICK, ///< mouse button is down, a click is pending |
| 191 |
STATE_IN_DOUBLE_CLICK, ///< mouse button is released, pending double click |
| 192 |
STATE_IN_DOUBLE_IGNORE, ///< mouse button is down following double click |
| 193 |
STATE_IN_DRAG |
| 194 |
}; ///< mouse button is down during a drag |
| 195 |
|
| 196 |
BUTTON_STATE m_state; |
| 197 |
unsigned int m_time; |
| 198 |
int m_x; |
| 199 |
int m_y; |
| 200 |
}; |
| 201 |
|
| 202 |
/*! \brief detect whether the mouse has moved |
| 203 |
|
| 204 |
Uses a trigger threshold of 2 pixels to detect mouse movement |
| 205 |
|
| 206 |
\return whether the mouse has moved past the trigger threshold. |
| 207 |
*/ |
| 208 |
bool MovedPastThreshold() const; |
| 209 |
|
| 210 |
// state of the mouse |
| 211 |
MOUSE_STATE m_pointerState{MOUSE_STATE_NORMAL}; |
| 212 |
MouseState m_mouseState{}; |
| 213 |
bool m_mouseEnabled; |
| 214 |
CButtonState m_buttonState[MOUSE_MAX_BUTTON]; |
| 215 |
|
| 216 |
int m_maxX{0}; |
| 217 |
int m_maxY{0}; |
| 218 |
float m_speedX{0.0f}; |
| 219 |
float m_speedY{0.0f}; |
| 220 |
|
| 221 |
// active/click timers |
| 222 |
unsigned int m_lastActiveTime; |
| 223 |
|
| 224 |
bool bClick[MOUSE_MAX_BUTTON]{}; |
| 225 |
bool bDoubleClick[MOUSE_MAX_BUTTON]{}; |
| 226 |
HoldAction m_hold[MOUSE_MAX_BUTTON]{}; |
| 227 |
bool bLongClick[MOUSE_MAX_BUTTON]{}; |
| 228 |
|
| 229 |
uint32_t m_Key; |
| 230 |
}; |
| 231 |
|
| 232 |
/// \} |