refactoring: MoveTransition

This commit is contained in:
Nomango 2018-09-11 00:37:52 +08:00
parent 6f489a77fe
commit a626e488de
7 changed files with 137 additions and 70 deletions

View File

@ -77,15 +77,6 @@ namespace e2d
float anchor_y
) override;
// 分发鼠标消息
virtual bool Dispatch(
const MouseEvent& e,
bool handled
) override;
// 遍历节点
virtual void Visit() override;
protected:
E2D_DISABLE_COPY(Button);
@ -100,6 +91,15 @@ namespace e2d
// 刷新按钮显示
virtual void UpdateVisible();
// 分发鼠标消息
virtual bool Dispatch(
const MouseEvent& e,
bool handled
) override;
// 遍历节点
virtual void Visit() override;
protected:
Node * normal_;
Node * mouseover_;

View File

@ -169,13 +169,22 @@ namespace e2d
bool handled
) override;
// 设置转换矩阵
void SetTransform(
const D2D1::Matrix3x2F& matrix
);
// 获取转换矩阵
const D2D1::Matrix3x2F& GetTransform() const;
protected:
E2D_DISABLE_COPY(Scene);
protected:
Node * root_;
Node* root_;
bool border_visible_;
bool collider_visible_;
D2D1::Matrix3x2F transform_;
};
@ -410,6 +419,7 @@ namespace e2d
: public Ref
, public EventTarget
{
friend class Game;
friend class Scene;
friend class Collider;
@ -799,9 +809,6 @@ namespace e2d
// 获取所有任务
const Tasks& GetAllTasks() const;
// 更新节点时间
void UpdateTime();
// 分发鼠标消息
virtual bool Dispatch(
const MouseEvent& e,
@ -814,12 +821,12 @@ namespace e2d
bool handled
) override;
// 遍历节点
virtual void Visit();
protected:
E2D_DISABLE_COPY(Node);
// 遍历节点
virtual void Visit();
// 渲染节点边缘
void DrawBorder();
@ -843,6 +850,9 @@ namespace e2d
// 更新任务
void UpdateTasks();
// 更新节点时间
void UpdateTime();
protected:
String name_;
size_t hash_name_;

View File

@ -48,6 +48,7 @@ namespace e2d
float duration_;
float delta_;
Time started_;
Size window_size_;
Scene* out_scene_;
Scene* in_scene_;
ID2D1Layer * out_layer_;

View File

@ -90,10 +90,10 @@ void e2d::Node::Visit()
UpdateActions();
UpdateTasks();
auto updatableNode = dynamic_cast<Updatable*>(this);
if (updatableNode)
auto updatable_node = dynamic_cast<Updatable*>(this);
if (updatable_node)
{
updatableNode->Update();
updatable_node->Update();
}
}
@ -112,11 +112,11 @@ void e2d::Node::Visit()
if (children_.empty())
{
auto drawableNode = dynamic_cast<Drawable*>(this);
if (drawableNode)
auto drawable_node = dynamic_cast<Drawable*>(this);
if (drawable_node)
{
render_target->SetTransform(final_matrix_);
drawableNode->Draw();
drawable_node->Draw();
}
}
else
@ -148,11 +148,11 @@ void e2d::Node::Visit()
}
}
auto drawableNode = dynamic_cast<Drawable*>(this);
if (drawableNode)
auto drawable_node = dynamic_cast<Drawable*>(this);
if (drawable_node)
{
render_target->SetTransform(final_matrix_);
drawableNode->Draw();
drawable_node->Draw();
}
// 访问剩余节点
@ -229,11 +229,19 @@ void e2d::Node::UpdateTransform()
);
// 根据自身锚点变换 Final 矩阵
final_matrix_ = initial_matrix_ * D2D1::Matrix3x2F::Translation(-anchor.x, -anchor.y);
// ºÍ¸¸½Úµã¾ØÕóÏà³Ë
if (!fixed_position_ && parent_)
if (parent_)
{
initial_matrix_ = initial_matrix_ * parent_->initial_matrix_;
final_matrix_ = final_matrix_ * parent_->initial_matrix_;
if (!fixed_position_)
{
initial_matrix_ = initial_matrix_ * parent_->initial_matrix_;
final_matrix_ = final_matrix_ * parent_->initial_matrix_;
}
}
else if (parent_scene_)
{
initial_matrix_ = initial_matrix_ * parent_scene_->GetTransform();
final_matrix_ = final_matrix_ * parent_scene_->GetTransform();
}
// 重新构造轮廓
@ -613,8 +621,8 @@ void e2d::Node::SetAnchor(float anchor_x, float anchor_y)
if (anchor_.x == anchor_x && anchor_.y == anchor_y)
return;
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_.x = anchor_x;
anchor_.y = anchor_y;
dirty_transform_ = true;
}

