Adjust ImageButton to accept key focus and use the 'highlighted' texture

pull/303/head
scrawl 7 years ago
parent c11fe6788f
commit 1ad14b232f

@ -5,6 +5,15 @@
namespace Gui
{
ImageButton::ImageButton()
: Base()
, mMouseFocus(false)
, mMousePress(false)
, mKeyFocus(false)
{
setNeedKeyFocus(true);
}
void ImageButton::setPropertyOverride(const std::string &_key, const std::string &_value)
{
if (_key == "ImageHighlighted")
@ -24,22 +33,36 @@ namespace Gui
}
void ImageButton::onMouseSetFocus(Widget* _old)
{
setImageTexture(mImageHighlighted);
ImageBox::onMouseSetFocus(_old);
mMouseFocus = true;
updateImage();
Base::onMouseSetFocus(_old);
}
void ImageButton::onMouseLostFocus(Widget* _new)
{
setImageTexture(mImageNormal);
ImageBox::onMouseLostFocus(_new);
mMouseFocus = false;
updateImage();
Base::onMouseLostFocus(_new);
}
void ImageButton::onMouseButtonPressed(int _left, int _top, MyGUI::MouseButton _id)
{
if (_id == MyGUI::MouseButton::Left)
setImageTexture(mImagePushed);
{
mMousePress = true;
updateImage();
}
Base::onMouseButtonPressed(_left, _top, _id);
}
ImageBox::onMouseButtonPressed(_left, _top, _id);
void ImageButton::updateImage()
{
if (mMousePress)
setImageTexture(mImagePushed);
else if (mMouseFocus || mKeyFocus)
setImageTexture(mImageHighlighted);
else
setImageTexture(mImageNormal);
}
MyGUI::IntSize ImageButton::getRequestedSize()
@ -70,8 +93,23 @@ namespace Gui
void ImageButton::onMouseButtonReleased(int _left, int _top, MyGUI::MouseButton _id)
{
if (_id == MyGUI::MouseButton::Left)
setImageTexture(mImageHighlighted);
{
mMousePress = false;
updateImage();
}
Base::onMouseButtonReleased(_left, _top, _id);
}
ImageBox::onMouseButtonReleased(_left, _top, _id);
void ImageButton::onKeySetFocus(MyGUI::Widget *_old)
{
mKeyFocus = true;
updateImage();
}
void ImageButton::onKeyLostFocus(MyGUI::Widget *_new)
{
mKeyFocus = false;
updateImage();
}
}

@ -16,19 +16,30 @@ namespace Gui
public:
MyGUI::IntSize getRequestedSize();
ImageButton();
/// 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);
private:
void updateImage();
protected:
virtual void setPropertyOverride(const std::string& _key, const std::string& _value);
virtual void onMouseLostFocus(MyGUI::Widget* _new);
virtual void onMouseSetFocus(MyGUI::Widget* _old);
virtual void onMouseButtonPressed(int _left, int _top, MyGUI::MouseButton _id);
virtual void onMouseButtonReleased(int _left, int _top, MyGUI::MouseButton _id);
virtual void onKeySetFocus(MyGUI::Widget* _old);
virtual void onKeyLostFocus(MyGUI::Widget* _new);
std::string mImageHighlighted;
std::string mImageNormal;
std::string mImagePushed;
bool mMouseFocus;
bool mMousePress;
bool mKeyFocus;
};
}

Loading…
Cancel
Save