optimize: Node

This commit is contained in:
Nomango 2018-09-10 11:32:51 +08:00
parent 59ee637c4c
commit f7dba39243
3 changed files with 27 additions and 37 deletions

View File

@ -831,9 +831,6 @@ namespace e2d
Scene * scene Scene * scene
); );
// 子节点排序
void SortChildren();
// 更新转换矩阵 // 更新转换矩阵
void UpdateTransform(); void UpdateTransform();
@ -860,8 +857,8 @@ namespace e2d
int order_; int order_;
bool visible_; bool visible_;
bool clip_enabled_; bool clip_enabled_;
bool need_sort_; bool dirty_sort_;
bool need_transform_; bool dirty_transform_;
bool fixed_position_; bool fixed_position_;
Collider collider_; Collider collider_;
Scene * parent_scene_; Scene * parent_scene_;

View File

@ -383,8 +383,6 @@ namespace e2d
// 路径 // 路径
class Path class Path
{ {
friend class Game;
public: public:
// 获取数据的默认保存路径 // 获取数据的默认保存路径
static const String& GetDataPath(); static const String& GetDataPath();

View File

@ -36,8 +36,8 @@ e2d::Node::Node()
, parent_scene_(nullptr) , parent_scene_(nullptr)
, hash_name_(0) , hash_name_(0)
, clip_enabled_(false) , clip_enabled_(false)
, need_sort_(false) , dirty_sort_(false)
, need_transform_(false) , dirty_transform_(false)
, fixed_position_(false) , fixed_position_(false)
, collider_(this) , collider_(this)
, border_(nullptr) , border_(nullptr)
@ -122,7 +122,16 @@ void e2d::Node::Visit()
else else
{ {
// 子节点排序 // 子节点排序
SortChildren(); if (dirty_sort_)
{
std::sort(
std::begin(children_),
std::end(children_),
[](Node * n1, Node * n2) { return n1->GetOrder() < n2->GetOrder(); }
);
dirty_sort_ = false;
}
size_t i; size_t i;
for (i = 0; i < children_.size(); ++i) for (i = 0; i < children_.size(); ++i)
@ -195,10 +204,10 @@ void e2d::Node::DrawCollider()
void e2d::Node::UpdateTransform() void e2d::Node::UpdateTransform()
{ {
if (!need_transform_) if (!dirty_transform_)
return; return;
need_transform_ = false; dirty_transform_ = false;
// 计算锚点坐标 // 计算锚点坐标
D2D1_POINT_2F anchor = { size_.width * anchor_.x, size_.height * anchor_.y }; D2D1_POINT_2F anchor = { size_.width * anchor_.x, size_.height * anchor_.y };
@ -253,7 +262,7 @@ void e2d::Node::UpdateTransform()
// 通知子节点进行转换 // 通知子节点进行转换
for (const auto& child : children_) for (const auto& child : children_)
{ {
child->need_transform_ = true; child->dirty_transform_ = true;
} }
// 更新碰撞体 // 更新碰撞体
@ -297,20 +306,6 @@ bool e2d::Node::Dispatch(const KeyEvent & e, bool handled)
return handled; return handled;
} }
void e2d::Node::SortChildren()
{
if (need_sort_)
{
std::sort(
std::begin(children_),
std::end(children_),
[](Node * n1, Node * n2) { return n1->GetOrder() < n2->GetOrder(); }
);
need_sort_ = false;
}
}
void e2d::Node::UpdateOpacity() void e2d::Node::UpdateOpacity()
{ {
if (parent_) if (parent_)
@ -491,7 +486,7 @@ void e2d::Node::SetOrder(int order)
order_ = order; order_ = order;
if (parent_) if (parent_)
{ {
parent_->need_sort_ = true; parent_->dirty_sort_ = true;
} }
} }
@ -517,7 +512,7 @@ void e2d::Node::SetPos(float x, float y)
pos_.x = x; pos_.x = x;
pos_.y = y; pos_.y = y;
need_transform_ = true; dirty_transform_ = true;
} }
void e2d::Node::SetPosFixed(bool fixed) void e2d::Node::SetPosFixed(bool fixed)
@ -526,7 +521,7 @@ void e2d::Node::SetPosFixed(bool fixed)
return; return;
fixed_position_ = fixed; fixed_position_ = fixed;
need_transform_ = true; dirty_transform_ = true;
} }
void e2d::Node::Move(float x, float y) void e2d::Node::Move(float x, float y)
@ -561,7 +556,7 @@ void e2d::Node::SetScale(float scale_x, float scale_y)
scale_.x = scale_x; scale_.x = scale_x;
scale_.y = scale_y; scale_.y = scale_y;
need_transform_ = true; dirty_transform_ = true;
} }
void e2d::Node::SetSkewX(float skew_x) void e2d::Node::SetSkewX(float skew_x)
@ -581,7 +576,7 @@ void e2d::Node::SetSkew(float skew_x, float skew_y)
skew_.x = skew_x; skew_.x = skew_x;
skew_.y = skew_y; skew_.y = skew_y;
need_transform_ = true; dirty_transform_ = true;
} }
void e2d::Node::SetRotation(float angle) void e2d::Node::SetRotation(float angle)
@ -590,7 +585,7 @@ void e2d::Node::SetRotation(float angle)
return; return;
rotation_ = angle; rotation_ = angle;
need_transform_ = true; dirty_transform_ = true;
} }
void e2d::Node::SetOpacity(float opacity) void e2d::Node::SetOpacity(float opacity)
@ -620,7 +615,7 @@ void e2d::Node::SetAnchor(float anchor_x, float anchor_y)
anchor_.x = std::min(std::max(anchor_x, 0.f), 1.f); anchor_.x = std::min(std::max(anchor_x, 0.f), 1.f);
anchor_.y = std::min(std::max(anchor_y, 0.f), 1.f); anchor_.y = std::min(std::max(anchor_y, 0.f), 1.f);
need_transform_ = true; dirty_transform_ = true;
} }
void e2d::Node::SetWidth(float width) void e2d::Node::SetWidth(float width)
@ -640,7 +635,7 @@ void e2d::Node::SetSize(float width, float height)
size_.width = width; size_.width = width;
size_.height = height; size_.height = height;
need_transform_ = true; dirty_transform_ = true;
} }
void e2d::Node::SetSize(Size size) void e2d::Node::SetSize(Size size)
@ -699,9 +694,9 @@ void e2d::Node::AddChild(Node * child, int order /* = 0 */)
// 更新子节点透明度 // 更新子节点透明度
child->UpdateOpacity(); child->UpdateOpacity();
// 更新节点转换 // 更新节点转换
child->need_transform_ = true; child->dirty_transform_ = true;
// 更新子节点排序 // 更新子节点排序
need_sort_ = true; dirty_sort_ = true;
} }
} }