spacebox/src/Node.cpp

160 lines
3.3 KiB
C++

/* /\ +--------------------------------------------------------------+
____/ \____ /| - zlib/MIT/Unlicenced game framework licensed to freely use, |
\ / / | copy, and modify without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - originally created at [http://nugget.fun] |
| ~~~~~~~~~~~~ | +--------------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#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)
{
sb::Log::log("constructing node " + get_branch_as_string(), sb::Log::DEBUG);
}
void Node::set_parent(Node* other)
{
parent = other;
}
void Node::set_canvas(SDL_Texture* texture)
{
canvas = std::shared_ptr<SDL_Texture>(texture, SDL_DestroyTexture);
}
SDL_Texture* Node::get_canvas()
{
return canvas.get();
}
bool Node::is_active() const
{
return active;
}
const nlohmann::json& Node::configuration() const
{
return get_root()->configuration();
}
nlohmann::json& Node::configuration()
{
return get_root()->configuration();
}
Delegate& Node::get_delegate()
{
return get_root()->delegate;
}
const sb::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::window() const
{
return get_root()->window();
}
SDL_Window* Node::window()
{
return get_root()->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);
}
/* Get the window dimensions in pixels as a box object. If invert_y is set, the bottom left will be (0, 0).
* Otherwise, it the top left will be (0, 0). */
Box Node::window_box(bool invert_y)
{
return get_display().window_box(invert_y);
}
void Node::suppress_input()
{
get_root()->get_input().suppress();
}
void Node::suppress_input_temporarily(int length)
{
suppress_input();
if (length == 0)
{
length = configuration()["input"]["default-unsuppress-delay"];
}
get_root()->get_input().unsuppress_animation.play_once(length);
}
void Node::unsuppress_input()
{
get_root()->get_input().unsuppress();
}
const std::string Node::get_branch_as_string() const
{
const Node* current = this;
std::stringstream branch;
while (current != nullptr)
{
branch << current->class_name() << " @ " << current;
if (current->parent != nullptr)
{
branch << " -> ";
}
current = current->parent;
}
return branch.str();
}
Node::~Node()
{
sb::Log::log("destroying node " + get_branch_as_string(), sb::Log::DEBUG);
get_delegate().unsubscribe(this);
}