1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-07-02 18:32:20 +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;
Button::Button(Touchscreen &ts_, bool doubleClick_)
: ts(ts_)
, action(BIND(&Button::voidAction))
: action(BIND(&Button::voidAction))
, rect((SDL_Rect) { 0, 0, 0, 0 })
, ts(ts_)
, doubleClick(doubleClick_)
, lastTick(0)
{
}
void Button::paint() {
if (ts.inRect(rect))
if (!paintHover()) return;
}
bool Button::paintHover() {
return false;
}
bool Button::isPressed() {
return ts.pressed() && ts.inRect(rect);
}
@ -34,22 +25,20 @@ bool Button::handleTS() {
if (isReleased()) {
if (doubleClick) {
int tickNow = SDL_GetTicks();
if (tickNow - lastTick < 400)
exec();
if (tickNow - lastTick < 400) {
ts.setHandled();
action();
}
lastTick = tickNow;
} else {
exec();
ts.setHandled();
action();
}
return true;
}
return false;
}
void Button::exec() {
ts.setHandled();
action();
}
SDL_Rect Button::getRect() {
return rect;
}
@ -63,7 +52,3 @@ void Button::setPosition(int x, int y) {
rect.x = x;
rect.y = y;
}
void Button::setAction(function_t action) {
this->action = action;
}

View File

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

View File

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

View File

@ -8,25 +8,27 @@
class GMenu2X;
class Surface;
class IconButton : public Button {
class IconButton : private Button {
public:
IconButton(GMenu2X *gmenu2x, Touchscreen &ts,
const std::string &icon, const std::string &label = "");
virtual ~IconButton() {};
virtual void paint();
virtual bool paintHover();
virtual void setPosition(int x, int y);
void setAction(function_t action);
// Expose some Button functionality:
SDL_Rect getRect() { return Button::getRect(); }
bool handleTS() { return Button::handleTS(); }
private:
void updateSurfaces();
void recalcSize();
GMenu2X *gmenu2x;
std::string icon, label;
void recalcSize();
SDL_Rect iconRect, labelRect;
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);
}
bool Link::paintHover() {
void Link::paintHover() {
if (gmenu2x->useSelectionPng)
gmenu2x->sc["imgs/selection.png"]->blit(gmenu2x->s, rect, Font::HAlignCenter, Font::VAlignMiddle);
else
gmenu2x->s->box(rect.x, rect.y, rect.w, rect.h, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
return true;
}
void Link::updateSurfaces()

View File

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