Root/src/button.cpp

1#include "button.h"
2#include "gmenu2x.h"
3
4using namespace std;
5using namespace fastdelegate;
6
7Button::Button(Touchscreen &ts_, bool doubleClick_)
8    : ts(ts_)
9    , action(MakeDelegate(this, &Button::voidAction))
10    , rect((SDL_Rect) { 0, 0, 0, 0 })
11    , doubleClick(doubleClick_)
12    , lastTick(0)
13{
14}
15
16void Button::paint() {
17    if (ts.inRect(rect))
18        if (!paintHover()) return;
19}
20
21bool Button::paintHover() {
22    return false;
23}
24
25bool Button::isPressed() {
26    return ts.pressed() && ts.inRect(rect);
27}
28
29bool Button::isReleased() {
30    return ts.released() && ts.inRect(rect);
31}
32
33bool Button::handleTS() {
34    if (isReleased()) {
35        if (doubleClick) {
36            int tickNow = SDL_GetTicks();
37            if (tickNow - lastTick < 400)
38                exec();
39            lastTick = tickNow;
40        } else {
41            exec();
42        }
43        return true;
44    }
45    return false;
46}
47
48void Button::exec() {
49    ts.setHandled();
50    action();
51}
52
53SDL_Rect Button::getRect() {
54    return rect;
55}
56
57void Button::setSize(int w, int h) {
58    rect.w = w;
59    rect.h = h;
60}
61
62void Button::setPosition(int x, int y) {
63    rect.x = x;
64    rect.y = y;
65}
66
67void Button::setAction(ButtonAction action) {
68    this->action = action;
69}
70

Archive Download this file



interactive