couch/core/Node.cpp

136 lines
2.7 KiB
C++
Raw Normal View History

2021-01-26 23:28:20 -06:00
/*
Dane Johnson <dane@danejohnson.org>
2021-01-26 22:04:57 -06:00
2021-01-26 23:28:20 -06:00
LICENSE
2021-01-26 22:04:57 -06:00
Couch Copyright (C) 2021 Dane Johnson
This program comes with ABSOLUTELY NO WARRANTY; without event the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for details at
https://www.gnu.org/licenses/gpl-3.0.html
This is free software, and you are welcome to redistribute it
under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
2021-01-26 23:28:20 -06:00
DESCRIPTION
2021-01-26 22:04:57 -06:00
Node is the parent class for all classes that would be in the scene
tree. The root of the scene tree is always a node.
*/
2021-01-14 11:52:01 -06:00
#include "Node.h"
2021-01-24 16:37:35 -06:00
#include "Util.h"
2021-01-14 11:52:01 -06:00
// Note: Declare theses here, not in the header, otherwise
// Windows DLLs will make two versions of the singleton.
static NodeList freeList(false);
static Node root(false);
NodeList::NodeList() {
isPrefabList = true;
}
NodeList::NodeList(bool isPrefabList) {
this->isPrefabList = isPrefabList;
}
2021-01-26 16:42:28 -06:00
void NodeList::Append(Node *node) {
if (this->isPrefabList and not node->isPrefab) {
2021-01-30 15:34:27 -06:00
throw "Attempt to add instanced node to prefab list!";
2021-01-26 16:42:28 -06:00
}
if (node->isPrefab and not this->isPrefabList) {
2021-01-30 15:34:27 -06:00
throw "Attempt to add prefab node to instanced list!";
2021-01-26 16:42:28 -06:00
}
push_back(node);
}
void NodeList::Remove(Node *node) {
remove(node);
}
int NodeList::Length() {
return size();
}
2021-01-26 16:42:28 -06:00
bool NodeList::IsPrefabList() {
return isPrefabList;
}
void NodeList::FreeList() {
for(Node *node : *this) {
node->children.FreeList();
delete node;
}
clear();
}
Node::Node() {}
Node::Node(bool isPrefab) {
if (!isPrefab) {
this->isPrefab = false;
children.isPrefabList = false;
}
}
2021-01-21 15:26:39 -06:00
Name Node::GetType() const {return "Node";}
2021-01-26 16:42:28 -06:00
bool Node::IsPrefab() {
return isPrefab;
2021-01-14 11:52:01 -06:00
}
2021-01-26 16:42:28 -06:00
NodeList Node::GetChildren() {
return children;
2021-01-14 11:52:01 -06:00
}
2021-01-26 16:42:28 -06:00
void Node::AddChild(Node *child) {
child->parent = this;
2021-01-26 16:42:28 -06:00
children.Append(child);
}
Node *Node::GetParent() {
return parent;
}
void Node::QueueFree() {
parent->children.Remove(this);
freeList.Append(this);
}
void Node::DoFree() {
if (this != &root) {
2021-01-30 15:34:27 -06:00
throw "Tried to call DoFree from non-root node";
}
freeList.FreeList();
}
2021-01-26 16:42:28 -06:00
Node *Node::GetRoot() {
return &root;
2021-01-14 11:52:01 -06:00
}
2021-01-24 16:37:35 -06:00
Node* Node::Create() {
return new Node;
}
Node* Node::Duplicate() {
return Create();
}
Node* Node::Instance() {
2021-01-26 16:42:28 -06:00
if (not isPrefab) {
2021-01-30 15:34:27 -06:00
throw "Attempt to instance an instanced node!";
2021-01-24 16:37:35 -06:00
}
Node* instance = Duplicate();
instance->isPrefab = false;
instance->children.isPrefabList = false;
2021-01-26 16:42:28 -06:00
// Instance the children to the instanced list
2021-01-24 16:37:35 -06:00
for (Node *child : children) {
instance->children.Append(child->Instance());
}
return instance;
}