-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbflib_mouse.cpp
348 lines (329 loc) · 9.98 KB
/
bflib_mouse.cpp
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/******************************************************************************/
// Bullfrog Engine Emulation Library - for use to remake classic games like
// Syndicate Wars, Magic Carpet or Dungeon Keeper.
/******************************************************************************/
/** @file bflib_mouse.cpp
* Mouse related routines.
* @par Purpose:
* Pointer position, movement and cursor support.
* @par Comment:
* None.
* @author Tomasz Lis
* @date 12 Feb 2008 - 26 Oct 2010
* @par Copying and copyrights:
* 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 of the License, or
* (at your option) any later version.
*/
/******************************************************************************/
#include "bflib_mouse.h"
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include "bflib_basics.h"
#include "globals.h"
#include "bflib_video.h"
#include "bflib_memory.h"
#include "bflib_sprite.h"
#include "bflib_vidraw.h"
#include "bflib_mshandler.hpp"
#ifdef __cplusplus
extern "C" {
#endif
/******************************************************************************/
/*
struct mouse_buffer mbuffer;
struct mouse_info minfo;
char ptr[4096];
unsigned int redraw_active=0;
unsigned int mouse_initialised=0;
short volatile mouse_mickey_x;
short volatile mouse_mickey_y;
long volatile mouse_dx;
long volatile mouse_dy;
unsigned long mouse_pos_change_saved;
struct DevInput joy;
*/
volatile TbBool lbMouseAutoReset = true;
volatile TbDisplayStructEx lbDisplayEx;
/******************************************************************************/
TbResult LbMouseChangeSpriteAndHotspot(struct TbSprite *pointerSprite, long hot_x, long hot_y)
{
#if (BFDEBUG_LEVEL > 18)
if (pointerSprite == NULL)
SYNCLOG("Setting to %s","NONE");
else
SYNCLOG("Setting to %dx%d, data at %p",(int)pointerSprite->SWidth,(int)pointerSprite->SHeight,pointerSprite);
#endif
if (!lbMouseInstalled)
return Lb_FAIL;
if (!pointerHandler.SetMousePointerAndOffset(pointerSprite, hot_x, hot_y))
return Lb_FAIL;
return Lb_SUCCESS;
}
TbResult LbMouseSetup(struct TbSprite *pointerSprite)
{
TbResult ret;
long x,y;
if (lbMouseInstalled)
LbMouseSuspend();
y = (lbDisplay.MouseWindowHeight + lbDisplay.MouseWindowY) / 2;
x = (lbDisplay.MouseWindowWidth + lbDisplay.MouseWindowX) / 2;
pointerHandler.Install();
lbMouseOffline = true;
lbMouseInstalled = true;
LbMouseSetWindow(0,0,LbGraphicsScreenWidth(),LbGraphicsScreenHeight());
ret = Lb_SUCCESS;
if (LbMouseSetPosition(x,y) != Lb_SUCCESS)
ret = Lb_FAIL;
if (LbMouseChangeSprite(pointerSprite) != Lb_SUCCESS)
ret = Lb_FAIL;
lbMouseInstalled = (ret == Lb_SUCCESS);
lbMouseOffline = false;
return ret;
}
TbResult LbMouseSetPointerHotspot(long hot_x, long hot_y)
{
if (!lbMouseInstalled)
return Lb_FAIL;
if (!pointerHandler.SetPointerOffset(hot_x, hot_y))
return Lb_FAIL;
return Lb_SUCCESS;
}
TbResult LbMouseSetPosition(long x, long y)
{
if (!lbMouseInstalled)
return Lb_FAIL;
if (!pointerHandler.SetMousePosition(x, y))
return Lb_FAIL;
return Lb_SUCCESS;
}
TbResult LbMouseChangeSprite(struct TbSprite *pointerSprite)
{
#if (BFDEBUG_LEVEL > 18)
if (pointerSprite == NULL)
SYNCLOG("Setting to %s","NONE");
else
SYNCLOG("Setting to %dx%d, data at %p",(int)pointerSprite->SWidth,(int)pointerSprite->SHeight,pointerSprite);
#endif
if (!lbMouseInstalled)
return Lb_FAIL;
if (!pointerHandler.SetMousePointer(pointerSprite))
return Lb_FAIL;
return Lb_SUCCESS;
}
void GetPointerHotspot(long *hot_x, long *hot_y)
{
struct TbPoint *hotspot;
hotspot = pointerHandler.GetPointerOffset();
if (hotspot == NULL)
return;
*hot_x = hotspot->x;
*hot_y = hotspot->y;
}
TbResult LbMouseIsInstalled(void)
{
if (!lbMouseInstalled)
return Lb_FAIL;
if (!pointerHandler.IsInstalled())
return Lb_FAIL;
return Lb_SUCCESS;
}
TbResult LbMouseSetWindow(long x, long y, long width, long height)
{
if (!lbMouseInstalled)
return Lb_FAIL;
if (!pointerHandler.SetMouseWindow(x, y, width, height))
return Lb_FAIL;
return Lb_SUCCESS;
}
TbResult LbMouseOnMove(struct TbPoint shift)
{
if ((!lbMouseInstalled) || (lbMouseOffline))
return Lb_FAIL;
if (!pointerHandler.SetMousePosition(lbDisplay.MMouseX+shift.x, lbDisplay.MMouseY+shift.y))
return Lb_FAIL;
return Lb_SUCCESS;
}
/** Converts mouse coordinates into relative shift coordinates.
*
* @param pos Pointer to the structure with source point, and where result is placed.
*/
void MouseToScreen(struct TbPoint *pos)
{
// Static variables for storing last mouse coordinated; needed
// because lbDisplay.MMouse? coords are scaled
static long mx = 0;
static long my = 0;
struct TbRect clip;
struct TbPoint orig;
if ( lbMouseAutoReset )
{
if (!pointerHandler.GetMouseWindow(&clip))
return;
orig.x = pos->x;
orig.y = pos->y;
pos->x = ((pos->x - mx) * (long)lbDisplay.MouseMoveRatio)/256;
pos->y = ((pos->y - my) * (long)lbDisplay.MouseMoveRatio)/256;
mx = orig.x;
my = orig.y;
if ((mx < clip.left + 50) || (mx > clip.right - 50)
|| (my < clip.top + 50) || (my > clip.bottom - 50))
{
mx = (clip.right-clip.left)/2 + clip.left;
my = (clip.bottom-clip.top)/2 + clip.top;
SDL_WarpMouse(mx, my);
}
} else
{
orig.x = pos->x;
orig.y = pos->y;
pos->x = ((pos->x - mx) * (long)lbDisplay.MouseMoveRatio)/256;
pos->y = ((pos->y - my) * (long)lbDisplay.MouseMoveRatio)/256;
mx = orig.x;
my = orig.y;
}
}
TbResult LbMouseSuspend(void)
{
if (!lbMouseInstalled)
return Lb_FAIL;
if (!pointerHandler.Release())
return Lb_FAIL;
return Lb_SUCCESS;
}
TbResult LbMouseOnBeginSwap(void)
{
if (!pointerHandler.PointerBeginSwap())
return Lb_FAIL;
return Lb_SUCCESS;
}
TbResult LbMouseOnEndSwap(void)
{
if (!pointerHandler.PointerEndSwap())
return Lb_FAIL;
return Lb_SUCCESS;
}
void mouseControl(unsigned int action, struct TbPoint *pos)
{
struct TbPoint dstPos;
dstPos.x = pos->x;
dstPos.y = pos->y;
switch ( action )
{
case MActn_MOUSEMOVE:
MouseToScreen(&dstPos);
LbMouseOnMove(dstPos);
break;
case MActn_LBUTTONDOWN:
lbDisplay.MLeftButton = 1;
if ( !lbDisplay.LeftButton )
{
MouseToScreen(&dstPos);
LbMouseOnMove(dstPos);
lbDisplay.MouseX = lbDisplay.MMouseX;
lbDisplay.MouseY = lbDisplay.MMouseY;
lbDisplay.RLeftButton = 0;
lbDisplay.LeftButton = 1;
}
break;
case MActn_LBUTTONUP:
lbDisplay.MLeftButton = 0;
if ( !lbDisplay.RLeftButton )
{
MouseToScreen(&dstPos);
LbMouseOnMove(dstPos);
lbDisplay.RMouseX = lbDisplay.MMouseX;
lbDisplay.RMouseY = lbDisplay.MMouseY;
lbDisplay.RLeftButton = 1;
}
break;
case MActn_RBUTTONDOWN:
lbDisplay.MRightButton = 1;
if ( !lbDisplay.RightButton )
{
MouseToScreen(&dstPos);
LbMouseOnMove(dstPos);
lbDisplay.MouseX = lbDisplay.MMouseX;
lbDisplay.MouseY = lbDisplay.MMouseY;
lbDisplay.RRightButton = 0;
lbDisplay.RightButton = 1;
}
break;
case MActn_RBUTTONUP:
lbDisplay.MRightButton = 0;
if ( !lbDisplay.RRightButton )
{
MouseToScreen(&dstPos);
LbMouseOnMove(dstPos);
lbDisplay.RMouseX = lbDisplay.MMouseX;
lbDisplay.RMouseY = lbDisplay.MMouseY;
lbDisplay.RRightButton = 1;
}
break;
case MActn_MBUTTONDOWN:
lbDisplay.MMiddleButton = 1;
if ( !lbDisplay.MiddleButton )
{
MouseToScreen(&dstPos);
LbMouseOnMove(dstPos);
lbDisplay.MouseX = lbDisplay.MMouseX;
lbDisplay.MouseY = lbDisplay.MMouseY;
lbDisplay.MiddleButton = 1;
lbDisplay.RMiddleButton = 0;
}
break;
case MActn_MBUTTONUP:
lbDisplay.MMiddleButton = 0;
if ( !lbDisplay.RMiddleButton )
{
MouseToScreen(&dstPos);
LbMouseOnMove(dstPos);
lbDisplay.RMouseX = lbDisplay.MMouseX;
lbDisplay.RMouseY = lbDisplay.MMouseY;
lbDisplay.RMiddleButton = 1;
}
break;
case MActn_WHEELMOVEUP:
lbDisplayEx.WhellPosition--;
lbDisplayEx.WhellMoveUp++;
lbDisplayEx.WhellMoveDown = 0;
break;
case MActn_WHEELMOVEDOWN:
lbDisplayEx.WhellPosition++;
lbDisplayEx.WhellMoveUp = 0;
lbDisplayEx.WhellMoveDown++;
break;
default:
break;
}
}
/**
* Changes mouse movement ratio.
* Note that this function can be run even before mouse setup. Still, the factor
* will be reset during the installation - so use it after LbMouseSetup().
*
* @param ratio_x Movement ratio in X direction; 256 means unchanged ratio from OS.
* @param ratio_y Movement ratio in Y direction; 256 means unchanged ratio from OS.
* @return Lb_SUCCESS if the ratio values were of correct range and have been set.
*/
TbResult LbMouseChangeMoveRatio(long ratio_x, long ratio_y)
{
if ((ratio_x < -8192) || (ratio_x > 8192) || (ratio_x == 0))
return Lb_FAIL;
if ((ratio_y < -8192) || (ratio_y > 8192) || (ratio_y == 0))
return Lb_FAIL;
SYNCLOG("New ratio %ldx%ld",ratio_x, ratio_y);
// Currently we don't have two ratio factors, so let's store an average
lbDisplay.MouseMoveRatio = (ratio_x + ratio_y)/2;
//TODO INPUT Separate mouse ratios in X and Y direction when lbDisplay from DLL will no longer be used.
//minfo.XMoveRatio = ratio_x;
//minfo.YMoveRatio = ratio_y;
return Lb_SUCCESS;
}
/******************************************************************************/
#ifdef __cplusplus
}
#endif