spacebox/src/Node.cpp

158 lines
2.7 KiB
C++

#include "Configuration.hpp"
#include "Delegate.hpp"
#include "Display.hpp"
#include "Input.hpp"
#include "Box.hpp"
#include "Game.hpp"
#include "Audio.hpp"
#include "Node.hpp"
Node::Node() : Node(nullptr) {}
Node::Node(Node* parent) : parent(parent)
{
debug("constructing node " + get_branch_as_string());
}
void Node::set_parent(Node* other)
{
parent = other;
}
void Node::set_canvas(SDL_Texture* texture)
{
canvas = texture;
}
SDL_Texture* Node::get_canvas()
{
return canvas;
}
bool Node::is_active() const
{
return active;
}
const nlohmann::json& Node::get_configuration() const
{
return get_root()->configuration.config;
}
nlohmann::json& Node::get_configuration()
{
return get_root()->configuration.config;
}
Delegate& Node::get_delegate()
{
return get_root()->delegate;
}
const Display& Node::get_display() const
{
return get_root()->display;
}
const SDL_Renderer* Node::get_renderer() const
{
return get_root()->get_renderer();
}
SDL_Renderer* Node::get_renderer()
{
return get_root()->get_renderer();
}
const SDL_Window* Node::get_window() const
{
return get_root()->get_window();
}
SDL_Window* Node::get_window()
{
return get_root()->get_window();
}
const Input& Node::get_input() const
{
return get_root()->get_input();
}
Input& Node::get_input()
{
return get_root()->get_input();
}
Audio& Node::get_audio()
{
return get_root()->get_audio();
}
const Game* Node::get_root() const
{
const Node* r = this;
while (r->parent != NULL)
{
r = r->parent;
}
return dynamic_cast<const Game*>(r);
}
Box Node::get_window_box()
{
return get_display().get_window_box();
}
void Node::suppress_input()
{
get_root()->get_input().suppress();
}
void Node::suppress_input_temporarily(int length)
{
suppress_input();
if (length == 0)
{
length = get_configuration()["input"]["default-unsuppress-delay"];
}
get_root()->get_input().unsuppress_animation.play_once(length);
}
void Node::unsuppress_input()
{
get_root()->get_input().unsuppress();
}
void Node::log(const std::string& message) const
{
get_root()->log(message);
}
void Node::debug(const std::string& message) const
{
get_root()->debug(message);
}
const std::string Node::get_branch_as_string() const
{
const Node* current = this;
std::stringstream branch;
while (current != nullptr)
{
branch << current->get_class_name() << " @ " << current;
if (current->parent != nullptr)
{
branch << " -> ";
}
current = current->parent;
}
return branch.str();
}
Node::~Node()
{
debug("destroying node " + get_branch_as_string());
get_delegate().unsubscribe(this);
}