From 6378fcfcd776cc0a10cf1e9130b78410ff1198b8 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Tue, 13 Aug 2013 01:47:02 +0200 Subject: [PATCH] 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. --- src/button.cpp | 31 ++++++++----------------------- src/button.h | 30 ++++++++++++++---------------- src/iconbutton.cpp | 8 -------- src/iconbutton.h | 10 ++++++---- src/link.cpp | 3 +-- src/link.h | 9 +++++++-- 6 files changed, 36 insertions(+), 55 deletions(-) diff --git a/src/button.cpp b/src/button.cpp index a3d41bb..e4eb980 100644 --- a/src/button.cpp +++ b/src/button.cpp @@ -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; -} diff --git a/src/button.h b/src/button.h index 5e95528..f3901b0 100644 --- a/src/button.h +++ b/src/button.h @@ -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 diff --git a/src/iconbutton.cpp b/src/iconbutton.cpp index 2552198..e779966 100644 --- a/src/iconbutton.cpp +++ b/src/iconbutton.cpp @@ -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) { diff --git a/src/iconbutton.h b/src/iconbutton.h index 9d26240..86d1874 100644 --- a/src/iconbutton.h +++ b/src/iconbutton.h @@ -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; diff --git a/src/link.cpp b/src/link.cpp index c35fe70..ebe24ca 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -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() diff --git a/src/link.h b/src/link.h index 4d537d8..6010673 100644 --- a/src/link.h +++ b/src/link.h @@ -35,7 +35,7 @@ Base class that represents a link on screen. @author Massimiliano Torromeo */ -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