Magic_Game/src/core/Node.cpp

611 lines
12 KiB
C++
Raw Normal View History

2018-10-03 22:02:46 +08:00
// Copyright (c) 2016-2018 Easy2D - 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.
#include "Node.h"
#include "Action.h"
2018-11-21 17:18:59 +08:00
#include "Factory.h"
#include "Scene.h"
#include "Task.h"
#include "render.h"
2018-11-15 17:59:18 +08:00
#include "logs.h"
2018-11-08 00:21:59 +08:00
namespace easy2d
{
2018-11-15 14:35:19 +08:00
namespace
{
2019-01-24 12:21:01 +08:00
float default_anchor_x = 0.f;
float default_anchor_y = 0.f;
2018-11-15 14:35:19 +08:00
}
2019-01-24 12:21:01 +08:00
void Node::SetDefaultAnchor(float anchor_x, float anchor_y)
2018-11-15 14:35:19 +08:00
{
2019-01-24 12:21:01 +08:00
default_anchor_x = anchor_x;
default_anchor_y = anchor_y;
2018-11-15 14:35:19 +08:00
}
2018-11-08 00:21:59 +08:00
Node::Node()
: visible_(true)
2019-01-24 12:21:01 +08:00
, pause_(false)
2019-01-26 13:09:58 +08:00
, hover_(false)
, pressed_(false)
, responsible_(false)
, dirty_transform_(false)
2018-11-25 15:00:15 +08:00
, dirty_transform_inverse_(false)
2018-11-08 00:21:59 +08:00
, parent_(nullptr)
, hash_name_(0)
2018-11-21 19:24:18 +08:00
, z_order_(0)
2018-11-18 20:26:41 +08:00
, opacity_(1.f)
2018-11-08 00:21:59 +08:00
, display_opacity_(1.f)
2019-01-24 12:21:01 +08:00
, anchor_(default_anchor_x, default_anchor_y)
2018-11-21 19:24:18 +08:00
{
}
2019-01-24 15:14:55 +08:00
void Node::Update(Duration dt)
2018-09-07 23:47:21 +08:00
{
2019-01-24 12:21:01 +08:00
if (pause_)
return;
2018-11-21 19:24:18 +08:00
UpdateActions(this, dt);
UpdateTasks(dt);
2019-02-11 19:07:31 +08:00
if (cb_update_)
cb_update_(dt);
OnUpdate(dt);
2018-11-21 19:24:18 +08:00
if (!children_.IsEmpty())
{
NodePtr next;
for (auto child = children_.First(); child; child = next)
2018-11-21 19:24:18 +08:00
{
next = child->NextItem();
2018-11-21 19:24:18 +08:00
child->Update(dt);
}
}
}
2018-11-21 19:24:18 +08:00
void Node::Render()
{
2018-11-08 00:21:59 +08:00
if (!visible_)
return;
2018-11-18 20:26:41 +08:00
UpdateTransform();
2019-01-24 12:21:01 +08:00
auto rt = RenderSystem::Instance();
2018-11-18 23:41:15 +08:00
2018-11-15 23:40:13 +08:00
if (children_.IsEmpty())
2018-01-30 16:45:38 +08:00
{
2019-01-24 12:21:01 +08:00
rt->SetTransform(transform_matrix_);
rt->SetOpacity(display_opacity_);
2018-11-21 19:24:18 +08:00
OnRender();
2018-11-08 00:21:59 +08:00
}
else
{
2018-11-21 19:24:18 +08:00
// render children those are less than 0 in Z-Order
Node* child = children_.First().Get();
while (child)
2018-01-30 16:45:38 +08:00
{
if (child->GetZOrder() >= 0)
2018-11-08 00:21:59 +08:00
break;
child->Render();
child = child->NextItem().Get();
2018-01-30 16:45:38 +08:00
}
2018-11-08 00:21:59 +08:00
2019-01-24 12:21:01 +08:00
rt->SetTransform(transform_matrix_);
rt->SetOpacity(display_opacity_);
2018-11-08 00:21:59 +08:00
2018-11-21 19:24:18 +08:00
OnRender();
2018-11-08 00:21:59 +08:00
while (child)
2018-11-15 23:40:13 +08:00
{
2018-11-21 19:24:18 +08:00
child->Render();
child = child->NextItem().Get();
2018-11-15 23:40:13 +08:00
}
2018-11-08 00:21:59 +08:00
}
2018-10-06 09:45:28 +08:00
}
2018-11-08 00:21:59 +08:00
2019-01-24 12:21:01 +08:00
void Node::Dispatch(Event& evt)
2018-11-22 19:31:44 +08:00
{
if (!visible_)
return;
NodePtr prev;
2018-11-22 19:31:44 +08:00
for (auto child = children_.Last(); child; child = prev)
{
prev = child->PrevItem();
2019-01-24 12:21:01 +08:00
child->Dispatch(evt);
2018-11-22 19:31:44 +08:00
}
2018-11-25 15:00:15 +08:00
2019-01-26 13:09:58 +08:00
if (responsible_ && MouseEvent::Check(evt.type))
2018-11-25 15:00:15 +08:00
{
2019-01-24 12:21:01 +08:00
if (evt.type == MouseEvent::Move)
2018-11-25 15:00:15 +08:00
{
2019-01-26 13:09:58 +08:00
if (!evt.target && ContainsPoint(Point{ evt.mouse.x, evt.mouse.y }))
2018-11-25 15:00:15 +08:00
{
2019-01-26 13:09:58 +08:00
evt.target = this;
2018-11-25 15:00:15 +08:00
if (!hover_)
{
hover_ = true;
2019-01-24 12:21:01 +08:00
Event hover = evt;
2018-11-25 15:00:15 +08:00
hover.type = MouseEvent::Hover;
2019-01-26 13:09:58 +08:00
EventDispatcher::Dispatch(hover);
2018-11-25 15:00:15 +08:00
}
}
else if (hover_)
{
hover_ = false;
pressed_ = false;
2019-01-24 12:21:01 +08:00
Event out = evt;
2019-01-26 13:09:58 +08:00
out.target = this;
2019-01-24 12:21:01 +08:00
out.type = MouseEvent::Out;
2019-01-26 13:09:58 +08:00
EventDispatcher::Dispatch(out);
2018-11-25 15:00:15 +08:00
}
}
2019-01-24 12:21:01 +08:00
if (evt.type == MouseEvent::Down && hover_)
2018-11-25 15:00:15 +08:00
{
pressed_ = true;
2019-01-26 13:09:58 +08:00
evt.target = this;
2018-11-25 15:00:15 +08:00
}
2019-01-24 12:21:01 +08:00
if (evt.type == MouseEvent::Up && pressed_)
2018-11-25 15:00:15 +08:00
{
pressed_ = false;
2019-01-26 13:09:58 +08:00
evt.target = this;
2018-11-25 15:00:15 +08:00
2019-01-24 12:21:01 +08:00
Event click = evt;
2018-11-25 15:00:15 +08:00
click.type = MouseEvent::Click;
2019-01-26 13:09:58 +08:00
EventDispatcher::Dispatch(click);
2018-11-25 15:00:15 +08:00
}
}
2019-01-24 12:21:01 +08:00
EventDispatcher::Dispatch(evt);
}
void Node::PauseUpdating()
{
pause_ = true;
}
void Node::ResumeUpdating()
{
pause_ = false;
2018-11-22 19:31:44 +08:00
}
2018-11-25 15:00:15 +08:00
Matrix const & Node::GetTransformMatrix() const
2018-11-20 01:20:06 +08:00
{
UpdateTransform();
return transform_matrix_;
2018-11-20 01:20:06 +08:00
}
2018-11-25 15:00:15 +08:00
Matrix const & Node::GetTransformInverseMatrix() const
{
UpdateTransform();
if (dirty_transform_inverse_)
{
transform_matrix_inverse_ = Matrix::Invert(transform_matrix_);
dirty_transform_inverse_ = false;
}
return transform_matrix_inverse_;
}
2018-11-25 19:29:32 +08:00
Node* Node::GetParent() const
2018-11-22 19:31:44 +08:00
{
return parent_;
}
2018-11-25 19:29:32 +08:00
Scene* Node::GetScene() const
2018-11-22 19:31:44 +08:00
{
return scene_;
}
2018-11-25 15:00:15 +08:00
void Node::UpdateTransform() const
2018-07-07 18:04:18 +08:00
{
2018-11-08 00:21:59 +08:00
if (!dirty_transform_)
return;
dirty_transform_ = false;
2018-11-25 15:00:15 +08:00
dirty_transform_inverse_ = true;
2018-11-08 00:21:59 +08:00
2019-02-11 19:07:31 +08:00
transform_matrix_ = transform_.ToMatrix();
2018-11-20 01:20:06 +08:00
2019-01-24 12:21:01 +08:00
Point offset{ -size_.x * anchor_.x, -size_.y * anchor_.y };
transform_matrix_.Translate(offset);
2018-11-08 00:21:59 +08:00
if (parent_)
transform_matrix_ = transform_matrix_ * parent_->transform_matrix_;
2018-07-07 18:04:18 +08:00
2018-11-21 19:24:18 +08:00
// update children's transform
for (Node* child = children_.First().Get(); child; child = child->NextItem().Get())
2018-11-08 00:21:59 +08:00
child->dirty_transform_ = true;
2018-11-20 01:20:06 +08:00
}
2018-11-22 19:31:44 +08:00
void Node::UpdateOpacity()
2018-11-08 00:21:59 +08:00
{
2018-11-22 19:31:44 +08:00
if (parent_)
2018-11-08 00:21:59 +08:00
{
2018-11-22 19:31:44 +08:00
display_opacity_ = opacity_ * parent_->display_opacity_;
2018-11-08 00:21:59 +08:00
}
for (Node* child = children_.First().Get(); child; child = child->NextItem().Get())
2018-11-08 00:21:59 +08:00
{
2018-11-22 19:31:44 +08:00
child->UpdateOpacity();
2018-11-08 00:21:59 +08:00
}
2018-09-11 00:37:52 +08:00
}
2018-11-08 00:21:59 +08:00
2018-11-22 19:31:44 +08:00
void Node::SetScene(Scene * scene)
2018-07-13 00:45:39 +08:00
{
2018-11-22 19:31:44 +08:00
scene_ = scene;
for (Node* child = children_.First().Get(); child; child = child->NextItem().Get())
2018-11-08 00:21:59 +08:00
{
2018-11-22 19:31:44 +08:00
child->scene_ = scene;
2018-11-08 00:21:59 +08:00
}
2018-07-13 00:45:39 +08:00
}
void Node::SetZOrder(int zorder)
2018-11-08 00:21:59 +08:00
{
z_order_ = zorder;
2018-11-08 00:21:59 +08:00
if (parent_)
{
NodePtr me = this;
parent_->children_.Remove(me);
Node* sibling = parent_->children_.Last().Get();
if (sibling && sibling->GetZOrder() > zorder)
{
2019-02-03 00:16:53 +08:00
sibling = sibling->PrevItem().Get();
while (sibling)
{
if (sibling->GetZOrder() <= zorder)
break;
sibling = sibling->PrevItem().Get();
}
}
if (sibling)
{
parent_->children_.InsertAfter(me, sibling);
}
else
{
parent_->children_.PushFront(me);
}
2018-11-08 00:21:59 +08:00
}
}
2018-11-08 00:21:59 +08:00
void Node::SetOpacity(float opacity)
{
2018-11-18 20:26:41 +08:00
if (opacity_ == opacity)
2018-11-08 00:21:59 +08:00
return;
2018-11-18 20:26:41 +08:00
display_opacity_ = opacity_ = std::min(std::max(opacity, 0.f), 1.f);
2018-11-08 00:21:59 +08:00
UpdateOpacity();
}
2019-01-24 12:21:01 +08:00
void Node::SetAnchorX(float anchor_x)
2018-11-08 00:21:59 +08:00
{
2019-01-24 12:21:01 +08:00
this->SetAnchor(anchor_x, anchor_.y);
2018-11-08 00:21:59 +08:00
}
2019-01-24 12:21:01 +08:00
void Node::SetAnchorY(float anchor_y)
2018-11-08 00:21:59 +08:00
{
2019-01-24 12:21:01 +08:00
this->SetAnchor(anchor_.x, anchor_y);
2018-11-08 00:21:59 +08:00
}
2017-10-15 02:46:24 +08:00
2019-01-24 12:21:01 +08:00
void Node::SetAnchor(float anchor_x, float anchor_y)
2018-11-08 00:21:59 +08:00
{
2019-01-24 12:21:01 +08:00
if (anchor_.x == anchor_x && anchor_.y == anchor_y)
2018-11-08 00:21:59 +08:00
return;
2017-10-15 02:46:24 +08:00
2019-01-24 12:21:01 +08:00
anchor_.x = anchor_x;
anchor_.y = anchor_y;
2018-11-08 00:21:59 +08:00
dirty_transform_ = true;
}
2017-10-15 02:46:24 +08:00
2018-11-08 00:21:59 +08:00
void Node::SetWidth(float width)
{
2018-11-25 15:00:15 +08:00
this->SetSize(width, size_.y);
2018-11-08 00:21:59 +08:00
}
2017-10-15 02:46:24 +08:00
2018-11-08 00:21:59 +08:00
void Node::SetHeight(float height)
{
2018-11-25 15:00:15 +08:00
this->SetSize(size_.x, height);
2018-11-08 00:21:59 +08:00
}
2018-11-08 00:21:59 +08:00
void Node::SetSize(const Size& size)
{
2018-11-25 15:00:15 +08:00
this->SetSize(size.x, size.y);
2018-11-08 00:21:59 +08:00
}
2018-11-20 01:20:06 +08:00
void Node::SetSize(float width, float height)
{
2018-11-25 15:00:15 +08:00
if (size_.x == width && size_.y == height)
2018-11-20 01:20:06 +08:00
return;
2018-11-25 15:00:15 +08:00
size_.x = width;
size_.y = height;
2018-11-20 01:20:06 +08:00
dirty_transform_ = true;
}
void Node::SetTransform(Transform const& transform)
2018-11-08 00:21:59 +08:00
{
transform_ = transform;
dirty_transform_ = true;
}
2018-11-21 19:24:18 +08:00
void Node::SetVisible(bool val)
{
visible_ = val;
}
2019-01-21 21:24:45 +08:00
void Node::SetName(String const& name)
2018-11-21 19:24:18 +08:00
{
2019-02-07 23:54:19 +08:00
if (!IsName(name))
2018-11-21 19:24:18 +08:00
{
2019-02-07 23:54:19 +08:00
Object::SetName(name);
2019-01-21 21:24:45 +08:00
hash_name_ = std::hash<String>{}(name);
2018-11-21 19:24:18 +08:00
}
}
void Node::SetPositionX(float x)
{
this->SetPosition(x, transform_.position.y);
}
void Node::SetPositionY(float y)
{
this->SetPosition(transform_.position.x, y);
}
void Node::SetPosition(const Point & p)
{
this->SetPosition(p.x, p.y);
}
void Node::SetPosition(float x, float y)
{
if (transform_.position.x == x && transform_.position.y == y)
return;
transform_.position.x = x;
transform_.position.y = y;
dirty_transform_ = true;
}
void Node::Move(float x, float y)
{
this->SetPosition(transform_.position.x + x, transform_.position.y + y);
}
void Node::Move(const Point & v)
{
this->Move(v.x, v.y);
}
void Node::SetScaleX(float scale_x)
{
this->SetScale(scale_x, transform_.scale.y);
}
void Node::SetScaleY(float scale_y)
{
this->SetScale(transform_.scale.x, scale_y);
}
void Node::SetScale(float scale)
{
this->SetScale(scale, scale);
}
void Node::SetScale(float scale_x, float scale_y)
{
if (transform_.scale.x == scale_x && transform_.scale.y == scale_y)
return;
transform_.scale.x = scale_x;
transform_.scale.y = scale_y;
dirty_transform_ = true;
}
void Node::SetSkewX(float skew_x)
{
this->SetSkew(skew_x, transform_.skew.y);
}
void Node::SetSkewY(float skew_y)
{
this->SetSkew(transform_.skew.x, skew_y);
}
void Node::SetSkew(float skew_x, float skew_y)
{
if (transform_.skew.x == skew_x && transform_.skew.y == skew_y)
return;
transform_.skew.x = skew_x;
transform_.skew.y = skew_y;
dirty_transform_ = true;
}
void Node::SetRotation(float angle)
{
if (transform_.rotation == angle)
return;
transform_.rotation = angle;
dirty_transform_ = true;
}
void Node::AddChild(NodePtr const& child)
2018-11-08 00:21:59 +08:00
{
2018-11-22 23:48:40 +08:00
E2D_ASSERT(child && "Node::AddChild failed, NULL pointer exception");
2018-03-01 19:28:22 +08:00
2018-11-08 00:21:59 +08:00
if (child)
{
2018-11-16 15:53:39 +08:00
#ifdef E2D_DEBUG
2018-11-21 19:24:18 +08:00
if (child->parent_)
E2D_ERROR_LOG(L"The node to be added already has a parent");
2018-11-21 19:24:18 +08:00
2018-11-16 15:53:39 +08:00
for (Node* parent = parent_; parent; parent = parent->parent_)
if (parent == child)
E2D_ERROR_LOG(L"A node cannot be its own parent");
2018-11-21 19:24:18 +08:00
2018-11-16 15:53:39 +08:00
#endif // E2D_DEBUG
2018-03-01 19:28:22 +08:00
children_.PushBack(child);
2018-11-08 00:21:59 +08:00
child->parent_ = this;
2018-11-22 19:31:44 +08:00
child->SetScene(this->scene_);
2018-11-08 00:21:59 +08:00
child->dirty_transform_ = true;
2018-11-16 15:53:39 +08:00
child->UpdateOpacity();
child->SetZOrder(child->GetZOrder());
2018-11-08 00:21:59 +08:00
}
}
void Node::AddChildren(Array<NodePtr> const& children)
2018-11-08 00:21:59 +08:00
{
for (const auto& node : children)
2018-11-08 00:21:59 +08:00
{
this->AddChild(node);
2018-11-08 00:21:59 +08:00
}
}
2018-11-25 15:00:15 +08:00
Rect Node::GetBounds() const
{
2018-11-20 01:20:06 +08:00
return Rect(Point{}, size_);
}
Rect Node::GetBoundingBox() const
{
return GetTransformMatrix().Transform(GetBounds());
}
Array<NodePtr> Node::GetChildren(String const& name) const
2017-10-14 11:40:47 +08:00
{
Array<NodePtr> children;
2019-01-21 21:24:45 +08:00
size_t hash_code = std::hash<String>{}(name);
2018-02-03 22:04:43 +08:00
for (Node* child = children_.First().Get(); child; child = child->NextItem().Get())
2017-10-14 11:40:47 +08:00
{
2019-02-07 23:54:19 +08:00
if (child->hash_name_ == hash_code && child->IsName(name))
2018-05-24 00:58:16 +08:00
{
2018-11-08 00:21:59 +08:00
children.push_back(child);
2018-05-24 00:58:16 +08:00
}
2017-10-14 11:40:47 +08:00
}
2018-11-08 00:21:59 +08:00
return children;
}
NodePtr Node::GetChild(String const& name) const
2018-11-08 00:21:59 +08:00
{
2019-01-21 21:24:45 +08:00
size_t hash_code = std::hash<String>{}(name);
2017-10-14 18:43:32 +08:00
for (Node* child = children_.First().Get(); child; child = child->NextItem().Get())
2017-10-17 21:22:25 +08:00
{
2019-02-07 23:54:19 +08:00
if (child->hash_name_ == hash_code && child->IsName(name))
2018-11-08 00:21:59 +08:00
{
return child;
}
2017-10-17 21:22:25 +08:00
}
2018-11-08 00:21:59 +08:00
return nullptr;
2017-10-14 11:40:47 +08:00
}
2018-11-15 23:40:13 +08:00
Node::Children const & Node::GetChildren() const
{
2018-11-08 00:21:59 +08:00
return children_;
}
2018-11-08 00:21:59 +08:00
void Node::RemoveFromParent()
2018-04-24 13:28:21 +08:00
{
2018-11-08 00:21:59 +08:00
if (parent_)
2018-04-24 13:28:21 +08:00
{
2018-11-08 00:21:59 +08:00
parent_->RemoveChild(this);
2018-04-24 13:28:21 +08:00
}
}
bool Node::RemoveChild(NodePtr const& child)
{
return RemoveChild(child.Get());
}
bool Node::RemoveChild(Node * child)
2018-11-08 00:21:59 +08:00
{
2018-11-22 23:48:40 +08:00
E2D_ASSERT(child && "Node::RemoveChild failed, NULL pointer exception");
2018-02-03 22:04:43 +08:00
2018-11-15 23:40:13 +08:00
if (children_.IsEmpty())
2018-11-08 00:21:59 +08:00
return false;
2018-02-03 22:04:43 +08:00
2018-11-08 00:21:59 +08:00
if (child)
{
2018-11-22 19:31:44 +08:00
child->parent_ = nullptr;
if (child->scene_) child->SetScene(nullptr);
children_.Remove(NodePtr(child));
2018-11-15 23:40:13 +08:00
return true;
2018-11-08 00:21:59 +08:00
}
return false;
2017-10-14 18:43:32 +08:00
}
2019-01-21 21:24:45 +08:00
void Node::RemoveChildren(String const& child_name)
2017-10-14 18:43:32 +08:00
{
2018-11-15 23:40:13 +08:00
if (children_.IsEmpty())
2017-10-14 18:43:32 +08:00
{
2018-11-08 00:21:59 +08:00
return;
}
2018-02-03 22:04:43 +08:00
2019-01-21 21:24:45 +08:00
size_t hash_code = std::hash<String>{}(child_name);
Node* next;
for (Node* child = children_.First().Get(); child; child = next)
2018-11-08 00:21:59 +08:00
{
next = child->NextItem().Get();
2018-11-15 23:40:13 +08:00
2019-02-07 23:54:19 +08:00
if (child->hash_name_ == hash_code && child->IsName(child_name))
2018-11-22 19:31:44 +08:00
{
RemoveChild(child);
}
2017-10-14 18:43:32 +08:00
}
}
2017-10-14 11:40:47 +08:00
2018-11-08 00:21:59 +08:00
void Node::RemoveAllChildren()
2017-10-14 18:43:32 +08:00
{
2018-11-15 23:40:13 +08:00
children_.Clear();
2017-10-14 18:43:32 +08:00
}
2019-01-26 13:09:58 +08:00
void Node::SetResponsible(bool enable)
{
responsible_ = enable;
}
2018-11-25 15:00:15 +08:00
bool Node::ContainsPoint(const Point& point) const
2018-11-08 00:21:59 +08:00
{
2018-11-25 15:00:15 +08:00
if (size_.x == 0.f || size_.y == 0.f)
2018-11-08 00:21:59 +08:00
return false;
2019-01-26 13:09:58 +08:00
Point local = GetTransformInverseMatrix().Transform(point);
2018-11-25 15:00:15 +08:00
return GetBounds().ContainsPoint(local);
2017-10-14 11:40:47 +08:00
}
2018-11-21 19:24:18 +08:00
}