From b3858a91e0f6b2c4706ea7b4418a26d4ef14baa7 Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Tue, 5 Jul 2011 00:23:22 +0200 Subject: Added textpopup on mouse hovering support to buttons. I added a use of it to the menu buttons. --- src/gui/widgets/button.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++- src/gui/widgets/button.h | 15 ++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) (limited to 'src/gui/widgets') diff --git a/src/gui/widgets/button.cpp b/src/gui/widgets/button.cpp index f072ef61..b2fa9e89 100644 --- a/src/gui/widgets/button.cpp +++ b/src/gui/widgets/button.cpp @@ -25,6 +25,7 @@ #include "graphics.h" #include "gui/palette.h" +#include "gui/textpopup.h" #include "resources/image.h" #include "resources/theme.h" @@ -36,7 +37,8 @@ int Button::mInstances = 0; float Button::mAlpha = 1.0; -ImageRect *Button::mButton; +ImageRect* Button::mButton; +TextPopup* Button::mTextPopup = 0; enum{ BUTTON_STANDARD, // 0 @@ -159,6 +161,10 @@ void Button::init() btn[mode]->decRef(); } updateAlpha(); + + // Load the popup + if (!mTextPopup) + mTextPopup = new TextPopup(); } mInstances++; } @@ -175,6 +181,9 @@ Button::~Button() dtor()); } delete[] mButton; + + // Remove the popup + delete mTextPopup; } removeButtonIcon(); } @@ -301,3 +310,50 @@ void Button::setCaption(const std::string& caption) mCaption = caption; adjustSize(); } + +void Button::logic() +{ + gcn::Button::logic(); + mTextPopup->logic(); +} + +void Button::mouseMoved(gcn::MouseEvent &event) +{ + gcn::Button::mouseMoved(event); + mTextPopup->mouseMoved(event); + + int x = event.getX(); + int y = event.getY(); + + if (event.getSource() == this && !mPopupText.empty()) + { + if (mParent) + { + x += mParent->getX(); + y += mParent->getY(); + } + + mTextPopup->show(x + getX(), y + getY(), mPopupText); + } + else + { + mTextPopup->setVisible(false); + } +} + +void Button::mouseExited(gcn::MouseEvent &event) +{ + gcn::Button::mouseExited(event); + mTextPopup->mouseExited(event); + + mTextPopup->setVisible(false); +} + +void Button::setButtonPopupText(const std::string& text) +{ + mPopupText = text; + if (!mPopupText.empty()) + mTextPopup->show(getX(), getY(), mPopupText); + else + mTextPopup->setVisible(false); +} diff --git a/src/gui/widgets/button.h b/src/gui/widgets/button.h index 6d8f773c..cfc5043e 100644 --- a/src/gui/widgets/button.h +++ b/src/gui/widgets/button.h @@ -26,6 +26,7 @@ class ImageRect; class Image; +class TextPopup; /** * Button widget. Same as the Guichan button but with custom look. @@ -74,6 +75,17 @@ class Button : public gcn::Button void setButtonIcon(const std::string& iconFile = std::string(), int frameHeight = 0, int frameWidth = 0); + /** + * Set the button popup text when hovering it for a few seconds. + * + * @note: An empty text will disable the popup. + */ + void setButtonPopupText(const std::string& text = ""); + + void logic(); + void mouseMoved(gcn::MouseEvent &event); + void mouseExited(gcn::MouseEvent &event); + private: void init(); @@ -84,6 +96,9 @@ class Button : public gcn::Button static float mAlpha; Image** mButtonIcon; /**< Button Icons graphics */ + + static TextPopup* mTextPopup; /**< The buttons popup */ + std::string mPopupText; /**< the current button text */ }; #endif -- cgit v1.2.3-70-g09d2