View File

@ -5,6 +5,7 @@ e2d::Scene::Scene()
: root_(nullptr)
, border_visible_(false)
, collider_visible_(false)
, transform_(D2D1::Matrix3x2F::Identity())
{
}
@ -12,6 +13,7 @@ e2d::Scene::Scene(Node * root)
: root_(nullptr)
, border_visible_(false)
, collider_visible_(false)
, transform_(D2D1::Matrix3x2F::Identity())
{
this->SetRoot(root);
}
@ -98,3 +100,18 @@ bool e2d::Scene::Dispatch(const KeyEvent & e, bool handled)
}
return false;
}
void e2d::Scene::SetTransform(const D2D1::Matrix3x2F& matrix)
{
transform_ = matrix;
if (root_)
{
root_->dirty_transform_ = true;
}
}
const D2D1::Matrix3x2F & e2d::Scene::GetTransform() const
{
return transform_;
}

View File

@ -12,44 +12,67 @@ void e2d::MoveTransition::Init(Scene * prev, Scene * next)
{
Transition::Init(prev, next);
auto size = Window::GetInstance()->GetSize();
if (direction_ == Direction::Up)
{
pos_delta_ = Point(0, -size.height);
start_pos_ = Point(0, size.height);
pos_delta_ = Point(0, -window_size_.height);
start_pos_ = Point(0, window_size_.height);
}
else if (direction_ == Direction::Down)
{
pos_delta_ = Point(0, size.height);
start_pos_ = Point(0, -size.height);
pos_delta_ = Point(0, window_size_.height);
start_pos_ = Point(0, -window_size_.height);
}
else if (direction_ == Direction::Left)
{
pos_delta_ = Point(-size.width, 0);
start_pos_ = Point(size.width, 0);
pos_delta_ = Point(-window_size_.width, 0);
start_pos_ = Point(window_size_.width, 0);
}
else if (direction_ == Direction::Right)
{
pos_delta_ = Point(size.width, 0);
start_pos_ = Point(-size.width, 0);
pos_delta_ = Point(window_size_.width, 0);
start_pos_ = Point(-window_size_.width, 0);
}
if (out_scene_ && out_scene_->GetRoot()) out_scene_->GetRoot()->SetPos(0, 0);
if (in_scene_->GetRoot()) in_scene_->GetRoot()->SetPos(start_pos_);
if (out_scene_)
{
out_scene_->SetTransform(D2D1::Matrix3x2F::Identity());
}
if (in_scene_)
{
in_scene_->SetTransform(
D2D1::Matrix3x2F::Translation(
start_pos_.x,
start_pos_.y
)
);
}
}
void e2d::MoveTransition::Update()
{
Transition::Update();
if (out_scene_ && out_scene_->GetRoot())
if (out_scene_)
{
out_scene_->GetRoot()->SetPos(pos_delta_ * delta_);
auto translation = pos_delta_ * delta_;
out_scene_->SetTransform(
D2D1::Matrix3x2F::Translation(
translation.x,
translation.y
)
);
}
if (in_scene_->GetRoot())
if (in_scene_)
{
in_scene_->GetRoot()->SetPos(start_pos_ + pos_delta_ * delta_);
auto translation = start_pos_ + pos_delta_ * delta_;
in_scene_->SetTransform(
D2D1::Matrix3x2F::Translation(
translation.x,
translation.y
)
);
}
if (delta_ >= 1)
@ -60,13 +83,13 @@ void e2d::MoveTransition::Update()
void e2d::MoveTransition::Reset()
{
if (out_scene_ && out_scene_->GetRoot())
if (out_scene_)
{
out_scene_->GetRoot()->SetPos(0, 0);
out_scene_->SetTransform(D2D1::Matrix3x2F::Identity());
}
if (in_scene_->GetRoot())
if (in_scene_)
{
in_scene_->GetRoot()->SetPos(0, 0);
in_scene_->SetTransform(D2D1::Matrix3x2F::Identity());
}
}

View File

@ -6,6 +6,7 @@ e2d::Transition::Transition(float duration)
: done_(false)
, started_()
, delta_(0)
, window_size_()
, out_scene_(nullptr)
, in_scene_(nullptr)
, out_layer_(nullptr)
@ -56,8 +57,14 @@ void e2d::Transition::Init(Scene * prev, Scene * next)
);
}
window_size_ = Window::GetInstance()->GetSize();
out_layer_param_ = in_layer_param_ = D2D1::LayerParameters(
D2D1::InfiniteRect(),
D2D1::RectF(
0.f,
0.f,
window_size_.width,
window_size_.height
),
nullptr,
D2D1_ANTIALIAS_MODE_PER_PRIMITIVE,
D2D1::Matrix3x2F::Identity(),
@ -83,19 +90,19 @@ void e2d::Transition::Update()
void e2d::Transition::Draw()
{
auto render_target = Renderer::GetInstance()->GetRenderTarget();
auto size = Window::GetInstance()->GetSize();
if (out_scene_ && out_scene_->GetRoot())
if (out_scene_)
{
auto root_pos = out_scene_->GetRoot()->GetPos();
auto clip_rect = D2D1::RectF(
std::max(root_pos.x, 0.f),
std::max(root_pos.y, 0.f),
std::min(root_pos.x + size.width, size.width),
std::min(root_pos.y + size.height, size.height)
render_target->SetTransform(out_scene_->GetTransform());
render_target->PushAxisAlignedClip(
D2D1::RectF(
0.f,
0.f,
window_size_.width,
window_size_.height
),
D2D1_ANTIALIAS_MODE_PER_PRIMITIVE
);
render_target->SetTransform(D2D1::Matrix3x2F::Identity());
render_target->PushAxisAlignedClip(clip_rect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
render_target->PushLayer(out_layer_param_, out_layer_);
out_scene_->Draw();
@ -104,17 +111,18 @@ void e2d::Transition::Draw()
render_target->PopAxisAlignedClip();
}
if (in_scene_ && in_scene_->GetRoot())
if (in_scene_)
{
Point root_pos = in_scene_->GetRoot()->GetPos();
auto clip_rect = D2D1::RectF(
std::max(root_pos.x, 0.f),
std::max(root_pos.y, 0.f),
std::min(root_pos.x + size.width, size.width),
std::min(root_pos.y + size.height, size.height)
render_target->SetTransform(in_scene_->GetTransform());
render_target->PushAxisAlignedClip(
D2D1::RectF(
0.f,
0.f,
window_size_.width,
window_size_.height
),
D2D1_ANTIALIAS_MODE_PER_PRIMITIVE
);
render_target->SetTransform(D2D1::Matrix3x2F::Identity());
render_target->PushAxisAlignedClip(clip_rect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
render_target->PushLayer(in_layer_param_, in_layer_);
in_scene_->Draw();