Magic_Game/src/kiwano/core/Director.cpp

192 lines
3.9 KiB
C++
Raw Normal View History

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.
2020-01-16 18:33:42 +08:00
#include <kiwano/core/Director.h>
2019-10-11 21:55:29 +08:00
#include <kiwano/2d/Actor.h>
#include <kiwano/2d/Stage.h>
#include <kiwano/2d/Transition.h>
#include <kiwano/2d/DebugActor.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/RenderContext.h>
2019-08-12 14:51:54 +08:00
namespace kiwano
{
Director::Director()
2019-08-12 14:51:54 +08:00
: render_border_enabled_(false)
{
}
Director::~Director()
2019-08-12 14:51:54 +08:00
{
}
2019-10-14 10:43:11 +08:00
void Director::EnterStage(StagePtr stage, TransitionPtr transition)
2019-08-12 14:51:54 +08:00
{
2019-08-20 19:32:36 +08:00
KGE_ASSERT(stage && "Director::EnterStage failed, NULL pointer exception");
2019-08-12 14:51:54 +08:00
2019-10-14 10:43:11 +08:00
if (current_stage_ == stage || next_stage_ == stage)
2019-08-12 14:51:54 +08:00
return;
2019-08-20 19:32:36 +08:00
next_stage_ = stage;
2019-10-14 10:43:11 +08:00
if (transition && next_stage_)
{
if (transition_)
{
transition_->Stop();
}
transition_ = transition;
transition_->Init(current_stage_, next_stage_);
}
2019-08-12 14:51:54 +08:00
}
2019-10-14 10:43:11 +08:00
void Director::PushStage(StagePtr stage, TransitionPtr transition)
2019-08-12 14:51:54 +08:00
{
2019-10-14 10:43:11 +08:00
EnterStage(stage, transition);
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();
}
2019-08-12 14:51:54 +08:00
2019-08-20 19:32:36 +08:00
if (transition && next_stage_)
2019-08-12 14:51:54 +08:00
{
if (transition_)
{
transition_->Stop();
}
transition_ = transition;
2019-10-14 10:43:11 +08:00
transition_->Init(current_stage_, next_stage_);
2019-08-12 14:51:54 +08:00
}
}
StagePtr Director::GetCurrentStage()
2019-08-12 14:51:54 +08:00
{
2019-10-14 10:43:11 +08:00
return current_stage_;
2019-08-12 14:51:54 +08:00
}
void Director::SetRenderBorderEnabled(bool enabled)
2019-08-12 14:51:54 +08:00
{
render_border_enabled_ = enabled;
}
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()
{
2019-10-14 10:43:11 +08:00
while (!stages_.empty())
stages_.pop();
current_stage_.reset();
2019-08-20 19:32:36 +08:00
next_stage_.reset();
2019-08-13 21:16:38 +08:00
transition_.reset();
2020-01-17 16:46:17 +08:00
debug_actor_.reset();
2019-08-13 21:16:38 +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;
}
2019-08-20 19:32:36 +08:00
if (next_stage_ && !transition_)
2019-08-12 14:51:54 +08:00
{
2019-10-14 10:43:11 +08:00
if (current_stage_)
2019-08-12 14:51:54 +08:00
{
2019-10-14 10:43:11 +08:00
current_stage_->OnExit();
2019-08-12 14:51:54 +08:00
}
2019-08-20 19:32:36 +08:00
next_stage_->OnEnter();
2019-08-12 14:51:54 +08:00
2019-10-14 10:43:11 +08:00
current_stage_ = next_stage_;
2019-08-20 19:32:36 +08:00
next_stage_ = nullptr;
2019-08-12 14:51:54 +08:00
}
2019-10-14 10:43:11 +08:00
if (current_stage_)
current_stage_->Update(dt);
2019-08-12 14:51:54 +08:00
2019-08-20 19:32:36 +08:00
if (next_stage_)
next_stage_->Update(dt);
2019-08-12 14:51:54 +08:00
2019-08-13 21:16:38 +08:00
if (debug_actor_)
debug_actor_->Update(dt);
2019-08-12 14:51:54 +08:00
}
2020-01-10 15:22:12 +08:00
void Director::OnRender(RenderContext& ctx)
2019-08-12 14:51:54 +08:00
{
if (transition_)
{
2020-01-10 15:22:12 +08:00
transition_->Render(ctx);
2019-08-12 14:51:54 +08:00
}
2019-10-14 10:43:11 +08:00
else if (current_stage_)
2019-08-12 14:51:54 +08:00
{
2020-01-10 15:22:12 +08:00
current_stage_->Render(ctx);
2019-08-12 14:51:54 +08:00
2020-01-17 16:46:17 +08:00
if (render_border_enabled_)
2019-08-15 11:22:51 +08:00
{
2020-01-10 15:22:12 +08:00
current_stage_->RenderBorder(ctx);
2019-08-15 11:22:51 +08:00
}
2019-08-12 14:51:54 +08:00
}
2019-08-20 19:32:36 +08:00
if (debug_actor_)
{
2020-01-10 15:22:12 +08:00
debug_actor_->Render(ctx);
2019-08-20 19:32:36 +08:00
}
2019-08-12 14:51:54 +08:00
}
2020-01-16 18:33:42 +08:00
void Director::HandleEvent(Event* evt)
2019-08-12 14:51:54 +08:00
{
2019-10-14 10:43:11 +08:00
if (current_stage_)
2020-01-10 18:08:54 +08:00
current_stage_->DispatchEvent(evt);
2020-01-09 08:45:00 +08:00
if (next_stage_)
2020-01-10 18:08:54 +08:00
next_stage_->DispatchEvent(evt);
2020-01-09 08:45:00 +08:00
if (debug_actor_)
2020-01-10 18:08:54 +08:00
debug_actor_->DispatchEvent(evt);
2019-08-12 14:51:54 +08:00
}
}