1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-07-04 20:52:56 +03:00

Make Link and IconButton inherit from Button privately

Each part of the code deals with either Links or IconButtons, but
not both: the base class is only used to share implementation and
not interface. Make this explicit by doing private inheritance.
This commit is contained in:
Maarten ter Huurne 2013-08-13 01:47:02 +02:00
parent 06ee35bb7a
commit 6378fcfcd7
6 changed files with 36 additions and 55 deletions

View File

@ -5,23 +5,14 @@
using namespace std; using namespace std;
Button::Button(Touchscreen &ts_, bool doubleClick_) Button::Button(Touchscreen &ts_, bool doubleClick_)
: ts(ts_) : action(BIND(&Button::voidAction))
, action(BIND(&Button::voidAction))
, rect((SDL_Rect) { 0, 0, 0, 0 }) , rect((SDL_Rect) { 0, 0, 0, 0 })
, ts(ts_)
, doubleClick(doubleClick_) , doubleClick(doubleClick_)
, lastTick(0) , lastTick(0)
{ {
} }
void Button::paint() {
if (ts.inRect(rect))
if (!paintHover()) return;
}
bool Button::paintHover() {
return false;
}
bool Button::isPressed() { bool Button::isPressed() {
return ts.pressed() && ts.inRect(rect); return ts.pressed() && ts.inRect(rect);
} }
@ -34,22 +25,20 @@ bool Button::handleTS() {
if (isReleased()) { if (isReleased()) {
if (doubleClick) { if (doubleClick) {
int tickNow = SDL_GetTicks(); int tickNow = SDL_GetTicks();
if (tickNow - lastTick < 400) if (tickNow - lastTick < 400) {
exec(); ts.setHandled();
action();
}
lastTick = tickNow; lastTick = tickNow;
} else { } else {
exec(); ts.setHandled();
action();
} }
return true; return true;
} }
return false; return false;
} }
void Button::exec() {
ts.setHandled();
action();
}
SDL_Rect Button::getRect() { SDL_Rect Button::getRect() {
return rect; return rect;
} }
@ -63,7 +52,3 @@ void Button::setPosition(int x, int y) {
rect.x = x; rect.x = x;
rect.y = y; rect.y = y;
} }
void Button::setAction(function_t action) {
this->action = action;
}

View File

@ -28,31 +28,29 @@
class Touchscreen; class Touchscreen;
class Button { class Button {
protected:
Touchscreen &ts;
function_t action;
SDL_Rect rect;
bool doubleClick;
int lastTick;
public: public:
SDL_Rect getRect();
virtual void setPosition(int x, int y);
bool isPressed();
bool handleTS();
protected:
Button(Touchscreen &ts, bool doubleClick = false); Button(Touchscreen &ts, bool doubleClick = false);
virtual ~Button() {}; virtual ~Button() {};
SDL_Rect getRect();
void setSize(int w, int h); void setSize(int w, int h);
virtual void setPosition(int x, int y);
virtual void paint(); function_t action;
virtual bool paintHover(); SDL_Rect rect;
bool isPressed(); private:
bool isReleased(); bool isReleased();
bool handleTS();
void exec();
void voidAction() {}; void voidAction() {};
void setAction(function_t action);
Touchscreen &ts;
bool doubleClick;
int lastTick;
}; };
#endif // BUTTON_H #endif // BUTTON_H

View File

@ -14,10 +14,6 @@ IconButton::IconButton(
{ {
this->icon = icon; this->icon = icon;
this->label = label; this->label = label;
updateSurfaces();
}
void IconButton::updateSurfaces() {
iconSurface = gmenu2x->sc[icon]; iconSurface = gmenu2x->sc[icon];
recalcSize(); recalcSize();
} }
@ -39,10 +35,6 @@ void IconButton::paint() {
} }
} }
bool IconButton::paintHover() {
return true;
}
void IconButton::recalcSize() { void IconButton::recalcSize() {
Uint16 h = 0, w = 0; Uint16 h = 0, w = 0;
if (iconSurface) { if (iconSurface) {

View File

@ -8,25 +8,27 @@
class GMenu2X; class GMenu2X;
class Surface; class Surface;
class IconButton : public Button { class IconButton : private Button {
public: public:
IconButton(GMenu2X *gmenu2x, Touchscreen &ts, IconButton(GMenu2X *gmenu2x, Touchscreen &ts,
const std::string &icon, const std::string &label = ""); const std::string &icon, const std::string &label = "");
virtual ~IconButton() {}; virtual ~IconButton() {};
virtual void paint(); virtual void paint();
virtual bool paintHover();
virtual void setPosition(int x, int y); virtual void setPosition(int x, int y);
void setAction(function_t action); void setAction(function_t action);
// Expose some Button functionality:
SDL_Rect getRect() { return Button::getRect(); }
bool handleTS() { return Button::handleTS(); }
private: private:
void updateSurfaces(); void recalcSize();
GMenu2X *gmenu2x; GMenu2X *gmenu2x;
std::string icon, label; std::string icon, label;
void recalcSize();
SDL_Rect iconRect, labelRect; SDL_Rect iconRect, labelRect;
Surface *iconSurface; Surface *iconSurface;

View File

@ -51,12 +51,11 @@ void Link::paint() {
gmenu2x->s->write(gmenu2x->font, getTitle(), iconX+16, rect.y + gmenu2x->skinConfInt["linkHeight"]-padding, Font::HAlignCenter, Font::VAlignBottom); gmenu2x->s->write(gmenu2x->font, getTitle(), iconX+16, rect.y + gmenu2x->skinConfInt["linkHeight"]-padding, Font::HAlignCenter, Font::VAlignBottom);
} }
bool Link::paintHover() { void Link::paintHover() {
if (gmenu2x->useSelectionPng) if (gmenu2x->useSelectionPng)
gmenu2x->sc["imgs/selection.png"]->blit(gmenu2x->s, rect, Font::HAlignCenter, Font::VAlignMiddle); gmenu2x->sc["imgs/selection.png"]->blit(gmenu2x->s, rect, Font::HAlignCenter, Font::VAlignMiddle);
else else
gmenu2x->s->box(rect.x, rect.y, rect.w, rect.h, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); gmenu2x->s->box(rect.x, rect.y, rect.w, rect.h, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
return true;
} }
void Link::updateSurfaces() void Link::updateSurfaces()

View File

@ -35,7 +35,7 @@ Base class that represents a link on screen.
@author Massimiliano Torromeo <massimiliano.torromeo@gmail.com> @author Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
*/ */
class Link : public Button { class Link : private Button {
private: private:
void recalcCoordinates(); void recalcCoordinates();
@ -59,7 +59,7 @@ public:
virtual ~Link() {}; virtual ~Link() {};
virtual void paint(); virtual void paint();
virtual bool paintHover(); void paintHover();
virtual void loadIcon(); virtual void loadIcon();
@ -75,6 +75,11 @@ public:
const std::string &getIconPath(); const std::string &getIconPath();
void run(); void run();
// Expose some Button functionality:
//SDL_Rect getRect() { return Button::getRect(); }
bool isPressed() { return Button::isPressed(); }
bool handleTS() { return Button::handleTS(); }
}; };
#endif #endif