2010-02-04 13:33:47 +02:00
|
|
|
#include "iconbutton.h"
|
|
|
|
#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;
|
|
|
|
labelPosition = IconButton::DISP_RIGHT;
|
|
|
|
labelMargin = 2;
|
|
|
|
this->setLabel(label);
|
2010-05-02 15:52:46 +03:00
|
|
|
updateSurfaces();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconButton::updateSurfaces()
|
|
|
|
{
|
|
|
|
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) {
|
2010-05-02 15:52:46 +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,
|
|
|
|
labelHAlign, labelVAlign);
|
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IconButton::paintHover() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconButton::recalcSize() {
|
|
|
|
uint h = 0, w = 0;
|
|
|
|
uint margin = labelMargin;
|
|
|
|
|
2010-05-02 15:52:46 +03:00
|
|
|
if (iconSurface == NULL || label == "")
|
2010-02-04 13:33:47 +02:00
|
|
|
margin = 0;
|
|
|
|
|
2010-05-02 15:52:46 +03:00
|
|
|
if (iconSurface != NULL) {
|
2011-06-02 23:44:04 +03:00
|
|
|
w += iconSurface->width();
|
|
|
|
h += iconSurface->height();
|
2010-02-04 13:33:47 +02:00
|
|
|
iconRect.w = w;
|
|
|
|
iconRect.h = h;
|
|
|
|
iconRect.x = rect.x;
|
|
|
|
iconRect.y = rect.y;
|
|
|
|
} else {
|
|
|
|
iconRect.x = 0;
|
|
|
|
iconRect.y = 0;
|
|
|
|
iconRect.w = 0;
|
|
|
|
iconRect.h = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (label != "") {
|
|
|
|
labelRect.w = gmenu2x->font->getTextWidth(label);
|
|
|
|
labelRect.h = gmenu2x->font->getHeight();
|
|
|
|
if (labelPosition == IconButton::DISP_LEFT || labelPosition == IconButton::DISP_RIGHT) {
|
|
|
|
w += margin + labelRect.w;
|
|
|
|
//if (labelRect.h > h) h = labelRect.h;
|
2011-05-09 06:17:25 +03:00
|
|
|
labelHAlign = ASFont::HAlignLeft;
|
|
|
|
labelVAlign = ASFont::VAlignMiddle;
|
2010-02-04 13:33:47 +02:00
|
|
|
} else {
|
|
|
|
h += margin + labelRect.h;
|
|
|
|
//if (labelRect.w > w) w = labelRect.w;
|
2011-05-09 06:17:25 +03:00
|
|
|
labelHAlign = ASFont::HAlignCenter;
|
|
|
|
labelVAlign = ASFont::VAlignTop;
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (labelPosition) {
|
|
|
|
case IconButton::DISP_BOTTOM:
|
|
|
|
labelRect.x = iconRect.x + iconRect.w/2;
|
|
|
|
labelRect.y = iconRect.y + iconRect.h + margin;
|
|
|
|
break;
|
|
|
|
case IconButton::DISP_TOP:
|
|
|
|
labelRect.x = iconRect.x + iconRect.w/2;
|
|
|
|
labelRect.y = rect.y;
|
|
|
|
iconRect.y += labelRect.h + margin;
|
|
|
|
break;
|
|
|
|
case IconButton::DISP_LEFT:
|
|
|
|
labelRect.x = rect.x;
|
|
|
|
labelRect.y = rect.y+h/2;
|
|
|
|
iconRect.x += labelRect.w + margin;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
labelRect.x = iconRect.x + iconRect.w + margin;
|
|
|
|
labelRect.y = rect.y+h/2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setSize(w, h);
|
|
|
|
}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
const string &IconButton::getLabel() {
|
2010-02-04 13:33:47 +02:00
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
void IconButton::setLabel(const string &label) {
|
2010-02-04 13:33:47 +02:00
|
|
|
this->label = label;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconButton::setLabelPosition(int pos, int margin) {
|
|
|
|
labelPosition = pos;
|
|
|
|
labelMargin = margin;
|
|
|
|
recalcSize();
|
|
|
|
}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
const string &IconButton::getIcon() {
|
2010-02-04 13:33:47 +02:00
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
void IconButton::setIcon(const string &icon) {
|
2010-02-04 13:33:47 +02:00
|
|
|
this->icon = icon;
|
2010-05-02 15:52:46 +03:00
|
|
|
updateSurfaces();
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconButton::setAction(ButtonAction action) {
|
|
|
|
this->action = action;
|
|
|
|
}
|