2010-02-04 13:33:47 +02:00
|
|
|
#include "iconbutton.h"
|
2011-10-23 13:37:39 +03:00
|
|
|
|
|
|
|
#include "asfont.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;
|
|
|
|
|
2011-12-23 15:03:05 +02:00
|
|
|
IconButton::IconButton(
|
|
|
|
GMenu2X *gmenu2x_, Touchscreen &ts_,
|
|
|
|
const string &icon, const string &label)
|
|
|
|
: Button(ts_)
|
2010-07-27 23:12:28 +03:00
|
|
|
, gmenu2x(gmenu2x_)
|
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
this->icon = icon;
|
2011-10-23 13:30:20 +03:00
|
|
|
this->label = label;
|
2010-05-02 15:52:46 +03:00
|
|
|
updateSurfaces();
|
|
|
|
}
|
|
|
|
|
2011-10-23 13:30:20 +03:00
|
|
|
void IconButton::updateSurfaces() {
|
2010-05-02 15:52:46 +03:00
|
|
|
iconSurface = gmenu2x->sc[icon];
|
|
|
|
recalcSize();
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconButton::setPosition(int x, int y) {
|
2011-10-23 13:06:59 +03:00
|
|
|
if (rect.x != x || rect.y != y) {
|
2010-02-04 13:33:47 +02:00
|
|
|
Button::setPosition(x,y);
|
|
|
|
recalcSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconButton::paint() {
|
2011-10-23 13:05:27 +03:00
|
|
|
if (iconSurface) {
|
2011-10-23 13:30:20 +03:00
|
|
|
iconSurface->blit(gmenu2x->s, iconRect);
|
2011-10-23 13:05:27 +03:00
|
|
|
}
|
|
|
|
if (label != "") {
|
|
|
|
gmenu2x->s->write(gmenu2x->font, label, labelRect.x, labelRect.y,
|
2011-10-23 13:30:20 +03:00
|
|
|
ASFont::HAlignLeft, ASFont::VAlignMiddle);
|
2011-10-23 13:05:27 +03:00
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IconButton::paintHover() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconButton::recalcSize() {
|
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();
|
2011-10-23 13:30:20 +03:00
|
|
|
iconRect = (SDL_Rect) { rect.x, rect.y, w, h };
|
2010-02-04 13:33:47 +02:00
|
|
|
} else {
|
2011-10-23 13:30:20 +03:00
|
|
|
iconRect = (SDL_Rect) { 0, 0, 0, 0 };
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (label != "") {
|
2011-10-23 13:30:20 +03:00
|
|
|
uint margin = iconSurface ? 2 : 0;
|
|
|
|
labelRect = (SDL_Rect) {
|
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)),
|
|
|
|
static_cast<Uint16>(gmenu2x->font->getHeight())
|
2011-10-23 13:30:20 +03:00
|
|
|
};
|
|
|
|
w += margin + labelRect.w;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2011-10-23 13:30:20 +03:00
|
|
|
setSize(w, h);
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2013-07-29 19:58:25 +03:00
|
|
|
void IconButton::setAction(function_t action) {
|
2010-02-04 13:33:47 +02:00
|
|
|
this->action = action;
|
|
|
|
}
|