2010-02-04 13:33:47 +02:00
|
|
|
#include "iconbutton.h"
|
2011-10-23 13:37:39 +03:00
|
|
|
|
2013-08-03 23:30:12 +03:00
|
|
|
#include "font.h"
|
2010-02-04 13:33:47 +02:00
|
|
|
#include "gmenu2x.h"
|
2010-05-02 15:52:46 +03:00
|
|
|
#include "surface.h"
|
2010-02-04 13:33:47 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2013-08-14 05:21:56 +03:00
|
|
|
|
2011-12-23 15:03:05 +02:00
|
|
|
IconButton::IconButton(
|
2013-08-14 05:21:56 +03:00
|
|
|
GMenu2X *gmenu2x, Touchscreen &ts,
|
2011-12-23 15:03:05 +02:00
|
|
|
const string &icon, const string &label)
|
2013-08-14 05:21:56 +03:00
|
|
|
: gmenu2x(gmenu2x)
|
|
|
|
, ts(ts)
|
|
|
|
, icon(icon)
|
|
|
|
, label(label)
|
|
|
|
, action([] {})
|
|
|
|
, rect({ 0, 0, 0, 0 })
|
2010-07-27 23:12:28 +03:00
|
|
|
{
|
2010-05-02 15:52:46 +03:00
|
|
|
iconSurface = gmenu2x->sc[icon];
|
2013-08-14 05:21:56 +03:00
|
|
|
recalcRects();
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 05:21:56 +03:00
|
|
|
void IconButton::setAction(function_t action) {
|
|
|
|
this->action = action;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 05:21:56 +03:00
|
|
|
void IconButton::setPosition(int x, int y) {
|
|
|
|
if (rect.x != x || rect.y != y) {
|
|
|
|
rect.x = x;
|
|
|
|
rect.y = y;
|
|
|
|
recalcRects();
|
2011-10-23 13:05:27 +03:00
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 05:21:56 +03:00
|
|
|
void IconButton::recalcRects() {
|
2012-04-11 00:01:16 +03:00
|
|
|
Uint16 h = 0, w = 0;
|
2011-10-23 13:30:20 +03:00
|
|
|
if (iconSurface) {
|
2011-06-02 23:44:04 +03:00
|
|
|
w += iconSurface->width();
|
|
|
|
h += iconSurface->height();
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
2013-08-14 05:21:56 +03:00
|
|
|
iconRect = { rect.x, rect.y, w, h };
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2013-08-14 05:21:56 +03:00
|
|
|
if (!label.empty()) {
|
|
|
|
Uint16 margin = iconSurface ? 2 : 0;
|
|
|
|
labelRect = {
|
2012-04-11 00:01:16 +03:00
|
|
|
static_cast<Sint16>(iconRect.x + iconRect.w + margin),
|
|
|
|
static_cast<Sint16>(rect.y + h / 2),
|
|
|
|
static_cast<Uint16>(gmenu2x->font->getTextWidth(label)),
|
2014-06-18 23:07:09 +03:00
|
|
|
static_cast<Uint16>(gmenu2x->font->getLineSpacing())
|
2011-10-23 13:30:20 +03:00
|
|
|
};
|
|
|
|
w += margin + labelRect.w;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 05:21:56 +03:00
|
|
|
rect.w = w;
|
|
|
|
rect.h = h;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 05:21:56 +03:00
|
|
|
bool IconButton::handleTS() {
|
|
|
|
if (ts.released() && ts.inRect(rect)) {
|
|
|
|
ts.setHandled();
|
|
|
|
action();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-10 05:26:59 +03:00
|
|
|
void IconButton::paint(Surface& s) {
|
2013-08-14 05:21:56 +03:00
|
|
|
if (iconSurface) {
|
2014-01-16 02:03:15 +02:00
|
|
|
iconSurface->blit(s, iconRect);
|
2013-08-14 05:21:56 +03:00
|
|
|
}
|
|
|
|
if (!label.empty()) {
|
2014-08-01 00:10:16 +03:00
|
|
|
gmenu2x->font->write(s, label, labelRect.x, labelRect.y,
|
2013-08-14 05:21:56 +03:00
|
|
|
Font::HAlignLeft, Font::VAlignMiddle);
|
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|