diff --git a/src/kiwano/2d/Button.cpp b/src/kiwano/2d/Button.cpp index 6f1dccf1..519b88f4 100644 --- a/src/kiwano/2d/Button.cpp +++ b/src/kiwano/2d/Button.cpp @@ -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(handler); + actor->AddListener(handler); + actor->AddListener(handler); + actor->AddListener(handler); + actor->AddListener(handler); +} + SpriteButton::SpriteButton() { - SetResponsible(true); - - EventListener::Callback handler = Closure(this, &SpriteButton::UpdateStatus); - AddListener(handler); - AddListener(handler); - AddListener(handler); - AddListener(handler); - AddListener(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(handler); - AddListener(handler); - AddListener(handler); - AddListener(handler); - AddListener(handler); + BindActor(this); } TextButtonPtr TextButton::Create(Callback const& click) diff --git a/src/kiwano/2d/Button.h b/src/kiwano/2d/Button.h index 5746e0ed..e1ebc063 100644 --- a/src/kiwano/2d/Button.h +++ b/src/kiwano/2d/Button.h @@ -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 diff --git a/src/kiwano/2d/Sprite.cpp b/src/kiwano/2d/Sprite.cpp index b6080fb9..1cf477cd 100644 --- a/src/kiwano/2d/Sprite.cpp +++ b/src/kiwano/2d/Sprite.cpp @@ -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() }); } diff --git a/src/kiwano/2d/Sprite.h b/src/kiwano/2d/Sprite.h index 08784310..410b560c 100644 --- a/src/kiwano/2d/Sprite.h +++ b/src/kiwano/2d/Sprite.h @@ -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;