This commit is contained in:
Nomango 2019-08-20 21:15:15 +08:00
parent ff8a05c3ac
commit d9e67fd737
17 changed files with 112 additions and 91 deletions

View File

@ -352,9 +352,7 @@ namespace kiwano
void RemoveFromParent(); void RemoveFromParent();
// 判断点是否在角色内 // 判断点是否在角色内
bool ContainsPoint( virtual bool ContainsPoint(const Point& point) const;
const Point& point
) const;
// 暂停角色更新 // 暂停角色更新
inline void PauseUpdating() { update_pausing_ = true; } inline void PauseUpdating() { update_pausing_ = true; }

View File

@ -90,7 +90,7 @@ namespace kiwano
void GifSprite::OnRender(RenderTarget* rt) void GifSprite::OnRender(RenderTarget* rt)
{ {
if (frame_.IsValid() && rt->CheckVisibility(size_, transform_matrix_)) if (frame_.IsValid() && rt->CheckVisibility(GetBounds(), GetTransformMatrix()))
{ {
PrepareRender(rt); PrepareRender(rt);

View File

@ -44,10 +44,7 @@ namespace kiwano
Rect ShapeActor::GetBounds() const Rect ShapeActor::GetBounds() const
{ {
if (!geo_) return bounds_;
return Rect{};
return geo_.GetBoundingBox(Matrix3x2());
} }
Rect ShapeActor::GetBoundingBox() const Rect ShapeActor::GetBoundingBox() const
@ -58,6 +55,11 @@ namespace kiwano
return geo_.GetBoundingBox(GetTransformMatrix()); return geo_.GetBoundingBox(GetTransformMatrix());
} }
bool ShapeActor::ContainsPoint(const Point& point) const
{
return geo_.ContainsPoint(point, GetTransformMatrix());
}
void ShapeActor::SetFillColor(const Color & color) void ShapeActor::SetFillColor(const Color & color)
{ {
fill_color_ = color; fill_color_ = color;
@ -78,9 +80,24 @@ namespace kiwano
stroke_style_ = stroke_style; stroke_style_ = stroke_style;
} }
void ShapeActor::SetGeometry(Geometry geometry)
{
geo_ = geometry;
if (geo_)
{
bounds_ = geo_.GetBoundingBox(Matrix3x2());
SetSize(bounds_.GetSize());
}
else
{
bounds_ = Rect{};
SetSize(0.f, 0.f);
}
}
void ShapeActor::OnRender(RenderTarget* rt) void ShapeActor::OnRender(RenderTarget* rt)
{ {
if (geo_ && rt->CheckVisibility(size_, transform_matrix_)) if (geo_ && rt->CheckVisibility(GetBounds(), GetTransformMatrix()))
{ {
PrepareRender(rt); PrepareRender(rt);
@ -117,12 +134,12 @@ namespace kiwano
void LineActor::SetPoint(Point const& point) void LineActor::SetPoint(Point const& point)
{ {
geo_ = Geometry::CreateLine(Point{}, point); Geometry geo = Geometry::CreateLine(Point{}, point);
if (geo_) if (geo)
{ {
point_ = point; point_ = point;
SetSize(point_); SetGeometry(geo);
} }
} }
@ -146,12 +163,12 @@ namespace kiwano
void RectActor::SetRectSize(Size const& size) void RectActor::SetRectSize(Size const& size)
{ {
geo_ = Geometry::CreateRect(Rect{ Point{}, size }); Geometry geo = Geometry::CreateRect(Rect{ Point{}, size });
if (geo_) if (geo)
{ {
rect_size_ = size; rect_size_ = size;
SetSize(size); SetGeometry(geo);
} }
} }
@ -185,12 +202,12 @@ namespace kiwano
void RoundRectActor::SetRoundedRect(Size const& size, Vec2 const& radius) void RoundRectActor::SetRoundedRect(Size const& size, Vec2 const& radius)
{ {
geo_ = Geometry::CreateRoundedRect(Rect{ Point{}, size }, radius); Geometry geo = Geometry::CreateRoundedRect(Rect{ Point{}, size }, radius);
if (geo_) if (geo)
{ {
rect_size_ = size; rect_size_ = size;
SetSize(size); SetGeometry(geo);
} }
} }
@ -215,11 +232,11 @@ namespace kiwano
void CircleActor::SetRadius(Float32 radius) void CircleActor::SetRadius(Float32 radius)
{ {
geo_ = Geometry::CreateCircle(Point{}, radius); Geometry geo = Geometry::CreateCircle(Point{}, radius);
if (geo_) if (geo)
{ {
SetSize(Size{ radius * 2, radius * 2 }); SetGeometry(geo);
} }
} }
@ -243,11 +260,11 @@ namespace kiwano
void EllipseActor::SetRadius(Vec2 const& radius) void EllipseActor::SetRadius(Vec2 const& radius)
{ {
geo_ = Geometry::CreateEllipse(Point{}, radius); Geometry geo = Geometry::CreateEllipse(Point{}, radius);
if (geo_) if (geo)
{ {
SetSize(radius * 2); SetGeometry(geo);
} }
} }
@ -272,12 +289,11 @@ namespace kiwano
void PathActor::EndPath(bool closed) void PathActor::EndPath(bool closed)
{ {
sink_.EndPath(closed); sink_.EndPath(closed);
geo_ = sink_.GetGeometry(); Geometry geo = sink_.GetGeometry();
if (geo_) if (geo)
{ {
Rect bounds = geo_.GetBoundingBox(); SetGeometry(geo);
SetSize(bounds.GetSize());
} }
} }

View File

@ -38,16 +38,19 @@ namespace kiwano
virtual ~ShapeActor(); virtual ~ShapeActor();
// 삿혤輕념奈<EB8590> // 삿혤輕념奈<EB8590>
Color GetFillColor() const { return fill_color_; } inline Color GetFillColor() const { return fill_color_; }
// 삿혤窟係奈<E4BF82> // 삿혤窟係奈<E4BF82>
Color GetStrokeColor() const { return stroke_color_; } inline Color GetStrokeColor() const { return stroke_color_; }
// 삿혤窟係욱똑 // 삿혤窟係욱똑
Float32 GetStrokeWidth() const { return stroke_width_; } inline Float32 GetStrokeWidth() const { return stroke_width_; }
// 삿혤窟係湳駕 // 삿혤窟係湳駕
StrokeStyle SetStrokeStyle() const { return stroke_style_; } inline StrokeStyle SetStrokeStyle() const { return stroke_style_; }
// 获取形状
inline Geometry GetGeometry() const { return geo_; }
// 삿혤긋썹 // 삿혤긋썹
Rect GetBounds() const override; Rect GetBounds() const override;
@ -55,31 +58,23 @@ namespace kiwano
// 삿혤棍학관鍋분 // 삿혤棍학관鍋분
Rect GetBoundingBox() const override; Rect GetBoundingBox() const override;
// 判断点是否在形状内
bool ContainsPoint(const Point& point) const override;
// <20>零輕념奈<EB8590> // <20>零輕념奈<EB8590>
void SetFillColor( void SetFillColor(const Color& color);
const Color& color
);
// <20>零窟係奈<E4BF82> // <20>零窟係奈<E4BF82>
void SetStrokeColor( void SetStrokeColor(const Color& color);
const Color& color
);
// <20>零窟係욱똑 // <20>零窟係욱똑
void SetStrokeWidth( void SetStrokeWidth(Float32 width);
Float32 width
);
// <20>零窟係湳駕 // <20>零窟係湳駕
void SetStrokeStyle( void SetStrokeStyle(StrokeStyle stroke_style);
StrokeStyle stroke_style
);
// <20>零近榴 // <20>零近榴
inline void SetGeometry(Geometry geometry) { geo_ = geometry; } void SetGeometry(Geometry geometry);
// ťńČĄĐÎ×´
inline Geometry GetGeometry() const { return geo_; }
void OnRender(RenderTarget* rt) override; void OnRender(RenderTarget* rt) override;
@ -88,6 +83,7 @@ namespace kiwano
Color stroke_color_; Color stroke_color_;
Float32 stroke_width_; Float32 stroke_width_;
StrokeStyle stroke_style_; StrokeStyle stroke_style_;
Rect bounds_;
Geometry geo_; Geometry geo_;
}; };

View File

@ -104,7 +104,7 @@ namespace kiwano
void Sprite::OnRender(RenderTarget* rt) void Sprite::OnRender(RenderTarget* rt)
{ {
if (frame_ && rt->CheckVisibility(size_, transform_matrix_)) if (frame_ && rt->CheckVisibility(GetBounds(), GetTransformMatrix()))
{ {
PrepareRender(rt); PrepareRender(rt);

View File

@ -204,7 +204,7 @@ namespace kiwano
{ {
UpdateLayout(); UpdateLayout();
if (text_layout_ && rt->CheckVisibility(size_, transform_matrix_)) if (text_layout_ && rt->CheckVisibility(GetBounds(), GetTransformMatrix()))
{ {
PrepareRender(rt); PrepareRender(rt);
rt->DrawTextLayout(text_layout_); rt->DrawTextLayout(text_layout_);

View File

@ -40,7 +40,7 @@ namespace kiwano
} }
} }
EventListenerPtr EventDispatcher::AddListener(EventListenerPtr listener) EventListener* EventDispatcher::AddListener(EventListenerPtr listener)
{ {
KGE_ASSERT(listener && "AddListener failed, NULL pointer exception"); KGE_ASSERT(listener && "AddListener failed, NULL pointer exception");
@ -48,16 +48,13 @@ namespace kiwano
{ {
listeners_.push_back(listener); listeners_.push_back(listener);
} }
return listener; return listener.get();
} }
void EventDispatcher::AddListener(UInt32 type, EventCallback callback, String const& name) EventListener* EventDispatcher::AddListener(UInt32 type, EventCallback callback, String const& name)
{ {
EventListenerPtr listener = new EventListener(type, callback, name); EventListenerPtr listener = new EventListener(type, callback, name);
if (listener) return AddListener(listener);
{
listeners_.push_back(listener);
}
} }
void EventDispatcher::StartListeners(String const & listener_name) void EventDispatcher::StartListeners(String const & listener_name)

View File

@ -29,12 +29,12 @@ namespace kiwano
public: public:
// 添加监听器 // 添加监听器
EventListenerPtr AddListener( EventListener* AddListener(
EventListenerPtr listener EventListenerPtr listener
); );
// 添加监听器 // 添加监听器
void AddListener( EventListener* AddListener(
UInt32 type, UInt32 type,
EventCallback callback, EventCallback callback,
String const& name = L"" String const& name = L""

View File

@ -46,7 +46,7 @@ namespace kiwano
inline bool IsName(String const& name) const { return name_ ? (*name_ == name) : name.empty(); } inline bool IsName(String const& name) const { return name_ ? (*name_ == name) : name.empty(); }
inline UInt32 GetObjectID() const { return id_; } inline UInt32 GetObjectID() const { return id_; }
String DumpObject(); String DumpObject();

View File

@ -22,11 +22,6 @@
namespace kiwano namespace kiwano
{ {
Timer::Timer(Callback const& func, String const& name)
: Timer(func, Duration{}, -1, name)
{
}
Timer::Timer(Callback const& func, Duration delay, Int32 times, String const& name) Timer::Timer(Callback const& func, Duration delay, Int32 times, String const& name)
: running_(true) : running_(true)
, run_times_(0) , run_times_(0)

View File

@ -40,12 +40,7 @@ namespace kiwano
using Callback = Function<void()>; using Callback = Function<void()>;
public: public:
explicit Timer( Timer(
Callback const& func, /* 执行函数 */
String const& name = L"" /* 任务名称 */
);
explicit Timer(
Callback const& func, /* 执行函数 */ Callback const& func, /* 执行函数 */
Duration delay, /* 时间间隔(秒) */ Duration delay, /* 时间间隔(秒) */
Int32 times = -1, /* 执行次数(设 -1 为永久执行) */ Int32 times = -1, /* 执行次数(设 -1 为永久执行) */

View File

@ -41,7 +41,13 @@ namespace kiwano
} }
} }
void TimerManager::AddTimer(TimerPtr timer) Timer* TimerManager::AddTimer(Timer::Callback const& func, Duration delay, Int32 times, String const& name)
{
TimerPtr timer = new Timer(func, delay, times, name);
return AddTimer(timer);
}
Timer* TimerManager::AddTimer(TimerPtr timer)
{ {
KGE_ASSERT(timer && "AddTimer failed, NULL pointer exception"); KGE_ASSERT(timer && "AddTimer failed, NULL pointer exception");
@ -50,6 +56,8 @@ namespace kiwano
timer->Reset(); timer->Reset();
timers_.push_back(timer); timers_.push_back(timer);
} }
return timer.get();
} }
void TimerManager::StopTimers(String const& name) void TimerManager::StopTimers(String const& name)

View File

@ -28,8 +28,16 @@ namespace kiwano
using Timers = intrusive_list<TimerPtr>; using Timers = intrusive_list<TimerPtr>;
public: public:
// Ìí¼ÓÈÎÎñ // 添加定时器
void AddTimer( Timer* AddTimer(
Timer::Callback const& func, /* 执行函数 */
Duration delay, /* 时间间隔(秒) */
Int32 times = -1, /* 执行次数(设 -1 为永久执行) */
String const& name = L"" /* 任务名称 */
);
// 添加定时器
Timer* AddTimer(
TimerPtr timer TimerPtr timer
); );

View File

@ -38,6 +38,11 @@ namespace kiwano
{ {
} }
bool Geometry::IsValid() const
{
return geo_ != nullptr;
}
Rect Geometry::GetBoundingBox(Matrix3x2 const& transform) const Rect Geometry::GetBoundingBox(Matrix3x2 const& transform) const
{ {
if (!geo_) if (!geo_)
@ -49,7 +54,7 @@ namespace kiwano
return rect; return rect;
} }
Float32 Geometry::GetLength() Float32 Geometry::GetLength() const
{ {
Float32 length = 0.f; Float32 length = 0.f;
if (geo_) if (geo_)
@ -60,7 +65,7 @@ namespace kiwano
return length; return length;
} }
bool Geometry::ComputePointAtLength(Float32 length, Point& point, Vec2& tangent) bool Geometry::ComputePointAtLength(Float32 length, Point& point, Vec2& tangent) const
{ {
if (geo_) if (geo_)
{ {
@ -76,7 +81,7 @@ namespace kiwano
return false; return false;
} }
Geometry Geometry::CombineWith(Geometry input, CombineMode mode, Matrix3x2 const& input_matrix) Geometry Geometry::CombineWith(Geometry input, CombineMode mode, Matrix3x2 const& input_matrix) const
{ {
if (geo_ && input.geo_) if (geo_ && input.geo_)
{ {
@ -129,7 +134,7 @@ namespace kiwano
return Geometry(); return Geometry();
} }
Float32 Geometry::ComputeArea() Float32 Geometry::ComputeArea() const
{ {
if (!geo_) if (!geo_)
return 0.f; return 0.f;
@ -140,7 +145,7 @@ namespace kiwano
return area; return area;
} }
bool Geometry::ContainsPoint(Point const& point) bool Geometry::ContainsPoint(Point const& point, Matrix3x2 const& transform) const
{ {
if (!geo_) if (!geo_)
return false; return false;
@ -149,7 +154,7 @@ namespace kiwano
// no matter it failed or not // no matter it failed or not
geo_->FillContainsPoint( geo_->FillContainsPoint(
DX::ConvertToPoint2F(point), DX::ConvertToPoint2F(point),
D2D1::Matrix3x2F::Identity(), DX::ConvertToMatrix3x2F(transform),
&ret &ret
); );
return !!ret; return !!ret;

View File

@ -42,6 +42,8 @@ namespace kiwano
Geometry(ComPtr<ID2D1Geometry> geo); Geometry(ComPtr<ID2D1Geometry> geo);
bool IsValid() const;
// 获取外切包围盒 // 获取外切包围盒
Rect GetBoundingBox( Rect GetBoundingBox(
Matrix3x2 const& transform = Matrix3x2() Matrix3x2 const& transform = Matrix3x2()
@ -49,28 +51,29 @@ namespace kiwano
// 判断图形是否包含点 // 判断图形是否包含点
bool ContainsPoint( bool ContainsPoint(
Point const& point Point const& point,
); Matrix3x2 const& transform = Matrix3x2()
) const;
// 获取图形展开成一条直线的长度 // 获取图形展开成一条直线的长度
Float32 GetLength(); Float32 GetLength() const;
// 计算面积 // 计算面积
Float32 ComputeArea(); Float32 ComputeArea() const;
// 计算图形路径上点的位置和切线向量 // 计算图形路径上点的位置和切线向量
bool ComputePointAtLength( bool ComputePointAtLength(
Float32 length, Float32 length,
Point& point, Point& point,
Vec2& tangent Vec2& tangent
); ) const;
// 组合几何体 // 组合几何体
Geometry CombineWith( Geometry CombineWith(
Geometry input, Geometry input,
CombineMode mode, CombineMode mode,
Matrix3x2 const& input_matrix = Matrix3x2() Matrix3x2 const& input_matrix = Matrix3x2()
); ) const;
// 组合多个几何体 // 组合多个几何体
// 参数 modes 和 matrixs 的数量应为 1 或 geos 的数量减一 // 参数 modes 和 matrixs 的数量应为 1 或 geos 的数量减一
@ -114,7 +117,7 @@ namespace kiwano
inline void SetGeometry(ComPtr<ID2D1Geometry> geometry) { geo_ = geometry; } inline void SetGeometry(ComPtr<ID2D1Geometry> geometry) { geo_ = geometry; }
inline operator bool() const { return static_cast<bool>(geo_); } inline operator bool() const { return IsValid(); }
protected: protected:
ComPtr<ID2D1Geometry> geo_; ComPtr<ID2D1Geometry> geo_;

View File

@ -625,10 +625,10 @@ namespace kiwano
ThrowIfFailed(hr); ThrowIfFailed(hr);
} }
bool RenderTarget::CheckVisibility(Size const& content_size, Matrix3x2 const& transform) bool RenderTarget::CheckVisibility(Rect const& bounds, Matrix3x2 const& transform)
{ {
return Rect{ Point{}, reinterpret_cast<const Size&>(render_target_->GetSize()) }.Intersects( return Rect{ Point{}, reinterpret_cast<const Size&>(render_target_->GetSize()) }.Intersects(
transform.Transform(Rect{ Point{}, content_size }) transform.Transform(bounds)
); );
} }

View File

@ -159,7 +159,7 @@ namespace kiwano
); );
bool CheckVisibility( bool CheckVisibility(
Size const& content_size, Rect const& bounds,
Matrix3x2 const& transform Matrix3x2 const& transform
); );