From 066488a6f4d086b57f4fe32e8799c207552cccb7 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 14 Oct 2012 18:50:27 +0300 Subject: Add theming to labels. New theme file: label.xml Parameters: padding --- src/gui/widgets/desktop.h | 8 ++---- src/gui/widgets/label.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++---- src/gui/widgets/label.h | 15 +++++++++++ 3 files changed, 81 insertions(+), 11 deletions(-) (limited to 'src/gui/widgets') 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 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 + #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 +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 -- cgit v1.2.3-60-g2f50