| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | // Copyright (c) 2016-2018 Kiwano - 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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | #include "Director.h"
 | 
					
						
							|  |  |  | #include "../2d/Actor.h"
 | 
					
						
							|  |  |  | #include "../2d/Stage.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | #include "../2d/Transition.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-13 23:21:20 +08:00
										 |  |  | #include "../2d/DebugActor.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace kiwano | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	Director::Director() | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 		: render_border_enabled_(false) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	Director::~Director() | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Director::EnterStage(StagePtr scene) | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		KGE_ASSERT(scene && "Director::EnterStage failed, NULL pointer exception"); | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (curr_scene_ == scene || next_scene_ == scene) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		next_scene_ = scene; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Director::EnterStage(StagePtr scene, TransitionPtr transition) | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		EnterStage(scene); | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (transition && next_scene_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (transition_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				transition_->Stop(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			transition_ = transition; | 
					
						
							|  |  |  | 			transition_->Init(curr_scene_, next_scene_); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	StagePtr Director::GetCurrentStage() | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return curr_scene_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Director::SetRenderBorderEnabled(bool enabled) | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		render_border_enabled_ = enabled; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Director::ShowDebugInfo(bool show) | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (show) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 			if (!debug_actor_) | 
					
						
							| 
									
										
										
										
											2019-08-14 00:28:25 +08:00
										 |  |  | 				debug_actor_ = new DebugActor; | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 			debug_actor_.reset(); | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 	void Director::ClearStages() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		curr_scene_.reset(); | 
					
						
							|  |  |  | 		next_scene_.reset(); | 
					
						
							|  |  |  | 		debug_actor_.reset(); | 
					
						
							|  |  |  | 		transition_.reset(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Director::OnUpdate(Duration dt) | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (transition_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			transition_->Update(dt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (transition_->IsDone()) | 
					
						
							|  |  |  | 				transition_ = nullptr; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (next_scene_ && !transition_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (curr_scene_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				curr_scene_->OnExit(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			next_scene_->OnEnter(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			curr_scene_ = next_scene_; | 
					
						
							|  |  |  | 			next_scene_ = nullptr; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (curr_scene_) | 
					
						
							|  |  |  | 			curr_scene_->Update(dt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (next_scene_) | 
					
						
							|  |  |  | 			next_scene_->Update(dt); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 		if (debug_actor_) | 
					
						
							|  |  |  | 			debug_actor_->Update(dt); | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 00:28:25 +08:00
										 |  |  | 	void Director::OnRender(Renderer* renderer) | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (transition_) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-14 00:28:25 +08:00
										 |  |  | 			transition_->Render(renderer); | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else if (curr_scene_) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-14 00:28:25 +08:00
										 |  |  | 			curr_scene_->Render(renderer); | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 		if (debug_actor_) | 
					
						
							| 
									
										
										
										
											2019-08-14 00:28:25 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			debug_actor_->Render(renderer); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Director::AfterRender() | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (render_border_enabled_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (curr_scene_) | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 				curr_scene_->RenderBorder(); | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Director::HandleEvent(Event& evt) | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 		if (debug_actor_) | 
					
						
							|  |  |  | 			debug_actor_->Dispatch(evt); | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (curr_scene_) | 
					
						
							|  |  |  | 			curr_scene_->Dispatch(evt); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |