| 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 |
#include "mouse.h" |
| 30 |
|
| 31 |
#include <SDL_events.h> |
| 32 |
#include <SDL_mouse.h> |
| 33 |
|
| 34 |
#include "blit.h" |
| 35 |
#include "config.h" |
| 36 |
#include "defs.h" |
| 37 |
#include "log.h" |
| 38 |
|
| 39 |
#define MOUSE_REPEAT_TICKS 600 |
| 40 |
#define MOUSE_MOVE_DEAD_ZONE 12 |
| 41 |
#define TRAIL_NUM_DOTS 4 |
| 42 |
|
| 43 |
void MouseInit(Mouse *mouse) |
| 44 |
{ |
| 45 |
memset(mouse, 0, sizeof *mouse); |
| 46 |
for (SDL_SystemCursor i = SDL_SYSTEM_CURSOR_ARROW; |
| 47 |
i < SDL_NUM_SYSTEM_CURSORS; i++) |
| 48 |
{ |
| 49 |
mouse->cursors[i] = SDL_CreateSystemCursor(i); |
| 50 |
} |
| 51 |
} |
| 52 |
void MouseTerminate(Mouse *m) |
| 53 |
{ |
| 54 |
SDL_FreeCursor(m->cursor); |
| 55 |
for (SDL_SystemCursor i = SDL_SYSTEM_CURSOR_ARROW; |
| 56 |
i < SDL_NUM_SYSTEM_CURSORS; i++) |
| 57 |
{ |
| 58 |
SDL_FreeCursor(m->cursors[i]); |
| 59 |
} |
| 60 |
} |
| 61 |
void MouseReset(Mouse *m) |
| 62 |
{ |
| 63 |
memset(m->previousButtons, 0, sizeof m->previousButtons); |
| 64 |
memset(m->currentButtons, 0, sizeof m->currentButtons); |
| 65 |
memset(m->pressedButtons, 0, sizeof m->pressedButtons); |
| 66 |
m->previousPos = svec2i_zero(); |
| 67 |
m->currentPos = svec2i_zero(); |
| 68 |
m->wheel = svec2i_zero(); |
| 69 |
m->repeatedTicks = 0; |
| 70 |
m->mouseMovePos = svec2i_zero(); |
| 71 |
} |
| 72 |
|
| 73 |
void MousePrePoll(Mouse *mouse) |
| 74 |
{ |
| 75 |
memset(mouse->pressedButtons, 0, sizeof mouse->pressedButtons); |
| 76 |
memcpy( |
| 77 |
mouse->previousButtons, mouse->currentButtons, |
| 78 |
sizeof mouse->previousButtons); |
| 79 |
mouse->wheel = svec2i_zero(); |
| 80 |
mouse->previousPos = mouse->currentPos; |
| 81 |
SDL_GetMouseState(&mouse->currentPos.x, &mouse->currentPos.y); |
| 82 |
int scale = ConfigGetInt(&gConfig, "Graphics.ScaleFactor"); |
| 83 |
if (scale == 0) |
| 84 |
scale = 1; |
| 85 |
mouse->currentPos = svec2i_scale_divide(mouse->currentPos, scale); |
| 86 |
} |
| 87 |
|
| 88 |
void MouseOnButtonDown(Mouse *mouse, Uint8 button) |
| 89 |
{ |
| 90 |
mouse->currentButtons[button] = true; |
| 91 |
|
| 92 |
SDL_CaptureMouse(SDL_TRUE); |
| 93 |
} |
| 94 |
void MouseOnButtonUp(Mouse *mouse, Uint8 button) |
| 95 |
{ |
| 96 |
|
| 97 |
|
| 98 |
if (mouse->currentButtons[button] && !mouse->previousButtons[button]) |
| 99 |
{ |
| 100 |
mouse->pressedButtons[button] = true; |
| 101 |
} |
| 102 |
mouse->currentButtons[button] = false; |
| 103 |
SDL_CaptureMouse(SDL_FALSE); |
| 104 |
} |
| 105 |
void MouseOnWheel(Mouse *m, const Sint32 x, const Sint32 y) |
| 106 |
{ |
| 107 |
m->wheel = svec2i(x, y); |
| 108 |
} |
| 109 |
|
| 110 |
void MousePostPoll(Mouse *mouse, const Uint32 ticks) |
| 111 |
{ |
| 112 |
int areSameButtonsPressed = 1; |
| 113 |
int i; |
| 114 |
for (i = 0; i < 8; i++) |
| 115 |
{ |
| 116 |
if (mouse->previousButtons[i] ^ mouse->currentButtons[i]) |
| 117 |
{ |
| 118 |
areSameButtonsPressed = 0; |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
if (areSameButtonsPressed) |
| 125 |
{ |
| 126 |
mouse->repeatedTicks += ticks; |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
mouse->repeatedTicks = 0; |
| 131 |
} |
| 132 |
|
| 133 |
if (mouse->repeatedTicks > MOUSE_REPEAT_TICKS) |
| 134 |
{ |
| 135 |
mouse->repeatedTicks -= MOUSE_REPEAT_TICKS; |
| 136 |
for (i = 0; i < 8; i++) |
| 137 |
{ |
| 138 |
mouse->pressedButtons[i] |= mouse->currentButtons[i]; |
| 139 |
} |
| 140 |
memcpy( |
| 141 |
mouse->pressedButtons, mouse->currentButtons, |
| 142 |
sizeof mouse->pressedButtons); |
| 143 |
} |
| 144 |
else |
| 145 |
{ |
| 146 |
for (i = 0; i < 8; i++) |
| 147 |
{ |
| 148 |
mouse->pressedButtons[i] |= |
| 149 |
mouse->currentButtons[i] && !mouse->previousButtons[i]; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
bool MouseHasMoved(const Mouse *m) |
| 155 |
{ |
| 156 |
return !svec2i_is_equal(m->previousPos, m->currentPos); |
| 157 |
} |
| 158 |
|
| 159 |
int MouseGetPressed(const Mouse *m) |
| 160 |
{ |
| 161 |
for (int i = 0; i < 8; i++) |
| 162 |
{ |
| 163 |
if (m->pressedButtons[i]) |
| 164 |
{ |
| 165 |
return i; |
| 166 |
} |
| 167 |
} |
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
bool MouseIsDown(const Mouse *m, const int button) |
| 172 |
{ |
| 173 |
return m->currentButtons[button]; |
| 174 |
} |
| 175 |
|
| 176 |
bool MouseIsPressed(const Mouse *m, const int button) |
| 177 |
{ |
| 178 |
return m->pressedButtons[button]; |
| 179 |
} |
| 180 |
|
| 181 |
bool MouseIsReleased(const Mouse *m, const int button) |
| 182 |
{ |
| 183 |
return !m->currentButtons[button] && m->previousButtons[button]; |
| 184 |
} |
| 185 |
|
| 186 |
struct vec2i MouseWheel(const Mouse *m) |
| 187 |
{ |
| 188 |
return m->wheel; |
| 189 |
} |
| 190 |
|
| 191 |
int MouseGetMove(Mouse *mouse, const struct vec2i pos) |
| 192 |
{ |
| 193 |
int cmd = 0; |
| 194 |
const int dx = abs(mouse->currentPos.x - pos.x); |
| 195 |
const int dy = abs(mouse->currentPos.y - pos.y); |
| 196 |
mouse->mouseMovePos = pos; |
| 197 |
|
| 198 |
mouse->mouseMovePos.x += 8; |
| 199 |
const bool isInDeadZone = |
| 200 |
dx <= MOUSE_MOVE_DEAD_ZONE && dy <= MOUSE_MOVE_DEAD_ZONE; |
| 201 |
if (!isInDeadZone) |
| 202 |
{ |
| 203 |
if (2 * dx > dy) |
| 204 |
{ |
| 205 |
if (pos.x < mouse->currentPos.x) |
| 206 |
cmd |= CMD_RIGHT; |
| 207 |
else if (pos.x > mouse->currentPos.x) |
| 208 |
cmd |= CMD_LEFT; |
| 209 |
} |
| 210 |
if (2 * dy > dx) |
| 211 |
{ |
| 212 |
if (pos.y < mouse->currentPos.y) |
| 213 |
cmd |= CMD_DOWN; |
| 214 |
else if (pos.y > mouse->currentPos.y) |
| 215 |
cmd |= CMD_UP; |
| 216 |
} |
| 217 |
} |
| 218 |
return cmd; |
| 219 |
} |
| 220 |
|
| 221 |
void MouseSetCursor(Mouse *m, const SDL_SystemCursor sc) |
| 222 |
{ |
| 223 |
SDL_FreeCursor(m->cursor); |
| 224 |
m->cursor = NULL; |
| 225 |
SDL_SetCursor(m->cursors[sc]); |
| 226 |
SDL_ShowCursor(SDL_ENABLE); |
| 227 |
} |
| 228 |
void MouseSetPicCursor(Mouse *m, const Pic *cursor) |
| 229 |
{ |
| 230 |
if (cursor) |
| 231 |
{ |
| 232 |
SDL_FreeCursor(m->cursor); |
| 233 |
SDL_Surface *surf = SDL_CreateRGBSurfaceFrom( |
| 234 |
cursor->Data, cursor->size.x, cursor->size.y, 32, |
| 235 |
4 * cursor->size.x, gGraphicsDevice.Format->Rmask, |
| 236 |
gGraphicsDevice.Format->Gmask, gGraphicsDevice.Format->Bmask, |
| 237 |
0xff000000); |
| 238 |
m->cursor = |
| 239 |
SDL_CreateColorCursor(surf, -cursor->offset.x, -cursor->offset.y); |
| 240 |
SDL_FreeSurface(surf); |
| 241 |
if (m->cursor == NULL) |
| 242 |
{ |
| 243 |
LOG(LM_MAIN, LL_WARN, "cannot load mouse cursor: %s", |
| 244 |
SDL_GetError()); |
| 245 |
} |
| 246 |
else |
| 247 |
{ |
| 248 |
SDL_SetCursor(m->cursor); |
| 249 |
} |
| 250 |
} |
| 251 |
else |
| 252 |
{ |
| 253 |
if (m->cursor != NULL) |
| 254 |
{ |
| 255 |
SDL_FreeCursor(m->cursor); |
| 256 |
} |
| 257 |
m->cursor = NULL; |
| 258 |
} |
| 259 |
SDL_ShowCursor(cursor != NULL ? SDL_ENABLE : SDL_DISABLE); |
| 260 |
} |