From bafc0d9e90cd94c4b02588b29bf919f38af8bd16 Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Sat, 9 Nov 2019 12:51:12 +0400 Subject: [PATCH] Added support for texture fragment usage to the ImageButton --- components/widgets/imagebutton.cpp | 43 +++++++++++++++++++++++++----- components/widgets/imagebutton.hpp | 5 ++++ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/components/widgets/imagebutton.cpp b/components/widgets/imagebutton.cpp index 2ea494ebd0..0d1f798da5 100644 --- a/components/widgets/imagebutton.cpp +++ b/components/widgets/imagebutton.cpp @@ -14,6 +14,8 @@ namespace Gui , mMouseFocus(false) , mMousePress(false) , mKeyFocus(false) + , mUseWholeTexture(true) + , mTextureRect(MyGUI::IntCoord(0, 0, 0, 0)) { setNeedKeyFocus(sDefaultNeedKeyFocus); } @@ -23,6 +25,13 @@ namespace Gui sDefaultNeedKeyFocus = enabled; } + void ImageButton::setTextureRect(MyGUI::IntCoord coord) + { + mTextureRect = coord; + mUseWholeTexture = (coord == MyGUI::IntCoord(0, 0, 0, 0)); + updateImage(); + } + void ImageButton::setPropertyOverride(const std::string &_key, const std::string &_value) { if (_key == "ImageHighlighted") @@ -37,6 +46,11 @@ namespace Gui } mImageNormal = _value; } + else if (_key == "TextureRect") + { + mTextureRect = MyGUI::IntCoord::parse(_value); + mUseWholeTexture = (mTextureRect == MyGUI::IntCoord(0, 0, 0, 0)); + } else ImageBox::setPropertyOverride(_key, _value); } @@ -66,12 +80,25 @@ namespace Gui void ImageButton::updateImage() { + std::string textureName = mImageNormal; if (mMousePress) - setImageTexture(mImagePushed); + textureName = mImagePushed; else if (mMouseFocus || mKeyFocus) - setImageTexture(mImageHighlighted); - else - setImageTexture(mImageNormal); + textureName = mImageHighlighted; + + if (!mUseWholeTexture) + { + int scale = 1.f; + MyGUI::ITexture* texture = MyGUI::RenderManager::getInstance().getTexture(textureName); + if (texture && getHeight() != 0) + scale = texture->getHeight() / getHeight(); + + setImageTile(MyGUI::IntSize(mTextureRect.width * scale, mTextureRect.height * scale)); + MyGUI::IntCoord scaledSize(mTextureRect.left * scale, mTextureRect.top * scale, mTextureRect.width * scale, mTextureRect.height * scale); + setImageCoord(scaledSize); + } + + setImageTexture(textureName); } MyGUI::IntSize ImageButton::getRequestedSize() @@ -82,7 +109,11 @@ namespace Gui Log(Debug::Error) << "ImageButton: can't find image " << mImageNormal; return MyGUI::IntSize(0,0); } - return MyGUI::IntSize (texture->getWidth(), texture->getHeight()); + + if (mUseWholeTexture) + return MyGUI::IntSize(texture->getWidth(), texture->getHeight()); + + return MyGUI::IntSize(mTextureRect.width, mTextureRect.height); } void ImageButton::setImage(const std::string &image) @@ -96,7 +127,7 @@ namespace Gui mImageHighlighted = imageNoExt + "_over" + ext; mImagePushed = imageNoExt + "_pressed" + ext; - setImageTexture(mImageNormal); + updateImage(); } void ImageButton::onMouseButtonReleased(int _left, int _top, MyGUI::MouseButton _id) diff --git a/components/widgets/imagebutton.hpp b/components/widgets/imagebutton.hpp index bfcff79979..bb2baa91ff 100644 --- a/components/widgets/imagebutton.hpp +++ b/components/widgets/imagebutton.hpp @@ -23,6 +23,8 @@ namespace Gui /// Set mImageNormal, mImageHighlighted and mImagePushed based on file convention (image_idle.ext, image_over.ext and image_pressed.ext) void setImage(const std::string& image); + void setTextureRect(MyGUI::IntCoord coord); + private: void updateImage(); @@ -44,6 +46,9 @@ namespace Gui bool mMouseFocus; bool mMousePress; bool mKeyFocus; + bool mUseWholeTexture; + + MyGUI::IntCoord mTextureRect; }; }