From b4404db7b716ed75048badce971a61f7c37f4027 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Mon, 11 Feb 2019 19:07:31 +0800 Subject: [PATCH] add callback on updating Node minor --- src/core/Node.cpp | 12 ++++++------ src/core/Node.h | 10 +++++++++- src/core/Transform.hpp | 9 +++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/core/Node.cpp b/src/core/Node.cpp index 31b19018..0991cd6a 100644 --- a/src/core/Node.cpp +++ b/src/core/Node.cpp @@ -62,10 +62,14 @@ namespace easy2d if (pause_) return; - OnUpdate(dt); UpdateActions(this, dt); UpdateTasks(dt); + if (cb_update_) + cb_update_(dt); + + OnUpdate(dt); + if (!children_.IsEmpty()) { NodePtr next; @@ -225,11 +229,7 @@ namespace easy2d dirty_transform_ = false; dirty_transform_inverse_ = true; - // matrix multiplication is optimized by expression template - transform_matrix_ = Matrix::Scaling(transform_.scale) - * Matrix::Skewing(transform_.skew.x, transform_.skew.y) - * Matrix::Rotation(transform_.rotation) - * Matrix::Translation(transform_.position); + transform_matrix_ = transform_.ToMatrix(); Point offset{ -size_.x * anchor_.x, -size_.y * anchor_.y }; transform_matrix_.Translate(offset); diff --git a/src/core/Node.h b/src/core/Node.h index 8c5053c4..b38b9901 100644 --- a/src/core/Node.h +++ b/src/core/Node.h @@ -45,6 +45,7 @@ namespace easy2d friend class IntrusiveList; using Children = IntrusiveList; + using UpdateCallback = std::function; public: Node(); @@ -349,7 +350,13 @@ namespace easy2d void ResumeUpdating(); // 节点更新是否暂停 - inline bool IsUpdatePausing() const { return pause_; } + inline bool IsUpdatePausing() const { return pause_; } + + // 设置更新时的回调函数 + inline void SetCallbackOnUpdate(UpdateCallback const& cb) { cb_update_ = cb; } + + // 获取更新时的回调函数 + inline UpdateCallback const& GetCallbackOnUpdate() { return cb_update_; } // 设置默认锚点 static void SetDefaultAnchor( @@ -384,6 +391,7 @@ namespace easy2d Node* parent_; Scene* scene_; Children children_; + UpdateCallback cb_update_; mutable bool dirty_transform_; mutable bool dirty_transform_inverse_; diff --git a/src/core/Transform.hpp b/src/core/Transform.hpp index 72cfea85..8c2cbefe 100644 --- a/src/core/Transform.hpp +++ b/src/core/Transform.hpp @@ -46,5 +46,14 @@ namespace easy2d skew == other.skew && rotation == other.rotation; } + + inline math::Matrix ToMatrix() const + { + // matrix multiplication is optimized by expression template + return Matrix::Scaling(scale) + * Matrix::Skewing(skew.x, skew.y) + * Matrix::Rotation(rotation) + * Matrix::Translation(position); + } }; }