Root/src/iconbutton.cpp

1#include "iconbutton.h"
2#include "gmenu2x.h"
3#include "surface.h"
4
5using namespace std;
6using namespace fastdelegate;
7
8IconButton::IconButton(GMenu2X *gmenu2x_, const string &icon,
9                       const string &label)
10    : Button(gmenu2x_->ts)
11    , gmenu2x(gmenu2x_)
12{
13    this->icon = icon;
14    labelPosition = IconButton::DISP_RIGHT;
15    labelMargin = 2;
16    this->setLabel(label);
17    updateSurfaces();
18}
19
20void IconButton::updateSurfaces()
21{
22    iconSurface = gmenu2x->sc[icon];
23    recalcSize();
24}
25
26void IconButton::setPosition(int x, int y) {
27    if (rect.x != x && rect.y != y) {
28        Button::setPosition(x,y);
29        recalcSize();
30    }
31}
32
33void IconButton::paint() {
34    uint margin = labelMargin;
35    if (iconSurface == NULL || label == "")
36        margin = 0;
37    if (iconSurface != NULL)
38        iconSurface->blit(gmenu2x->s,iconRect);
39    if (label != "")
40        gmenu2x->s->write(gmenu2x->font, label, labelRect.x, labelRect.y, labelHAlign, labelVAlign);
41}
42
43bool IconButton::paintHover() {
44    return true;
45}
46
47void IconButton::recalcSize() {
48    uint h = 0, w = 0;
49    uint margin = labelMargin;
50
51    if (iconSurface == NULL || label == "")
52        margin = 0;
53
54    if (iconSurface != NULL) {
55        w += iconSurface->raw->w;
56        h += iconSurface->raw->h;
57        iconRect.w = w;
58        iconRect.h = h;
59        iconRect.x = rect.x;
60        iconRect.y = rect.y;
61    } else {
62        iconRect.x = 0;
63        iconRect.y = 0;
64        iconRect.w = 0;
65        iconRect.h = 0;
66    }
67
68    if (label != "") {
69        labelRect.w = gmenu2x->font->getTextWidth(label);
70        labelRect.h = gmenu2x->font->getHeight();
71        if (labelPosition == IconButton::DISP_LEFT || labelPosition == IconButton::DISP_RIGHT) {
72            w += margin + labelRect.w;
73            //if (labelRect.h > h) h = labelRect.h;
74            labelHAlign = ASFont::HAlignLeft;
75            labelVAlign = ASFont::VAlignMiddle;
76        } else {
77            h += margin + labelRect.h;
78            //if (labelRect.w > w) w = labelRect.w;
79            labelHAlign = ASFont::HAlignCenter;
80            labelVAlign = ASFont::VAlignTop;
81        }
82
83        switch (labelPosition) {
84            case IconButton::DISP_BOTTOM:
85                labelRect.x = iconRect.x + iconRect.w/2;
86                labelRect.y = iconRect.y + iconRect.h + margin;
87            break;
88            case IconButton::DISP_TOP:
89                labelRect.x = iconRect.x + iconRect.w/2;
90                labelRect.y = rect.y;
91                iconRect.y += labelRect.h + margin;
92            break;
93            case IconButton::DISP_LEFT:
94                labelRect.x = rect.x;
95                labelRect.y = rect.y+h/2;
96                iconRect.x += labelRect.w + margin;
97            break;
98            default:
99                labelRect.x = iconRect.x + iconRect.w + margin;
100                labelRect.y = rect.y+h/2;
101            break;
102        }
103    }
104    setSize(w, h);
105}
106
107const string &IconButton::getLabel() {
108    return label;
109}
110
111void IconButton::setLabel(const string &label) {
112    this->label = label;
113}
114
115void IconButton::setLabelPosition(int pos, int margin) {
116    labelPosition = pos;
117    labelMargin = margin;
118    recalcSize();
119}
120
121const string &IconButton::getIcon() {
122    return icon;
123}
124
125void IconButton::setIcon(const string &icon) {
126    this->icon = icon;
127    updateSurfaces();
128}
129
130void IconButton::setAction(ButtonAction action) {
131    this->action = action;
132}
133

Archive Download this file



interactive