add Value & ValueObserver

This commit is contained in:
Nomango 2020-10-08 11:39:29 +08:00
parent ea67276033
commit 4be1d7eccb
7 changed files with 175 additions and 191 deletions

View File

@ -34,7 +34,7 @@
<ClInclude Include="..\..\src\kiwano\core\Singleton.h" />
<ClInclude Include="..\..\src\kiwano\core\String.h" />
<ClInclude Include="..\..\src\kiwano\core\Time.h" />
<ClInclude Include="..\..\src\kiwano\core\Property.h" />
<ClInclude Include="..\..\src\kiwano\core\Value.h" />
<ClInclude Include="..\..\src\kiwano\event\Event.h" />
<ClInclude Include="..\..\src\kiwano\event\EventDispatcher.h" />
<ClInclude Include="..\..\src\kiwano\event\EventListener.h" />

View File

@ -357,15 +357,15 @@
<ClInclude Include="..\..\src\kiwano\render\TextStyle.h">
<Filter>render</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\core\Property.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\core\BitOperator.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\core\Flag.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\kiwano\core\Value.h">
<Filter>core</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\kiwano\2d\Canvas.cpp">

View File

@ -448,65 +448,65 @@ public:
/// \~chinese
/// @brief 获取可见性属性
inline Property<bool> VisibleProperty()
inline Value<bool> VisibleProperty()
{
return Property<bool>(&visible_);
return Value<bool>(visible_);
}
/// \~chinese
/// @brief 获取不透明度属性
inline Property<float, FlagUint8> OpacityProperty()
inline Value<float, FlagUint8> OpacityProperty()
{
return Property<float, FlagUint8>(&opacity_, &dirty_flag_, DirtyFlag::DirtyOpacity);
return Value<float, FlagUint8>(opacity_, { dirty_flag_, DirtyFlag::DirtyOpacity });
}
/// \~chinese
/// @brief 获取锚点属性
inline Property<Point, FlagUint8> AnchorProperty()
inline Value<Point, FlagUint8> AnchorProperty()
{
return Property<Point, FlagUint8>(&anchor_, &dirty_flag_, DirtyFlag::DirtyTransform);
return Value<Point, FlagUint8>(anchor_, { dirty_flag_, DirtyFlag::DirtyTransform });
}
/// \~chinese
/// @brief 获取大小属性
inline Property<Size, FlagUint8> SizeProperty()
inline Value<Size, FlagUint8> SizeProperty()
{
return Property<Size, FlagUint8>(&size_, &dirty_flag_, DirtyFlag::DirtyTransform);
return Value<Size, FlagUint8>(size_, { dirty_flag_, DirtyFlag::DirtyTransform });
}
/// \~chinese
/// @brief 获取位置属性
inline Property<Point, FlagUint8> PositionProperty()
inline Value<Point, FlagUint8> PositionProperty()
{
return Property<Point, FlagUint8>(&transform_.position, &dirty_flag_, DirtyFlag::DirtyTransform);
return Value<Point, FlagUint8>(transform_.position, { dirty_flag_, DirtyFlag::DirtyTransform });
}
/// \~chinese
/// @brief 获取旋转角度属性
inline Property<float, FlagUint8> RotationProperty()
inline Value<float, FlagUint8> RotationProperty()
{
return Property<float, FlagUint8>(&transform_.rotation, &dirty_flag_, DirtyFlag::DirtyTransform);
return Value<float, FlagUint8>(transform_.rotation, { dirty_flag_, DirtyFlag::DirtyTransform });
}
/// \~chinese
/// @brief 获取缩放属性
inline Property<Point, FlagUint8> ScaleProperty()
inline Value<Point, FlagUint8> ScaleProperty()
{
return Property<Point, FlagUint8>(&transform_.scale, &dirty_flag_, DirtyFlag::DirtyTransform);
return Value<Point, FlagUint8>(transform_.scale, { dirty_flag_, DirtyFlag::DirtyTransform });
}
/// \~chinese
/// @brief 获取错切角度属性
inline Property<Point, FlagUint8> SkewProperty()
inline Value<Point, FlagUint8> SkewProperty()
{
return Property<Point, FlagUint8>(&transform_.skew, &dirty_flag_, DirtyFlag::DirtyTransform);
return Value<Point, FlagUint8>(transform_.skew, { dirty_flag_, DirtyFlag::DirtyTransform });
}
/// \~chinese
/// @brief 获取Z轴顺序属性
inline Property<int, Function<void()>> ZOrderProperty()
inline Value<int, Function<void()>> ZOrderProperty()
{
return Property<int, Function<void()>>(&z_order_, Closure(this, &Actor::Reorder));
return Value<int, Function<void()>>(z_order_, Closure(this, &Actor::Reorder));
}
protected:

View File

@ -119,9 +119,9 @@ public:
/// \~chinese
/// @brief 获取图像帧属性
inline Property<FramePtr, Function<void()>> FrameProperty()
inline Value<FramePtr, Function<void()>> FrameProperty()
{
return Property<FramePtr, Function<void()>>(std::addressof(frame_), Closure(this, &Sprite::ResetSize));
return Value<FramePtr, Function<void()>>(frame_, Closure(this, &Sprite::ResetSize));
}
void OnRender(RenderContext& ctx) override;

View File

@ -29,14 +29,14 @@
#include <unordered_map>
#include <unordered_set>
#include <kiwano/macros.h>
#include <kiwano/core/Property.h>
#include <kiwano/core/BitOperator.h>
#include <kiwano/core/Flag.h>
#include <kiwano/core/String.h>
#include <kiwano/core/Function.h>
#include <kiwano/core/Singleton.h>
#include <kiwano/core/Any.h>
#include <kiwano/core/Allocator.h>
#include <kiwano/core/BitOperator.h>
#include <kiwano/core/Flag.h>
#include <kiwano/core/Value.h>
namespace kiwano
{

View File

@ -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 <kiwano/core/BitOperator.h>
#include <kiwano/core/Flag.h>
#include <kiwano/core/Function.h>
namespace kiwano
{
template <typename _Ty, typename _NotifierTy = void>
class Property;
template <typename _Ty>
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 <typename _Ty, typename _FlagTy>
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 <typename _Ty>
class Property<_Ty, Function<void()>>
{
public:
typedef _Ty value_type;
typedef Function<void()> notifier_type;
inline Property(_Ty* ptr, const Function<void()>& 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<void()> notifier_;
};
template <typename _Ty, typename _NotifierTy>
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_;
};
}

148
src/kiwano/core/Value.h Normal file
View File

@ -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 <kiwano/core/Function.h>
#include <kiwano/core/BitOperator.h>
#include <kiwano/core/Flag.h>
namespace kiwano
{
template <typename _Ty, typename _FlagTy>
struct ValueObserver;
template <typename _Ty, typename _FlagTy>
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 <typename _Ty, typename _FlagTy>
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 <typename _Ty>
struct ValueObserver<_Ty, Function<void()>>
{
inline ValueObserver(const Function<void()>& callback)
: callback(callback)
{
}
inline void Changed(const _Ty& old_val, const _Ty& new_val)
{
if (callback)
{
callback();
}
}
Function<void()> callback;
};
template <typename _Ty, typename _ObserverTy = void>
class Value;
template <typename _Ty>
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 <typename _Ty, typename _ObserverTy>
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_;
};
}