diff --git a/core/base/Action.hpp b/core/base/Action.hpp index a79ca1da..5ade761e 100644 --- a/core/base/Action.hpp +++ b/core/base/Action.hpp @@ -24,12 +24,14 @@ namespace easy2d { + class ActionManager; + class Action : public RefCounter { E2D_DISABLE_COPY(Action); - friend class Node; + friend class ActionManager; friend class Loop; friend class Sequence; friend class Spawn; @@ -51,12 +53,6 @@ namespace easy2d // 停止动作 inline void Stop() { done_ = true; } - // 获取动作名称 - inline const String& GetName() const { return name_; } - - // 设置动作名称 - inline void SetName(const String& name) { name_ = name; } - inline bool IsDone() const { return done_; } // 获取动作的拷贝 @@ -70,7 +66,6 @@ namespace easy2d { initialized_ = false; done_ = false; - started_ = time::Now(); } protected: @@ -80,13 +75,9 @@ namespace easy2d this->Reset(); } - virtual void Init(Node* target) - { - initialized_ = true; - started_ = time::Now(); - } + virtual void Init(Node* target) {} - virtual void Update(Node* target) + virtual void Update(Node* target, Duration const& dt) { if (!initialized_) { @@ -94,13 +85,9 @@ namespace easy2d } } - virtual void ResetTime() {} - protected: - String name_; - bool running_; - bool done_; - bool initialized_; - TimePoint started_; + bool running_; + bool done_; + bool initialized_; }; } diff --git a/core/base/ActionCombined.cpp b/core/base/ActionCombined.cpp index 47aacf0f..2155ad3d 100644 --- a/core/base/ActionCombined.cpp +++ b/core/base/ActionCombined.cpp @@ -75,9 +75,9 @@ namespace easy2d } } - void Loop::Update(Node* target) + void Loop::Update(Node* target, Duration const& dt) { - Action::Update(target); + Action::Update(target, dt); if (times_ == total_times_) { @@ -87,7 +87,7 @@ namespace easy2d if (action_) { - action_->Update(target); + action_->Update(target, dt); if (action_->IsDone()) { @@ -111,11 +111,6 @@ namespace easy2d times_ = 0; } - void Loop::ResetTime() - { - if (action_) action_->ResetTime(); - } - //------------------------------------------------------- // Sequence @@ -142,12 +137,12 @@ namespace easy2d actions_[0]->Init(target); } - void Sequence::Update(Node* target) + void Sequence::Update(Node* target, Duration const& dt) { - Action::Update(target); + Action::Update(target, dt); auto &action = actions_[action_index_]; - action->Update(target); + action->Update(target, dt); if (action->IsDone()) { @@ -173,14 +168,6 @@ namespace easy2d action_index_ = 0; } - void Sequence::ResetTime() - { - for (const auto& action : actions_) - { - action->ResetTime(); - } - } - void Sequence::Add(spAction const& action) { if (action) @@ -258,9 +245,9 @@ namespace easy2d } } - void Spawn::Update(Node* target) + void Spawn::Update(Node* target, Duration const& dt) { - Action::Update(target); + Action::Update(target, dt); size_t done_num = 0; for (const auto& action : actions_) @@ -271,7 +258,7 @@ namespace easy2d } else { - action->Update(target); + action->Update(target, dt); } } @@ -290,14 +277,6 @@ namespace easy2d } } - void Spawn::ResetTime() - { - for (const auto& action : actions_) - { - action->ResetTime(); - } - } - void Spawn::Add(spAction const& action) { if (action) diff --git a/core/base/ActionCombined.h b/core/base/ActionCombined.h index 39bd6397..bd6f3690 100644 --- a/core/base/ActionCombined.h +++ b/core/base/ActionCombined.h @@ -51,10 +51,7 @@ namespace easy2d virtual void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target) override; - - // 重置动作时间 - virtual void ResetTime() override; + virtual void Update(Node* target, Duration const& dt) override; protected: spAction action_; @@ -102,10 +99,7 @@ namespace easy2d virtual void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target) override; - - // 重置动作时间 - virtual void ResetTime() override; + virtual void Update(Node* target, Duration const& dt) override; protected: UINT action_index_; @@ -152,10 +146,7 @@ namespace easy2d virtual void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target) override; - - // 重置动作时间 - virtual void ResetTime() override; + virtual void Update(Node* target, Duration const& dt) override; protected: Actions actions_; diff --git a/core/base/ActionFiniteTime.cpp b/core/base/ActionFiniteTime.cpp index 22653f70..25395946 100644 --- a/core/base/ActionFiniteTime.cpp +++ b/core/base/ActionFiniteTime.cpp @@ -29,16 +29,16 @@ namespace easy2d // FiniteTimeAction //------------------------------------------------------- - FiniteTimeAction::FiniteTimeAction(float duration) - : delta_(0) - , duration_(std::max(duration, 0.f)) + FiniteTimeAction::FiniteTimeAction(Duration const& duration) + : process_(0) + , duration_(duration) { } void FiniteTimeAction::Reset() { Action::Reset(); - delta_ = 0; + process_ = 0; } void FiniteTimeAction::Init(Node* target) @@ -46,38 +46,33 @@ namespace easy2d Action::Init(target); } - void FiniteTimeAction::Update(Node* target) + void FiniteTimeAction::Update(Node* target, Duration const& dt) { - Action::Update(target); + Action::Update(target, dt); - if (duration_ == 0) + if (duration_.IsZero()) { - delta_ = 1; + process_ = 1.f; this->Stop(); } else { - delta_ = std::min((time::Now() - started_).Seconds() / duration_, 1.f); + process_ += dt / duration_; + process_ = std::min(process_, 1.f); - if (delta_ >= 1) + if (process_ >= 1) { this->Stop(); } } } - void FiniteTimeAction::ResetTime() - { - Action::ResetTime(); - started_ = time::Now() - time::Second * (delta_ * duration_); - } - //------------------------------------------------------- // Move Action //------------------------------------------------------- - MoveBy::MoveBy(float duration, Point vector) + MoveBy::MoveBy(Duration const& duration, Point const& vector) : FiniteTimeAction(duration) { delta_pos_ = vector; @@ -93,9 +88,9 @@ namespace easy2d } } - void MoveBy::Update(Node* target) + void MoveBy::Update(Node* target, Duration const& dt) { - FiniteTimeAction::Update(target); + FiniteTimeAction::Update(target, dt); if (target) { @@ -103,7 +98,7 @@ namespace easy2d Point diff = currentPos - prev_pos_; start_pos_ = start_pos_ + diff; - Point newPos = start_pos_ + (delta_pos_ * delta_); + Point newPos = start_pos_ + (delta_pos_ * process_); target->SetPosition(newPos); prev_pos_ = newPos; @@ -120,7 +115,7 @@ namespace easy2d return new (std::nothrow) MoveBy(duration_, -delta_pos_); } - MoveTo::MoveTo(float duration, Point pos) + MoveTo::MoveTo(Duration const& duration, Point const& pos) : MoveBy(duration, Point()) { end_pos_ = pos; @@ -142,7 +137,7 @@ namespace easy2d // Jump Action //------------------------------------------------------- - JumpBy::JumpBy(float duration, const Point & vec, float height, int jumps) + JumpBy::JumpBy(Duration const& duration, Point const& vec, float height, int jumps) : FiniteTimeAction(duration) , delta_pos_(vec) , height_(height) @@ -170,16 +165,16 @@ namespace easy2d } } - void JumpBy::Update(Node* target) + void JumpBy::Update(Node* target, Duration const& dt) { - FiniteTimeAction::Update(target); + FiniteTimeAction::Update(target, dt); if (target) { - float frac = fmod(delta_ * jumps_, 1.f); - float x = delta_pos_.x * delta_; + float frac = fmod(process_ * jumps_, 1.f); + float x = delta_pos_.x * process_; float y = height_ * 4 * frac * (1 - frac); - y += delta_pos_.y * delta_; + y += delta_pos_.y * process_; Point currentPos = target->GetPosition(); @@ -193,7 +188,7 @@ namespace easy2d } } - JumpTo::JumpTo(float duration, const Point & pos, float height, int jumps) + JumpTo::JumpTo(Duration const& duration, Point const& pos, float height, int jumps) : JumpBy(duration, Point(), height, jumps) , end_pos_(pos) { @@ -215,14 +210,14 @@ namespace easy2d // Scale Action //------------------------------------------------------- - ScaleBy::ScaleBy(float duration, float scale) + ScaleBy::ScaleBy(Duration const& duration, float scale) : FiniteTimeAction(duration) { delta_x_ = scale; delta_y_ = scale; } - ScaleBy::ScaleBy(float duration, float scale_x, float scale_y) + ScaleBy::ScaleBy(Duration const& duration, float scale_x, float scale_y) : FiniteTimeAction(duration) { delta_x_ = scale_x; @@ -240,13 +235,13 @@ namespace easy2d } } - void ScaleBy::Update(Node* target) + void ScaleBy::Update(Node* target, Duration const& dt) { - FiniteTimeAction::Update(target); + FiniteTimeAction::Update(target, dt); if (target) { - target->SetScale(start_scale_x_ + delta_x_ * delta_, start_scale_y_ + delta_y_ * delta_); + target->SetScale(start_scale_x_ + delta_x_ * process_, start_scale_y_ + delta_y_ * process_); } } @@ -260,14 +255,14 @@ namespace easy2d return new (std::nothrow) ScaleBy(duration_, -delta_x_, -delta_y_); } - ScaleTo::ScaleTo(float duration, float scale) + ScaleTo::ScaleTo(Duration const& duration, float scale) : ScaleBy(duration, 0, 0) { end_scale_x_ = scale; end_scale_y_ = scale; } - ScaleTo::ScaleTo(float duration, float scale_x, float scale_y) + ScaleTo::ScaleTo(Duration const& duration, float scale_x, float scale_y) : ScaleBy(duration, 0, 0) { end_scale_x_ = scale_x; @@ -291,7 +286,7 @@ namespace easy2d // Opacity Action //------------------------------------------------------- - OpacityBy::OpacityBy(float duration, float opacity) + OpacityBy::OpacityBy(Duration const& duration, float opacity) : FiniteTimeAction(duration) { delta_val_ = opacity; @@ -307,13 +302,13 @@ namespace easy2d } } - void OpacityBy::Update(Node* target) + void OpacityBy::Update(Node* target, Duration const& dt) { - FiniteTimeAction::Update(target); + FiniteTimeAction::Update(target, dt); if (target) { - target->SetOpacity(start_val_ + delta_val_ * delta_); + target->SetOpacity(start_val_ + delta_val_ * process_); } } @@ -327,7 +322,7 @@ namespace easy2d return new (std::nothrow) OpacityBy(duration_, -delta_val_); } - OpacityTo::OpacityTo(float duration, float opacity) + OpacityTo::OpacityTo(Duration const& duration, float opacity) : OpacityBy(duration, 0) { end_val_ = opacity; @@ -344,12 +339,12 @@ namespace easy2d delta_val_ = end_val_ - start_val_; } - FadeIn::FadeIn(float duration) + FadeIn::FadeIn(Duration const& duration) : OpacityTo(duration, 1) { } - FadeOut::FadeOut(float duration) + FadeOut::FadeOut(Duration const& duration) : OpacityTo(duration, 0) { } @@ -359,7 +354,7 @@ namespace easy2d // Rotate Action //------------------------------------------------------- - RotateBy::RotateBy(float duration, float rotation) + RotateBy::RotateBy(Duration const& duration, float rotation) : FiniteTimeAction(duration) { delta_val_ = rotation; @@ -375,13 +370,13 @@ namespace easy2d } } - void RotateBy::Update(Node* target) + void RotateBy::Update(Node* target, Duration const& dt) { - FiniteTimeAction::Update(target); + FiniteTimeAction::Update(target, dt); if (target) { - target->SetRotation(start_val_ + delta_val_ * delta_); + target->SetRotation(start_val_ + delta_val_ * process_); } } @@ -395,7 +390,7 @@ namespace easy2d return new (std::nothrow) RotateBy(duration_, -delta_val_); } - RotateTo::RotateTo(float duration, float rotation) + RotateTo::RotateTo(Duration const& duration, float rotation) : RotateBy(duration, 0) { end_val_ = rotation; @@ -417,16 +412,16 @@ namespace easy2d // Delay //------------------------------------------------------- - Delay::Delay(float duration) - : delta_(0) - , delay_(std::max(duration, 0.f)) + Delay::Delay(Duration const& duration) + : delta_() + , delay_(duration) { } void Delay::Reset() { Action::Reset(); - delta_ = 0; + delta_ = Duration{}; } void Delay::Init(Node* target) @@ -434,11 +429,11 @@ namespace easy2d Action::Init(target); } - void Delay::Update(Node* target) + void Delay::Update(Node* target, Duration const& dt) { - Action::Update(target); + Action::Update(target, dt); - delta_ = (time::Now() - started_).Seconds(); + delta_ += dt; if (delta_ >= delay_) { @@ -446,12 +441,6 @@ namespace easy2d } } - void Delay::ResetTime() - { - Action::ResetTime(); - started_ = time::Now() - time::Second * delta_; - } - spAction Delay::Clone() const { return new (std::nothrow) Delay(delay_); diff --git a/core/base/ActionFiniteTime.h b/core/base/ActionFiniteTime.h index bb30f6f4..9256764a 100644 --- a/core/base/ActionFiniteTime.h +++ b/core/base/ActionFiniteTime.h @@ -32,7 +32,7 @@ namespace easy2d public: // 创建特定时长的持续动作 explicit FiniteTimeAction( - float duration + Duration const& duration ); // 重置动作 @@ -43,14 +43,11 @@ namespace easy2d virtual void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target) override; - - // 重置动作时间 - virtual void ResetTime() override; + virtual void Update(Node* target, Duration const& dt) override; protected: - float duration_; - float delta_; + Duration duration_; + float process_; }; @@ -62,8 +59,8 @@ namespace easy2d public: explicit MoveBy( - float duration, /* 持续时长 */ - Point vector /* 移动距离 */ + Duration const& duration, /* 持续时长 */ + Point const& vector /* 移动距离 */ ); // 获取该动作的拷贝对象 @@ -77,7 +74,7 @@ namespace easy2d virtual void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target) override; + virtual void Update(Node* target, Duration const& dt) override; protected: Point start_pos_; @@ -94,8 +91,8 @@ namespace easy2d public: explicit MoveTo( - float duration, /* 持续时长 */ - Point pos /* 目的坐标 */ + Duration const& duration, /* 持续时长 */ + Point const& pos /* 目的坐标 */ ); // 获取该动作的拷贝对象 @@ -125,10 +122,10 @@ namespace easy2d public: explicit JumpBy( - float duration, /* 持续时长 */ - const Point& vec, /* 跳跃距离 */ - float height, /* 跳跃高度 */ - int jumps = 1 /* 跳跃次数 */ + Duration const& duration, /* 持续时长 */ + Point const& vec, /* 跳跃距离 */ + float height, /* 跳跃高度 */ + int jumps = 1 /* 跳跃次数 */ ); // 获取该动作的拷贝对象 @@ -142,7 +139,7 @@ namespace easy2d virtual void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target) override; + virtual void Update(Node* target, Duration const& dt) override; protected: Point start_pos_; @@ -161,10 +158,10 @@ namespace easy2d public: explicit JumpTo( - float duration, /* 持续时长 */ - const Point& pos, /* 目的坐标 */ - float height, /* 跳跃高度 */ - int jumps = 1 /* 跳跃次数 */ + Duration const& duration, /* 持续时长 */ + Point const& pos, /* 目的坐标 */ + float height, /* 跳跃高度 */ + int jumps = 1 /* 跳跃次数 */ ); // 获取该动作的拷贝对象 @@ -194,14 +191,14 @@ namespace easy2d public: explicit ScaleBy( - float duration, /* 持续时长 */ - float scale /* 相对变化值 */ + Duration const& duration, /* 持续时长 */ + float scale /* 相对变化值 */ ); explicit ScaleBy( - float duration, /* 持续时长 */ - float scale_x, /* 横向缩放相对变化值 */ - float scale_y /* 纵向缩放相对变化值 */ + Duration const& duration, /* 持续时长 */ + float scale_x, /* 横向缩放相对变化值 */ + float scale_y /* 纵向缩放相对变化值 */ ); // 获取该动作的拷贝对象 @@ -215,7 +212,7 @@ namespace easy2d virtual void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target) override; + virtual void Update(Node* target, Duration const& dt) override; protected: float start_scale_x_; @@ -233,14 +230,14 @@ namespace easy2d public: explicit ScaleTo( - float duration, /* 持续时长 */ - float scale /* 目标值 */ + Duration const& duration, /* 持续时长 */ + float scale /* 目标值 */ ); explicit ScaleTo( - float duration, /* 持续时长 */ - float scale_x, /* 横向缩放目标值 */ - float scale_y /* 纵向缩放目标值 */ + Duration const& duration, /* 持续时长 */ + float scale_x, /* 横向缩放目标值 */ + float scale_y /* 纵向缩放目标值 */ ); // 获取该动作的拷贝对象 @@ -271,8 +268,8 @@ namespace easy2d public: explicit OpacityBy( - float duration, /* 持续时长 */ - float opacity /* 相对变化值 */ + Duration const& duration, /* 持续时长 */ + float opacity /* 相对变化值 */ ); // 获取该动作的拷贝对象 @@ -286,7 +283,7 @@ namespace easy2d virtual void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target) override; + virtual void Update(Node* target, Duration const& dt) override; protected: float start_val_; @@ -302,8 +299,8 @@ namespace easy2d public: explicit OpacityTo( - float duration, /* 持续时长 */ - float opacity /* 目标值 */ + Duration const& duration, /* 持续时长 */ + float opacity /* 目标值 */ ); // 获取该动作的拷贝对象 @@ -334,7 +331,7 @@ namespace easy2d public: // 创建淡入动作 explicit FadeIn( - float duration /* 持续时长 */ + Duration const& duration /* 持续时长 */ ); }; @@ -348,7 +345,7 @@ namespace easy2d public: // 创建淡出动作 explicit FadeOut( - float duration /* 持续时长 */ + Duration const& duration /* 持续时长 */ ); }; @@ -361,8 +358,8 @@ namespace easy2d public: explicit RotateBy( - float duration, /* 持续时长 */ - float rotation /* 相对变化值 */ + Duration const& duration, /* 持续时长 */ + float rotation /* 相对变化值 */ ); // 获取该动作的拷贝对象 @@ -376,7 +373,7 @@ namespace easy2d virtual void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target) override; + virtual void Update(Node* target, Duration const& dt) override; protected: float start_val_; @@ -392,8 +389,8 @@ namespace easy2d public: explicit RotateTo( - float duration, /* 持续时长 */ - float rotation /* 目标值 */ + Duration const& duration, /* 持续时长 */ + float rotation /* 目标值 */ ); // 获取该动作的拷贝对象 @@ -423,7 +420,7 @@ namespace easy2d public: explicit Delay( - float duration /* 延迟时长(秒) */ + Duration const& duration /* 延迟时长(秒) */ ); // 获取该动作的拷贝对象 @@ -440,13 +437,10 @@ namespace easy2d virtual void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target) override; - - // 重置动作时间 - virtual void ResetTime() override; + virtual void Update(Node* target, Duration const& dt) override; protected: - float delay_; - float delta_; + Duration delay_; + Duration delta_; }; } diff --git a/core/base/ActionManager.cpp b/core/base/ActionManager.cpp new file mode 100644 index 00000000..a157ec5b --- /dev/null +++ b/core/base/ActionManager.cpp @@ -0,0 +1,109 @@ +// 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 "ActionManager.h" + +namespace easy2d +{ + void ActionManager::UpdateActions(Node* target, Duration const& dt) + { + if (actions_.empty()) + return; + + std::vector currActions; + currActions.reserve(actions_.size()); + std::copy_if( + actions_.begin(), + actions_.end(), + std::back_inserter(currActions), + [](spAction action) { return action->IsRunning() && !action->IsDone(); } + ); + + // 遍历所有正在运行的动作 + for (const auto& action : currActions) + action->Update(target, dt); + + // 清除完成的动作 + for (auto iter = actions_.begin(); iter != actions_.end();) + { + if ((*iter)->IsDone()) + { + iter = actions_.erase(iter); + } + else + { + ++iter; + } + } + } + + void ActionManager::RunAction(spAction const& action) + { + E2D_WARNING_IF(!action, "Action NULL pointer exception!"); + + if (action) + { + auto iter = std::find(actions_.begin(), actions_.end(), action); + if (iter == actions_.end()) + { + action->Start(); + actions_.push_back(action); + } + } + } + + void ActionManager::ResumeAllActions() + { + if (actions_.empty()) + return; + + for (const auto& action : actions_) + { + action->Resume(); + } + } + + void ActionManager::PauseAllActions() + { + if (actions_.empty()) + return; + + for (const auto& action : actions_) + { + action->Pause(); + } + } + + void ActionManager::StopAllActions() + { + if (actions_.empty()) + return; + + for (const auto& action : actions_) + { + action->Stop(); + } + } + + const Actions& ActionManager::GetAllActions() const + { + return actions_; + } +} diff --git a/core/math/Transform.h b/core/base/ActionManager.h similarity index 69% rename from core/math/Transform.h rename to core/base/ActionManager.h index 6af36321..d37e72e0 100644 --- a/core/math/Transform.h +++ b/core/base/ActionManager.h @@ -19,33 +19,34 @@ // THE SOFTWARE. #pragma once -#include "../base/BaseTypes.h" -#include "../base/Size.h" -#include "Matrix.hpp" +#include "Action.hpp" namespace easy2d { - namespace math + class ActionManager { - class Transform - { - public: - Point position; // 坐标 - Size size; // 大小 - float scale_x; // 横向缩放 - float scale_y; // 纵向缩放 - float rotation; // 旋转 - float skew_x; // 横向倾斜角度 - float skew_y; // 纵向倾斜角度 - float pivot_x; // 支点横坐标 - float pivot_y; // 支点纵坐标 + public: + // 执行动作 + void RunAction( + spAction const& action + ); - public: - Transform(); + // 继续所有暂停动作 + void ResumeAllActions(); - Matrix ToMatrix() const; + // 暂停所有动作 + void PauseAllActions(); - bool operator== (const Transform& other) const; - }; - } + // 停止所有动作 + void StopAllActions(); + + // 获取所有动作 + const Actions& GetAllActions() const; + + protected: + void UpdateActions(Node* target, Duration const& dt); + + protected: + Actions actions_; + }; } diff --git a/core/base/Animation.cpp b/core/base/Animation.cpp index 100c91ea..7b887cd2 100644 --- a/core/base/Animation.cpp +++ b/core/base/Animation.cpp @@ -19,6 +19,7 @@ // THE SOFTWARE. #include "Animation.h" +#include "Image.h" #include "Sprite.h" namespace easy2d @@ -70,9 +71,9 @@ namespace easy2d } } - void Animate::Update(Node* target) + void Animate::Update(Node* target, Duration const& dt) { - Action::Update(target); + Action::Update(target, dt); if (!animation_) { @@ -80,7 +81,8 @@ namespace easy2d return; } - while ((time::Now() - started_).Seconds() >= animation_->GetInterval()) + delta_ += dt; + while (delta_ >= animation_->GetInterval()) { auto& frames = animation_->GetFrames(); auto sprite_target = dynamic_cast(target); @@ -90,7 +92,7 @@ namespace easy2d sprite_target->Load(frames[frame_index_]); } - started_ += time::Second * animation_->GetInterval(); + delta_ -= animation_->GetInterval(); ++frame_index_; if (frame_index_ == frames.size()) @@ -101,11 +103,6 @@ namespace easy2d } } - void Animate::ResetTime() - { - Action::ResetTime(); - } - void Animate::Reset() { Action::Reset(); @@ -150,12 +147,12 @@ namespace easy2d this->Add(frames); } - Animation::Animation(float interval) + Animation::Animation(Duration const& interval) : interval_(interval) { } - Animation::Animation(float interval, const Images& frames) + Animation::Animation(Duration const& interval, const Images& frames) : interval_(interval) { this->Add(frames); @@ -165,9 +162,9 @@ namespace easy2d { } - void Animation::SetInterval(float interval) + void Animation::SetInterval(Duration const& interval) { - interval_ = std::max(interval, 0.f); + interval_ = interval; } void Animation::Add(spImage const& frame) @@ -187,7 +184,7 @@ namespace easy2d } } - float Animation::GetInterval() const + Duration const& Animation::GetInterval() const { return interval_; } diff --git a/core/base/Animation.h b/core/base/Animation.h index da88ad36..830601c0 100644 --- a/core/base/Animation.h +++ b/core/base/Animation.h @@ -20,7 +20,6 @@ #pragma once #include "Action.hpp" -#include "Image.h" namespace easy2d { @@ -34,16 +33,16 @@ namespace easy2d Animation(); explicit Animation( - const Images& frames /* 关键帧数组 */ + const Images& frames /* 关键帧数组 */ ); explicit Animation( - float interval /* 帧间隔(秒) */ + Duration const& interval /* 帧间隔(秒) */ ); explicit Animation( - float interval, /* 帧间隔(秒) */ - const Images& frames /* 关键帧数组 */ + Duration const& interval, /* 帧间隔(秒) */ + const Images& frames /* 关键帧数组 */ ); virtual ~Animation(); @@ -59,14 +58,14 @@ namespace easy2d ); // 获取帧间隔 - float GetInterval() const; + Duration const& GetInterval() const; // 获取关键帧 const Images& GetFrames() const; // 设置每一帧的时间间隔 void SetInterval( - float interval /* 帧间隔(秒) */ + Duration const& interval /* 帧间隔(秒) */ ); // 获取帧动画的拷贝对象 @@ -76,8 +75,8 @@ namespace easy2d spAnimation Reverse() const; protected: - float interval_; - Images frames_; + Duration interval_; + Images frames_; }; @@ -118,13 +117,11 @@ namespace easy2d virtual void Init(Node* target) override; // 更新动作 - virtual void Update(Node* target) override; - - // 重置动作时间 - virtual void ResetTime() override; + virtual void Update(Node* target, Duration const& dt) override; protected: - UINT frame_index_; + size_t frame_index_; + Duration delta_; spAnimation animation_; }; } diff --git a/core/base/CallFunc.cpp b/core/base/CallFunc.cpp index f9e1f10c..b3bb6255 100644 --- a/core/base/CallFunc.cpp +++ b/core/base/CallFunc.cpp @@ -41,7 +41,7 @@ namespace easy2d { } - void CallFunc::Update(Node*) + void CallFunc::Update(Node*, Duration const&) { callback_(); this->Stop(); diff --git a/core/base/CallFunc.h b/core/base/CallFunc.h index fa8e5fb1..856b67c3 100644 --- a/core/base/CallFunc.h +++ b/core/base/CallFunc.h @@ -48,7 +48,7 @@ namespace easy2d virtual void Init(Node*) override; // 更新动作 - virtual void Update(Node*) override; + virtual void Update(Node*, Duration const&) override; protected: Callback callback_; diff --git a/core/base/Game.cpp b/core/base/Game.cpp index 74cd2f8f..def75cdb 100644 --- a/core/base/Game.cpp +++ b/core/base/Game.cpp @@ -19,17 +19,15 @@ // THE SOFTWARE. #include "Game.h" -#include "Node.h" #include "Scene.h" #include "Transition.h" #include "Image.h" -#include "time.h" +#include "../utils/Player.h" +#include "../math/Matrix.hpp" #include "render.h" #include "input.h" #include "audio.h" #include "modules.h" -#include "../utils/Player.h" -#include "../math/Matrix.hpp" #include namespace easy2d @@ -131,7 +129,7 @@ namespace easy2d if (dur.Milliseconds() > min_interval) { - float dt = (now - last).Seconds(); + const auto dt = now - last; last = now; devices::Input::Instance().Update( @@ -168,18 +166,27 @@ namespace easy2d quit_ = true; } - void Game::EnterScene(spScene const& scene, spTransition const& transition) + bool Game::EnterScene(spScene const & scene) { if (!scene) { E2D_WARNING("Next scene is null pointer!"); - return; + return false; } - if (curr_scene_ == scene) { return; } + if (curr_scene_ == scene || + next_scene_ == scene) + return false; next_scene_ = scene; + return true; + } + bool Game::EnterScene(spScene const& scene, spTransition const& transition) + { + if (!EnterScene(scene)) + return false; + if (transition) { if (transition_) @@ -189,6 +196,7 @@ namespace easy2d transition_ = transition; transition_->Init(curr_scene_, next_scene_); } + return true; } spScene const& Game::GetCurrentScene() @@ -196,32 +204,21 @@ namespace easy2d return curr_scene_; } - bool Game::IsTransitioning() const + void Game::UpdateScene(Duration const& dt) { - return transition_; - } - - void Game::UpdateScene(float dt) - { - auto update = [&](spScene const& scene) -> void + if (curr_scene_) { - if (scene) - { - scene->OnUpdate(dt); - spNode const& root = scene->GetRoot(); - if (root) - { - root->UpdateChildren(dt); - } - } - }; + curr_scene_->Update(dt); + } - update(curr_scene_); - update(next_scene_); + if (next_scene_) + { + next_scene_->Update(dt); + } if (transition_) { - transition_->Update(); + transition_->Update(dt); if (transition_->IsDone()) { @@ -247,6 +244,24 @@ namespace easy2d } } + void Game::Dispatch(MouseEvent const & e) + { + if (transition_) + return; + + if (curr_scene_) + curr_scene_->Dispatch(e, false); + } + + void Game::Dispatch(KeyEvent const & e) + { + if (transition_) + return; + + if (curr_scene_) + curr_scene_->Dispatch(e, false); + } + void Game::DrawScene() { auto& graphics = devices::Graphics::Instance(); @@ -258,22 +273,22 @@ namespace easy2d } else if (curr_scene_) { - curr_scene_->Draw(); + curr_scene_->Visit(); } if (debug_enabled_) { - if (curr_scene_ && curr_scene_->GetRoot()) + if (curr_scene_) { graphics.SetTransform(math::Matrix()); graphics.SetBrushOpacity(1.f); - curr_scene_->GetRoot()->DrawBorder(); + curr_scene_->DrawBorder(); } - if (next_scene_ && next_scene_->GetRoot()) + if (next_scene_) { graphics.SetTransform(math::Matrix()); graphics.SetBrushOpacity(1.f); - next_scene_->GetRoot()->DrawBorder(); + next_scene_->DrawBorder(); } graphics.DrawDebugInfo(); diff --git a/core/base/Game.h b/core/base/Game.h index 38e512b0..ca984bcb 100644 --- a/core/base/Game.h +++ b/core/base/Game.h @@ -21,8 +21,9 @@ #pragma once #include "base.h" #include "window.h" -#include "Scene.h" -#include "Transition.h" +#include "time.h" +#include "KeyEvent.h" +#include "MouseEvent.h" namespace easy2d { @@ -76,20 +77,31 @@ namespace easy2d void Quit(); // 切换场景 - void EnterScene( - spScene const& scene, /* 场景 */ - spTransition const& transition = nullptr /* 场景动画 */ + bool EnterScene( + spScene const& scene /* 场景 */ + ); + + // 切换场景 + bool EnterScene( + spScene const& scene, /* 场景 */ + spTransition const& transition /* 场景动画 */ ); // 获取当前场景 spScene const& GetCurrentScene(); - bool IsTransitioning() const; - void DrawScene(); void UpdateScene( - float dt + Duration const& dt + ); + + void Dispatch( + MouseEvent const& e + ); + + void Dispatch( + KeyEvent const& e ); private: diff --git a/core/base/Node.cpp b/core/base/Node.cpp index a48ada18..ba70b52c 100644 --- a/core/base/Node.cpp +++ b/core/base/Node.cpp @@ -22,7 +22,6 @@ #include "Scene.h" #include "Task.h" #include "Action.hpp" -#include "time.h" #include "render.h" #include @@ -41,8 +40,6 @@ namespace easy2d , display_opacity_(1.f) , real_opacity_(1.f) , children_() - , actions_() - , tasks_() , initial_matrix_() , final_matrix_() , border_color_(Color::Red, 0.6f) @@ -112,13 +109,13 @@ namespace easy2d } } - void Node::UpdateChildren(float dt) + void Node::Update(Duration const& dt) { if (children_.empty()) { OnUpdate(dt); - UpdateActions(); - UpdateTasks(); + UpdateActions(this, dt); + UpdateTasks(dt); UpdateTransform(); } else @@ -130,7 +127,7 @@ namespace easy2d // 访问 Order 小于零的节点 if (child->GetOrder() < 0) { - child->UpdateChildren(dt); + child->Update(dt); } else { @@ -139,13 +136,13 @@ namespace easy2d } OnUpdate(dt); - UpdateActions(); - UpdateTasks(); + UpdateActions(this, dt); + UpdateTasks(dt); UpdateTransform(); // 访问剩余节点 for (; i < children_.size(); ++i) - children_[i]->UpdateChildren(dt); + children_[i]->Update(dt); } } @@ -176,8 +173,8 @@ namespace easy2d // 根据自身支点计算 Initial 矩阵,子节点将根据这个矩阵进行变换 auto pivot = Point( - transform_.size.width * transform_.pivot_x, - transform_.size.height * transform_.pivot_y + transform_.size.width * transform_.pivot.x, + transform_.size.height * transform_.pivot.y ); initial_matrix_ = final_matrix_ * math::Matrix::Translation(pivot); @@ -243,38 +240,6 @@ namespace easy2d } } - void Node::UpdateActions() - { - if (actions_.empty()) - return; - - std::vector currActions; - currActions.reserve(actions_.size()); - std::copy_if( - actions_.begin(), - actions_.end(), - std::back_inserter(currActions), - [](spAction action) { return action->IsRunning() && !action->IsDone(); } - ); - - // 遍历所有正在运行的动作 - for (const auto& action : currActions) - action->Update(this); - - // 清除完成的动作 - for (auto iter = actions_.begin(); iter != actions_.end();) - { - if ((*iter)->IsDone()) - { - iter = actions_.erase(iter); - } - else - { - ++iter; - } - } - } - bool Node::IsVisible() const { return visible_; @@ -297,12 +262,12 @@ namespace easy2d float Node::GetWidth() const { - return transform_.size.width * transform_.scale_x; + return transform_.size.width * transform_.scale.x; } float Node::GetHeight() const { - return transform_.size.height * transform_.scale_y; + return transform_.size.height * transform_.scale.y; } float Node::GetRealWidth() const @@ -322,12 +287,12 @@ namespace easy2d float Node::GetPivotX() const { - return transform_.pivot_x; + return transform_.pivot.x; } float Node::GetPivotY() const { - return transform_.pivot_y; + return transform_.pivot.y; } Size Node::GetSize() const @@ -337,22 +302,22 @@ namespace easy2d float Node::GetScaleX() const { - return transform_.scale_x; + return transform_.scale.x; } float Node::GetScaleY() const { - return transform_.scale_y; + return transform_.scale.y; } float Node::GetSkewX() const { - return transform_.skew_x; + return transform_.skew.x; } float Node::GetSkewY() const { - return transform_.skew_y; + return transform_.skew.y; } float Node::GetRotation() const @@ -360,11 +325,6 @@ namespace easy2d return transform_.rotation; } - const math::Transform & Node::GetTransform() const - { - return transform_; - } - float Node::GetOpacity() const { return real_opacity_; @@ -429,12 +389,12 @@ namespace easy2d void Node::SetScaleX(float scale_x) { - this->SetScale(scale_x, transform_.scale_y); + this->SetScale(scale_x, transform_.scale.y); } void Node::SetScaleY(float scale_y) { - this->SetScale(transform_.scale_x, scale_y); + this->SetScale(transform_.scale.x, scale_y); } void Node::SetScale(float scale) @@ -444,31 +404,31 @@ namespace easy2d void Node::SetScale(float scale_x, float scale_y) { - if (transform_.scale_x == scale_x && transform_.scale_y == scale_y) + if (transform_.scale.x == scale_x && transform_.scale.y == scale_y) return; - transform_.scale_x = scale_x; - transform_.scale_y = scale_y; + 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); + this->SetSkew(skew_x, transform_.skew.y); } void Node::SetSkewY(float skew_y) { - this->SetSkew(transform_.skew_x, 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) + if (transform_.skew.x == skew_x && transform_.skew.y == skew_y) return; - transform_.skew_x = skew_x; - transform_.skew_y = skew_y; + transform_.skew.x = skew_x; + transform_.skew.y = skew_y; dirty_transform_ = true; } @@ -493,21 +453,21 @@ namespace easy2d void Node::SetPivotX(float pivot_x) { - this->SetPivot(pivot_x, transform_.pivot_y); + this->SetPivot(pivot_x, transform_.pivot.y); } void Node::SetPivotY(float pivot_y) { - this->SetPivot(transform_.pivot_x, pivot_y); + this->SetPivot(transform_.pivot.x, pivot_y); } void Node::SetPivot(float pivot_x, float pivot_y) { - if (transform_.pivot_x == pivot_x && transform_.pivot_y == pivot_y) + if (transform_.pivot.x == pivot_x && transform_.pivot.y == pivot_y) return; - transform_.pivot_x = pivot_x; - transform_.pivot_y = pivot_y; + transform_.pivot.x = pivot_x; + transform_.pivot.y = pivot_y; dirty_transform_ = true; } @@ -536,7 +496,12 @@ namespace easy2d this->SetSize(size.width, size.height); } - void Node::SetTransform(const math::Transform & transform) + math::Transform const& Node::GetTransform() const + { + return transform_; + } + + void Node::SetTransform(math::Transform const& transform) { transform_ = transform; dirty_transform_ = true; @@ -695,63 +660,6 @@ namespace easy2d children_.clear(); } - void Node::RunAction(spAction const& action) - { - E2D_WARNING_IF(!action, "Action NULL pointer exception!"); - - if (action) - { - auto iter = std::find(actions_.begin(), actions_.end(), action); - if (iter == actions_.end()) - { - action->Start(); - actions_.push_back(action); - } - } - } - - void Node::ResumeAction(const String& name) - { - if (actions_.empty()) - return; - - for (const auto& action : actions_) - { - if (action->GetName() == name) - { - action->Resume(); - } - } - } - - void Node::PauseAction(const String& name) - { - if (actions_.empty()) - return; - - for (const auto& action : actions_) - { - if (action->GetName() == name) - { - action->Pause(); - } - } - } - - void Node::StopAction(const String& name) - { - if (actions_.empty()) - return; - - for (const auto& action : actions_) - { - if (action->GetName() == name) - { - action->Stop(); - } - } - } - bool Node::ContainsPoint(const Point& point) { if (transform_.size.width == 0.f || transform_.size.height == 0.f) @@ -792,169 +700,6 @@ namespace easy2d relation != D2D1_GEOMETRY_RELATION_DISJOINT; } - void Node::ResumeAllActions() - { - if (actions_.empty()) - return; - - for (const auto& action : actions_) - { - action->Resume(); - } - } - - void Node::PauseAllActions() - { - if (actions_.empty()) - return; - - for (const auto& action : actions_) - { - action->Pause(); - } - } - - void Node::StopAllActions() - { - if (actions_.empty()) - return; - - for (const auto& action : actions_) - { - action->Stop(); - } - } - - const Actions& Node::GetAllActions() const - { - return actions_; - } - - void Node::AddTask(spTask const& task) - { - if (task) - { - auto iter = std::find(tasks_.begin(), tasks_.end(), task); - if (iter == tasks_.end()) - { - task->last_time_ = time::Now(); - tasks_.push_back(task); - } - } - } - - void Node::StopTasks(const String& name) - { - for (const auto& task : tasks_) - { - if (task->GetName() == name) - { - task->Stop(); - } - } - } - - void Node::StartTasks(const String& name) - { - for (const auto& task : tasks_) - { - if (task->GetName() == name) - { - task->Start(); - } - } - } - - void Node::RemoveTasks(const String& name) - { - for (const auto& task : tasks_) - { - if (task->GetName() == name) - { - task->stopped_ = true; - } - } - } - - void Node::StopAllTasks() - { - for (const auto& task : tasks_) - { - task->Stop(); - } - } - - void Node::StartAllTasks() - { - for (const auto& task : tasks_) - { - task->Start(); - } - } - - void Node::RemoveAllTasks() - { - for (const auto& task : tasks_) - { - task->stopped_ = true; - } - } - - const Tasks & Node::GetAllTasks() const - { - return tasks_; - } - - void Node::UpdateTasks() - { - if (tasks_.empty()) - return; - - std::vector currTasks; - currTasks.reserve(tasks_.size()); - std::copy_if( - tasks_.begin(), - tasks_.end(), - std::back_inserter(currTasks), - [](spTask const& task) { return task->IsReady() && !task->stopped_; } - ); - - // 遍历就绪的任务 - for (const auto& task : currTasks) - task->Update(); - - // 清除结束的任务 - for (auto iter = tasks_.begin(); iter != tasks_.end();) - { - if ((*iter)->stopped_) - { - iter = tasks_.erase(iter); - } - else - { - ++iter; - } - } - } - - void Node::UpdateTime() - { - for (const auto& action : actions_) - { - action->ResetTime(); - } - - for (const auto& task : tasks_) - { - task->ResetTime(); - } - - for (const auto& child : children_) - { - child->UpdateTime(); - } - } - void Node::SetVisible(bool val) { visible_ = val; diff --git a/core/base/Node.h b/core/base/Node.h index 2e5ca350..b13db09f 100644 --- a/core/base/Node.h +++ b/core/base/Node.h @@ -20,24 +20,27 @@ #pragma once #include "base.h" +#include "time.h" +#include "ActionManager.h" +#include "TaskManager.h" #include "KeyEvent.h" #include "MouseEvent.h" -#include "../math/Transform.h" +#include "../math/Transform.hpp" #include "../math/Matrix.hpp" namespace easy2d { class Game; - class Scene; - class Action; - class Task; // 节点 class Node : public RefCounter + , public ActionManager + , public TaskManager { friend class Game; friend class Scene; + friend class Transition; E2D_DISABLE_COPY(Node); @@ -50,7 +53,7 @@ namespace easy2d virtual void OnDraw() const {} // 更新节点 - virtual void OnUpdate(float dt) {} + virtual void OnUpdate(Duration const& dt) {} // 获取节点显示状态 bool IsVisible() const; @@ -106,9 +109,6 @@ namespace easy2d // 获取节点旋转角度 float GetRotation() const; - // 获取二维转换矩阵 - const math::Transform& GetTransform() const; - // 获取节点透明度 float GetOpacity() const; @@ -129,108 +129,108 @@ namespace easy2d ); // 设置节点横坐标 - virtual void SetPositionX( + void SetPositionX( float x ); // 设置节点纵坐标 - virtual void SetPositionY( + void SetPositionY( float y ); // 设置节点坐标 - virtual void SetPosition( + void SetPosition( const Point & point ); // 设置节点坐标 - virtual void SetPosition( + void SetPosition( float x, float y ); // 移动节点 - virtual void MoveBy( + void MoveBy( float x, float y ); // 移动节点 - virtual void MoveBy( + void MoveBy( const Point & vector ); // 设置节点绘图顺序 // 默认为 0 - virtual void SetOrder( + void SetOrder( int order ); // 设置横向缩放比例 // 默认为 1.0 - virtual void SetScaleX( + void SetScaleX( float scale_x ); // 设置纵向缩放比例 // 默认为 1.0 - virtual void SetScaleY( + void SetScaleY( float scale_y ); // 设置缩放比例 // 默认为 (1.0, 1.0) - virtual void SetScale( + void SetScale( float scale_x, float scale_y ); // 设置缩放比例 // 默认为 1.0 - virtual void SetScale( + void SetScale( float scale ); // 设置横向倾斜角度 // 默认为 0 - virtual void SetSkewX( + void SetSkewX( float skew_x ); // 设置纵向倾斜角度 // 默认为 0 - virtual void SetSkewY( + void SetSkewY( float skew_y ); // 设置倾斜角度 // 默认为 (0, 0) - virtual void SetSkew( + void SetSkew( float skew_x, float skew_y ); // 设置旋转角度 // 默认为 0 - virtual void SetRotation( + void SetRotation( float rotation ); // 设置透明度 // 默认为 1.0, 范围 [0, 1] - virtual void SetOpacity( + void SetOpacity( float opacity ); // 设置支点的横向位置 // 默认为 0, 范围 [0, 1] - virtual void SetPivotX( + void SetPivotX( float pivot_x ); // 设置支点的纵向位置 // 默认为 0, 范围 [0, 1] - virtual void SetPivotY( + void SetPivotY( float pivot_y ); @@ -247,36 +247,37 @@ namespace easy2d ); // 修改节点高度 - virtual void SetHeight( + void SetHeight( float height ); // 修改节点大小 - virtual void SetSize( + void SetSize( float width, float height ); // 修改节点大小 - virtual void SetSize( + void SetSize( const Size & size ); - // 设置二维转换 - virtual void SetTransform( - const math::Transform& transform - ); - // 启用或关闭渲染区域裁剪 - virtual void SetClipEnabled( + void SetClipEnabled( bool enabled ); // 设置节点边缘颜色 - virtual void SetBorderColor( + void SetBorderColor( const Color& color ); + math::Transform const& GetTransform() const; + + void SetTransform( + math::Transform const& transform + ); + // 判断点是否在节点内 bool ContainsPoint( const Point& point @@ -331,109 +332,29 @@ namespace easy2d // 从父节点移除 void RemoveFromParent(); - // 执行动作 - void RunAction( - spAction const& action - ); - - // 继续动作 - void ResumeAction( - const String& name - ); - - // 暂停动作 - void PauseAction( - const String& name - ); - - // 停止动作 - void StopAction( - const String& name - ); - - // 继续所有暂停动作 - void ResumeAllActions(); - - // 暂停所有动作 - void PauseAllActions(); - - // 停止所有动作 - void StopAllActions(); - - // 获取所有动作 - const Actions& GetAllActions() const; - - // 添加任务 - void AddTask( - spTask const& task - ); - - // 启动任务 - void StartTasks( - const String& task_name - ); - - // 停止任务 - void StopTasks( - const String& task_name - ); - - // 移除任务 - void RemoveTasks( - const String& task_name - ); - - // 启动所有任务 - void StartAllTasks(); - - // 停止所有任务 - void StopAllTasks(); - - // 移除所有任务 - void RemoveAllTasks(); - - // 获取所有任务 - const Tasks& GetAllTasks() const; - protected: - // 遍历节点 virtual void Visit(); - // 分发鼠标消息 virtual bool Dispatch( const MouseEvent& e, bool handled ); - // 分发按键消息 virtual bool Dispatch( const KeyEvent& e, bool handled ); - private: - // 渲染节点边缘 + protected: void DrawBorder(); - // 更新子节点 - void UpdateChildren(float dt); + void Update(Duration const& dt); - // 更新转换矩阵 void UpdateTransform(); - // 更新节点透明度 void UpdateOpacity(); - // 更新动作 - void UpdateActions(); - - // 更新任务 - void UpdateTasks(); - - // 更新节点时间 - void UpdateTime(); - - private: + protected: String name_; size_t hash_name_; float display_opacity_; @@ -445,8 +366,6 @@ namespace easy2d bool dirty_transform_; Node* parent_; Color border_color_; - Actions actions_; - Tasks tasks_; Nodes children_; ID2D1Geometry* border_; math::Transform transform_; diff --git a/core/base/Scene.cpp b/core/base/Scene.cpp index 72b273f3..f1b06003 100644 --- a/core/base/Scene.cpp +++ b/core/base/Scene.cpp @@ -24,83 +24,10 @@ namespace easy2d { Scene::Scene() - : root_(nullptr) - , transform_() { } - Scene::Scene(spNode const& root) - : root_(nullptr) - , transform_() - { - this->SetRoot(root); - } - Scene::~Scene() { } - - void Scene::SetRoot(spNode const& root) - { - if (root_ == root) - return; - - root_ = root; - } - - spNode const& Scene::GetRoot() const - { - return root_; - } - - void Scene::Draw() - { - if (root_) - { - root_->Visit(); - } - } - - void Scene::Dispatch(const MouseEvent & e) - { - auto handler = dynamic_cast(this); - if (handler) - { - handler->Handle(e); - } - - if (root_) - { - root_->Dispatch(e, false); - } - } - - void Scene::Dispatch(const KeyEvent & e) - { - auto handler = dynamic_cast(this); - if (handler) - { - handler->Handle(e); - } - - if (root_) - { - root_->Dispatch(e, false); - } - } - - void Scene::SetTransform(const math::Matrix& matrix) - { - transform_ = matrix; - - if (root_) - { - root_->dirty_transform_ = true; - } - } - - const math::Matrix& Scene::GetTransform() const - { - return transform_; - } } diff --git a/core/base/Scene.h b/core/base/Scene.h index 125e4593..cbad061a 100644 --- a/core/base/Scene.h +++ b/core/base/Scene.h @@ -19,28 +19,19 @@ // THE SOFTWARE. #pragma once -#include "base.h" -#include "KeyEvent.h" -#include "MouseEvent.h" -#include "../math/Matrix.hpp" +#include "Node.h" namespace easy2d { - class Node; - // 场景 class Scene - : public RefCounter + : public Node { E2D_DISABLE_COPY(Scene); public: Scene(); - explicit Scene( - spNode const& root - ); - virtual ~Scene(); // 进入场景 @@ -48,41 +39,5 @@ namespace easy2d // 退出场景 virtual void OnExit() {} - - // 更新场景 - virtual void OnUpdate(float dt) {} - - // 设置根节点 - void SetRoot( - spNode const& root - ); - - // 获取根节点 - spNode const& GetRoot() const; - - // 渲染场景 - void Draw(); - - // 分发鼠标消息 - virtual void Dispatch( - const MouseEvent& e - ); - - // 分发按键消息 - virtual void Dispatch( - const KeyEvent& e - ); - - // 设置转换矩阵 - void SetTransform( - const math::Matrix& matrix - ); - - // 获取转换矩阵 - const math::Matrix& GetTransform() const; - - private: - spNode root_; - math::Matrix transform_; }; } diff --git a/core/base/Task.cpp b/core/base/Task.cpp index e14beaf4..437d60c8 100644 --- a/core/base/Task.cpp +++ b/core/base/Task.cpp @@ -35,13 +35,14 @@ namespace easy2d , delay_(delay) , callback_(func) , name_(name) + , delta_() { } void Task::Start() { running_ = true; - last_time_ = time::Now(); + delta_ = Duration{}; } void Task::Stop() @@ -49,16 +50,25 @@ namespace easy2d running_ = false; } - void Task::Update() + void Task::Update(Duration const& dt) { + if (!running_) + return; + if (total_times_ == 0) { stopped_ = true; return; } + if (!delay_.IsZero()) + { + delta_ += dt; + if (delta_ < delay_) + return; + } + ++run_times_; - last_time_ += delay_; if (callback_) { @@ -72,25 +82,9 @@ namespace easy2d } } - void Task::ResetTime() + void Task::Reset() { - last_time_ = time::Now(); - } - - bool Task::IsReady() const - { - if (running_) - { - if (delay_.Milliseconds() == 0) - { - return true; - } - if (time::Now() - last_time_ >= delay_) - { - return true; - } - } - return false; + delta_ = Duration{}; } bool Task::IsRunning() const diff --git a/core/base/Task.h b/core/base/Task.h index 05a44b3e..bba29764 100644 --- a/core/base/Task.h +++ b/core/base/Task.h @@ -25,11 +25,13 @@ namespace easy2d { + class TaskManager; + // 定时任务 class Task : public RefCounter { - friend class Node; + friend class TaskManager; typedef std::function Callback; @@ -59,11 +61,9 @@ namespace easy2d const String& GetName() const; protected: - bool IsReady() const; + void Update(Duration const& dt); - void Update(); - - void ResetTime(); + void Reset(); private: bool running_; @@ -72,7 +72,7 @@ namespace easy2d int total_times_; String name_; Duration delay_; - TimePoint last_time_; + Duration delta_; Callback callback_; }; } diff --git a/core/base/TaskManager.cpp b/core/base/TaskManager.cpp new file mode 100644 index 00000000..4eef63ec --- /dev/null +++ b/core/base/TaskManager.cpp @@ -0,0 +1,131 @@ +// 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 "TaskManager.h" + +namespace easy2d +{ + void TaskManager::AddTask(spTask const& task) + { + if (task) + { + auto iter = std::find(tasks_.begin(), tasks_.end(), task); + if (iter == tasks_.end()) + { + task->Reset(); + tasks_.push_back(task); + } + } + } + + void TaskManager::StopTasks(const String& name) + { + for (const auto& task : tasks_) + { + if (task->GetName() == name) + { + task->Stop(); + } + } + } + + void TaskManager::StartTasks(const String& name) + { + for (const auto& task : tasks_) + { + if (task->GetName() == name) + { + task->Start(); + } + } + } + + void TaskManager::RemoveTasks(const String& name) + { + for (const auto& task : tasks_) + { + if (task->GetName() == name) + { + task->stopped_ = true; + } + } + } + + void TaskManager::StopAllTasks() + { + for (const auto& task : tasks_) + { + task->Stop(); + } + } + + void TaskManager::StartAllTasks() + { + for (const auto& task : tasks_) + { + task->Start(); + } + } + + void TaskManager::RemoveAllTasks() + { + for (const auto& task : tasks_) + { + task->stopped_ = true; + } + } + + const Tasks & TaskManager::GetAllTasks() const + { + return tasks_; + } + + void TaskManager::UpdateTasks(Duration const& dt) + { + if (tasks_.empty()) + return; + + std::vector currTasks; + currTasks.reserve(tasks_.size()); + std::copy_if( + tasks_.begin(), + tasks_.end(), + std::back_inserter(currTasks), + [](spTask const& task) { return !task->stopped_; } + ); + + // 遍历就绪的任务 + for (const auto& task : currTasks) + task->Update(dt); + + // 清除结束的任务 + for (auto iter = tasks_.begin(); iter != tasks_.end();) + { + if ((*iter)->stopped_) + { + iter = tasks_.erase(iter); + } + else + { + ++iter; + } + } + } +} diff --git a/core/math/Transform.cpp b/core/base/TaskManager.h similarity index 57% rename from core/math/Transform.cpp rename to core/base/TaskManager.h index fa691f61..e449dd18 100644 --- a/core/math/Transform.cpp +++ b/core/base/TaskManager.h @@ -18,45 +18,50 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include "Transform.h" +#pragma once +#include "Task.h" namespace easy2d { - namespace math + class TaskManager { - Transform::Transform() - : position() - , size() - , scale_x(1.f) - , scale_y(1.f) - , rotation(0) - , skew_x(0) - , skew_y(0) - , pivot_x(0) - , pivot_y(0) - { - } + public: + // 添加任务 + void AddTask( + spTask const& task + ); - Matrix Transform::ToMatrix() const - { - auto pivot = Vector2(size.width * pivot_x, size.height * pivot_y); - return Matrix().Scale(scale_x, scale_y, pivot) - .Skew(skew_x, skew_y, pivot) - .Rotate(rotation, pivot) - .Translate(position - pivot); - } + // 启动任务 + void StartTasks( + const String& task_name + ); - bool Transform::operator==(const Transform & other) const - { - return position == other.position && - size == other.size && - scale_x == other.scale_x && - scale_y == other.scale_y && - skew_x == other.skew_x && - skew_y == other.skew_y && - rotation == other.rotation && - pivot_x == other.pivot_x && - pivot_y == other.pivot_y; - } - } -} \ No newline at end of file + // 停止任务 + void StopTasks( + const String& task_name + ); + + // 移除任务 + void RemoveTasks( + const String& task_name + ); + + // 启动所有任务 + void StartAllTasks(); + + // 停止所有任务 + void StopAllTasks(); + + // 移除所有任务 + void RemoveAllTasks(); + + // 获取所有任务 + const Tasks& GetAllTasks() const; + + protected: + void UpdateTasks(Duration const& dt); + + protected: + Tasks tasks_; + }; +} diff --git a/core/base/Transition.cpp b/core/base/Transition.cpp index fed26552..7c160062 100644 --- a/core/base/Transition.cpp +++ b/core/base/Transition.cpp @@ -23,7 +23,6 @@ #include "Scene.h" #include "window.h" #include "render.h" -#include "../math/Matrix.hpp" namespace easy2d { @@ -31,9 +30,10 @@ namespace easy2d // Transition //------------------------------------------------------- - Transition::Transition(float duration) + Transition::Transition(Duration const& duration) : done_(false) - , started_() + , duration_(duration) + , delta_() , process_(0) , window_size_() , out_scene_(nullptr) @@ -43,7 +43,6 @@ namespace easy2d , out_layer_prop_() , in_layer_prop_() { - duration_ = std::max(duration, 0.f); } Transition::~Transition() @@ -59,7 +58,9 @@ namespace easy2d void Transition::Init(spScene const& prev, spScene const& next) { - started_ = time::Now(); + process_ = 0; + delta_ = Duration{}; + out_scene_ = prev; in_scene_ = next; @@ -81,16 +82,16 @@ namespace easy2d out_layer_prop_ = in_layer_prop_ = LayerProperties{ Rect(Point(), window_size_),1.f }; } - void Transition::Update() + void Transition::Update(Duration const& dt) { - if (duration_ == 0) + if (duration_.IsZero()) { process_ = 1; } else { - process_ = (time::Now() - started_).Seconds() / duration_; - process_ = std::min(process_, 1.f); + delta_ += dt; + process_ = std::min(delta_ / duration_, 1.f); } if (process_ >= 1) @@ -101,32 +102,34 @@ namespace easy2d void Transition::Draw() { + auto& graphics = devices::Graphics::Instance(); + if (out_scene_) { - devices::Graphics::Instance().PushClip( - out_scene_->GetTransform(), + graphics.PushClip( + out_scene_->GetTransform().ToMatrix(), window_size_ ); - devices::Graphics::Instance().PushLayer(out_layer_, out_layer_prop_); + graphics.PushLayer(out_layer_, out_layer_prop_); - out_scene_->Draw(); + out_scene_->Visit(); - devices::Graphics::Instance().PopLayer(); - devices::Graphics::Instance().PopClip(); + graphics.PopLayer(); + graphics.PopClip(); } if (in_scene_) { - devices::Graphics::Instance().PushClip( - in_scene_->GetTransform(), + graphics.PushClip( + in_scene_->GetTransform().ToMatrix(), window_size_ ); - devices::Graphics::Instance().PushLayer(in_layer_, in_layer_prop_); + graphics.PushLayer(in_layer_, in_layer_prop_); - in_scene_->Draw(); + in_scene_->Visit(); - devices::Graphics::Instance().PopLayer(); - devices::Graphics::Instance().PopClip(); + graphics.PopLayer(); + graphics.PopClip(); } } @@ -140,7 +143,7 @@ namespace easy2d // BoxTransition //------------------------------------------------------- - BoxTransition::BoxTransition(float duration) + BoxTransition::BoxTransition(Duration const& duration) : Transition(duration) { } @@ -152,9 +155,9 @@ namespace easy2d in_layer_prop_.opacity = 0; } - void BoxTransition::Update() + void BoxTransition::Update(Duration const& dt) { - Transition::Update(); + Transition::Update(dt); if (process_ < .5f) { @@ -182,7 +185,7 @@ namespace easy2d // EmergeTransition //------------------------------------------------------- - EmergeTransition::EmergeTransition(float duration) + EmergeTransition::EmergeTransition(Duration const& duration) : Transition(duration) { } @@ -195,9 +198,9 @@ namespace easy2d in_layer_prop_.opacity = 0; } - void EmergeTransition::Update() + void EmergeTransition::Update(Duration const& dt) { - Transition::Update(); + Transition::Update(dt); out_layer_prop_.opacity = 1 - process_; in_layer_prop_.opacity = process_; @@ -207,7 +210,7 @@ namespace easy2d // FadeTransition //------------------------------------------------------- - FadeTransition::FadeTransition(float duration) + FadeTransition::FadeTransition(Duration const& duration) : Transition(duration) { } @@ -220,9 +223,9 @@ namespace easy2d in_layer_prop_.opacity = 0; } - void FadeTransition::Update() + void FadeTransition::Update(Duration const& dt) { - Transition::Update(); + Transition::Update(dt); if (process_ < 0.5) { @@ -240,7 +243,7 @@ namespace easy2d // MoveTransition //------------------------------------------------------- - MoveTransition::MoveTransition(float duration, Direction direction) + MoveTransition::MoveTransition(Duration const& duration, Direction direction) : Transition(duration) , direction_(direction) { @@ -272,44 +275,33 @@ namespace easy2d if (out_scene_) { - out_scene_->SetTransform(math::Matrix()); + out_scene_->SetTransform(math::Transform{}); } if (in_scene_) { - in_scene_->SetTransform( - math::Matrix::Translation( - start_pos_.x, - start_pos_.y - ) - ); + auto transform = math::Transform{}; + transform.position = start_pos_; + in_scene_->SetTransform(transform); } } - void MoveTransition::Update() + void MoveTransition::Update(Duration const& dt) { - Transition::Update(); + Transition::Update(dt); if (out_scene_) { - auto translation = pos_delta_ * process_; - out_scene_->SetTransform( - math::Matrix::Translation( - translation.x, - translation.y - ) - ); + auto transform = math::Transform{}; + transform.position = pos_delta_ * process_; + out_scene_->SetTransform(transform); } if (in_scene_) { - auto translation = start_pos_ + pos_delta_ * process_; - in_scene_->SetTransform( - math::Matrix::Translation( - translation.x, - translation.y - ) - ); + auto transform = math::Transform{}; + transform.position = start_pos_ + pos_delta_ * process_; + in_scene_->SetTransform(transform); } } @@ -317,12 +309,12 @@ namespace easy2d { if (out_scene_) { - out_scene_->SetTransform(math::Matrix()); + out_scene_->SetTransform(math::Transform{}); } if (in_scene_) { - in_scene_->SetTransform(math::Matrix()); + in_scene_->SetTransform(math::Transform{}); } } @@ -330,7 +322,7 @@ namespace easy2d // RotationTransition //------------------------------------------------------- - RotationTransition::RotationTransition(float duration, float rotation) + RotationTransition::RotationTransition(Duration const& duration, float rotation) : Transition(duration) , rotation_(rotation) { @@ -340,42 +332,35 @@ namespace easy2d { Transition::Init(prev, next); + auto transform = math::Transform{}; + transform.pivot = Point{ 0.5f, 0.5f }; + transform.position = Point{ window_size_.width / 2, window_size_.height / 2 }; + if (out_scene_) { - out_scene_->SetTransform(math::Matrix()); + out_scene_->SetTransform(transform); } if (in_scene_) { - in_scene_->SetTransform(math::Matrix()); + in_scene_->SetTransform(transform); } in_layer_prop_.opacity = 0; } - void RotationTransition::Update() + void RotationTransition::Update(Duration const& dt) { - Transition::Update(); - - auto center_pos = math::Vector2( - window_size_.width / 2, - window_size_.height / 2 - ); + Transition::Update(dt); if (process_ < .5f) { if (out_scene_) { - out_scene_->SetTransform( - math::Matrix::Scaling( - (.5f - process_) * 2, - (.5f - process_) * 2, - center_pos - ) * math::Matrix::Rotation( - rotation_ * (.5f - process_) * 2, - center_pos - ) - ); + auto transform = out_scene_->GetTransform(); + transform.scale = Point{ (.5f - process_) * 2, (.5f - process_) * 2 }; + transform.rotation = rotation_ * (.5f - process_) * 2; + out_scene_->SetTransform(transform); } } else @@ -385,16 +370,11 @@ namespace easy2d out_layer_prop_.opacity = 0; in_layer_prop_.opacity = 1; - in_scene_->SetTransform( - math::Matrix::Scaling( - (process_ - .5f) * 2, - (process_ - .5f) * 2, - center_pos - ) * math::Matrix::Rotation( - rotation_ * (process_ - .5f) * 2, - center_pos - ) - ); + auto transform = in_scene_->GetTransform(); + transform.scale = Point{ (process_ - .5f) * 2, (process_ - .5f) * 2 }; + transform.rotation = rotation_ * (process_ - .5f) * 2; + + in_scene_->SetTransform(transform); } } } @@ -403,12 +383,12 @@ namespace easy2d { if (out_scene_) { - out_scene_->SetTransform(math::Matrix()); + out_scene_->SetTransform(math::Transform{}); } if (in_scene_) { - in_scene_->SetTransform(math::Matrix()); + in_scene_->SetTransform(math::Transform{}); } } } diff --git a/core/base/Transition.h b/core/base/Transition.h index 484196db..921d05ba 100644 --- a/core/base/Transition.h +++ b/core/base/Transition.h @@ -34,38 +34,32 @@ namespace easy2d public: explicit Transition( - float duration + Duration const& duration ); virtual ~Transition(); - // 场景过渡动画是否结束 bool IsDone(); protected: - // 初始化场景过渡动画 virtual void Init( spScene const& prev, spScene const& next ); - // 更新场景过渡动画 - virtual void Update(); + virtual void Update(Duration const& dt); - // 渲染场景过渡动画 virtual void Draw(); - // 停止场景过渡动画 virtual void Stop(); - // 重置场景过渡动画 virtual void Reset() { }; protected: bool done_; - float duration_; float process_; - TimePoint started_; + Duration duration_; + Duration delta_; Size window_size_; spScene out_scene_; spScene in_scene_; @@ -82,12 +76,12 @@ namespace easy2d { public: explicit FadeTransition( - float duration /* 动画持续时长 */ + Duration const& duration /* 动画持续时长 */ ); protected: // 更新动画 - virtual void Update() override; + virtual void Update(Duration const& dt) override; virtual void Init( spScene const& prev, @@ -102,11 +96,11 @@ namespace easy2d { public: explicit EmergeTransition( - float duration /* 动画持续时长 */ + Duration const& duration /* 动画持续时长 */ ); protected: - virtual void Update() override; + virtual void Update(Duration const& dt) override; virtual void Init( spScene const& prev, @@ -121,11 +115,11 @@ namespace easy2d { public: explicit BoxTransition( - float duration /* 动画持续时长 */ + Duration const& duration /* 动画持续时长 */ ); protected: - virtual void Update() override; + virtual void Update(Duration const& dt) override; virtual void Init( spScene const& prev, @@ -140,12 +134,12 @@ namespace easy2d { public: explicit MoveTransition( - float moveDuration, /* 动画持续时长 */ - Direction direction = Direction::Left /* 移动方向 */ + Duration const& moveDuration, /* 动画持续时长 */ + Direction direction /* 移动方向 */ ); protected: - virtual void Update() override; + virtual void Update(Duration const& dt) override; virtual void Init( spScene const& prev, @@ -167,12 +161,12 @@ namespace easy2d { public: explicit RotationTransition( - float moveDuration, /* 动画持续时长 */ - float rotation = 360 /* 旋转度数 */ + Duration const& moveDuration, /* 动画持续时长 */ + float rotation = 360 /* 旋转度数 */ ); protected: - virtual void Update() override; + virtual void Update(Duration const& dt) override; virtual void Init( spScene const& prev, diff --git a/core/base/time.cpp b/core/base/time.cpp index 30666abf..3f9b9eb3 100644 --- a/core/base/time.cpp +++ b/core/base/time.cpp @@ -56,16 +56,6 @@ namespace easy2d { } - time_t TimePoint::GetTimeStamp() const - { - return static_cast(dur_since_epoch_.Seconds()); - } - - bool TimePoint::IsZero() const - { - return !!dur_since_epoch_.Milliseconds(); - } - const TimePoint TimePoint::operator+(const Duration & dur) const { return TimePoint(dur_since_epoch_ + dur); @@ -145,11 +135,6 @@ namespace easy2d { } - int64_t Duration::Milliseconds() const - { - return milliseconds_; - } - float Duration::Seconds() const { int64_t sec = milliseconds_ / Second.milliseconds_; @@ -173,7 +158,7 @@ namespace easy2d std::wstring easy2d::time::Duration::ToString() const { - if (milliseconds_ == 0LL) + if (IsZero()) { return std::wstring(L"0s"); } @@ -246,6 +231,11 @@ namespace easy2d return milliseconds_ <= other.milliseconds_; } + float easy2d::time::Duration::operator/(const Duration & other) const + { + return static_cast(milliseconds_) / other.milliseconds_; + } + const Duration Duration::operator+(const Duration & other) const { return Duration(milliseconds_ + other.milliseconds_); diff --git a/core/base/time.h b/core/base/time.h index 1650eaa9..8082f650 100644 --- a/core/base/time.h +++ b/core/base/time.h @@ -49,7 +49,7 @@ namespace easy2d ); // 转化为毫秒 - int64_t Milliseconds() const; + inline int64_t Milliseconds() const { return milliseconds_; } // 转化为秒 float Seconds() const; @@ -60,6 +60,9 @@ namespace easy2d // 转化为小时 float Hours() const; + // 时长是否是零 + inline bool IsZero() const { return milliseconds_ == 0LL; } + // 转为字符串 std::wstring ToString() const; @@ -70,6 +73,8 @@ namespace easy2d bool operator< (const Duration &) const; bool operator<= (const Duration &) const; + float operator / (const Duration &) const; + const Duration operator + (const Duration &) const; const Duration operator - (const Duration &) const; const Duration operator - () const; @@ -155,11 +160,13 @@ namespace easy2d TimePoint&& other ); + inline Duration const& SinceEpoch() const { return dur_since_epoch_; } + // 获取时间戳 - time_t GetTimeStamp() const; + inline time_t GetTimeStamp() const { return static_cast(dur_since_epoch_.Seconds()); } // 是否是零时 - bool IsZero() const; + inline bool IsZero() const { return dur_since_epoch_.IsZero(); } const TimePoint operator + (const Duration &) const; const TimePoint operator - (const Duration &) const; diff --git a/core/base/window.cpp b/core/base/window.cpp index b75c8451..3dda8a45 100644 --- a/core/base/window.cpp +++ b/core/base/window.cpp @@ -21,7 +21,6 @@ #include "window.h" #include "render.h" #include "Game.h" -#include "Scene.h" #include "KeyEvent.h" #include "MouseEvent.h" #include "../math/scalar.hpp" @@ -284,14 +283,7 @@ namespace easy2d case WM_MOUSEMOVE: case WM_MOUSEWHEEL: { - if (game->IsTransitioning()) - break; - - auto curr_scene = game->GetCurrentScene(); - if (curr_scene) - { - curr_scene->Dispatch(MouseEvent(msg, w_param, l_param)); - } + game->Dispatch(MouseEvent(msg, w_param, l_param)); } result = 0; was_handled = true; @@ -301,14 +293,7 @@ namespace easy2d case WM_KEYDOWN: case WM_KEYUP: { - if (game->IsTransitioning()) - break; - - auto curr_scene = game->GetCurrentScene(); - if (curr_scene) - { - curr_scene->Dispatch(KeyEvent(msg, w_param, l_param)); - } + game->Dispatch(KeyEvent(msg, w_param, l_param)); } result = 0; was_handled = true; diff --git a/core/easy2d.h b/core/easy2d.h index d2aad23d..32d07322 100644 --- a/core/easy2d.h +++ b/core/easy2d.h @@ -76,7 +76,7 @@ #include "math/scalar.hpp" #include "math/vector.hpp" #include "math/Matrix.hpp" -#include "math/Transform.h" +#include "math/Transform.hpp" #include "math/rand.h" diff --git a/core/math/Transform.hpp b/core/math/Transform.hpp new file mode 100644 index 00000000..fde7d9bd --- /dev/null +++ b/core/math/Transform.hpp @@ -0,0 +1,69 @@ +// 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. + +#pragma once +#include "../base/BaseTypes.h" +#include "Matrix.hpp" + +namespace easy2d +{ + namespace math + { + class Transform + { + public: + Size size; // 大小 + float rotation; // 旋转 + math::Vector2 position; // 坐标 + math::Vector2 scale; // 缩放 + math::Vector2 skew; // 错切角度 + math::Vector2 pivot; // 支点 + + public: + Transform() + : position() + , size() + , rotation(0) + , scale(1.f, 1.f) + , skew(0.f, 0.f) + , pivot(0.f, 0.f) + {} + + inline Matrix ToMatrix() const + { + auto center = Vector2{ size.width * pivot.x, size.height * pivot.y }; + return Matrix{}.Scale(scale.x, scale.y, center) + .Skew(skew.x, skew.y, center) + .Rotate(rotation, center) + .Translate(position - center); + } + + bool operator== (const Transform& other) const + { + return position == other.position && + size == other.size && + scale == other.scale && + skew == other.skew && + rotation == other.rotation && + pivot == other.pivot; + } + }; + } +} diff --git a/core/ui/Button.h b/core/ui/Button.h index 9542a962..f6c6af72 100644 --- a/core/ui/Button.h +++ b/core/ui/Button.h @@ -73,22 +73,22 @@ namespace easy2d ); // 设置一般情况下显示的按钮 - virtual void SetNormal( + void SetNormal( spNode const& normal ); // 设置鼠标移入按钮时显示的按钮 - virtual void SetMouseOver( + void SetMouseOver( spNode const& mouseover ); // 设置鼠标按下按钮时显示的按钮 - virtual void SetSelected( + void SetSelected( spNode const& selected ); // 设置按钮被禁用时显示的按钮 - virtual void SetDisabled( + void SetDisabled( spNode const& disabled ); diff --git a/project/vs2013/Easy2D.vcxproj b/project/vs2013/Easy2D.vcxproj index c30e174f..a7adfffe 100644 --- a/project/vs2013/Easy2D.vcxproj +++ b/project/vs2013/Easy2D.vcxproj @@ -22,6 +22,7 @@ + @@ -50,6 +51,7 @@ + @@ -59,7 +61,7 @@ - + @@ -72,6 +74,7 @@ + @@ -92,13 +95,13 @@ + - diff --git a/project/vs2013/Easy2D.vcxproj.filters b/project/vs2013/Easy2D.vcxproj.filters index 040a1ad8..f6f9434a 100644 --- a/project/vs2013/Easy2D.vcxproj.filters +++ b/project/vs2013/Easy2D.vcxproj.filters @@ -104,9 +104,6 @@ math - - math - math @@ -146,6 +143,15 @@ base + + base + + + base + + + math + @@ -243,9 +249,6 @@ math - - math - utils @@ -270,5 +273,11 @@ base + + base + + + base + \ No newline at end of file diff --git a/project/vs2015/Easy2D.vcxproj b/project/vs2015/Easy2D.vcxproj index 3b1fa467..147515de 100644 --- a/project/vs2015/Easy2D.vcxproj +++ b/project/vs2015/Easy2D.vcxproj @@ -22,6 +22,7 @@ + @@ -50,6 +51,7 @@ + @@ -59,7 +61,7 @@ - + @@ -72,6 +74,7 @@ + @@ -92,13 +95,13 @@ + - diff --git a/project/vs2015/Easy2D.vcxproj.filters b/project/vs2015/Easy2D.vcxproj.filters index 040a1ad8..f6f9434a 100644 --- a/project/vs2015/Easy2D.vcxproj.filters +++ b/project/vs2015/Easy2D.vcxproj.filters @@ -104,9 +104,6 @@ math - - math - math @@ -146,6 +143,15 @@ base + + base + + + base + + + math + @@ -243,9 +249,6 @@ math - - math - utils @@ -270,5 +273,11 @@ base + + base + + + base + \ No newline at end of file diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index b4ec43f5..bf0ad502 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -22,6 +22,7 @@ + @@ -50,6 +51,7 @@ + @@ -59,7 +61,7 @@ - + @@ -72,6 +74,7 @@ + @@ -92,13 +95,13 @@ + - @@ -197,7 +200,7 @@ Disabled WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) false - EditAndContinue + None false true diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 040a1ad8..f6f9434a 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -104,9 +104,6 @@ math - - math - math @@ -146,6 +143,15 @@ base + + base + + + base + + + math + @@ -243,9 +249,6 @@ math - - math - utils @@ -270,5 +273,11 @@ base + + base + + + base + \ No newline at end of file