Root/
| 1 | #include "button.h" |
| 2 | #include "gmenu2x.h" |
| 3 | |
| 4 | using namespace std; |
| 5 | using namespace fastdelegate; |
| 6 | |
| 7 | Button::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 | |
| 16 | void Button::paint() { |
| 17 | if (ts.inRect(rect)) |
| 18 | if (!paintHover()) return; |
| 19 | } |
| 20 | |
| 21 | bool Button::paintHover() { |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | bool Button::isPressed() { |
| 26 | return ts.pressed() && ts.inRect(rect); |
| 27 | } |
| 28 | |
| 29 | bool Button::isReleased() { |
| 30 | return ts.released() && ts.inRect(rect); |
| 31 | } |
| 32 | |
| 33 | bool 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 | |
| 48 | void Button::exec() { |
| 49 | ts.setHandled(); |
| 50 | action(); |
| 51 | } |
| 52 | |
| 53 | SDL_Rect Button::getRect() { |
| 54 | return rect; |
| 55 | } |
| 56 | |
| 57 | void Button::setSize(int w, int h) { |
| 58 | rect.w = w; |
| 59 | rect.h = h; |
| 60 | } |
| 61 | |
| 62 | void Button::setPosition(int x, int y) { |
| 63 | rect.x = x; |
| 64 | rect.y = y; |
| 65 | } |
| 66 | |
| 67 | void Button::setAction(ButtonAction action) { |
| 68 | this->action = action; |
| 69 | } |
| 70 |
Branches:
install_locations
master
opkrun
packages
