refactoring: MoveTransition
This commit is contained in:
		
							parent
							
								
									6f489a77fe
								
							
						
					
					
						commit
						a626e488de
					
				|  | @ -77,15 +77,6 @@ namespace e2d | ||||||
| 			float anchor_y | 			float anchor_y | ||||||
| 		) override; | 		) override; | ||||||
| 
 | 
 | ||||||
| 		// 分发鼠标消息
 |  | ||||||
| 		virtual bool Dispatch( |  | ||||||
| 			const MouseEvent& e, |  | ||||||
| 			bool handled |  | ||||||
| 		) override; |  | ||||||
| 
 |  | ||||||
| 		// 遍历节点
 |  | ||||||
| 		virtual void Visit() override; |  | ||||||
| 
 |  | ||||||
| 	protected: | 	protected: | ||||||
| 		E2D_DISABLE_COPY(Button); | 		E2D_DISABLE_COPY(Button); | ||||||
| 
 | 
 | ||||||
|  | @ -100,6 +91,15 @@ namespace e2d | ||||||
| 		// 刷新按钮显示
 | 		// 刷新按钮显示
 | ||||||
| 		virtual void UpdateVisible(); | 		virtual void UpdateVisible(); | ||||||
| 
 | 
 | ||||||
|  | 		// 分发鼠标消息
 | ||||||
|  | 		virtual bool Dispatch( | ||||||
|  | 			const MouseEvent& e, | ||||||
|  | 			bool handled | ||||||
|  | 		) override; | ||||||
|  | 
 | ||||||
|  | 		// 遍历节点
 | ||||||
|  | 		virtual void Visit() override; | ||||||
|  | 
 | ||||||
| 	protected: | 	protected: | ||||||
| 		Node * normal_; | 		Node * normal_; | ||||||
| 		Node *		mouseover_; | 		Node *		mouseover_; | ||||||
|  |  | ||||||
|  | @ -169,13 +169,22 @@ namespace e2d | ||||||
| 			bool handled | 			bool handled | ||||||
| 		) override; | 		) override; | ||||||
| 
 | 
 | ||||||
|  | 		// 设置转换矩阵
 | ||||||
|  | 		void SetTransform( | ||||||
|  | 			const D2D1::Matrix3x2F& matrix | ||||||
|  | 		); | ||||||
|  | 
 | ||||||
|  | 		// 获取转换矩阵
 | ||||||
|  | 		const D2D1::Matrix3x2F& GetTransform() const; | ||||||
|  | 
 | ||||||
| 	protected: | 	protected: | ||||||
| 		E2D_DISABLE_COPY(Scene); | 		E2D_DISABLE_COPY(Scene); | ||||||
| 
 | 
 | ||||||
