spacebox/src/Node.cpp

121 lines
2.0 KiB
C++

#include "Node.hpp"
#include "Game.hpp"
Node::Node() : Node(NULL) {}
Node::Node(Node *parent) : parent(parent)
{
std::cout << "Constructing ";
print_branch();
}
void Node::set_parent(Node* other)
{
parent = other;
}
bool Node::is_active() const
{
return active;
}
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();
}
const Game* Node::get_root() const
{
const Node* root = this;
while (root->parent != NULL)
{
root = parent;
}
return dynamic_cast<const Game*>(root);
}
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::print_branch()
{
Node* current = this;
while (current != NULL)
{
std::cout << current->get_class_name() << " @ " << current;
if (current->parent != NULL)
{
std::cout << " -> ";
}
else
{
std::cout << std::endl;
}
current = current->parent;
}
}
Node::~Node()
{
// std::cout << "Destructing ";
// print_branch();
get_delegate().unsubscribe(this);
}