Commit d6fe1e60 authored by bitplane's avatar bitplane

Fixed button state sprite animations for pressed, focused and hovered.



git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@4039 dfc29bdd-3216-0410-991c-e03cc46cb475
parent ade2a393
...@@ -21,8 +21,9 @@ CGUIButton::CGUIButton(IGUIEnvironment* environment, IGUIElement* parent, ...@@ -21,8 +21,9 @@ CGUIButton::CGUIButton(IGUIEnvironment* environment, IGUIElement* parent,
s32 id, core::rect<s32> rectangle, bool noclip) s32 id, core::rect<s32> rectangle, bool noclip)
: IGUIButton(environment, parent, id, rectangle), : IGUIButton(environment, parent, id, rectangle),
SpriteBank(0), OverrideFont(0), Image(0), PressedImage(0), SpriteBank(0), OverrideFont(0), Image(0), PressedImage(0),
ClickTime(0), IsPushButton(false), Pressed(false), ClickTime(0), HoverTime(0), FocusTime(0),
UseAlphaChannel(false), DrawBorder(true), ScaleImage(false), ButtonState(EGBS_BUTTON_UP) IsPushButton(false), Pressed(false),
UseAlphaChannel(false), DrawBorder(true), ScaleImage(false)
{ {
#ifdef _DEBUG #ifdef _DEBUG
setDebugName("CGUIButton"); setDebugName("CGUIButton");
...@@ -133,7 +134,6 @@ bool CGUIButton::OnEvent(const SEvent& event) ...@@ -133,7 +134,6 @@ bool CGUIButton::OnEvent(const SEvent& event)
if (!event.KeyInput.PressedDown && Pressed && if (!event.KeyInput.PressedDown && Pressed &&
(event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE)) (event.KeyInput.Key == KEY_RETURN || event.KeyInput.Key == KEY_SPACE))
{ {
//Environment->removeFocus(this);
if (!IsPushButton) if (!IsPushButton)
setPressed(false); setPressed(false);
...@@ -151,21 +151,22 @@ bool CGUIButton::OnEvent(const SEvent& event) ...@@ -151,21 +151,22 @@ bool CGUIButton::OnEvent(const SEvent& event)
} }
break; break;
case EET_GUI_EVENT: case EET_GUI_EVENT:
if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST) if (event.GUIEvent.Caller == this)
{ {
if (event.GUIEvent.Caller == this && !IsPushButton) if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
setPressed(false); {
ButtonState = EGBS_BUTTON_NOT_FOCUSED; if (!IsPushButton)
} setPressed(false);
else if (event.GUIEvent.EventType == EGET_ELEMENT_HOVERED) FocusTime = os::Timer::getTime();
{ }
if (event.GUIEvent.Caller == this) else if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUSED)
ButtonState = EGBS_BUTTON_MOUSE_OVER; {
} FocusTime = os::Timer::getTime();
else if (event.GUIEvent.EventType == EGET_ELEMENT_LEFT) }
{ else if (event.GUIEvent.EventType == EGET_ELEMENT_HOVERED || event.GUIEvent.EventType == EGET_ELEMENT_LEFT)
if (event.GUIEvent.Caller == this) {
ButtonState = EGBS_BUTTON_UP; HoverTime = os::Timer::getTime();
}
} }
break; break;
case EET_MOUSE_INPUT_EVENT: case EET_MOUSE_INPUT_EVENT:
...@@ -188,7 +189,6 @@ bool CGUIButton::OnEvent(const SEvent& event) ...@@ -188,7 +189,6 @@ bool CGUIButton::OnEvent(const SEvent& event)
if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
{ {
bool wasPressed = Pressed; bool wasPressed = Pressed;
//Environment->removeFocus(this);
if ( !AbsoluteClippingRect.isPointInside( core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y ) ) ) if ( !AbsoluteClippingRect.isPointInside( core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y ) ) )
{ {
...@@ -236,7 +236,6 @@ void CGUIButton::draw() ...@@ -236,7 +236,6 @@ void CGUIButton::draw()
video::IVideoDriver* driver = Environment->getVideoDriver(); video::IVideoDriver* driver = Environment->getVideoDriver();
// todo: move sprite up and text down if the pressed state has a sprite // todo: move sprite up and text down if the pressed state has a sprite
// draw sprites for focused and mouse-over
const core::position2di spritePos = AbsoluteRect.getCenter(); const core::position2di spritePos = AbsoluteRect.getCenter();
if (!Pressed) if (!Pressed)
...@@ -256,12 +255,34 @@ void CGUIButton::draw() ...@@ -256,12 +255,34 @@ void CGUIButton::draw()
ImageRect, &AbsoluteClippingRect, ImageRect, &AbsoluteClippingRect,
0, UseAlphaChannel); 0, UseAlphaChannel);
} }
if (SpriteBank && ButtonSprites[ButtonState].Index != -1) if (SpriteBank)
{ {
// draw pressed sprite // pressed / unpressed animation
SpriteBank->draw2DSprite(ButtonSprites[ButtonState].Index, spritePos, u32 state = Pressed ? (u32)EGBS_BUTTON_DOWN : (u32)EGBS_BUTTON_UP;
&AbsoluteClippingRect, ButtonSprites[ButtonState].Color, ClickTime, os::Timer::getTime(), if (ButtonSprites[state].Index != -1)
ButtonSprites[ButtonState].Loop, true); {
SpriteBank->draw2DSprite(ButtonSprites[state].Index, spritePos,
&AbsoluteClippingRect, ButtonSprites[state].Color, ClickTime, os::Timer::getTime(),
ButtonSprites[state].Loop, true);
}
// focused / unfocused animation
state = Environment->hasFocus(this) ? (u32)EGBS_BUTTON_FOCUSED : (u32)EGBS_BUTTON_NOT_FOCUSED;
if (ButtonSprites[state].Index != -1)
{
SpriteBank->draw2DSprite(ButtonSprites[state].Index, spritePos,
&AbsoluteClippingRect, ButtonSprites[state].Color, FocusTime, os::Timer::getTime(),
ButtonSprites[state].Loop, true);
}
// mouse over / off animation
state = Environment->getHovered() == this ? (u32)EGBS_BUTTON_MOUSE_OVER : (u32)EGBS_BUTTON_MOUSE_OFF;
if (ButtonSprites[state].Index != -1)
{
SpriteBank->draw2DSprite(ButtonSprites[state].Index, spritePos,
&AbsoluteClippingRect, ButtonSprites[state].Color, HoverTime, os::Timer::getTime(),
ButtonSprites[state].Loop, true);
}
} }
} }
else else
...@@ -418,8 +439,6 @@ void CGUIButton::setPressed(bool pressed) ...@@ -418,8 +439,6 @@ void CGUIButton::setPressed(bool pressed)
{ {
ClickTime = os::Timer::getTime(); ClickTime = os::Timer::getTime();
Pressed = pressed; Pressed = pressed;
if (Pressed)
ButtonState = EGBS_BUTTON_DOWN;
} }
} }
......
...@@ -122,12 +122,10 @@ namespace gui ...@@ -122,12 +122,10 @@ namespace gui
video::ITexture* Image; video::ITexture* Image;
video::ITexture* PressedImage; video::ITexture* PressedImage;
EGUI_BUTTON_STATE ButtonState;
core::rect<s32> ImageRect; core::rect<s32> ImageRect;
core::rect<s32> PressedImageRect; core::rect<s32> PressedImageRect;
u32 ClickTime; u32 ClickTime, HoverTime, FocusTime;
bool IsPushButton; bool IsPushButton;
bool Pressed; bool Pressed;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment