From 5a9a9d113f88ab4e3f9e570397a744760d4520c1 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 17 Oct 2012 20:39:51 +0300 Subject: Extend radio button theming. New options: padding - padding from all sides except left imagePadding - padding from left before image imageSize - image size for computation width spacing - space between image and text width = imagePadding + imageSize + spacing + text width + padding height = padding + text height + padding --- src/gui/widgets/checkbox.cpp | 2 +- src/gui/widgets/radiobutton.cpp | 137 ++++++++++++++++++++++------------------ src/gui/widgets/radiobutton.h | 16 +++-- 3 files changed, 85 insertions(+), 70 deletions(-) (limited to 'src/gui/widgets') diff --git a/src/gui/widgets/checkbox.cpp b/src/gui/widgets/checkbox.cpp index 8349247a4..f3e5993b0 100644 --- a/src/gui/widgets/checkbox.cpp +++ b/src/gui/widgets/checkbox.cpp @@ -128,7 +128,7 @@ void CheckBox::drawBox(gcn::Graphics *const graphics) if (!mSkin) return; - ImageRect &rect = mSkin->getBorder(); + const ImageRect &rect = mSkin->getBorder(); int index = 0; if (mEnabled && isVisible()) diff --git a/src/gui/widgets/radiobutton.cpp b/src/gui/widgets/radiobutton.cpp index a88a89201..ebe179b53 100644 --- a/src/gui/widgets/radiobutton.cpp +++ b/src/gui/widgets/radiobutton.cpp @@ -32,51 +32,44 @@ #include "resources/image.h" +#include + #include "debug.h" int RadioButton::instances = 0; +Skin *RadioButton::mSkin = nullptr; float RadioButton::mAlpha = 1.0; -Image *RadioButton::radioNormal; -Image *RadioButton::radioChecked; -Image *RadioButton::radioDisabled; -Image *RadioButton::radioDisabledChecked; -Image *RadioButton::radioNormalHi; -Image *RadioButton::radioCheckedHi; RadioButton::RadioButton(const std::string &caption, const std::string &group, const bool marked): gcn::RadioButton(caption, group, marked), - mHasMouse(false) + mHasMouse(false), + mPadding(0), + mImagePadding(0), + mImageSize(9), + mSpacing(2) { mForegroundColor = Theme::getThemeColor(Theme::RADIOBUTTON); if (instances == 0) { if (Theme::instance()) { - ImageRect rect; - Theme::instance()->loadRect(rect, "radio.xml", "", 0, 3); - radioChecked = rect.grid[0]; - radioDisabledChecked = rect.grid[0]; - radioCheckedHi = rect.grid[1]; - radioNormal = rect.grid[2]; - radioDisabled = rect.grid[2]; - radioNormalHi = rect.grid[3]; + mSkin = Theme::instance()->load("radio.xml", ""); + updateAlpha(); } - if (radioNormal) - radioNormal->setAlpha(mAlpha); - if (radioChecked) - radioChecked->setAlpha(mAlpha); -// if (radioDisabled) -// radioDisabled->setAlpha(mAlpha); -// if (radioDisabledChecked) -// radioDisabledChecked->setAlpha(mAlpha); - if (radioNormalHi) - radioNormalHi->setAlpha(mAlpha); - if (radioCheckedHi) - radioCheckedHi->setAlpha(mAlpha); } instances++; + + if (mSkin) + { + mPadding = mSkin->getPadding(); + mImagePadding = mSkin->getOption("imagePadding"); + mImageSize = mSkin->getOption("imageSize"); + mSpacing = mSkin->getOption("spacing"); + } + + adjustSize(); } RadioButton::~RadioButton() @@ -85,65 +78,74 @@ RadioButton::~RadioButton() if (instances == 0) { - if (radioNormal) - radioNormal->decRef(); - if (radioChecked) - radioChecked->decRef(); -// if (radioDisabled) -// radioDisabled->decRef(); -// if (radioDisabledChecked) -// radioDisabledChecked->decRef(); - if (radioNormalHi) - radioNormalHi->decRef(); - if (radioCheckedHi) - radioCheckedHi->decRef(); + if (Theme::instance()) + Theme::instance()->unload(mSkin); } } -void RadioButton::drawBox(gcn::Graphics* graphics) +void RadioButton::updateAlpha() { - if (Client::getGuiAlpha() != mAlpha) + const float alpha = std::max(Client::getGuiAlpha(), + Theme::instance()->getMinimumOpacity()); + + if (mAlpha != alpha) { - mAlpha = Client::getGuiAlpha(); - if (radioNormal) - radioNormal->setAlpha(mAlpha); - if (radioChecked) - radioChecked->setAlpha(mAlpha); -// if (radioDisabled) -// radioDisabled->setAlpha(mAlpha); -// if (radioDisabledChecked) -// radioDisabledChecked->setAlpha(mAlpha); - if (radioNormalHi) - radioNormalHi->setAlpha(mAlpha); - if (radioCheckedHi) - radioCheckedHi->setAlpha(mAlpha); + mAlpha = alpha; + if (mSkin) + { + ImageRect &rect = mSkin->getBorder(); + for (int a = 0; a < 4; a ++) + { + Image *const image = rect.grid[a]; + if (image) + image->setAlpha(mAlpha); + } + } } +} + +void RadioButton::drawBox(gcn::Graphics* graphics) +{ + if (!mSkin) + return; - const Image *box = nullptr; + const ImageRect &rect = mSkin->getBorder(); + int index = 0; if (mEnabled && isVisible()) { if (mSelected) + { if (mHasMouse) - box = radioCheckedHi; + index = 1; else - box = radioChecked; + index = 0; + } else + { if (mHasMouse) - box = radioNormalHi; + index = 3; else - box = radioNormal; + index = 2; + } } else { if (mSelected) - box = radioDisabledChecked; + index = 0; else - box = radioDisabled; + index = 2; } + const Image *const box = rect.grid[index]; + + updateAlpha(); + if (box) - static_cast(graphics)->drawImage(box, 3, 3); + { + static_cast(graphics)->drawImage( + box, mImagePadding, (getHeight() - mImageSize) / 2); + } } void RadioButton::draw(gcn::Graphics* graphics) @@ -152,7 +154,9 @@ void RadioButton::draw(gcn::Graphics* graphics) graphics->setFont(getFont()); graphics->setColor(mForegroundColor); - graphics->drawText(getCaption(), 16, 0); + + graphics->drawText(getCaption(), mPadding + mImageSize + mSpacing, + mPadding); } void RadioButton::mouseEntered(gcn::MouseEvent& event A_UNUSED) @@ -176,3 +180,10 @@ void RadioButton::keyPressed(gcn::KeyEvent& keyEvent) keyEvent.consume(); } } + +void RadioButton::adjustSize() +{ + setHeight(getFont()->getHeight() + 2 * mPadding); + setWidth(mImagePadding + mImageSize + mSpacing + + getFont()->getWidth(mCaption) + mPadding); +} diff --git a/src/gui/widgets/radiobutton.h b/src/gui/widgets/radiobutton.h index 7728d094e..ecf6862ed 100644 --- a/src/gui/widgets/radiobutton.h +++ b/src/gui/widgets/radiobutton.h @@ -28,6 +28,7 @@ #include "localconsts.h" class Image; +class Skin; /** * Guichan based RadioButton with custom look @@ -71,16 +72,19 @@ class RadioButton final : public gcn::RadioButton void keyPressed(gcn::KeyEvent& keyEvent) override; + void updateAlpha(); + + void adjustSize(); + private: static int instances; + static Skin *mSkin; static float mAlpha; bool mHasMouse; - static Image *radioNormal; - static Image *radioChecked; - static Image *radioDisabled; - static Image *radioDisabledChecked; - static Image *radioNormalHi; - static Image *radioCheckedHi; + int mPadding; + int mImagePadding; + int mImageSize; + int mSpacing; }; #endif // RADIOBUTTON_H -- cgit v1.2.3-70-g09d2