Magic_Game/src/kiwano/base/Director.cpp

161 lines
3.2 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-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
{
}
void Director::EnterStage(StagePtr scene)
2019-08-12 14:51:54 +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;
}
void Director::EnterStage(StagePtr scene, TransitionPtr transition)
2019-08-12 14:51:54 +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_);
}
}
StagePtr Director::GetCurrentStage()
2019-08-12 14:51:54 +08:00
{
return curr_scene_;
}
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_)
debug_actor_ = new DebugNode;
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();
}
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
}
void Director::OnRender()
2019-08-12 14:51:54 +08:00
{
if (transition_)
{
transition_->Render();
}
else if (curr_scene_)
{
curr_scene_->Render();
}
2019-08-13 21:16:38 +08:00
if (debug_actor_)
debug_actor_->Render();
2019-08-12 14:51:54 +08:00
}
void Director::AfterRender()
2019-08-12 14:51:54 +08:00
{
if (render_border_enabled_)
{
if (curr_scene_)
curr_scene_->RenderBorder();
}
}
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);
}
}