Update Button & Sprite

This commit is contained in:
Nomango 2020-02-11 22:56:12 +08:00
parent 27fa580cc1
commit d2cd4f8cba
4 changed files with 68 additions and 45 deletions

View File

@ -55,11 +55,6 @@ void Button::SetPressedCallback(const Callback& func)
pressed_callback_ = func;
}
void Button::SetReleasedCallback(const Callback& func)
{
released_callback_ = func;
}
void Button::SetMouseOverCallback(const Callback& func)
{
mouse_over_callback_ = func;
@ -87,12 +82,7 @@ void Button::SetStatus(Status status)
{
Window::GetInstance().SetCursor(CursorType::Hand);
if (old_status == Status::Pressed)
{
if (released_callback_)
released_callback_(this);
}
else
if (old_status != Status::Pressed)
{
if (mouse_over_callback_)
mouse_over_callback_(this);
@ -141,16 +131,21 @@ void Button::UpdateStatus(Event* evt)
}
}
void Button::BindActor(Actor* actor)
{
actor->SetResponsible(true);
EventListener::Callback handler = Closure(this, &Button::UpdateStatus);
actor->AddListener<MouseHoverEvent>(handler);
actor->AddListener<MouseOutEvent>(handler);
actor->AddListener<MouseDownEvent>(handler);
actor->AddListener<MouseUpEvent>(handler);
actor->AddListener<MouseClickEvent>(handler);
}
SpriteButton::SpriteButton()
{
SetResponsible(true);
EventListener::Callback handler = Closure(this, &SpriteButton::UpdateStatus);
AddListener<MouseHoverEvent>(handler);
AddListener<MouseOutEvent>(handler);
AddListener<MouseDownEvent>(handler);
AddListener<MouseUpEvent>(handler);
AddListener<MouseClickEvent>(handler);
BindActor(this);
}
SpriteButtonPtr SpriteButton::Create(Callback const& click)
@ -179,14 +174,7 @@ SpriteButtonPtr SpriteButton::Create(Callback const& click, Callback const& pres
TextButton::TextButton()
{
SetResponsible(true);
EventListener::Callback handler = Closure(this, &TextButton::UpdateStatus);
AddListener<MouseHoverEvent>(handler);
AddListener<MouseOutEvent>(handler);
AddListener<MouseDownEvent>(handler);
AddListener<MouseUpEvent>(handler);
AddListener<MouseClickEvent>(handler);
BindActor(this);
}
TextButtonPtr TextButton::Create(Callback const& click)

View File

@ -59,10 +59,6 @@ public:
/// @brief 设置按钮被按下时的回调函数
void SetPressedCallback(const Callback& func);
/// \~chinese
/// @brief 设置按钮被抬起时的回调函数
void SetReleasedCallback(const Callback& func);
/// \~chinese
/// @brief 设置鼠标移入按钮时的回调函数
void SetMouseOverCallback(const Callback& func);
@ -93,12 +89,15 @@ protected:
/// @brief 更新按钮状态
void UpdateStatus(Event* evt);
/// \~chinese
/// @brief 绑定到角色
void BindActor(Actor* actor);
private:
bool enabled_;
Status status_;
Callback click_callback_;
Callback pressed_callback_;
Callback released_callback_;
Callback mouse_over_callback_;
Callback mouse_out_callback_;
};
@ -149,4 +148,5 @@ public:
/// @param mouse_out 按钮移出回调函数
static TextButtonPtr Create(Callback const& click, Callback const& pressed, Callback const& mouse_over, Callback const& mouse_out);
};
} // namespace kiwano

View File

@ -56,27 +56,47 @@ SpritePtr Sprite::Create(FramePtr frame)
return ptr;
}
SpritePtr Sprite::Create(String const& file_path, const Rect& crop_rect)
{
SpritePtr ptr = Sprite::Create(file_path);
if (ptr)
{
ptr->SetCropRect(crop_rect);
}
return ptr;
}
SpritePtr Sprite::Create(Resource const& res, const Rect& crop_rect)
{
SpritePtr ptr = Sprite::Create(res);
if (ptr)
{
ptr->SetCropRect(crop_rect);
}
return ptr;
}
Sprite::Sprite() {}
Sprite::~Sprite() {}
bool Sprite::Load(String const& file_path)
bool Sprite::Load(String const& file_path, bool autoresize)
{
FramePtr frame = new (std::nothrow) Frame;
if (frame->Load(file_path))
FramePtr frame = Frame::Create(file_path);
if (frame)
{
SetFrame(frame);
SetFrame(frame, autoresize);
return true;
}
return false;
}
bool Sprite::Load(Resource const& res)
bool Sprite::Load(Resource const& res, bool autoresize)
{
FramePtr frame = new (std::nothrow) Frame;
if (frame->Load(res))
FramePtr frame = Frame::Create(res);
if (frame)
{
SetFrame(frame);
SetFrame(frame, autoresize);
return true;
}
return false;
@ -91,12 +111,12 @@ void Sprite::SetCropRect(const Rect& crop_rect)
}
}
void Sprite::SetFrame(FramePtr frame)
void Sprite::SetFrame(FramePtr frame, bool autoresize)
{
if (frame_ != frame)
{
frame_ = frame;
if (frame_)
if (frame_ && autoresize)
{
SetSize(Size{ frame_->GetWidth(), frame_->GetHeight() });
}

View File

@ -53,6 +53,18 @@ public:
/// @param frame 图像帧
static SpritePtr Create(FramePtr frame);
/// \~chinese
/// @brief 创建精灵
/// @param file_path 本地图片路径
/// @param crop_rect 裁剪矩形
static SpritePtr Create(String const& file_path, const Rect& crop_rect);
/// \~chinese
/// @brief 创建精灵
/// @param res 图片资源
/// @param crop_rect 裁剪矩形
static SpritePtr Create(Resource const& res, const Rect& crop_rect);
Sprite();
virtual ~Sprite();
@ -60,12 +72,14 @@ public:
/// \~chinese
/// @brief 加载本地图片
/// @param file_path 本地图片路径
bool Load(String const& file_path);
/// @param[in] autoresize 是否自动调整自身大小为图像大小
bool Load(String const& file_path, bool autoresize = true);
/// \~chinese
/// @brief 加载图像资源
/// @param res 图片资源
bool Load(Resource const& res);
/// @param[in] autoresize 是否自动调整自身大小为图像大小
bool Load(Resource const& res, bool autoresize = true);
/// \~chinese
/// @brief 使用矩形区域裁剪精灵
@ -79,7 +93,8 @@ public:
/// \~chinese
/// @brief 设置图像帧
/// @param[in] frame 图像帧
void SetFrame(FramePtr frame);
/// @param[in] autoresize 是否自动调整自身大小为图像大小
void SetFrame(FramePtr frame, bool autoresize = true);
void OnRender(RenderContext& ctx) override;