diff --git a/src/kiwano/2d/Actor.h b/src/kiwano/2d/Actor.h index 839add72..54067c75 100644 --- a/src/kiwano/2d/Actor.h +++ b/src/kiwano/2d/Actor.h @@ -502,6 +502,13 @@ public: return Property(&transform_.skew, &dirty_flag_, DirtyFlag::DirtyTransform); } + /// \~chinese + /// @brief 获取Z轴顺序属性 + inline Property> ZOrderProperty() + { + return Property>(&z_order_, Closure(this, &Actor::Reorder)); + } + protected: /// \~chinese /// @brief 更新自身和所有子角色 diff --git a/src/kiwano/2d/Sprite.cpp b/src/kiwano/2d/Sprite.cpp index b06af6ce..5e643bd7 100644 --- a/src/kiwano/2d/Sprite.cpp +++ b/src/kiwano/2d/Sprite.cpp @@ -128,13 +128,25 @@ void Sprite::SetFrame(FramePtr frame, bool autoresize) if (frame_ != frame) { frame_ = frame; - if (frame_ && autoresize) + if (autoresize) { - SetSize(frame_->GetSize()); + ResetSize(); } } } +void Sprite::ResetSize() +{ + if (frame_) + { + SetSize(frame_->GetSize()); + } + else + { + SetSize(Size()); + } +} + void Sprite::OnRender(RenderContext& ctx) { if (frame_ && frame_->IsValid()) diff --git a/src/kiwano/2d/Sprite.h b/src/kiwano/2d/Sprite.h index f754d2f9..7ceebfb1 100644 --- a/src/kiwano/2d/Sprite.h +++ b/src/kiwano/2d/Sprite.h @@ -24,6 +24,7 @@ namespace kiwano { + KGE_DECLARE_SMART_PTR(Sprite); /** @@ -112,6 +113,17 @@ public: /// @param autoresize 是否自动调整自身大小为图像大小 void SetFrame(FramePtr frame, bool autoresize = true); + /// \~chinese + /// @brief 重置精灵大小为图像帧大小 + void ResetSize(); + + /// \~chinese + /// @brief 获取图像帧属性 + inline Property> FrameProperty() + { + return Property>(std::addressof(frame_), Closure(this, &Sprite::ResetSize)); + } + void OnRender(RenderContext& ctx) override; protected: diff --git a/src/kiwano/core/Flag.h b/src/kiwano/core/Flag.h index 731334b5..291fcb17 100644 --- a/src/kiwano/core/Flag.h +++ b/src/kiwano/core/Flag.h @@ -80,155 +80,4 @@ typedef Flag FlagInt16; typedef Flag FlagInt32; typedef Flag FlagInt64; -namespace bits -{ - -template <> -inline void Set(FlagUint8& old, FlagUint8 flag) -{ - old.Set(flag.value); -} - -template <> -inline void Set(FlagUint16& old, FlagUint16 flag) -{ - old.Set(flag.value); -} - -template <> -inline void Set(FlagUint32& old, FlagUint32 flag) -{ - old.Set(flag.value); -} - -template <> -inline void Set(FlagUint64& old, FlagUint64 flag) -{ - old.Set(flag.value); -} - -template <> -inline void Set(FlagInt8& old, FlagInt8 flag) -{ - old.Set(flag.value); -} - -template <> -inline void Set(FlagInt16& old, FlagInt16 flag) -{ - old.Set(flag.value); -} - -template <> -inline void Set(FlagInt32& old, FlagInt32 flag) -{ - old.Set(flag.value); -} - -template <> -inline void Set(FlagInt64& old, FlagInt64 flag) -{ - old.Set(flag.value); -} - - -template <> -inline void Unset(FlagUint8& old, FlagUint8 flag) -{ - old.Unset(flag.value); -} - -template <> -inline void Unset(FlagUint16& old, FlagUint16 flag) -{ - old.Unset(flag.value); -} - -template <> -inline void Unset(FlagUint32& old, FlagUint32 flag) -{ - old.Unset(flag.value); -} - -template <> -inline void Unset(FlagUint64& old, FlagUint64 flag) -{ - old.Unset(flag.value); -} - -template <> -inline void Unset(FlagInt8& old, FlagInt8 flag) -{ - old.Unset(flag.value); -} - -template <> -inline void Unset(FlagInt16& old, FlagInt16 flag) -{ - old.Unset(flag.value); -} - -template <> -inline void Unset(FlagInt32& old, FlagInt32 flag) -{ - old.Unset(flag.value); -} - -template <> -inline void Unset(FlagInt64& old, FlagInt64 flag) -{ - old.Unset(flag.value); -} - - -template <> -inline bool Has(FlagUint8 old, FlagUint8 flag) -{ - return old.Has(flag.value); -} - -template <> -inline bool Has(FlagUint16 old, FlagUint16 flag) -{ - return old.Has(flag.value); -} - -template <> -inline bool Has(FlagUint32 old, FlagUint32 flag) -{ - return old.Has(flag.value); -} - -template <> -inline bool Has(FlagUint64 old, FlagUint64 flag) -{ - return old.Has(flag.value); -} - -template <> -inline bool Has(FlagInt8 old, FlagInt8 flag) -{ - return old.Has(flag.value); -} - -template <> -inline bool Has(FlagInt16 old, FlagInt16 flag) -{ - return old.Has(flag.value); -} - -template <> -inline bool Has(FlagInt32 old, FlagInt32 flag) -{ - return old.Has(flag.value); -} - -template <> -inline bool Has(FlagInt64 old, FlagInt64 flag) -{ - return old.Has(flag.value); -} - - -} // namespace bits } // namespace kiwano diff --git a/src/kiwano/core/Property.h b/src/kiwano/core/Property.h index bffc126b..e426cd3f 100644 --- a/src/kiwano/core/Property.h +++ b/src/kiwano/core/Property.h @@ -21,6 +21,7 @@ #pragma once #include #include +#include namespace kiwano { @@ -33,7 +34,8 @@ template class Property<_Ty, void> { public: - typedef _Ty value_type; + typedef _Ty value_type; + typedef void notifier_type; inline Property(_Ty* ptr) : ptr_(ptr) @@ -55,20 +57,83 @@ private: }; +template +class Property<_Ty, Flag<_FlagTy>> +{ +public: + typedef _Ty value_type; + typedef Flag<_FlagTy> notifier_type; + + inline Property(_Ty* ptr, Flag<_FlagTy>* flag, _FlagTy flag_value) + : ptr_(ptr) + , flag_(flag) + , flag_value_(flag_value) + { + } + + inline _Ty Get() const + { + return *ptr_; + } + + inline void Set(const _Ty& value) + { + *ptr_ = value; + if (flag_) + { + flag_->Set(flag_value_); + } + } + +private: + _Ty* ptr_; + Flag<_FlagTy>* flag_; + _FlagTy flag_value_; +}; + + +template +class Property<_Ty, Function> +{ +public: + typedef _Ty value_type; + typedef Function notifier_type; + + inline Property(_Ty* ptr, const Function& notifier) + : ptr_(ptr) + , notifier_(notifier) + { + } + + inline _Ty Get() const + { + return *ptr_; + } + + inline void Set(const _Ty& value) + { + *ptr_ = value; + if (notifier_) + { + notifier_(); + } + } + +private: + _Ty* ptr_; + Function notifier_; +}; + + template class Property { public: + static_assert(std::is_arithmetic<_NotifierTy>::value, "_NotifierTy must be an arithmetic type"); + typedef _Ty value_type; typedef _NotifierTy notifier_type; - inline Property(_Ty* ptr) - : ptr_(ptr) - , notifier_(nullptr) - , notify_value_() - { - } - inline Property(_Ty* ptr, _NotifierTy* notifier, _NotifierTy notify_value) : ptr_(ptr) , notifier_(notifier)