Magic_Game/src/kiwano/base/Director.cpp

164 lines
3.3 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.
#include "Director.h"
#include "../2d/Actor.h"
#include "../2d/Stage.h"
2019-08-12 14:51:54 +08:00
#include "../2d/Transition.h"
#include "../2d/DebugActor.h"
2019-08-20 19:32:36 +08:00
#include "../renderer/RenderTarget.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-08-20 19:32:36 +08:00
void Director::EnterStage(StagePtr stage)
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-08-20 19:32:36 +08:00
if (curr_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-08-12 14:51:54 +08:00
}
2019-08-20 19:32:36 +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
EnterStage(stage);
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-08-20 19:32:36 +08:00
transition_->Init(curr_stage_, next_stage_);
2019-08-12 14:51:54 +08:00
}
}
StagePtr Director::GetCurrentStage()
2019-08-12 14:51:54 +08:00
{
2019-08-20 19:32:36 +08:00
return curr_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-08-20 19:32:36 +08:00
curr_stage_.reset();
next_stage_.reset();
2019-08-13 21:16:38 +08:00
debug_actor_.reset();
transition_.reset();
}
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-08-20 19:32:36 +08:00
if (curr_stage_)
2019-08-12 14:51:54 +08:00
{
2019-08-20 19:32:36 +08:00
curr_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-08-20 19:32:36 +08:00
curr_stage_ = next_stage_;
next_stage_ = nullptr;
2019-08-12 14:51:54 +08:00
}
2019-08-20 19:32:36 +08:00
if (curr_stage_)
curr_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
}
2019-08-20 19:32:36 +08:00
void Director::OnRender(RenderTarget* rt)
2019-08-12 14:51:54 +08:00
{
if (transition_)
{
2019-08-20 19:32:36 +08:00
transition_->Render(rt);
2019-08-12 14:51:54 +08:00
}
2019-08-20 19:32:36 +08:00
else if (curr_stage_)
2019-08-12 14:51:54 +08:00
{
2019-08-20 19:32:36 +08:00
curr_stage_->Render(rt);
2019-08-12 14:51:54 +08:00
}
if (render_border_enabled_)
{
2019-08-20 19:32:36 +08:00
rt->SetOpacity(1.f);
if (curr_stage_)
2019-08-15 11:22:51 +08:00
{
2019-08-20 19:32:36 +08:00
curr_stage_->RenderBorder(rt);
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_)
{
debug_actor_->Render(rt);
}
2019-08-12 14:51:54 +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
2019-08-20 19:32:36 +08:00
if (curr_stage_)
curr_stage_->Dispatch(evt);
2019-08-12 14:51:54 +08:00
}
}