| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | // 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:
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | // 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-10-11 21:55:29 +08:00
										 |  |  | #include <kiwano/2d/Actor.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | #include <kiwano/2d/DebugActor.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-11 21:55:29 +08:00
										 |  |  | #include <kiwano/2d/Stage.h>
 | 
					
						
							|  |  |  | #include <kiwano/2d/Transition.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | #include <kiwano/core/Director.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-17 16:55:47 +08:00
										 |  |  | #include <kiwano/render/RenderContext.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace kiwano | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | Director::Director() | 
					
						
							|  |  |  |     : render_border_enabled_(false) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Director::~Director() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Director::EnterStage(StagePtr stage, TransitionPtr transition) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KGE_ASSERT(stage && "Director::EnterStage failed, NULL pointer exception"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (current_stage_ == stage || next_stage_ == stage) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     next_stage_ = stage; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (transition && next_stage_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (transition_) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             transition_->Stop(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         transition_ = transition; | 
					
						
							|  |  |  |         transition_->Init(current_stage_, next_stage_); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Director::PushStage(StagePtr stage, TransitionPtr transition) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     EnterStage(stage, transition); | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     if (current_stage_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         stages_.push(current_stage_); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Director::PopStage(TransitionPtr transition) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     KGE_ASSERT(!stages_.empty() && "Director::PopStage failed, calling pop() on empty stage stack"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!stages_.empty()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         next_stage_ = stages_.top(); | 
					
						
							|  |  |  |         stages_.pop(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (transition && next_stage_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (transition_) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             transition_->Stop(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         transition_ = transition; | 
					
						
							|  |  |  |         transition_->Init(current_stage_, next_stage_); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | StagePtr Director::GetCurrentStage() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return current_stage_; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Director::SetRenderBorderEnabled(bool enabled) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     render_border_enabled_ = enabled; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Director::ShowDebugInfo(bool show) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (show) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!debug_actor_) | 
					
						
							|  |  |  |             debug_actor_ = new DebugActor; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |         debug_actor_.Reset(); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void Director::ClearStages() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     while (!stages_.empty()) | 
					
						
							|  |  |  |         stages_.pop(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |     current_stage_.Reset(); | 
					
						
							|  |  |  |     next_stage_.Reset(); | 
					
						
							|  |  |  |     transition_.Reset(); | 
					
						
							|  |  |  |     debug_actor_.Reset(); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Director::OnUpdate(Duration dt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (transition_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         transition_->Update(dt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (transition_->IsDone()) | 
					
						
							|  |  |  |             transition_ = nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (next_stage_ && !transition_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (current_stage_) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             current_stage_->OnExit(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         next_stage_->OnEnter(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         current_stage_ = next_stage_; | 
					
						
							|  |  |  |         next_stage_    = nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (current_stage_) | 
					
						
							|  |  |  |         current_stage_->Update(dt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (next_stage_) | 
					
						
							|  |  |  |         next_stage_->Update(dt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (debug_actor_) | 
					
						
							|  |  |  |         debug_actor_->Update(dt); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Director::OnRender(RenderContext& ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (transition_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         transition_->Render(ctx); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (current_stage_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         current_stage_->Render(ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (render_border_enabled_) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             current_stage_->RenderBorder(ctx); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (debug_actor_) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         debug_actor_->Render(ctx); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Director::HandleEvent(Event* evt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (current_stage_) | 
					
						
							|  |  |  |         current_stage_->DispatchEvent(evt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (next_stage_) | 
					
						
							|  |  |  |         next_stage_->DispatchEvent(evt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (debug_actor_) | 
					
						
							|  |  |  |         debug_actor_->DispatchEvent(evt); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace kiwano
 |