1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-17 22:58:06 +02:00

iconbutton: Cache icon surface instead of doing serveral hashtable lookups per

frame
This commit is contained in:
Lars-Peter Clausen 2010-05-02 14:52:46 +02:00
parent 40a26e1a9c
commit 28d3fc9ece
2 changed files with 21 additions and 8 deletions

View File

@ -1,5 +1,6 @@
#include "iconbutton.h" #include "iconbutton.h"
#include "gmenu2x.h" #include "gmenu2x.h"
#include "surface.h"
using namespace std; using namespace std;
using namespace fastdelegate; using namespace fastdelegate;
@ -10,6 +11,13 @@ IconButton::IconButton(GMenu2X *gmenu2x, const string &icon, const string &label
labelPosition = IconButton::DISP_RIGHT; labelPosition = IconButton::DISP_RIGHT;
labelMargin = 2; labelMargin = 2;
this->setLabel(label); this->setLabel(label);
updateSurfaces();
}
void IconButton::updateSurfaces()
{
iconSurface = gmenu2x->sc[icon];
recalcSize();
} }
void IconButton::setPosition(int x, int y) { void IconButton::setPosition(int x, int y) {
@ -21,10 +29,10 @@ void IconButton::setPosition(int x, int y) {
void IconButton::paint() { void IconButton::paint() {
uint margin = labelMargin; uint margin = labelMargin;
if (gmenu2x->sc[icon] == NULL || label == "") if (iconSurface == NULL || label == "")
margin = 0; margin = 0;
if (gmenu2x->sc[icon] != NULL) if (iconSurface != NULL)
gmenu2x->sc[icon]->blit(gmenu2x->s,iconRect); iconSurface->blit(gmenu2x->s,iconRect);
if (label != "") if (label != "")
gmenu2x->s->write(gmenu2x->font, label, labelRect.x, labelRect.y, labelHAlign, labelVAlign); gmenu2x->s->write(gmenu2x->font, label, labelRect.x, labelRect.y, labelHAlign, labelVAlign);
} }
@ -37,12 +45,12 @@ void IconButton::recalcSize() {
uint h = 0, w = 0; uint h = 0, w = 0;
uint margin = labelMargin; uint margin = labelMargin;
if (gmenu2x->sc[icon] == NULL || label == "") if (iconSurface == NULL || label == "")
margin = 0; margin = 0;
if (gmenu2x->sc[icon] != NULL) { if (iconSurface != NULL) {
w += gmenu2x->sc[icon]->raw->w; w += iconSurface->raw->w;
h += gmenu2x->sc[icon]->raw->h; h += iconSurface->raw->h;
iconRect.w = w; iconRect.w = w;
iconRect.h = h; iconRect.h = h;
iconRect.x = rect.x; iconRect.x = rect.x;
@ -113,7 +121,7 @@ const string &IconButton::getIcon() {
void IconButton::setIcon(const string &icon) { void IconButton::setIcon(const string &icon) {
this->icon = icon; this->icon = icon;
recalcSize(); updateSurfaces();
} }
void IconButton::setAction(ButtonAction action) { void IconButton::setAction(ButtonAction action) {

View File

@ -7,6 +7,7 @@
using std::string; using std::string;
class GMenu2X; class GMenu2X;
class Surface;
class IconButton : public Button { class IconButton : public Button {
protected: protected:
@ -16,6 +17,10 @@ protected:
void recalcSize(); void recalcSize();
SDL_Rect iconRect, labelRect; SDL_Rect iconRect, labelRect;
Surface *iconSurface;
void updateSurfaces();
public: public:
static const int DISP_RIGHT = 0; static const int DISP_RIGHT = 0;
static const int DISP_LEFT = 1; static const int DISP_LEFT = 1;