diff --git a/projects/kiwano/kiwano.vcxproj b/projects/kiwano/kiwano.vcxproj index a136bf40..9007e707 100644 --- a/projects/kiwano/kiwano.vcxproj +++ b/projects/kiwano/kiwano.vcxproj @@ -34,7 +34,7 @@ - + diff --git a/projects/kiwano/kiwano.vcxproj.filters b/projects/kiwano/kiwano.vcxproj.filters index bd437330..e89b444b 100644 --- a/projects/kiwano/kiwano.vcxproj.filters +++ b/projects/kiwano/kiwano.vcxproj.filters @@ -357,15 +357,15 @@ render - - core - core core + + core + diff --git a/src/kiwano/2d/Actor.h b/src/kiwano/2d/Actor.h index 54067c75..3a81048d 100644 --- a/src/kiwano/2d/Actor.h +++ b/src/kiwano/2d/Actor.h @@ -448,65 +448,65 @@ public: /// \~chinese /// @brief 获取可见性属性 - inline Property VisibleProperty() + inline Value VisibleProperty() { - return Property(&visible_); + return Value(visible_); } /// \~chinese /// @brief 获取不透明度属性 - inline Property OpacityProperty() + inline Value OpacityProperty() { - return Property(&opacity_, &dirty_flag_, DirtyFlag::DirtyOpacity); + return Value(opacity_, { dirty_flag_, DirtyFlag::DirtyOpacity }); } /// \~chinese /// @brief 获取锚点属性 - inline Property AnchorProperty() + inline Value AnchorProperty() { - return Property(&anchor_, &dirty_flag_, DirtyFlag::DirtyTransform); + return Value(anchor_, { dirty_flag_, DirtyFlag::DirtyTransform }); } /// \~chinese /// @brief 获取大小属性 - inline Property SizeProperty() + inline Value SizeProperty() { - return Property(&size_, &dirty_flag_, DirtyFlag::DirtyTransform); + return Value(size_, { dirty_flag_, DirtyFlag::DirtyTransform }); } /// \~chinese /// @brief 获取位置属性 - inline Property PositionProperty() + inline Value PositionProperty() { - return Property(&transform_.position, &dirty_flag_, DirtyFlag::DirtyTransform); + return Value(transform_.position, { dirty_flag_, DirtyFlag::DirtyTransform }); } /// \~chinese /// @brief 获取旋转角度属性 - inline Property RotationProperty() + inline Value RotationProperty() { - return Property(&transform_.rotation, &dirty_flag_, DirtyFlag::DirtyTransform); + return Value(transform_.rotation, { dirty_flag_, DirtyFlag::DirtyTransform }); } /// \~chinese /// @brief 获取缩放属性 - inline Property ScaleProperty() + inline Value ScaleProperty() { - return Property(&transform_.scale, &dirty_flag_, DirtyFlag::DirtyTransform); + return Value(transform_.scale, { dirty_flag_, DirtyFlag::DirtyTransform }); } /// \~chinese /// @brief 获取错切角度属性 - inline Property SkewProperty() + inline Value SkewProperty() { - return Property(&transform_.skew, &dirty_flag_, DirtyFlag::DirtyTransform); + return Value(transform_.skew, { dirty_flag_, DirtyFlag::DirtyTransform }); } /// \~chinese /// @brief 获取Z轴顺序属性 - inline Property> ZOrderProperty() + inline Value> ZOrderProperty() { - return Property>(&z_order_, Closure(this, &Actor::Reorder)); + return Value>(z_order_, Closure(this, &Actor::Reorder)); } protected: diff --git a/src/kiwano/2d/Sprite.h b/src/kiwano/2d/Sprite.h index 7ceebfb1..9a30f601 100644 --- a/src/kiwano/2d/Sprite.h +++ b/src/kiwano/2d/Sprite.h @@ -119,9 +119,9 @@ public: /// \~chinese /// @brief 获取图像帧属性 - inline Property> FrameProperty() + inline Value> FrameProperty() { - return Property>(std::addressof(frame_), Closure(this, &Sprite::ResetSize)); + return Value>(frame_, Closure(this, &Sprite::ResetSize)); } void OnRender(RenderContext& ctx) override; diff --git a/src/kiwano/core/Common.h b/src/kiwano/core/Common.h index 7fd87a30..0f40191b 100644 --- a/src/kiwano/core/Common.h +++ b/src/kiwano/core/Common.h @@ -29,14 +29,14 @@ #include #include #include -#include -#include -#include #include #include #include #include #include +#include +#include +#include namespace kiwano { diff --git a/src/kiwano/core/Property.h b/src/kiwano/core/Property.h deleted file mode 100644 index e426cd3f..00000000 --- a/src/kiwano/core/Property.h +++ /dev/null @@ -1,164 +0,0 @@ -// Copyright (c) 2019-2020 Kiwano - Nomango -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#pragma once -#include -#include -#include - -namespace kiwano -{ - -template -class Property; - - -template -class Property<_Ty, void> -{ -public: - typedef _Ty value_type; - typedef void notifier_type; - - inline Property(_Ty* ptr) - : ptr_(ptr) - { - } - - inline _Ty Get() const - { - return *ptr_; - } - - inline void Set(const _Ty& value) - { - *ptr_ = value; - } - -private: - _Ty* ptr_; -}; - - -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, _NotifierTy* notifier, _NotifierTy notify_value) - : ptr_(ptr) - , notifier_(notifier) - , notify_value_(notify_value) - { - } - - inline _Ty Get() const - { - return *ptr_; - } - - inline void Set(const _Ty& value) - { - *ptr_ = value; - if (notifier_) - { - bits::Set(*notifier_, notify_value_); - } - } - -private: - _Ty* ptr_; - _NotifierTy* notifier_; - _NotifierTy notify_value_; -}; - -} diff --git a/src/kiwano/core/Value.h b/src/kiwano/core/Value.h new file mode 100644 index 00000000..7dc2707f --- /dev/null +++ b/src/kiwano/core/Value.h @@ -0,0 +1,148 @@ +// Copyright (c) 2019-2020 Kiwano - Nomango +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#pragma once +#include +#include +#include + +namespace kiwano +{ + +template +struct ValueObserver; + +template +struct ValueObserver +{ + static_assert(std::is_arithmetic<_FlagTy>::value, "_Ty must be an arithmetic type"); + + inline ValueObserver(_FlagTy& observer, _FlagTy value) + : observer(observer) + , value(value) + { + } + + inline void Changed(const _Ty& old_val, const _Ty& new_val) + { + bits::Set(observer, value); + } + + _FlagTy& observer; + _FlagTy value; +}; + +template +struct ValueObserver<_Ty, Flag<_FlagTy>> +{ + inline ValueObserver(Flag<_FlagTy>& flag, _FlagTy flag_value) + : flag(flag) + , flag_value(flag_value) + { + } + + inline void Changed(const _Ty& old_val, const _Ty& new_val) + { + flag.Set(flag_value); + } + + Flag<_FlagTy>& flag; + _FlagTy flag_value; +}; + +template +struct ValueObserver<_Ty, Function> +{ + inline ValueObserver(const Function& callback) + : callback(callback) + { + } + + inline void Changed(const _Ty& old_val, const _Ty& new_val) + { + if (callback) + { + callback(); + } + } + + Function callback; +}; + +template +class Value; + +template +class Value<_Ty, void> +{ +public: + typedef _Ty value_type; + typedef void observer_type; + + inline Value(_Ty& ptr) + : ptr_(ptr) + { + } + + inline const _Ty& Get() const + { + return ptr_; + } + + inline void Set(const _Ty& value) + { + ptr_ = value; + } + +private: + _Ty& ptr_; +}; + +template +class Value +{ +public: + typedef _Ty value_type; + + typedef ValueObserver<_Ty, _ObserverTy> observer_type; + + inline Value(_Ty& ptr, const observer_type& observer) + : ptr_(ptr) + , observer_(observer) + { + } + + inline const _Ty& Get() const + { + return ptr_; + } + + inline void Set(const _Ty& value) + { + ptr_ = value; + observer_.Changed(ptr_, value); + } + +private: + _Ty& ptr_; + observer_type observer_; +}; + +}