From 42a095de15649f0f00ef6c681268d6623205900c Mon Sep 17 00:00:00 2001 From: Jared Adams Date: Sat, 28 Mar 2009 10:35:49 -0600 Subject: Add a sticky button to the Window class The Minimap window uses this so you can froce it to always be open. The Minimap toggle button can be used to show or hide it temporarily, as warping will reset it's visibility based on the sticky state and weather the 'new' map has a minimap. --- src/gui/minimap.cpp | 6 ++++-- src/gui/skin.cpp | 15 +++++++++++--- src/gui/skin.h | 10 ++++++++- src/gui/window.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++----- src/gui/window.h | 18 ++++++++++++++--- 5 files changed, 93 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/gui/minimap.cpp b/src/gui/minimap.cpp index 75ee7e3d..bfd34390 100644 --- a/src/gui/minimap.cpp +++ b/src/gui/minimap.cpp @@ -45,6 +45,9 @@ Minimap::Minimap(): setDefaultSize(5, 25, 100, 100); setResizable(true); + setStickyButton(true); + setSticky(false); + loadWindowState(); } @@ -92,8 +95,7 @@ void Minimap::setMapImage(Image *img) void Minimap::toggle() { - mShow = ! isVisible(); - setVisible(mShow); + setVisible(!isVisible(), true); } void Minimap::draw(gcn::Graphics *graphics) diff --git a/src/gui/skin.cpp b/src/gui/skin.cpp index b16bdfe6..0130ad71 100644 --- a/src/gui/skin.cpp +++ b/src/gui/skin.cpp @@ -47,14 +47,16 @@ class SkinConfigListener : public ConfigListener } }; -Skin::Skin(ImageRect skin, Image *close, +Skin::Skin(ImageRect skin, Image *close, Image *stickyUp, Image *stickyDown, const std::string &filePath, const std::string &name): instances(0), mFilePath(filePath), mName(name), border(skin), - closeImage(close) + closeImage(close), + stickyImageUp(stickyUp), + stickyImageDown(stickyDown) { } @@ -219,7 +221,14 @@ Skin *SkinLoader::load(const std::string &filename, // Hard-coded for now until we update the above code to look for window buttons. Image *closeImage = resman->getImage("graphics/gui/close_button.png"); - Skin *skin = new Skin(border, closeImage, filename); + Image *sticky = resman->getImage("graphics/gui/sticky_button.png"); + Image *stickyImageUp = sticky->getSubImage(0, 0, 15, 15); + Image *stickyImageDown = sticky->getSubImage(15, 0, 15, 15); + + sticky->decRef(); + + Skin *skin = new Skin(border, closeImage, stickyImageUp, stickyImageDown, + filename); mSkins[filename] = skin; diff --git a/src/gui/skin.h b/src/gui/skin.h index df905b05..30f7f360 100644 --- a/src/gui/skin.h +++ b/src/gui/skin.h @@ -34,7 +34,7 @@ class Image; class Skin { public: - Skin(ImageRect skin, Image *close, + Skin(ImageRect skin, Image *close, Image *stickyUp, Image *stickyDown, const std::string &filePath, const std::string &name = ""); @@ -62,6 +62,12 @@ class Skin */ Image *getCloseImage() const { return closeImage; } + /** + * Returns the image used by a sticky button for this skin. + */ + Image *getStickyImage(bool state) const + { return state ? stickyImageDown : stickyImageUp; } + /** * Returns the number of instances which use this skin. */ @@ -89,6 +95,8 @@ class Skin std::string mName; /**< Name of the skin to use */ ImageRect border; /**< The window border and background */ Image *closeImage; /**< Close Button Image */ + Image *stickyImageUp; /**< Sticky Button Image */ + Image *stickyImageDown; /**< Sticky Button Image */ }; // Map containing all window skins diff --git a/src/gui/window.cpp b/src/gui/window.cpp index a916ba38..1f9dab5b 100644 --- a/src/gui/window.cpp +++ b/src/gui/window.cpp @@ -48,6 +48,7 @@ Window::Window(const std::string &caption, bool modal, Window *parent, const std mShowTitle(true), mModal(modal), mCloseButton(false), + mStickyButton(false), mSticky(false), mMinWinWidth(100), mMinWinHeight(40), @@ -135,8 +136,18 @@ void Window::draw(gcn::Graphics *graphics) { g->drawImage(mSkin->getCloseImage(), getWidth() - mSkin->getCloseImage()->getWidth() - getPadding(), - getPadding() - ); + getPadding()); + } + + // Draw Sticky Button + if (mStickyButton) + { + Image *button = mSkin->getStickyImage(mSticky); + int x = getWidth() - button->getWidth() - getPadding(); + if (mCloseButton) + x -= mSkin->getCloseImage()->getWidth(); + + g->drawImage(button, x, getPadding()); } drawChildren(graphics); @@ -283,6 +294,11 @@ bool Window::isResizable() const return mGrip; } +void Window::setStickyButton(bool flag) +{ + mStickyButton = flag; +} + void Window::setSticky(bool sticky) { mSticky = sticky; @@ -290,7 +306,12 @@ void Window::setSticky(bool sticky) void Window::setVisible(bool visible) { - gcn::Window::setVisible(isSticky() || visible); + setVisible(visible, false); +} + +void Window::setVisible(bool visible, bool forceSticky) +{ + gcn::Window::setVisible((!forceSticky && isSticky()) || visible); } void Window::scheduleDelete() @@ -323,6 +344,22 @@ void Window::mousePressed(gcn::MouseEvent &event) } } + // Handle sticky button + if (mStickyButton) + { + Image *button = mSkin->getStickyImage(mSticky); + int rx = getWidth() - button->getWidth() - getPadding(); + if (mCloseButton) + rx -= mSkin->getCloseImage()->getWidth(); + gcn::Rectangle stickyButtonRect(rx, getPadding(), + button->getWidth(), button->getHeight()); + + if (stickyButtonRect.isPointInRect(x, y)) + { + setSticky(!isSticky()); + } + } + // Handle window resizing mouseResize = getResizeHandles(event); } @@ -467,7 +504,12 @@ void Window::loadWindowState() setPosition((int) config.getValue(name + "WinX", mDefaultX), (int) config.getValue(name + "WinY", mDefaultY)); - setVisible((bool) config.getValue(name + "Visible", false)); + + if (mCloseButton) + setVisible((bool) config.getValue(name + "Visible", false)); + + if (mStickyButton) + setSticky((bool) config.getValue(name + "Sticky", isSticky())); if (skinName.compare(mSkin->getFilePath()) != 0) { @@ -504,7 +546,13 @@ void Window::saveWindowState() { config.setValue(mWindowName + "WinX", getX()); config.setValue(mWindowName + "WinY", getY()); - config.setValue(mWindowName + "Visible", isVisible()); + + if (mCloseButton) + config.setValue(mWindowName + "Visible", isVisible()); + + if (mStickyButton) + config.setValue(mWindowName + "Sticky", isSticky()); + config.setValue(mWindowName + "Skin", mSkin->getFilePath()); if (mGrip) diff --git a/src/gui/window.h b/src/gui/window.h index c41a4221..18a64532 100644 --- a/src/gui/window.h +++ b/src/gui/window.h @@ -144,17 +144,22 @@ class Window : public gcn::Window, gcn::WidgetListener */ void setShowTitle(bool flag) { mShowTitle = flag; } + /** + * Sets whether or not the window has a sticky button. + */ + void setStickyButton(bool flag); + /** * Sets whether the window is sticky. A sticky window will not have * its visibility set to false on a general setVisible(false) call. + * Use this to set the default before you call loadWindowState(). */ void setSticky(bool sticky); /** * Returns whether the window is sticky. */ - bool isSticky() const - { return mSticky; } + bool isSticky() const { return mSticky; } /** * Overloads window setVisible by Guichan to allow sticky window @@ -162,6 +167,12 @@ class Window : public gcn::Window, gcn::WidgetListener */ void setVisible(bool visible); + /** + * Overloads window setVisible by Guichan to allow sticky window + * handling, or not, if you force the sticky state. + */ + void setVisible(bool visible, bool forceSticky); + /** * Returns the parent window. * @@ -325,7 +336,8 @@ class Window : public gcn::Window, gcn::WidgetListener bool mShowTitle; /**< Window has a title bar */ bool mModal; /**< Window is modal */ bool mCloseButton; /**< Window has a close button */ - bool mSticky; /**< Window resists minimization */ + bool mStickyButton; /**< Window has a sticky button */ + bool mSticky; /**< Window resists hiding*/ int mMinWinWidth; /**< Minimum window width */ int mMinWinHeight; /**< Minimum window height */ int mMaxWinWidth; /**< Maximum window width */ -- cgit v1.2.3-70-g09d2