Magic_Game/core/base/Node.cpp

597 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 "Scene.h"
#include "Task.h"
#include "Action.hpp"
#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
{
float default_pivot_x = 0.f;
float default_pivot_y = 0.f;
}
void Node::SetDefaultPivot(float pivot_x, float pivot_y)
{
default_pivot_x = pivot_x;
default_pivot_y = pivot_y;
}
2018-11-08 00:21:59 +08:00
Node::Node()
: visible_(true)
, parent_(nullptr)
, hash_name_(0)
, dirty_sort_(false)
, dirty_transform_(false)
, order_(0)
, transform_()
2018-11-18 20:26:41 +08:00
, opacity_(1.f)
2018-11-08 00:21:59 +08:00
, display_opacity_(1.f)
, children_()
, initial_matrix_()
, final_matrix_()
2018-11-08 00:21:59 +08:00
, border_color_(Color::Red, 0.6f)
2018-09-05 00:08:03 +08:00
{
2018-11-15 14:35:19 +08:00
transform_.pivot.x = default_pivot_x;
transform_.pivot.y = default_pivot_y;
2018-09-05 00:08:03 +08:00
}
2018-11-08 00:21:59 +08:00
Node::~Node()
2018-09-07 23:47:21 +08:00
{
}
2018-11-08 00:21:59 +08:00
void Node::Visit()
{
2018-11-08 00:21:59 +08:00
if (!visible_)
return;
2018-11-18 20:26:41 +08:00
UpdateTransform();
2018-11-18 23:41:15 +08:00
auto graphics = devices::Graphics::Instance();
2018-11-15 23:40:13 +08:00
if (children_.IsEmpty())
2018-01-30 16:45:38 +08:00
{
2018-11-15 14:35:19 +08:00
graphics->SetTransform(final_matrix_);
2018-11-18 20:26:41 +08:00
graphics->SetOpacity(display_opacity_);
2018-11-08 00:21:59 +08:00
OnDraw();
}
else
{
if (dirty_sort_)
2018-01-30 16:45:38 +08:00
{
2018-11-15 23:40:13 +08:00
children_.Sort(
[](spNode const& n1, spNode const& n2) { return n1->GetOrder() < n2->GetOrder(); }
2018-11-08 00:21:59 +08:00
);
dirty_sort_ = false;
2018-01-30 16:45:38 +08:00
}
2018-11-08 00:21:59 +08:00
2018-11-15 23:40:13 +08:00
spNode child = children_.First();
for (spNode next; child; child = next)
2018-01-30 16:45:38 +08:00
{
2018-11-16 15:53:39 +08:00
next = child->NextItem();
2018-11-08 00:21:59 +08:00
if (child->GetOrder() < 0)
{
child->Visit();
}
else
{
break;
}
2018-01-30 16:45:38 +08:00
}
2018-11-08 00:21:59 +08:00
2018-11-15 14:35:19 +08:00
graphics->SetTransform(final_matrix_);
2018-11-18 20:26:41 +08:00
graphics->SetOpacity(display_opacity_);
2018-11-08 00:21:59 +08:00
OnDraw();
2018-11-15 23:40:13 +08:00
for (spNode next; child; child = next)
{
2018-11-16 15:53:39 +08:00
next = child->NextItem();
2018-11-15 23:40:13 +08:00
child->Visit();
}
2018-01-30 16:45:38 +08:00
}
}
void Node::Update(Duration const& dt)
{
2018-11-15 23:40:13 +08:00
if (children_.IsEmpty())
2018-11-08 00:21:59 +08:00
{
OnUpdate(dt);
UpdateActions(this, dt);
UpdateTasks(dt);
2018-11-08 00:21:59 +08:00
}
else
{
2018-11-15 23:40:13 +08:00
// <20><><EFBFBD><EFBFBD> Order С<><D0A1><EFBFBD><EFBFBD><EFBFBD>Ľڵ<C4BD>
spNode child = children_.First();
for (spNode next; child; child = next)
2018-11-08 00:21:59 +08:00
{
if (child->GetOrder() < 0)
{
2018-11-16 15:53:39 +08:00
next = child->NextItem();
child->Update(dt);
2018-11-08 00:21:59 +08:00
}
else
{
break;
}
}
2018-11-08 00:21:59 +08:00
OnUpdate(dt);
UpdateActions(this, dt);
UpdateTasks(dt);
2018-11-08 00:21:59 +08:00
2018-11-15 23:40:13 +08:00
for (spNode next; child; child = next)
{
2018-11-16 15:53:39 +08:00
next = child->NextItem();
2018-11-15 23:40:13 +08:00
child->Update(dt);
}
2018-11-08 00:21:59 +08:00
}
2018-10-06 09:45:28 +08:00
}
2018-11-08 00:21:59 +08:00
void Node::DrawBorder()
2018-10-06 09:45:28 +08:00
{
2018-11-08 00:21:59 +08:00
if (visible_)
2018-10-06 09:45:28 +08:00
{
2018-11-08 00:21:59 +08:00
if (border_)
2018-10-06 09:45:28 +08:00
{
2018-11-18 20:26:41 +08:00
devices::Graphics::Instance()->DrawGeometry(border_, border_color_, 1.5f);
2018-10-06 09:45:28 +08:00
}
2018-11-08 00:21:59 +08:00
for (auto child = children_.First(); child; child = child->NextItem())
2018-10-06 09:45:28 +08:00
{
2018-11-08 00:21:59 +08:00
child->DrawBorder();
2018-10-06 09:45:28 +08:00
}
}
}
2018-11-08 00:21:59 +08:00
void Node::UpdateTransform()
2018-07-07 18:04:18 +08:00
{
2018-11-08 00:21:59 +08:00
if (!dirty_transform_)
return;
dirty_transform_ = false;
final_matrix_ = transform_.ToMatrix();
initial_matrix_ = final_matrix_ * math::Matrix::Translation(
Point{ transform_.size.width * transform_.pivot.x, transform_.size.height * transform_.pivot.y }
2018-11-08 00:21:59 +08:00
);
if (parent_)
2018-09-01 23:00:08 +08:00
{
2018-11-08 00:21:59 +08:00
initial_matrix_ = initial_matrix_ * parent_->initial_matrix_;
final_matrix_ = final_matrix_ * parent_->initial_matrix_;
}
2018-07-07 18:04:18 +08:00
2018-11-08 00:21:59 +08:00
// <20><><EFBFBD>¹<EFBFBD><C2B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ThrowIfFailed(
devices::Graphics::Instance()->CreateRectGeometry(border_, final_matrix_, transform_.size)
2018-11-08 00:21:59 +08:00
);
for (auto child = children_.First(); child; child = child->NextItem())
{
2018-11-08 00:21:59 +08:00
child->dirty_transform_ = true;
}
2018-07-07 18:04:18 +08:00
}
2018-11-08 00:21:59 +08:00
bool Node::Dispatch(const MouseEvent & e, bool handled)
{
if (visible_)
{
2018-11-15 23:40:13 +08:00
spNode prev;
for (auto child = children_.Last(); child; child = prev)
2018-11-15 23:40:13 +08:00
{
2018-11-16 15:53:39 +08:00
prev = child->PrevItem();
2018-11-15 23:40:13 +08:00
handled = child->Dispatch(e, handled);
}
2018-07-13 00:45:39 +08:00
2018-11-08 00:21:59 +08:00
auto handler = dynamic_cast<MouseEventHandler*>(this);
if (handler)
handler->Handle(e);
}
2018-09-16 16:07:51 +08:00
2018-11-08 00:21:59 +08:00
return handled;
}
2018-09-11 00:37:52 +08:00
2018-11-08 00:21:59 +08:00
bool Node::Dispatch(const KeyEvent & e, bool handled)
2018-09-11 00:37:52 +08:00
{
2018-11-08 00:21:59 +08:00
if (visible_)
{
2018-11-15 23:40:13 +08:00
spNode prev;
for (auto child = children_.Last(); child; child = prev)
2018-11-15 23:40:13 +08:00
{
2018-11-16 15:53:39 +08:00
prev = child->PrevItem();
2018-11-15 23:40:13 +08:00
handled = child->Dispatch(e, handled);
}
2018-11-08 00:21:59 +08:00
auto handler = dynamic_cast<KeyEventHandler*>(this);
if (handler)
handler->Handle(e);
}
return handled;
2018-09-11 00:37:52 +08:00
}
2018-11-08 00:21:59 +08:00
void Node::UpdateOpacity()
2018-07-13 00:45:39 +08:00
{
2018-11-08 00:21:59 +08:00
if (parent_)
{
2018-11-18 20:26:41 +08:00
display_opacity_ = opacity_ * parent_->display_opacity_;
2018-11-08 00:21:59 +08:00
}
for (auto child = children_.First(); child; child = child->NextItem())
2018-11-08 00:21:59 +08:00
{
child->UpdateOpacity();
}
2018-07-13 00:45:39 +08:00
}
2018-11-08 00:21:59 +08:00
void Node::SetOrder(int order)
{
if (order_ == order)
return;
2018-11-08 00:21:59 +08:00
order_ = order;
if (parent_)
{
parent_->dirty_sort_ = true;
}
}
2018-11-08 00:21:59 +08:00
void Node::SetPositionX(float x)
{
this->SetPosition(x, transform_.position.y);
}
2018-11-08 00:21:59 +08:00
void Node::SetPositionY(float y)
{
this->SetPosition(transform_.position.x, y);
}
2018-11-08 00:21:59 +08:00
void Node::SetPosition(const Point & p)
{
this->SetPosition(p.x, p.y);
}
2018-07-13 00:45:39 +08:00
2018-11-08 00:21:59 +08:00
void Node::SetPosition(float x, float y)
{
if (transform_.position.x == x && transform_.position.y == y)
return;
2018-10-18 00:17:27 +08:00
2018-11-08 00:21:59 +08:00
transform_.position.x = x;
transform_.position.y = y;
dirty_transform_ = true;
}
2017-10-15 02:46:24 +08:00
2018-11-08 00:21:59 +08:00
void Node::MoveBy(float x, float y)
{
this->SetPosition(transform_.position.x + x, transform_.position.y + y);
}
2018-11-08 00:21:59 +08:00
void Node::MoveBy(const Point & v)
{
2018-11-08 00:21:59 +08:00
this->MoveBy(v.x, v.y);
}
2018-11-08 00:21:59 +08:00
void Node::SetScaleX(float scale_x)
{
this->SetScale(scale_x, transform_.scale.y);
2018-11-08 00:21:59 +08:00
}
2018-11-08 00:21:59 +08:00
void Node::SetScaleY(float scale_y)
{
this->SetScale(transform_.scale.x, scale_y);
2018-11-08 00:21:59 +08:00
}
2018-11-08 00:21:59 +08:00
void Node::SetScale(float scale)
{
this->SetScale(scale, scale);
}
2018-11-08 00:21:59 +08:00
void Node::SetScale(float scale_x, float scale_y)
{
if (transform_.scale.x == scale_x && transform_.scale.y == scale_y)
2018-11-08 00:21:59 +08:00
return;
transform_.scale.x = scale_x;
transform_.scale.y = scale_y;
2018-11-08 00:21:59 +08:00
dirty_transform_ = true;
}
2018-11-08 00:21:59 +08:00
void Node::SetSkewX(float skew_x)
{
this->SetSkew(skew_x, transform_.skew.y);
2018-11-08 00:21:59 +08:00
}
2018-11-08 00:21:59 +08:00
void Node::SetSkewY(float skew_y)
{
this->SetSkew(transform_.skew.x, skew_y);
2018-11-08 00:21:59 +08:00
}
2018-11-08 00:21:59 +08:00
void Node::SetSkew(float skew_x, float skew_y)
{
if (transform_.skew.x == skew_x && transform_.skew.y == skew_y)
2018-11-08 00:21:59 +08:00
return;
2017-10-17 21:22:25 +08:00
transform_.skew.x = skew_x;
transform_.skew.y = skew_y;
2018-11-08 00:21:59 +08:00
dirty_transform_ = true;
}
2018-11-08 00:21:59 +08:00
void Node::SetRotation(float angle)
{
if (transform_.rotation == angle)
return;
2018-11-08 00:21:59 +08:00
transform_.rotation = angle;
dirty_transform_ = true;
}
2017-10-15 02:46:24 +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
// <20><><EFBFBD>½ڵ<C2BD>͸<EFBFBD><CDB8><EFBFBD><EFBFBD>
UpdateOpacity();
}
2018-11-08 00:21:59 +08:00
void Node::SetPivotX(float pivot_x)
{
this->SetPivot(pivot_x, transform_.pivot.y);
2018-11-08 00:21:59 +08:00
}
2018-11-08 00:21:59 +08:00
void Node::SetPivotY(float pivot_y)
{
this->SetPivot(transform_.pivot.x, pivot_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::SetPivot(float pivot_x, float pivot_y)
{
if (transform_.pivot.x == pivot_x && transform_.pivot.y == pivot_y)
2018-11-08 00:21:59 +08:00
return;
2017-10-15 02:46:24 +08:00
transform_.pivot.x = pivot_x;
transform_.pivot.y = pivot_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)
{
this->SetSize(width, transform_.size.height);
}
2017-10-15 02:46:24 +08:00
2018-11-08 00:21:59 +08:00
void Node::SetHeight(float height)
{
this->SetSize(transform_.size.width, height);
}
2018-11-08 00:21:59 +08:00
void Node::SetSize(float width, float height)
{
if (transform_.size.width == width && transform_.size.height == height)
return;
2018-11-08 00:21:59 +08:00
transform_.size.width = width;
transform_.size.height = height;
dirty_transform_ = true;
}
2018-11-08 00:21:59 +08:00
void Node::SetSize(const Size& size)
{
this->SetSize(size.width, size.height);
}
Transform const& Node::GetTransform() const
{
return transform_;
}
void Node::SetTransform(Transform const& transform)
2018-11-08 00:21:59 +08:00
{
transform_ = transform;
dirty_transform_ = true;
}
void Node::SetBorderColor(Color const& color)
2018-11-08 00:21:59 +08:00
{
border_color_ = color;
}
2018-03-01 19:28:22 +08:00
void Node::AddChild(spNode const& child, int order)
2018-11-08 00:21:59 +08:00
{
2018-11-15 17:59:18 +08:00
if (!child)
logs::Warningln("Node::AddChild failed, child is nullptr");
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
if (child->parent_)
2018-11-16 15:53:39 +08:00
logs::Errorln("The node to be added already has a parent");
for (Node* parent = parent_; parent; parent = parent->parent_)
if (parent == child)
logs::Errorln("A node cannot be its own parent");
#endif // E2D_DEBUG
2018-03-01 19:28:22 +08:00
2018-11-16 15:53:39 +08:00
children_.PushBack(Node::ItemType(child));
2018-11-08 00:21:59 +08:00
child->parent_ = this;
child->dirty_transform_ = true;
2018-11-16 15:53:39 +08:00
child->SetOrder(order);
child->UpdateOpacity();
2018-11-08 00:21:59 +08:00
dirty_sort_ = true;
}
}
2018-11-08 00:21:59 +08:00
void Node::AddChild(const Nodes& nodes, int order)
{
for (const auto& node : nodes)
{
this->AddChild(node, order);
}
}
Rect Node::GetBounds()
{
return Rect(Point{}, transform_.size);
}
Node::Nodes Node::GetChildren(String const& name) const
2017-10-14 11:40:47 +08:00
{
2018-11-08 00:21:59 +08:00
Nodes children;
size_t hash_code = std::hash<String>{}(name);
2018-02-03 22:04:43 +08:00
2018-11-16 15:53:39 +08:00
for (auto child = children_.First(); child != children_.Last(); child = child->NextItem())
2017-10-14 11:40:47 +08:00
{
2018-11-08 00:21:59 +08:00
if (child->hash_name_ == hash_code && child->name_ == 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;
}
spNode Node::GetChild(String const& name) const
2018-11-08 00:21:59 +08:00
{
size_t hash_code = std::hash<String>{}(name);
2017-10-14 18:43:32 +08:00
2018-11-16 15:53:39 +08:00
for (auto child = children_.First(); child != children_.Last(); child = child->NextItem())
2017-10-17 21:22:25 +08:00
{
2018-11-08 00:21:59 +08:00
if (child->hash_name_ == hash_code && child->name_ == name)
{
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(spNode const& child)
2018-11-08 00:21:59 +08:00
{
2018-11-15 17:59:18 +08:00
if (!child)
logs::Warningln("Node::RemoveChild failed, child is nullptr");
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-16 15:53:39 +08:00
children_.Remove(Node::ItemType(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
}
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
size_t hash_code = std::hash<String>{}(child_name);
2018-11-15 23:40:13 +08:00
spNode next;
for (auto child = children_.First(); child; child = next)
2018-11-08 00:21:59 +08:00
{
2018-11-16 15:53:39 +08:00
next = child->NextItem();
2018-11-15 23:40:13 +08:00
if (child->hash_name_ == hash_code && child->name_ == child_name)
children_.Remove(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
}
2018-11-08 00:21:59 +08:00
bool Node::ContainsPoint(const Point& point)
{
if (transform_.size.width == 0.f || transform_.size.height == 0.f)
return false;
UpdateTransform();
BOOL ret = 0;
ThrowIfFailed(
border_->FillContainsPoint(
D2D1::Point2F(point.x, point.y),
D2D1::Matrix3x2F::Identity(),
&ret
)
);
return ret != 0;
}
2018-09-05 00:08:03 +08:00
bool Node::Intersects(spNode const& node)
{
2018-11-08 00:21:59 +08:00
if (transform_.size.width == 0.f || transform_.size.height == 0.f || node->transform_.size.width == 0.f || node->transform_.size.height == 0.f)
return false;
// <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
UpdateTransform();
node->UpdateTransform();
// <20><>ȡ<EFBFBD>ཻ״̬
D2D1_GEOMETRY_RELATION relation = D2D1_GEOMETRY_RELATION_UNKNOWN;
ThrowIfFailed(
border_->CompareWithGeometry(
node->border_.Get(),
2018-11-08 00:21:59 +08:00
D2D1::Matrix3x2F::Identity(),
&relation
)
);
return relation != D2D1_GEOMETRY_RELATION_UNKNOWN &&
relation != D2D1_GEOMETRY_RELATION_DISJOINT;
}
void Node::SetVisible(bool val)
2018-09-05 00:08:03 +08:00
{
visible_ = val;
2018-09-05 00:08:03 +08:00
}
2017-10-21 19:09:31 +08:00
void Node::SetName(String const& name)
2017-10-14 11:40:47 +08:00
{
2018-11-08 00:21:59 +08:00
if (name_ != name)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>
name_ = name;
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> Hash <20><>
hash_name_ = std::hash<String>{}(name);
2018-11-08 00:21:59 +08:00
}
2017-10-14 11:40:47 +08:00
}
2017-10-17 21:22:25 +08:00
}