Added support for texture fragment usage to the ImageButton

pull/2590/head
Andrei Kortunov 5 years ago
parent f335044026
commit bafc0d9e90

@ -14,6 +14,8 @@ namespace Gui
, mMouseFocus(false) , mMouseFocus(false)
, mMousePress(false) , mMousePress(false)
, mKeyFocus(false) , mKeyFocus(false)
, mUseWholeTexture(true)
, mTextureRect(MyGUI::IntCoord(0, 0, 0, 0))
{ {
setNeedKeyFocus(sDefaultNeedKeyFocus); setNeedKeyFocus(sDefaultNeedKeyFocus);
} }
@ -23,6 +25,13 @@ namespace Gui
sDefaultNeedKeyFocus = enabled; 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) void ImageButton::setPropertyOverride(const std::string &_key, const std::string &_value)
{ {
if (_key == "ImageHighlighted") if (_key == "ImageHighlighted")
@ -37,6 +46,11 @@ namespace Gui
} }
mImageNormal = _value; mImageNormal = _value;
} }
else if (_key == "TextureRect")
{
mTextureRect = MyGUI::IntCoord::parse(_value);
mUseWholeTexture = (mTextureRect == MyGUI::IntCoord(0, 0, 0, 0));
}
else else
ImageBox::setPropertyOverride(_key, _value); ImageBox::setPropertyOverride(_key, _value);
} }
@ -66,12 +80,25 @@ namespace Gui
void ImageButton::updateImage() void ImageButton::updateImage()
{ {
std::string textureName = mImageNormal;
if (mMousePress) if (mMousePress)
setImageTexture(mImagePushed); textureName = mImagePushed;
else if (mMouseFocus || mKeyFocus) else if (mMouseFocus || mKeyFocus)
setImageTexture(mImageHighlighted); textureName = mImageHighlighted;
else
setImageTexture(mImageNormal); 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() MyGUI::IntSize ImageButton::getRequestedSize()
@ -82,7 +109,11 @@ namespace Gui
Log(Debug::Error) << "ImageButton: can't find image " << mImageNormal; Log(Debug::Error) << "ImageButton: can't find image " << mImageNormal;
return MyGUI::IntSize(0,0); 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) void ImageButton::setImage(const std::string &image)
@ -96,7 +127,7 @@ namespace Gui
mImageHighlighted = imageNoExt + "_over" + ext; mImageHighlighted = imageNoExt + "_over" + ext;
mImagePushed = imageNoExt + "_pressed" + ext; mImagePushed = imageNoExt + "_pressed" + ext;
setImageTexture(mImageNormal); updateImage();
} }
void ImageButton::onMouseButtonReleased(int _left, int _top, MyGUI::MouseButton _id) void ImageButton::onMouseButtonReleased(int _left, int _top, MyGUI::MouseButton _id)

@ -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) /// 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 setImage(const std::string& image);
void setTextureRect(MyGUI::IntCoord coord);
private: private:
void updateImage(); void updateImage();
@ -44,6 +46,9 @@ namespace Gui
bool mMouseFocus; bool mMouseFocus;
bool mMousePress; bool mMousePress;
bool mKeyFocus; bool mKeyFocus;
bool mUseWholeTexture;
MyGUI::IntCoord mTextureRect;
}; };
} }

Loading…
Cancel
Save