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;
|
|
|
|
using namespace fastdelegate;
|
|
|
|
|
2010-07-27 23:12:28 +03:00
|
|
|
IconButton::IconButton(GMenu2X *gmenu2x_, const string &icon,
|
|
|
|
const string &label)
|
|
|
|
: Button(gmenu2x_->ts)
|
|
|
|
, 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() {
|
|
|
|
uint 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) {
|
|
|
|
iconRect.x + iconRect.w + margin,
|
|
|
|
rect.y + h / 2,
|
|
|
|
gmenu2x->font->getTextWidth(label),
|
|
|
|
gmenu2x->font->getHeight()
|
|
|
|
};
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void IconButton::setAction(ButtonAction action) {
|
|
|
|
this->action = action;
|
|
|
|
}
|