diff options
Diffstat (limited to 'src/gui/widgets')
-rw-r--r-- | src/gui/widgets/desktop.h | 8 | ||||
-rw-r--r-- | src/gui/widgets/label.cpp | 69 | ||||
-rw-r--r-- | src/gui/widgets/label.h | 15 |
3 files changed, 81 insertions, 11 deletions
diff --git a/src/gui/widgets/desktop.h b/src/gui/widgets/desktop.h index 0a9759835..2e5eeea24 100644 --- a/src/gui/widgets/desktop.h +++ b/src/gui/widgets/desktop.h @@ -27,11 +27,7 @@ #include <guichan/widgetlistener.hpp> class Image; - -namespace gcn -{ - class Label; -} +class Label; /** * Desktop widget, for drawing a background image and color. @@ -68,7 +64,7 @@ class Desktop final : public Container, private gcn::WidgetListener void setBestFittingWallpaper(); Image *mWallpaper; - gcn::Label *mVersionLabel; + Label *mVersionLabel; gcn::Color mBackgroundColor; gcn::Color mBackgroundGrayColor; }; diff --git a/src/gui/widgets/label.cpp b/src/gui/widgets/label.cpp index e6f603fb2..188da1bf4 100644 --- a/src/gui/widgets/label.cpp +++ b/src/gui/widgets/label.cpp @@ -23,21 +23,80 @@ #include "gui/theme.h" +#include <guichan/font.hpp> + #include "debug.h" +Skin *Label::mSkin = nullptr; +int Label::mInstances = 0; + Label::Label() : - gcn::Label() + gcn::Label(), + mPadding(0) { - mForegroundColor = Theme::getThemeColor(Theme::LABEL); + init(); } Label::Label(const std::string &caption) : - gcn::Label(caption) + gcn::Label(caption), + mPadding(0) +{ + init(); +} + +Label::~Label() +{ + mInstances --; + if (mInstances == 0 && Theme::instance()) + { + Theme::instance()->unload(mSkin); + } +} + +void Label::init() { mForegroundColor = Theme::getThemeColor(Theme::LABEL); + if (mInstances == 0) + { + if (Theme::instance()) + mSkin = Theme::instance()->load("label.xml", ""); + mInstances ++; + } + if (mSkin) + mPadding = mSkin->getPadding(); + else + mPadding = 0; +} + +void Label::draw(gcn::Graphics* graphics) +{ + int textX; + const int textY = getHeight() / 2 - getFont()->getHeight() / 2; + + switch (getAlignment()) + { + case Graphics::LEFT: + default: + textX = mPadding; + break; + case Graphics::CENTER: + textX = getWidth() / 2; + break; + case Graphics::RIGHT: + if (getWidth() > mPadding) + textX = getWidth() - mPadding; + else + textX = 0; + break; + } + + graphics->setFont(getFont()); + graphics->setColor(mForegroundColor); + graphics->drawText(getCaption(), textX, textY, getAlignment()); } -void Label::draw(gcn::Graphics *graphics) +void Label::adjustSize() { - gcn::Label::draw(graphics); + setWidth(getFont()->getWidth(getCaption()) + 2 * mPadding); + setHeight(getFont()->getHeight() + 2 * mPadding); } diff --git a/src/gui/widgets/label.h b/src/gui/widgets/label.h index 046f2ca00..40632109d 100644 --- a/src/gui/widgets/label.h +++ b/src/gui/widgets/label.h @@ -24,6 +24,8 @@ #include <guichan/widgets/label.hpp> +class Skin; + /** * Label widget. Same as the Guichan label but modified to use the palette * system. @@ -46,10 +48,23 @@ class Label final : public gcn::Label A_DELETE_COPY(Label); + ~Label(); + + void init(); + /** * Draws the label. */ void draw(gcn::Graphics *graphics) override; + + void adjustSize(); + + static Skin *mSkin; + + static int mInstances; + + private: + int mPadding; }; #endif |