| 	protected: | 	protected: | ||||||
| 		Node * root_; | 		Node*	root_; | ||||||
| 		bool	border_visible_; | 		bool	border_visible_; | ||||||
| 		bool	collider_visible_; | 		bool	collider_visible_; | ||||||
|  | 		D2D1::Matrix3x2F transform_; | ||||||
| 	}; | 	}; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -410,6 +419,7 @@ namespace e2d | ||||||
| 		: public Ref | 		: public Ref | ||||||
| 		, public EventTarget | 		, public EventTarget | ||||||
| 	{ | 	{ | ||||||
|  | 		friend class Game; | ||||||
| 		friend class Scene; | 		friend class Scene; | ||||||
| 		friend class Collider; | 		friend class Collider; | ||||||
| 
 | 
 | ||||||
|  | @ -799,9 +809,6 @@ namespace e2d | ||||||
| 		// 获取所有任务
 | 		// 获取所有任务
 | ||||||
| 		const Tasks& GetAllTasks() const; | 		const Tasks& GetAllTasks() const; | ||||||
| 
 | 
 | ||||||
| 		// 更新节点时间
 |  | ||||||
| 		void UpdateTime(); |  | ||||||
| 
 |  | ||||||
| 		// 分发鼠标消息
 | 		// 分发鼠标消息
 | ||||||
| 		virtual bool Dispatch( | 		virtual bool Dispatch( | ||||||
| 			const MouseEvent& e, | 			const MouseEvent& e, | ||||||
|  | @ -814,12 +821,12 @@ namespace e2d | ||||||
| 			bool handled | 			bool handled | ||||||
| 		) override; | 		) override; | ||||||
| 
 | 
 | ||||||
| 		// 遍历节点
 |  | ||||||
| 		virtual void Visit(); |  | ||||||
| 
 |  | ||||||
| 	protected: | 	protected: | ||||||
| 		E2D_DISABLE_COPY(Node); | 		E2D_DISABLE_COPY(Node); | ||||||
| 
 | 
 | ||||||
|  | 		// 遍历节点
 | ||||||
|  | 		virtual void Visit(); | ||||||
|  | 
 | ||||||
| 		// 渲染节点边缘
 | 		// 渲染节点边缘
 | ||||||
| 		void DrawBorder(); | 		void DrawBorder(); | ||||||
| 
 | 
 | ||||||
|  | @ -843,6 +850,9 @@ namespace e2d | ||||||
| 		// 更新任务
 | 		// 更新任务
 | ||||||
| 		void UpdateTasks(); | 		void UpdateTasks(); | ||||||
| 
 | 
 | ||||||
|  | 		// 更新节点时间
 | ||||||
|  | 		void UpdateTime(); | ||||||
|  | 
 | ||||||
| 	protected: | 	protected: | ||||||
| 		String		name_; | 		String		name_; | ||||||
| 		size_t		hash_name_; | 		size_t		hash_name_; | ||||||
|  |  | ||||||
|  | @ -48,6 +48,7 @@ namespace e2d | ||||||
| 		float	duration_; | 		float	duration_; | ||||||
| 		float	delta_; | 		float	delta_; | ||||||
| 		Time	started_; | 		Time	started_; | ||||||
|  | 		Size	window_size_; | ||||||
| 		Scene*	out_scene_; | 		Scene*	out_scene_; | ||||||
| 		Scene*	in_scene_; | 		Scene*	in_scene_; | ||||||
| 		ID2D1Layer * out_layer_; | 		ID2D1Layer * out_layer_; | ||||||
|  |  | ||||||
|  | @ -90,10 +90,10 @@ void e2d::Node::Visit() | ||||||
| 		UpdateActions(); | 		UpdateActions(); | ||||||
| 		UpdateTasks(); | 		UpdateTasks(); | ||||||
| 
 | 
 | ||||||
| 		auto updatableNode = dynamic_cast<Updatable*>(this); | 		auto updatable_node = dynamic_cast<Updatable*>(this); | ||||||
| 		if (updatableNode) | 		if (updatable_node) | ||||||
| 		{ | 		{ | ||||||
| 			updatableNode->Update(); | 			updatable_node->Update(); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -112,11 +112,11 @@ void e2d::Node::Visit() | ||||||
| 
 | 
 | ||||||
| 	if (children_.empty()) | 	if (children_.empty()) | ||||||
| 	{ | 	{ | ||||||
| 		auto drawableNode = dynamic_cast<Drawable*>(this); | 		auto drawable_node = dynamic_cast<Drawable*>(this); | ||||||
| 		if (drawableNode) | 		if (drawable_node) | ||||||
| 		{ | 		{ | ||||||
| 			render_target->SetTransform(final_matrix_); | 			render_target->SetTransform(final_matrix_); | ||||||
| 			drawableNode->Draw(); | 			drawable_node->Draw(); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	else | 	else | ||||||
|  | @ -148,11 +148,11 @@ void e2d::Node::Visit() | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
| 		auto drawableNode = dynamic_cast<Drawable*>(this); | 		auto drawable_node = dynamic_cast<Drawable*>(this); | ||||||
| 		if (drawableNode) | 		if (drawable_node) | ||||||
| 		{ | 		{ | ||||||
| 			render_target->SetTransform(final_matrix_); | 			render_target->SetTransform(final_matrix_); | ||||||
| 			drawableNode->Draw(); | 			drawable_node->Draw(); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		// 访问剩余节点
 | 		// 访问剩余节点
 | ||||||
|  | @ -229,12 +229,20 @@ void e2d::Node::UpdateTransform() | ||||||
| 	); | 	); | ||||||
| 	// 根据自身锚点变换 Final 矩阵
 | 	// 根据自身锚点变换 Final 矩阵
 | ||||||
| 	final_matrix_ = initial_matrix_ * D2D1::Matrix3x2F::Translation(-anchor.x, -anchor.y); | 	final_matrix_ = initial_matrix_ * D2D1::Matrix3x2F::Translation(-anchor.x, -anchor.y); | ||||||
| 	// 和父节点矩阵相乘
 | 
 | ||||||
| 	if (!fixed_position_ && parent_) | 	if (parent_) | ||||||
|  | 	{ | ||||||
|  | 		if (!fixed_position_) | ||||||
| 		{ | 		{ | ||||||
| 			initial_matrix_ = initial_matrix_ * parent_->initial_matrix_; | 			initial_matrix_ = initial_matrix_ * parent_->initial_matrix_; | ||||||
| 			final_matrix_ = final_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(); | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	// 重新构造轮廓
 | 	// 重新构造轮廓
 | ||||||
| 	SafeRelease(border_); | 	SafeRelease(border_); | ||||||
|  | @ -613,8 +621,8 @@ void e2d::Node::SetAnchor(float anchor_x, float anchor_y) | ||||||
| 	if (anchor_.x == anchor_x && anchor_.y == anchor_y) | 	if (anchor_.x == anchor_x && anchor_.y == anchor_y) | ||||||
| 		return; | 		return; | ||||||
| 
 | 
 | ||||||
| 	anchor_.x = std::min(std::max(anchor_x, 0.f), 1.f); | 	anchor_.x = anchor_x; | ||||||
| 	anchor_.y = std::min(std::max(anchor_y, 0.f), 1.f); | 	anchor_.y = anchor_y; | ||||||
| 	dirty_transform_ = true; | 	dirty_transform_ = true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -5,6 +5,7 @@ e2d::Scene::Scene() | ||||||
| 	: root_(nullptr) | 	: root_(nullptr) | ||||||
| 	, border_visible_(false) | 	, border_visible_(false) | ||||||
| 	, collider_visible_(false) | 	, collider_visible_(false) | ||||||
|  | 	, transform_(D2D1::Matrix3x2F::Identity()) | ||||||
| { | { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -12,6 +13,7 @@ e2d::Scene::Scene(Node * root) | ||||||
| 	: root_(nullptr) | 	: root_(nullptr) | ||||||
| 	, border_visible_(false) | 	, border_visible_(false) | ||||||
| 	, collider_visible_(false) | 	, collider_visible_(false) | ||||||
|  | 	, transform_(D2D1::Matrix3x2F::Identity()) | ||||||
| { | { | ||||||
| 	this->SetRoot(root); | 	this->SetRoot(root); | ||||||
| } | } | ||||||
|  | @ -98,3 +100,18 @@ bool e2d::Scene::Dispatch(const KeyEvent & e, bool handled) | ||||||
| 	} | 	} | ||||||
| 	return false; | 	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_; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -12,44 +12,67 @@ void e2d::MoveTransition::Init(Scene * prev, Scene * next) | ||||||
| { | { | ||||||
| 	Transition::Init(prev, next); | 	Transition::Init(prev, next); | ||||||
| 	 | 	 | ||||||
| 	auto size = Window::GetInstance()->GetSize(); |  | ||||||
| 	if (direction_ == Direction::Up) | 	if (direction_ == Direction::Up) | ||||||
| 	{ | 	{ | ||||||
| 		pos_delta_ = Point(0, -size.height); | 		pos_delta_ = Point(0, -window_size_.height); | ||||||
| 		start_pos_ = Point(0, size.height); | 		start_pos_ = Point(0, window_size_.height); | ||||||
| 	} | 	} | ||||||
| 	else if (direction_ == Direction::Down) | 	else if (direction_ == Direction::Down) | ||||||
| 	{ | 	{ | ||||||
| 		pos_delta_ = Point(0, size.height); | 		pos_delta_ = Point(0, window_size_.height); | ||||||
| 		start_pos_ = Point(0, -size.height); | 		start_pos_ = Point(0, -window_size_.height); | ||||||
| 	} | 	} | ||||||
| 	else if (direction_ == Direction::Left) | 	else if (direction_ == Direction::Left) | ||||||
| 	{ | 	{ | ||||||
| 		pos_delta_ = Point(-size.width, 0); | 		pos_delta_ = Point(-window_size_.width, 0); | ||||||
| 		start_pos_ = Point(size.width, 0); | 		start_pos_ = Point(window_size_.width, 0); | ||||||
| 	} | 	} | ||||||
| 	else if (direction_ == Direction::Right) | 	else if (direction_ == Direction::Right) | ||||||
| 	{ | 	{ | ||||||
| 		pos_delta_ = Point(size.width, 0); | 		pos_delta_ = Point(window_size_.width, 0); | ||||||
| 		start_pos_ = Point(-size.width, 0); | 		start_pos_ = Point(-window_size_.width, 0); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if (out_scene_ && out_scene_->GetRoot()) out_scene_->GetRoot()->SetPos(0, 0); | 	if (out_scene_) | ||||||
| 	if (in_scene_->GetRoot()) in_scene_->GetRoot()->SetPos(start_pos_); | 	{ | ||||||
|  | 		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() | void e2d::MoveTransition::Update() | ||||||
| { | { | ||||||
| 	Transition::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) | 	if (delta_ >= 1) | ||||||
|  | @ -60,13 +83,13 @@ void e2d::MoveTransition::Update() | ||||||
| 
 | 
 | ||||||
| void e2d::MoveTransition::Reset() | 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()); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -6,6 +6,7 @@ e2d::Transition::Transition(float duration) | ||||||
| 	: done_(false) | 	: done_(false) | ||||||
| 	, started_() | 	, started_() | ||||||
| 	, delta_(0) | 	, delta_(0) | ||||||
|  | 	, window_size_() | ||||||
| 	, out_scene_(nullptr) | 	, out_scene_(nullptr) | ||||||
| 	, in_scene_(nullptr) | 	, in_scene_(nullptr) | ||||||
| 	, out_layer_(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( | 	out_layer_param_ = in_layer_param_ = D2D1::LayerParameters( | ||||||
| 		D2D1::InfiniteRect(), | 		D2D1::RectF( | ||||||
|  | 			0.f, | ||||||
|  | 			0.f, | ||||||
|  | 			window_size_.width, | ||||||
|  | 			window_size_.height | ||||||
|  | 		), | ||||||
| 		nullptr, | 		nullptr, | ||||||
| 		D2D1_ANTIALIAS_MODE_PER_PRIMITIVE, | 		D2D1_ANTIALIAS_MODE_PER_PRIMITIVE, | ||||||
| 		D2D1::Matrix3x2F::Identity(), | 		D2D1::Matrix3x2F::Identity(), | ||||||
|  | @ -83,19 +90,19 @@ void e2d::Transition::Update() | ||||||
| void e2d::Transition::Draw() | void e2d::Transition::Draw() | ||||||
| { | { | ||||||
| 	auto render_target = Renderer::GetInstance()->GetRenderTarget(); | 	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(); | 		render_target->SetTransform(out_scene_->GetTransform()); | ||||||
| 		auto clip_rect = D2D1::RectF( | 		render_target->PushAxisAlignedClip( | ||||||
| 			std::max(root_pos.x, 0.f), | 			D2D1::RectF( | ||||||
| 			std::max(root_pos.y, 0.f), | 				0.f, | ||||||
| 			std::min(root_pos.x + size.width, size.width), | 				0.f, | ||||||
| 			std::min(root_pos.y + size.height, size.height) | 				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_); | 		render_target->PushLayer(out_layer_param_, out_layer_); | ||||||
| 
 | 
 | ||||||
| 		out_scene_->Draw(); | 		out_scene_->Draw(); | ||||||
|  | @ -104,17 +111,18 @@ void e2d::Transition::Draw() | ||||||
| 		render_target->PopAxisAlignedClip(); | 		render_target->PopAxisAlignedClip(); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if (in_scene_ && in_scene_->GetRoot()) | 	if (in_scene_) | ||||||
| 	{ | 	{ | ||||||
| 		Point root_pos = in_scene_->GetRoot()->GetPos(); | 		render_target->SetTransform(in_scene_->GetTransform()); | ||||||
| 		auto clip_rect = D2D1::RectF( | 		render_target->PushAxisAlignedClip( | ||||||
| 			std::max(root_pos.x, 0.f), | 			D2D1::RectF( | ||||||
| 			std::max(root_pos.y, 0.f), | 				0.f, | ||||||
| 			std::min(root_pos.x + size.width, size.width), | 				0.f, | ||||||
| 			std::min(root_pos.y + size.height, size.height) | 				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_); | 		render_target->PushLayer(in_layer_param_, in_layer_); | ||||||
| 
 | 
 | ||||||
| 		in_scene_->Draw(); | 		in_scene_->Draw(); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue