2019-04-11 14:40:54 +08:00
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
// 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:
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2020-01-21 10:09:55 +08:00
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
// 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.
|
|
|
|
|
|
2019-10-11 21:55:29 +08:00
|
|
|
#include <kiwano/2d/Actor.h>
|
|
|
|
|
#include <kiwano/2d/Stage.h>
|
2019-11-13 14:33:15 +08:00
|
|
|
#include <kiwano/core/Logger.h>
|
2020-01-17 16:55:47 +08:00
|
|
|
#include <kiwano/render/Renderer.h>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
namespace
|
|
|
|
|
{
|
2020-02-06 16:54:47 +08:00
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
float default_anchor_x = 0.f;
|
|
|
|
|
float default_anchor_y = 0.f;
|
2020-02-06 16:54:47 +08:00
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
void Actor::SetDefaultAnchor(float anchor_x, float anchor_y)
|
|
|
|
|
{
|
|
|
|
|
default_anchor_x = anchor_x;
|
|
|
|
|
default_anchor_y = anchor_y;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 16:54:47 +08:00
|
|
|
ActorPtr Actor::Create()
|
|
|
|
|
{
|
2020-02-20 22:27:09 +08:00
|
|
|
ActorPtr ptr = memory::New<Actor>();
|
2020-02-06 16:54:47 +08:00
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
Actor::Actor()
|
|
|
|
|
: visible_(true)
|
|
|
|
|
, visible_in_rt_(true)
|
|
|
|
|
, update_pausing_(false)
|
|
|
|
|
, hover_(false)
|
|
|
|
|
, pressed_(false)
|
|
|
|
|
, responsible_(false)
|
|
|
|
|
, dirty_visibility_(true)
|
|
|
|
|
, dirty_transform_(false)
|
|
|
|
|
, dirty_transform_inverse_(false)
|
|
|
|
|
, cascade_opacity_(false)
|
|
|
|
|
, show_border_(false)
|
|
|
|
|
, is_fast_transform_(true)
|
|
|
|
|
, parent_(nullptr)
|
|
|
|
|
, stage_(nullptr)
|
|
|
|
|
, hash_name_(0)
|
|
|
|
|
, z_order_(0)
|
|
|
|
|
, opacity_(1.f)
|
|
|
|
|
, displayed_opacity_(1.f)
|
|
|
|
|
, anchor_(default_anchor_x, default_anchor_y)
|
2020-02-22 17:24:31 +08:00
|
|
|
, physic_body_(nullptr)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 11:50:05 +08:00
|
|
|
Actor::~Actor()
|
|
|
|
|
{
|
|
|
|
|
RemoveAllComponents();
|
|
|
|
|
RemoveAllChildren();
|
|
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
void Actor::Update(Duration dt)
|
|
|
|
|
{
|
|
|
|
|
UpdateActions(this, dt);
|
2020-02-19 11:50:05 +08:00
|
|
|
UpdateComponents(dt);
|
2020-05-19 21:25:00 +08:00
|
|
|
UpdateTasks(dt);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
if (!update_pausing_)
|
|
|
|
|
{
|
|
|
|
|
if (cb_update_)
|
|
|
|
|
cb_update_(dt);
|
|
|
|
|
|
|
|
|
|
OnUpdate(dt);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
if (!children_.IsEmpty())
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
ActorPtr next;
|
2020-02-15 17:32:32 +08:00
|
|
|
for (auto child = children_.GetFirst(); child; child = next)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-15 17:32:32 +08:00
|
|
|
next = child->GetNext();
|
2020-01-21 10:09:55 +08:00
|
|
|
child->Update(dt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::Render(RenderContext& ctx)
|
|
|
|
|
{
|
|
|
|
|
if (!visible_)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
UpdateTransform();
|
|
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
if (children_.IsEmpty())
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
if (CheckVisibility(ctx))
|
|
|
|
|
{
|
|
|
|
|
PrepareToRender(ctx);
|
2020-02-23 14:56:14 +08:00
|
|
|
RenderComponents(ctx);
|
2020-01-21 10:09:55 +08:00
|
|
|
OnRender(ctx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// render children those are less than 0 in Z-Order
|
2020-02-15 17:32:32 +08:00
|
|
|
ActorPtr child = children_.GetFirst();
|
2020-01-21 10:09:55 +08:00
|
|
|
while (child)
|
|
|
|
|
{
|
|
|
|
|
if (child->GetZOrder() >= 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
child->Render(ctx);
|
2020-02-15 17:32:32 +08:00
|
|
|
child = child->GetNext();
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CheckVisibility(ctx))
|
|
|
|
|
{
|
|
|
|
|
PrepareToRender(ctx);
|
2020-02-23 14:56:14 +08:00
|
|
|
RenderComponents(ctx);
|
2020-01-21 10:09:55 +08:00
|
|
|
OnRender(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (child)
|
|
|
|
|
{
|
|
|
|
|
child->Render(ctx);
|
2020-02-15 17:32:32 +08:00
|
|
|
child = child->GetNext();
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::PrepareToRender(RenderContext& ctx)
|
|
|
|
|
{
|
|
|
|
|
ctx.SetTransform(transform_matrix_);
|
|
|
|
|
ctx.SetBrushOpacity(GetDisplayedOpacity());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::RenderBorder(RenderContext& ctx)
|
|
|
|
|
{
|
|
|
|
|
if (show_border_ && !size_.IsOrigin())
|
|
|
|
|
{
|
|
|
|
|
Rect bounds = GetBounds();
|
|
|
|
|
|
|
|
|
|
ctx.SetTransform(transform_matrix_);
|
|
|
|
|
|
|
|
|
|
ctx.SetCurrentBrush(GetStage()->GetBorderFillBrush());
|
|
|
|
|
ctx.FillRectangle(bounds);
|
|
|
|
|
|
|
|
|
|
ctx.SetCurrentBrush(GetStage()->GetBorderStrokeBrush());
|
2020-02-16 20:14:01 +08:00
|
|
|
ctx.DrawRectangle(bounds);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
for (auto& child : children_)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-19 11:50:05 +08:00
|
|
|
child->RenderBorder(ctx);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Actor::CheckVisibility(RenderContext& ctx) const
|
|
|
|
|
{
|
|
|
|
|
if (dirty_visibility_)
|
|
|
|
|
{
|
|
|
|
|
dirty_visibility_ = false;
|
|
|
|
|
|
|
|
|
|
if (size_.IsOrigin())
|
|
|
|
|
{
|
|
|
|
|
visible_in_rt_ = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
visible_in_rt_ = ctx.CheckVisibility(GetBounds(), GetTransformMatrix());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return visible_in_rt_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Actor::DispatchEvent(Event* evt)
|
|
|
|
|
{
|
|
|
|
|
if (!visible_)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// Dispatch to children those are greater than 0 in Z-Order
|
2020-02-15 17:32:32 +08:00
|
|
|
ActorPtr child = children_.GetLast();
|
2020-01-21 10:09:55 +08:00
|
|
|
while (child)
|
|
|
|
|
{
|
|
|
|
|
if (child->GetZOrder() < 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (!child->DispatchEvent(evt))
|
|
|
|
|
return false;
|
|
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
child = child->GetPrev();
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
2020-02-18 12:53:18 +08:00
|
|
|
if (!HandleEvent(evt))
|
2020-01-21 10:09:55 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
while (child)
|
|
|
|
|
{
|
|
|
|
|
if (!child->DispatchEvent(evt))
|
|
|
|
|
return false;
|
|
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
child = child->GetPrev();
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 17:05:57 +08:00
|
|
|
void Actor::DoSerialize(Serializer* serializer) const
|
|
|
|
|
{
|
|
|
|
|
ObjectBase::DoSerialize(serializer);
|
|
|
|
|
(*serializer) << visible_ << update_pausing_ << cascade_opacity_ << responsible_ << z_order_ << opacity_ << anchor_
|
|
|
|
|
<< size_ << transform_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::DoDeserialize(Deserializer* deserializer)
|
|
|
|
|
{
|
|
|
|
|
ObjectBase::DoDeserialize(deserializer);
|
2020-04-16 08:56:00 +08:00
|
|
|
|
|
|
|
|
float opacity = 1.0f;
|
|
|
|
|
Transform transform;
|
|
|
|
|
(*deserializer) >> visible_ >> update_pausing_ >> cascade_opacity_ >> responsible_ >> z_order_ >> opacity >> anchor_
|
|
|
|
|
>> size_ >> transform;
|
|
|
|
|
|
|
|
|
|
SetOpacity(opacity);
|
|
|
|
|
SetTransform(transform);
|
2020-04-15 17:05:57 +08:00
|
|
|
}
|
|
|
|
|
|
2020-02-18 12:53:18 +08:00
|
|
|
bool Actor::HandleEvent(Event* evt)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-18 12:53:18 +08:00
|
|
|
if (!components_.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
ComponentPtr next;
|
|
|
|
|
for (auto component = components_.GetFirst(); component; component = next)
|
|
|
|
|
{
|
|
|
|
|
next = component->GetNext();
|
|
|
|
|
|
2020-02-19 11:50:05 +08:00
|
|
|
if (component->IsEnable())
|
|
|
|
|
{
|
|
|
|
|
component->HandleEvent(evt);
|
|
|
|
|
}
|
2020-02-18 12:53:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!EventDispatcher::DispatchEvent(evt))
|
|
|
|
|
return false;
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
if (responsible_)
|
|
|
|
|
{
|
|
|
|
|
if (evt->IsType<MouseMoveEvent>())
|
|
|
|
|
{
|
|
|
|
|
auto mouse_evt = dynamic_cast<MouseMoveEvent*>(evt);
|
|
|
|
|
bool contains = ContainsPoint(mouse_evt->pos);
|
|
|
|
|
if (!hover_ && contains)
|
|
|
|
|
{
|
|
|
|
|
hover_ = true;
|
|
|
|
|
|
2020-02-20 22:27:09 +08:00
|
|
|
MouseHoverEventPtr hover = memory::New<MouseHoverEvent>();
|
2020-01-21 10:09:55 +08:00
|
|
|
hover->pos = mouse_evt->pos;
|
2020-02-18 12:53:18 +08:00
|
|
|
HandleEvent(hover.Get());
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
else if (hover_ && !contains)
|
|
|
|
|
{
|
|
|
|
|
hover_ = false;
|
|
|
|
|
pressed_ = false;
|
|
|
|
|
|
2020-02-20 22:27:09 +08:00
|
|
|
MouseOutEventPtr out = memory::New<MouseOutEvent>();
|
2020-01-21 10:09:55 +08:00
|
|
|
out->pos = mouse_evt->pos;
|
2020-02-18 12:53:18 +08:00
|
|
|
HandleEvent(out.Get());
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (evt->IsType<MouseDownEvent>() && hover_)
|
|
|
|
|
{
|
|
|
|
|
pressed_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (evt->IsType<MouseUpEvent>() && pressed_)
|
|
|
|
|
{
|
|
|
|
|
pressed_ = false;
|
|
|
|
|
|
|
|
|
|
auto mouse_up_evt = dynamic_cast<MouseUpEvent*>(evt);
|
|
|
|
|
|
2020-02-20 22:27:09 +08:00
|
|
|
MouseClickEventPtr click = memory::New<MouseClickEvent>();
|
2020-01-21 10:09:55 +08:00
|
|
|
click->pos = mouse_up_evt->pos;
|
|
|
|
|
click->button = mouse_up_evt->button;
|
2020-02-18 12:53:18 +08:00
|
|
|
HandleEvent(click.Get());
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-18 12:53:18 +08:00
|
|
|
return true;
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
2020-02-19 11:50:05 +08:00
|
|
|
void Actor::UpdateComponents(Duration dt)
|
|
|
|
|
{
|
|
|
|
|
if (!components_.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
ComponentPtr next;
|
|
|
|
|
for (auto component = components_.GetFirst(); component; component = next)
|
|
|
|
|
{
|
|
|
|
|
next = component->GetNext();
|
|
|
|
|
|
|
|
|
|
if (component->IsEnable())
|
|
|
|
|
{
|
|
|
|
|
component->OnUpdate(dt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-23 14:56:14 +08:00
|
|
|
void Actor::RenderComponents(RenderContext& ctx)
|
|
|
|
|
{
|
|
|
|
|
if (!components_.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
ComponentPtr next;
|
|
|
|
|
for (auto component = components_.GetFirst(); component; component = next)
|
|
|
|
|
{
|
|
|
|
|
next = component->GetNext();
|
|
|
|
|
|
|
|
|
|
if (component->IsEnable())
|
|
|
|
|
{
|
|
|
|
|
component->OnRender(ctx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 12:06:29 +08:00
|
|
|
const Matrix3x2& Actor::GetTransformMatrix() const
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
UpdateTransform();
|
|
|
|
|
return transform_matrix_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 12:06:29 +08:00
|
|
|
const Matrix3x2& Actor::GetTransformInverseMatrix() const
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
UpdateTransform();
|
|
|
|
|
if (dirty_transform_inverse_)
|
|
|
|
|
{
|
|
|
|
|
transform_matrix_inverse_ = transform_matrix_.Invert();
|
|
|
|
|
dirty_transform_inverse_ = false;
|
|
|
|
|
}
|
|
|
|
|
return transform_matrix_inverse_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-22 17:24:31 +08:00
|
|
|
const Matrix3x2& Actor::GetTransformMatrixToParent() const
|
|
|
|
|
{
|
|
|
|
|
UpdateTransform();
|
|
|
|
|
return transform_matrix_to_parent_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
void Actor::UpdateTransform() const
|
|
|
|
|
{
|
|
|
|
|
if (!dirty_transform_)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
dirty_transform_ = false;
|
|
|
|
|
dirty_transform_inverse_ = true;
|
|
|
|
|
dirty_visibility_ = true;
|
|
|
|
|
|
|
|
|
|
if (is_fast_transform_)
|
|
|
|
|
{
|
2020-02-22 17:24:31 +08:00
|
|
|
transform_matrix_to_parent_ = Matrix3x2::Translation(transform_.position);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// matrix multiplication is optimized by expression template
|
2020-02-22 17:24:31 +08:00
|
|
|
transform_matrix_to_parent_ = transform_.ToMatrix();
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
2020-02-22 17:24:31 +08:00
|
|
|
Point anchor_offset(-size_.x * anchor_.x, -size_.y * anchor_.y);
|
|
|
|
|
transform_matrix_to_parent_.Translate(anchor_offset);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
2020-02-22 17:24:31 +08:00
|
|
|
transform_matrix_ = transform_matrix_to_parent_;
|
2020-01-21 10:09:55 +08:00
|
|
|
if (parent_)
|
|
|
|
|
{
|
|
|
|
|
transform_matrix_ *= parent_->transform_matrix_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update children's transform
|
2020-02-15 17:32:32 +08:00
|
|
|
for (const auto& child : children_)
|
2020-02-19 11:50:05 +08:00
|
|
|
child->dirty_transform_ = true;
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::UpdateOpacity()
|
|
|
|
|
{
|
|
|
|
|
if (parent_ && parent_->IsCascadeOpacityEnabled())
|
|
|
|
|
{
|
|
|
|
|
displayed_opacity_ = opacity_ * parent_->displayed_opacity_;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
displayed_opacity_ = opacity_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
for (auto& child : children_)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-19 11:50:05 +08:00
|
|
|
child->UpdateOpacity();
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::SetStage(Stage* stage)
|
|
|
|
|
{
|
|
|
|
|
if (stage_ != stage)
|
|
|
|
|
{
|
|
|
|
|
stage_ = stage;
|
2020-02-15 17:32:32 +08:00
|
|
|
for (auto& child : children_)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-19 11:50:05 +08:00
|
|
|
child->stage_ = stage;
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::Reorder()
|
|
|
|
|
{
|
|
|
|
|
if (parent_)
|
|
|
|
|
{
|
|
|
|
|
ActorPtr me = this;
|
|
|
|
|
|
2020-02-19 11:50:05 +08:00
|
|
|
parent_->children_.Remove(me);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
ActorPtr sibling = parent_->children_.GetLast();
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
if (sibling && sibling->GetZOrder() > z_order_)
|
|
|
|
|
{
|
2020-02-15 17:32:32 +08:00
|
|
|
sibling = sibling->GetPrev();
|
2020-01-21 10:09:55 +08:00
|
|
|
while (sibling)
|
|
|
|
|
{
|
|
|
|
|
if (sibling->GetZOrder() <= z_order_)
|
|
|
|
|
break;
|
2020-02-15 17:32:32 +08:00
|
|
|
sibling = sibling->GetPrev();
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sibling)
|
|
|
|
|
{
|
2020-02-19 11:50:05 +08:00
|
|
|
parent_->children_.InsertAfter(me, sibling);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-02-19 11:50:05 +08:00
|
|
|
parent_->children_.PushFront(me);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::SetZOrder(int zorder)
|
|
|
|
|
{
|
|
|
|
|
if (z_order_ != zorder)
|
|
|
|
|
{
|
|
|
|
|
z_order_ = zorder;
|
|
|
|
|
Reorder();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::SetOpacity(float opacity)
|
|
|
|
|
{
|
|
|
|
|
if (opacity_ == opacity)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
displayed_opacity_ = opacity_ = std::min(std::max(opacity, 0.f), 1.f);
|
|
|
|
|
UpdateOpacity();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::SetCascadeOpacityEnabled(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
if (cascade_opacity_ == enabled)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
cascade_opacity_ = enabled;
|
|
|
|
|
UpdateOpacity();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
void Actor::SetAnchor(const Vec2& anchor)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
if (anchor_ == anchor)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
anchor_ = anchor;
|
|
|
|
|
dirty_transform_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
void Actor::SetSize(const Size& size)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
if (size_ == size)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
size_ = size;
|
|
|
|
|
dirty_transform_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
void Actor::SetTransform(const Transform& transform)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
transform_ = transform;
|
|
|
|
|
dirty_transform_ = true;
|
|
|
|
|
is_fast_transform_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::SetVisible(bool val)
|
|
|
|
|
{
|
|
|
|
|
visible_ = val;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
void Actor::SetName(const String& name)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
if (!IsName(name))
|
|
|
|
|
{
|
|
|
|
|
ObjectBase::SetName(name);
|
|
|
|
|
hash_name_ = std::hash<String>{}(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::SetPosition(const Point& pos)
|
|
|
|
|
{
|
|
|
|
|
if (transform_.position == pos)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
transform_.position = pos;
|
|
|
|
|
dirty_transform_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
void Actor::SetScale(const Vec2& scale)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
if (transform_.scale == scale)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
transform_.scale = scale;
|
|
|
|
|
dirty_transform_ = true;
|
|
|
|
|
is_fast_transform_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
void Actor::SetSkew(const Vec2& skew)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
if (transform_.skew == skew)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
transform_.skew = skew;
|
|
|
|
|
dirty_transform_ = true;
|
|
|
|
|
is_fast_transform_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::SetRotation(float angle)
|
|
|
|
|
{
|
|
|
|
|
if (transform_.rotation == angle)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
transform_.rotation = angle;
|
|
|
|
|
dirty_transform_ = true;
|
|
|
|
|
is_fast_transform_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 11:50:05 +08:00
|
|
|
void Actor::AddChild(ActorPtr child, int zorder)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
KGE_ASSERT(child && "Actor::AddChild failed, NULL pointer exception");
|
|
|
|
|
|
|
|
|
|
if (child)
|
|
|
|
|
{
|
|
|
|
|
KGE_ASSERT(!child->parent_ && "Actor::AddChild failed, the actor to be added already has a parent");
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-10-21 11:15:22 +08:00
|
|
|
#ifdef KGE_DEBUG
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
for (Actor* parent = parent_; parent; parent = parent->parent_)
|
|
|
|
|
{
|
|
|
|
|
if (parent == child)
|
|
|
|
|
{
|
2020-02-10 13:47:00 +08:00
|
|
|
KGE_ERROR("A actor cannot be its own parent");
|
2020-01-21 10:09:55 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // KGE_DEBUG
|
|
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
children_.PushBack(child);
|
2020-01-21 10:09:55 +08:00
|
|
|
child->parent_ = this;
|
|
|
|
|
child->SetStage(this->stage_);
|
|
|
|
|
|
|
|
|
|
child->dirty_transform_ = true;
|
|
|
|
|
child->z_order_ = zorder;
|
|
|
|
|
child->Reorder();
|
|
|
|
|
child->UpdateOpacity();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
void Actor::AddChildren(const Vector<ActorPtr>& children)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
for (const auto& actor : children)
|
|
|
|
|
{
|
|
|
|
|
this->AddChild(actor);
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
Rect Actor::GetBounds() const
|
|
|
|
|
{
|
|
|
|
|
return Rect{ Point{}, size_ };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rect Actor::GetBoundingBox() const
|
|
|
|
|
{
|
|
|
|
|
return GetTransformMatrix().Transform(GetBounds());
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
Vector<ActorPtr> Actor::GetChildren(const String& name) const
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
Vector<ActorPtr> children;
|
|
|
|
|
size_t hash_code = std::hash<String>{}(name);
|
|
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
for (const auto& child : children_)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-19 11:50:05 +08:00
|
|
|
if (child->hash_name_ == hash_code && child->IsName(name))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-19 11:50:05 +08:00
|
|
|
children.push_back(child);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return children;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
ActorPtr Actor::GetChild(const String& name) const
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
size_t hash_code = std::hash<String>{}(name);
|
|
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
for (const auto& child : children_)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-19 11:50:05 +08:00
|
|
|
if (child->hash_name_ == hash_code && child->IsName(name))
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-19 11:50:05 +08:00
|
|
|
return child;
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 12:53:18 +08:00
|
|
|
ActorList& Actor::GetAllChildren()
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
return children_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
const ActorList& Actor::GetAllChildren() const
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
return children_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::RemoveFromParent()
|
|
|
|
|
{
|
|
|
|
|
if (parent_)
|
|
|
|
|
{
|
|
|
|
|
parent_->RemoveChild(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 12:53:18 +08:00
|
|
|
Component* Actor::AddComponent(ComponentPtr component)
|
|
|
|
|
{
|
|
|
|
|
KGE_ASSERT(component && "AddComponent failed, NULL pointer exception");
|
|
|
|
|
|
|
|
|
|
if (component)
|
|
|
|
|
{
|
2020-02-19 11:50:05 +08:00
|
|
|
component->InitComponent(this);
|
2020-02-18 12:53:18 +08:00
|
|
|
components_.PushBack(component);
|
|
|
|
|
}
|
2020-02-19 11:50:05 +08:00
|
|
|
return component.Get();
|
2020-02-18 12:53:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ComponentList& Actor::GetAllComponents()
|
|
|
|
|
{
|
|
|
|
|
return components_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ComponentList& Actor::GetAllComponents() const
|
|
|
|
|
{
|
|
|
|
|
return components_;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 11:50:05 +08:00
|
|
|
void Actor::RemoveComponent(ComponentPtr component)
|
|
|
|
|
{
|
|
|
|
|
auto iter = std::find(components_.begin(), components_.end(), component);
|
|
|
|
|
if (iter != components_.end())
|
|
|
|
|
{
|
|
|
|
|
component->DestroyComponent();
|
|
|
|
|
components_.Remove(component);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
void Actor::RemoveComponents(const String& name)
|
2020-02-18 12:53:18 +08:00
|
|
|
{
|
|
|
|
|
if (!components_.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
ComponentPtr next;
|
|
|
|
|
for (auto component = components_.GetFirst(); component; component = next)
|
|
|
|
|
{
|
|
|
|
|
next = component->GetNext();
|
|
|
|
|
|
|
|
|
|
if (component->IsName(name))
|
|
|
|
|
{
|
2020-02-19 11:50:05 +08:00
|
|
|
component->DestroyComponent();
|
2020-02-18 12:53:18 +08:00
|
|
|
components_.Remove(component);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::RemoveAllComponents()
|
|
|
|
|
{
|
2020-02-19 11:50:05 +08:00
|
|
|
// Destroy all components
|
|
|
|
|
if (!components_.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
ComponentPtr next;
|
|
|
|
|
for (auto component = components_.GetFirst(); component; component = next)
|
|
|
|
|
{
|
|
|
|
|
next = component->GetNext();
|
|
|
|
|
|
|
|
|
|
component->DestroyComponent();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-18 12:53:18 +08:00
|
|
|
components_.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
void Actor::RemoveChild(ActorPtr child)
|
|
|
|
|
{
|
|
|
|
|
KGE_ASSERT(child && "Actor::RemoveChild failed, NULL pointer exception");
|
|
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
if (children_.IsEmpty())
|
2020-01-21 10:09:55 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (child)
|
|
|
|
|
{
|
|
|
|
|
child->parent_ = nullptr;
|
|
|
|
|
if (child->stage_)
|
|
|
|
|
child->SetStage(nullptr);
|
2020-02-15 17:32:32 +08:00
|
|
|
children_.Remove(child);
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
void Actor::RemoveChildren(const String& child_name)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-15 17:32:32 +08:00
|
|
|
if (children_.IsEmpty())
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t hash_code = std::hash<String>{}(child_name);
|
|
|
|
|
|
2020-02-15 17:32:32 +08:00
|
|
|
ActorPtr next;
|
|
|
|
|
for (ActorPtr child = children_.GetFirst(); child; child = next)
|
2020-01-21 10:09:55 +08:00
|
|
|
{
|
2020-02-15 17:32:32 +08:00
|
|
|
next = child->GetNext();
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
if (child->hash_name_ == hash_code && child->IsName(child_name))
|
|
|
|
|
{
|
|
|
|
|
RemoveChild(child);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::RemoveAllChildren()
|
|
|
|
|
{
|
2020-02-15 17:32:32 +08:00
|
|
|
children_.Clear();
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Actor::SetResponsible(bool enable)
|
|
|
|
|
{
|
|
|
|
|
responsible_ = enable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Actor::ContainsPoint(const Point& point) const
|
|
|
|
|
{
|
|
|
|
|
if (size_.x == 0.f || size_.y == 0.f)
|
|
|
|
|
return false;
|
|
|
|
|
|
2020-02-22 17:24:31 +08:00
|
|
|
Point local = ConvertToLocal(point);
|
|
|
|
|
return local.x >= 0 && local.y >= 0 && local.x <= size_.x && local.y <= size_.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Point Actor::ConvertToLocal(const Point& point) const
|
|
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
Point local = GetTransformInverseMatrix().Transform(point);
|
2020-02-22 17:24:31 +08:00
|
|
|
return local;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Point Actor::ConvertToWorld(const Point& point) const
|
|
|
|
|
{
|
|
|
|
|
Point world = GetTransformMatrix().Transform(point);
|
|
|
|
|
return world;
|
2020-01-21 10:09:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace kiwano
|