Magic_Game/src/kiwano/2d/Actor.cpp

605 lines
12 KiB
C++
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 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.
2019-10-11 21:55:29 +08:00
#include <kiwano/2d/Actor.h>
#include <kiwano/2d/Stage.h>
#include <kiwano/base/Logger.h>
#include <kiwano/renderer/Renderer.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
namespace
{
2019-09-29 22:23:13 +08:00
float default_anchor_x = 0.f;
float default_anchor_y = 0.f;
}
2019-09-29 22:23:13 +08:00
void Actor::SetDefaultAnchor(float anchor_x, float anchor_y)
{
default_anchor_x = anchor_x;
default_anchor_y = anchor_y;
}
Actor::Actor()
: visible_(true)
2019-08-27 15:29:32 +08:00
, visible_in_rt_(true)
, update_pausing_(false)
, hover_(false)
, pressed_(false)
, responsible_(false)
2019-08-27 15:29:32 +08:00
, dirty_visibility_(true)
, dirty_transform_(false)
, dirty_transform_inverse_(false)
2019-07-29 13:42:13 +08:00
, cascade_opacity_(false)
, show_border_(false)
, is_fast_transform_(true)
, parent_(nullptr)
, stage_(nullptr)
, hash_name_(0)
, z_order_(0)
, opacity_(1.f)
2019-07-29 13:42:13 +08:00
, displayed_opacity_(1.f)
, anchor_(default_anchor_x, default_anchor_y)
{
}
void Actor::Update(Duration dt)
{
if (!update_pausing_)
{
UpdateActions(this, dt);
UpdateTimers(dt);
if (cb_update_)
cb_update_(dt);
OnUpdate(dt);
}
2019-08-20 19:32:36 +08:00
if (!children_.empty())
{
ActorPtr next;
2019-08-13 21:16:38 +08:00
for (auto child = children_.first_item(); child; child = next)
{
2019-08-13 21:16:38 +08:00
next = child->next_item();
child->Update(dt);
}
}
}
2019-08-20 19:32:36 +08:00
void Actor::Render(RenderTarget* rt)
{
if (!visible_)
return;
UpdateTransform();
2019-08-20 19:32:36 +08:00
if (children_.empty())
{
2019-08-20 19:32:36 +08:00
OnRender(rt);
}
else
{
// render children those are less than 0 in Z-Order
2019-08-13 21:16:38 +08:00
Actor* child = children_.first_item().get();
while (child)
{
if (child->GetZOrder() >= 0)
break;
2019-08-20 19:32:36 +08:00
child->Render(rt);
2019-08-13 21:16:38 +08:00
child = child->next_item().get();
}
2019-08-20 19:32:36 +08:00
OnRender(rt);
while (child)
{
2019-08-20 19:32:36 +08:00
child->Render(rt);
2019-08-13 21:16:38 +08:00
child = child->next_item().get();
}
}
}
2019-08-20 19:32:36 +08:00
void Actor::PrepareRender(RenderTarget* rt)
2019-08-14 00:28:25 +08:00
{
2019-08-20 19:32:36 +08:00
rt->SetTransform(transform_matrix_);
rt->SetOpacity(displayed_opacity_);
2019-08-14 00:28:25 +08:00
}
2019-08-20 19:32:36 +08:00
void Actor::RenderBorder(RenderTarget* rt)
{
2019-08-20 19:32:36 +08:00
if (show_border_ && !size_.IsOrigin())
{
2019-09-09 16:20:40 +08:00
Rect bounds = GetBounds();
2019-08-05 09:07:58 +08:00
2019-08-20 19:32:36 +08:00
rt->SetTransform(transform_matrix_);
2019-09-09 22:02:53 +08:00
rt->SetDefaultBrushColor(Color(Color::Red, .4f));
rt->FillRectangle(bounds);
rt->SetDefaultBrushColor(Color(Color::Red, .8f));
rt->DrawRectangle(bounds, 2.f);
}
2019-08-13 21:16:38 +08:00
for (auto child = children_.first_item(); child; child = child->next_item())
{
2019-08-20 19:32:36 +08:00
child->RenderBorder(rt);
}
}
2019-08-27 15:29:32 +08:00
bool Actor::CheckVisibilty(RenderTarget* rt) const
{
if (dirty_visibility_)
{
dirty_visibility_ = false;
visible_in_rt_ = rt->CheckVisibility(GetBounds(), GetTransformMatrix());
}
return visible_in_rt_;
}
void Actor::Dispatch(Event& evt)
{
if (!visible_)
return;
ActorPtr prev;
2019-08-13 21:16:38 +08:00
for (auto child = children_.last_item(); child; child = prev)
{
2019-08-13 21:16:38 +08:00
prev = child->prev_item();
child->Dispatch(evt);
}
if (responsible_ && MouseEvent::Check(evt.type))
{
if (evt.type == Event::MouseMove)
{
if (!evt.target && ContainsPoint(Point{ evt.mouse.x, evt.mouse.y }))
{
evt.target = this;
if (!hover_)
{
hover_ = true;
Event hover = evt;
hover.type = Event::MouseHover;
EventDispatcher::Dispatch(hover);
}
}
else if (hover_)
{
hover_ = false;
pressed_ = false;
Event out = evt;
out.target = this;
out.type = Event::MouseOut;
EventDispatcher::Dispatch(out);
}
}
if (evt.type == Event::MouseBtnDown && hover_)
{
pressed_ = true;
evt.target = this;
}
if (evt.type == Event::MouseBtnUp && pressed_)
{
pressed_ = false;
evt.target = this;
Event click = evt;
click.type = Event::Click;
EventDispatcher::Dispatch(click);
}
}
EventDispatcher::Dispatch(evt);
}
2019-08-14 23:36:29 +08:00
Matrix3x2 const & Actor::GetTransformMatrix() const
{
UpdateTransform();
return transform_matrix_;
}
2019-08-14 23:36:29 +08:00
Matrix3x2 const & Actor::GetTransformInverseMatrix() const
{
UpdateTransform();
if (dirty_transform_inverse_)
{
2019-07-29 12:58:17 +08:00
transform_matrix_inverse_ = transform_matrix_.Invert();
dirty_transform_inverse_ = false;
}
return transform_matrix_inverse_;
}
void Actor::UpdateTransform() const
{
if (!dirty_transform_)
return;
dirty_transform_ = false;
dirty_transform_inverse_ = true;
2019-08-27 15:29:32 +08:00
dirty_visibility_ = true;
2019-07-29 12:58:17 +08:00
if (is_fast_transform_)
{
2019-08-14 23:36:29 +08:00
transform_matrix_ = Matrix3x2::Translation(transform_.position);
2019-07-29 12:58:17 +08:00
}
else
{
// matrix multiplication is optimized by expression template
2019-08-20 19:32:36 +08:00
transform_matrix_ = transform_.ToMatrix();
2019-07-29 12:58:17 +08:00
}
2019-07-30 10:46:01 +08:00
transform_matrix_.Translate(Point{ -size_.x * anchor_.x, -size_.y * anchor_.y });
if (parent_)
2019-07-29 12:58:17 +08:00
{
transform_matrix_ *= parent_->transform_matrix_;
2019-07-29 12:58:17 +08:00
}
// update children's transform
2019-08-13 21:16:38 +08:00
for (Actor* child = children_.first_item().get(); child; child = child->next_item().get())
child->dirty_transform_ = true;
}
void Actor::UpdateOpacity()
{
2019-07-29 13:42:13 +08:00
if (parent_ && parent_->IsCascadeOpacityEnabled())
{
displayed_opacity_ = opacity_ * parent_->displayed_opacity_;
}
else
{
2019-07-29 13:42:13 +08:00
displayed_opacity_ = opacity_;
}
2019-07-29 13:42:13 +08:00
2019-08-13 21:16:38 +08:00
for (Actor* child = children_.first_item().get(); child; child = child->next_item().get())
{
child->UpdateOpacity();
}
}
2019-08-20 19:32:36 +08:00
void Actor::SetStage(Stage* stage)
{
2019-08-20 19:32:36 +08:00
if (stage && stage_ != stage)
{
2019-08-20 19:32:36 +08:00
stage_ = stage;
2019-08-13 21:16:38 +08:00
for (Actor* child = children_.first_item().get(); child; child = child->next_item().get())
{
2019-08-20 19:32:36 +08:00
child->stage_ = stage;
}
}
}
void Actor::Reorder()
{
if (parent_)
{
ActorPtr me = this;
2019-08-20 19:32:36 +08:00
parent_->children_.remove(me);
2019-08-13 21:16:38 +08:00
Actor* sibling = parent_->children_.last_item().get();
2019-06-06 12:56:38 +08:00
if (sibling && sibling->GetZOrder() > z_order_)
{
2019-08-13 21:16:38 +08:00
sibling = sibling->prev_item().get();
while (sibling)
{
2019-06-06 12:56:38 +08:00
if (sibling->GetZOrder() <= z_order_)
break;
2019-08-13 21:16:38 +08:00
sibling = sibling->prev_item().get();
}
}
if (sibling)
{
2019-08-13 21:16:38 +08:00
parent_->children_.insert_after(me, sibling);
}
else
{
2019-08-20 19:32:36 +08:00
parent_->children_.push_front(me);
}
}
}
2019-09-29 22:23:13 +08:00
void Actor::SetZOrder(int zorder)
2019-06-06 12:56:38 +08:00
{
if (z_order_ != zorder)
{
z_order_ = zorder;
Reorder();
}
}
2019-09-29 22:23:13 +08:00
void Actor::SetOpacity(float opacity)
{
if (opacity_ == opacity)
return;
2019-07-29 13:42:13 +08:00
displayed_opacity_ = opacity_ = std::min(std::max(opacity, 0.f), 1.f);
UpdateOpacity();
}
void Actor::SetCascadeOpacityEnabled(bool enabled)
2019-07-29 13:42:13 +08:00
{
if (cascade_opacity_ == enabled)
return;
cascade_opacity_ = enabled;
UpdateOpacity();
}
2019-08-20 19:32:36 +08:00
void Actor::SetAnchor(Vec2 const& anchor)
{
2019-08-20 19:32:36 +08:00
if (anchor_ == anchor)
return;
2019-08-20 19:32:36 +08:00
anchor_ = anchor;
dirty_transform_ = true;
}
2019-09-29 22:23:13 +08:00
void Actor::SetWidth(float width)
{
2019-08-20 19:32:36 +08:00
SetSize(Size{ width, size_.y });
}
2019-09-29 22:23:13 +08:00
void Actor::SetHeight(float height)
{
2019-08-20 19:32:36 +08:00
SetSize(Size{ size_.x, height });
}
2019-08-20 19:32:36 +08:00
void Actor::SetSize(Size const& size)
{
2019-08-20 19:32:36 +08:00
if (size_ == size)
return;
2019-08-20 19:32:36 +08:00
size_ = size;
dirty_transform_ = true;
}
void Actor::SetTransform(Transform const& transform)
{
transform_ = transform;
dirty_transform_ = true;
2019-07-29 12:58:17 +08:00
is_fast_transform_ = false;
}
void Actor::SetVisible(bool val)
{
visible_ = val;
}
void Actor::SetName(String const& name)
{
if (!IsName(name))
{
2019-08-18 10:23:54 +08:00
ObjectBase::SetName(name);
hash_name_ = std::hash<String>{}(name);
}
}
2019-08-20 19:32:36 +08:00
void Actor::SetPosition(const Point & pos)
{
2019-08-20 19:32:36 +08:00
if (transform_.position == pos)
return;
2019-08-20 19:32:36 +08:00
transform_.position = pos;
dirty_transform_ = true;
}
2019-09-29 22:23:13 +08:00
void Actor::SetPositionX(float x)
{
2019-08-20 19:32:36 +08:00
SetPosition(Point{ x, transform_.position.y });
}
2019-09-29 22:23:13 +08:00
void Actor::SetPositionY(float y)
{
2019-08-20 19:32:36 +08:00
SetPosition(Point{ transform_.position.x, y });
}
2019-08-20 19:32:36 +08:00
void Actor::Move(Vec2 const& v)
{
2019-08-20 19:32:36 +08:00
this->SetPosition(Point{ transform_.position.x + v.x, transform_.position.y + v.y });
}
2019-08-20 19:32:36 +08:00
void Actor::SetScale(Vec2 const& scale)
{
2019-08-20 19:32:36 +08:00
if (transform_.scale == scale)
return;
2019-08-20 19:32:36 +08:00
transform_.scale = scale;
dirty_transform_ = true;
2019-07-29 12:58:17 +08:00
is_fast_transform_ = false;
}
2019-08-20 19:32:36 +08:00
void Actor::SetSkew(Vec2 const& skew)
{
2019-08-20 19:32:36 +08:00
if (transform_.skew == skew)
return;
2019-08-20 19:32:36 +08:00
transform_.skew = skew;
dirty_transform_ = true;
2019-07-29 12:58:17 +08:00
is_fast_transform_ = false;
}
2019-09-29 22:23:13 +08:00
void Actor::SetRotation(float angle)
{
if (transform_.rotation == angle)
return;
transform_.rotation = angle;
dirty_transform_ = true;
2019-07-29 12:58:17 +08:00
is_fast_transform_ = false;
}
void Actor::AddChild(ActorPtr child)
{
KGE_ASSERT(child && "Actor::AddChild failed, NULL pointer exception");
if (child)
{
2019-04-11 14:40:54 +08:00
#ifdef KGE_DEBUG
if (child->parent_)
2019-08-13 21:16:38 +08:00
KGE_ERROR_LOG(L"The actor to be added already has a parent");
for (Actor* parent = parent_; parent; parent = parent->parent_)
if (parent == child)
2019-08-13 21:16:38 +08:00
KGE_ERROR_LOG(L"A actor cannot be its own parent");
2019-04-11 14:40:54 +08:00
#endif // KGE_DEBUG
2019-08-20 19:32:36 +08:00
children_.push_back(child);
child->parent_ = this;
child->SetStage(this->stage_);
child->dirty_transform_ = true;
child->UpdateOpacity();
2019-06-06 12:56:38 +08:00
child->Reorder();
}
}
2019-08-13 21:16:38 +08:00
void Actor::AddChildren(Vector<ActorPtr> const& children)
{
2019-08-13 21:16:38 +08:00
for (const auto& actor : children)
{
2019-08-13 21:16:38 +08:00
this->AddChild(actor);
}
}
Rect Actor::GetBounds() const
{
2019-08-20 19:32:36 +08:00
return Rect{ Point{}, size_ };
}
Rect Actor::GetBoundingBox() const
{
return GetTransformMatrix().Transform(GetBounds());
}
2019-08-13 21:16:38 +08:00
Vector<ActorPtr> Actor::GetChildren(String const& name) const
{
2019-08-13 21:16:38 +08:00
Vector<ActorPtr> children;
2019-10-12 11:26:41 +08:00
size_t hash_code = std::hash<String>{}(name);
2019-08-13 21:16:38 +08:00
for (Actor* child = children_.first_item().get(); child; child = child->next_item().get())
{
if (child->hash_name_ == hash_code && child->IsName(name))
{
children.push_back(child);
}
}
return children;
}
ActorPtr Actor::GetChild(String const& name) const
{
2019-10-12 11:26:41 +08:00
size_t hash_code = std::hash<String>{}(name);
2019-08-13 21:16:38 +08:00
for (Actor* child = children_.first_item().get(); child; child = child->next_item().get())
{
if (child->hash_name_ == hash_code && child->IsName(name))
{
return child;
}
}
return nullptr;
}
Actor::Children const & Actor::GetChildren() const
{
return children_;
}
void Actor::RemoveFromParent()
{
if (parent_)
{
parent_->RemoveChild(this);
}
}
void Actor::RemoveChild(ActorPtr child)
{
2019-08-13 21:16:38 +08:00
RemoveChild(child.get());
}
void Actor::RemoveChild(Actor * child)
{
KGE_ASSERT(child && "Actor::RemoveChild failed, NULL pointer exception");
2019-08-20 19:32:36 +08:00
if (children_.empty())
return;
if (child)
{
child->parent_ = nullptr;
if (child->stage_) child->SetStage(nullptr);
2019-08-20 19:32:36 +08:00
children_.remove(ActorPtr(child));
}
}
void Actor::RemoveChildren(String const& child_name)
{
2019-08-20 19:32:36 +08:00
if (children_.empty())
{
return;
}
2019-10-12 11:26:41 +08:00
size_t hash_code = std::hash<String>{}(child_name);
Actor* next;
2019-08-13 21:16:38 +08:00
for (Actor* child = children_.first_item().get(); child; child = next)
{
2019-08-13 21:16:38 +08:00
next = child->next_item().get();
if (child->hash_name_ == hash_code && child->IsName(child_name))
{
RemoveChild(child);
}
}
}
void Actor::RemoveAllChildren()
{
2019-08-20 19:32:36 +08:00
children_.clear();
}
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;
Point local = GetTransformInverseMatrix().Transform(point);
return GetBounds().ContainsPoint(local);
}
}