[go: up one dir, main page]

Menu

[r115]: / lpi_gui_base.cpp  Maximize  Restore  History

Download this file

582 lines (490 with data), 14.6 kB

  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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
Copyright (c) 2005-2009 Lode Vandevenne
All rights reserved.
This file is part of Lode's Programming Interface.
Lode's Programming Interface 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 3 of the License, or
(at your option) any later version.
Lode's Programming Interface is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Lode's Programming Interface. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lpi_gui_base.h"
#include <cstdlib> //abs on int
namespace lpi
{
namespace gui
{
bool IGUIInputClick::doubleClicked(GUIMouseButton button) const
{
double timeBetween = 0.5;
bool dclick = 0;
bool down = mouseButtonDown(button); //is the button down
switch(doubleClickState[button])
{
case 0:
if(down)
{
doubleClickTime[button] = getSeconds();
doubleClickState[button] = 1;
doubleClickX[button] = mouseX();
doubleClickY[button] = mouseY();
}
break;
case 1:
if(!down)
doubleClickState[button] = 2;
break;
case 2:
if(down)
doubleClickState[button] = 3;
break;
case 3:
if(!down)
{
doubleClickState[button] = 0;
if(getSeconds() - doubleClickTime[button] < timeBetween) dclick = 1;
}
break;
}
if
(
(doubleClickState[button] > 0 && (getSeconds() - doubleClickTime[button]) > timeBetween)
|| std::abs(mouseX() - doubleClickX[button]) > 1 || std::abs(mouseY() - doubleClickY[button]) > 1 //movement of 1 pixel allowed
)
{
doubleClickState[button] = 0;
doubleClickTime[button] = 0;
}
return dclick;
}
bool IGUIInputClick::tripleClicked(GUIMouseButton button) const
{
double timeBetween = 0.75;
bool tclick = 0;
bool down = mouseButtonDown(button); //is the button down
switch(tripleClickState[button])
{
case 0:
if(down)
{
tripleClickTime[button] = getSeconds();
tripleClickState[button] = 1;
tripleClickX[button] = mouseX();
tripleClickY[button] = mouseY();
}
break;
case 1:
case 3:
if(!down)
tripleClickState[button]++;
break;
case 2:
case 4:
if(down)
tripleClickState[button]++;
break;
case 5:
if(!down)
{
tripleClickState[button] = 0;
if(getSeconds() - tripleClickTime[button] < timeBetween) tclick = 1;
}
break;
}
if
(
(tripleClickState[button] > 0 && (getSeconds() - tripleClickTime[button]) > timeBetween)
|| std::abs(mouseX() - tripleClickX[button]) > 1 || std::abs(mouseY() - tripleClickY[button]) > 1 //movement of 1 pixel allowed
)
{
tripleClickState[button] = 0;
tripleClickTime[button] = 0;
}
return tclick;
}
bool IGUIInputClick::quadrupleClicked(GUIMouseButton button) const
{
double timeBetween = 1.0;
bool tclick = 0;
bool down = mouseButtonDown(button); //is the button down
switch(quadrupleClickState[button])
{
case 0:
if(down)
{
quadrupleClickTime[button] = getSeconds();
quadrupleClickState[button] = 1;
quadrupleClickX[button] = mouseX();
quadrupleClickY[button] = mouseY();
}
break;
case 1:
case 3:
case 5:
if(!down)
quadrupleClickState[button]++;
break;
case 2:
case 4:
case 6:
if(down)
quadrupleClickState[button]++;
break;
case 7:
if(!down)
{
quadrupleClickState[button] = 0;
if(getSeconds() - quadrupleClickTime[button] < timeBetween) tclick = 1;
}
break;
}
if
(
(quadrupleClickState[button] > 0 && (getSeconds() - quadrupleClickTime[button]) > timeBetween)
|| std::abs(mouseX() - quadrupleClickX[button]) > 1 || std::abs(mouseY() - quadrupleClickY[button]) > 1 //movement of 1 pixel allowed
)
{
quadrupleClickState[button] = 0;
quadrupleClickTime[button] = 0;
}
return tclick;
}
double IGUIInputClick::mouseSpeedImp(int pos, std::vector<int>& mousePosHistory, std::vector<double>& mousePosTimeHistory) const
{
static const size_t MIN_VALUES = 2;
static const size_t MAX_VALUES = 3;
//static const double TRACK_TIME = 0.1; //how a long mouse history we want to track
mousePosHistory.push_back(pos);
mousePosTimeHistory.push_back(getSeconds());
double dt = mousePosTimeHistory.back() - mousePosTimeHistory[0];
size_t size = mousePosHistory.size();
double result = dt == 0 ? 0 : (double)(mousePosHistory.back() - mousePosHistory[0]) / dt;
bool remove = false;
if(size > MAX_VALUES) remove = true;
//if(dt > TRACK_TIME) remove = true;
if(size < MIN_VALUES) remove = false;
if(remove)
{
mousePosHistory.erase(mousePosHistory.begin());
mousePosTimeHistory.erase(mousePosTimeHistory.begin());
}
return result;
}
double IGUIInputClick::mouseSpeedX() const
{
return mouseSpeedImp(mouseX(), mousePosXHistory, mousePosXTimeHistory);
}
double IGUIInputClick::mouseSpeedY() const
{
return mouseSpeedImp(mouseY(), mousePosYHistory, mousePosYTimeHistory);
}
////////////////////////////////////////////////////////////////////////////////
MouseOverState::MouseOverState()
: over_prev(false)
, left_prev(false)
{
}
bool MouseOverState::mouseJustOver(bool over_now)
{
bool result = over_now && !over_prev;
over_prev = over_now;
return result;
}
bool MouseOverState::mouseJustLeft(bool over_now)
{
bool result = !over_now && left_prev;
left_prev = over_now;
return result;
}
////////////////////////////////////////////////////////////////////////////////
bool mouseJustDown(bool& prevstate, bool over, bool down)
{
if(over && down)
{
if(!prevstate)
{
prevstate = true;
return true;
}
else return false;
}
else
{
prevstate = false;
return false;
}
}
bool mouseJustDownHere(bool& prevstate, bool over, bool down)
{
if(over && down)
{
if(!prevstate)
{
prevstate = true;
return true;
}
else return false;
}
else
{
if(over)
prevstate = false;
else
prevstate = true; //so that it can't return true anymore after mouse was not on this
return false;
}
}
MouseState::MouseState()
{
justdown_prev = 0;
justdownhere_prev = 0;
grabbed_grabbed = 0;
grabbed_prev = 0;
downhere_bool1 = 0;
downhere_bool2 = 0;
justuphere_bool1 = 0;
justuphere_bool2 = 0;
}
bool MouseState::mouseDownHere(bool over, bool down)
{
if(!down)
{
downhere_bool1 = false;
downhere_bool2 = false;
}
if(down && downhere_bool2 == false) //downhere_bool2 means justOver (it's the prevstate)
{
downhere_bool2 = true;
downhere_bool1 = over; //true means it was inside when just clicked, false that it was not
}
return downhere_bool1 && over;
}
bool MouseState::mouseGrabbed(bool over, bool down, int x, int y, int relx, int rely)
{
//grab
bool temp_bool = grabbed_prev;
if(lpi::gui::mouseJustDownHere(temp_bool, over, down))
{
grabbed_grabbed = true;
grabx = x;
graby = y;
grabrelx = relx;
grabrely = rely;
}
grabbed_prev = temp_bool;
//ungrab
if(!down)
{
grabbed_grabbed = false;
grabbed_prev = false;
}
return grabbed_grabbed;
}
void MouseState::mouseUngrab()
{
grabbed_grabbed = false;
grabbed_prev = false;
}
bool MouseState::mouseJustDown(bool over, bool down)
{
bool temp_bool = justdown_prev;
bool result = lpi::gui::mouseJustDown(temp_bool, over, down);
justdown_prev = temp_bool;
return result;
}
bool MouseState::mouseJustDownHere(bool over, bool down)
{
bool temp_bool = justdownhere_prev;
bool result = lpi::gui::mouseJustDownHere(temp_bool, over, down);
justdownhere_prev = temp_bool;
return result;
}
bool MouseState::mouseJustUpHere(bool over, bool down)
{
bool temp_bool = justuphere_bool1;
if(lpi::gui::mouseJustDownHere(temp_bool, over, down))
{
justuphere_bool2 = true;
}
justuphere_bool1 = temp_bool;
if(justuphere_bool2 && !down)
{
justuphere_bool2 = false;
if(over) return true;
else return false;
}
return false;
}
void MouseState::mouseGrab(int x, int y, int relx, int rely)
{
grabbed_grabbed = true;
grabx = x;
graby = y;
grabrelx = relx;
grabrely = rely;
}
////////////////////////////////////////////////////////////////////////////////
//ELEMENTSHAPE
////////////////////////////////////////////////////////////////////////////////
bool ElementShape::mouseJustOver(const IGUIInput& input)
{
return mouse_over_state.mouseJustOver(mouseOver(input));
}
bool ElementShape::mouseJustLeft(const IGUIInput& input)
{
return mouse_over_state.mouseJustLeft(mouseOver(input));
}
bool ElementShape::mouseDown(const IGUIInput& input, GUIMouseButton button) const
{
return mouseOver(input) && input.mouseButtonDown(button);
}
bool ElementShape::mouseDownHere(const IGUIInput& input, GUIMouseButton button)
{
return _mouseState[button].mouseDownHere(mouseOver(input), input.mouseButtonDown(button));
}
bool ElementShape::mouseGrabbed(const IGUIInput& input, GUIMouseButton button)
{
if(!mouseGrabbable()) return false;
return _mouseState[button].mouseGrabbed(mouseOver(input)
, input.mouseButtonDown(button)
, input.mouseX()
, input.mouseY()
, mouseGetRelPosX(input)
, mouseGetRelPosY(input));
}
void ElementShape::mouseGrab(const IGUIInput& input, GUIMouseButton button)
{
return _mouseState[button].mouseGrab(input.mouseX()
, input.mouseY()
, mouseGetRelPosX(input)
, mouseGetRelPosY(input));
}
void ElementShape::mouseUngrab(GUIMouseButton button)
{
_mouseState[button].mouseUngrab();
}
int ElementShape::mouseGetGrabDiffX(const IGUIInput& input, GUIMouseButton button) const
{
return mouseGetRelGrabX(button) - mouseGetRelPosX(input);
}
int ElementShape::mouseGetGrabDiffY(const IGUIInput& input, GUIMouseButton button) const
{
return mouseGetRelGrabY(button) - mouseGetRelPosY(input);
}
bool ElementShape::mouseJustDown(const IGUIInput& input, GUIMouseButton button)
{
return _mouseState[button].mouseJustDown(mouseOver(input), input.mouseButtonDown(button));
}
bool ElementShape::mouseJustDownHere(const IGUIInput& input, GUIMouseButton button)
{
return _mouseState[button].mouseJustDownHere(mouseOver(input), input.mouseButtonDown(button));
}
bool ElementShape::mouseJustUpHere(const IGUIInput& input, GUIMouseButton button)
{
return _mouseState[button].mouseJustUpHere(mouseOver(input), input.mouseButtonDown(button));
}
bool ElementShape::pressed(const IGUIInput& input, GUIMouseButton button)
{
return mouseActive() && mouseJustDownHere(input, button);
}
bool ElementShape::clicked(const IGUIInput& input, GUIMouseButton button)
{
return mouseActive() && mouseJustUpHere(input, button);
}
bool ElementShape::mouseScrollUp(const IGUIInput& input) const
{
return mouseActive() && mouseOver(input) && input.mouseWheelUp();
}
bool ElementShape::mouseScrollDown(const IGUIInput& input) const
{
return mouseActive() && mouseOver(input) && input.mouseWheelDown();
}
bool ElementShape::mouseDoubleClicked(const IGUIInput& input, GUIMouseButton button) const
{
return mouseOver(input) && input.doubleClicked(button);
}
bool ElementShape::mouseTripleClicked(const IGUIInput& input, GUIMouseButton button) const
{
return mouseOver(input) && input.tripleClicked(button);
}
bool ElementShape::mouseQuadrupleClicked(const IGUIInput& input, GUIMouseButton button) const
{
return mouseOver(input) && input.quadrupleClicked(button);
}
ElementShape::ElementShape()
{
}
bool ElementShape::mouseOver(const IGUIInput& input) const
{
return isInside(input.mouseX(), input.mouseY());
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//ELEMENTRECTANGULAR
////////////////////////////////////////////////////////////////////////////////
ElementRectangular::ElementRectangular() : x0(0),
y0(0),
x1(0),
y1(0)
{
}
void ElementRectangular::initPosition(int x0, int y0, int x1, int y1)
{
this->x0 = x0;
this->y0 = y0;
this->x1 = x1;
this->y1 = y1;
}
int ElementRectangular::mouseGetRelPosX(const IGUIInput& input) const
{
return input.mouseX() - x0;
}
int ElementRectangular::mouseGetRelPosY(const IGUIInput& input) const
{
return input.mouseY() - y0;
}
bool ElementRectangular::isInside(int x, int y) const
{
if(x >= x0 && x < x1 && y >= y0 && y < y1) return true;
else return false;
//old "shape" code. Now, to get a shape like triangle, make child class of the gui element and override mouseOverShape with the new shape code
/*switch(shape)
{
case 0: //rectangle
over = 1;
break;
case 1: //triangle pointing up
relX = globalMouseX - x0;
relY = globalMouseY - y0;
symX = std::abs(getSizex() / 2 - relX); //it's symmetrical
if(relY >= (2 * symX * getSizey()) / getSizex()) over = 1;
break;
case 2: //triangle pointing right
relX = globalMouseX - x0;
relY = globalMouseY - y0;
symY = std::abs(getSizey() / 2 - relY); //it's symmetrical
if(getSizex() - relX >= (2 * symY * getSizex()) / getSizey()) over = 1;
break;
case 3: //triangle pointing down
relX = globalMouseX - x0;
relY = globalMouseY - y0;
symX = std::abs(getSizex() / 2 - relX); //it's symmetrical
if(getSizey() - relY >= (2 * symX * getSizey()) / getSizex()) over = 1;
case 4: //triangle pointing left
relX = globalMouseX - x0;
relY = globalMouseY - y0;
symY = std::abs(getSizey() / 2 - relY); //it's symmetrical
if(relX >= (2 * symY * getSizey()) / getSizey()) over = 1;
break;
default: //rectangle
over = 1;
break;
}*/
}
} //namespace gui
} //namespace lpi