1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
#include "SceneNode.h"
#include "../Application.h"
#include "../../Main.h"
#include "../tracy.h"
namespace nCine
{
/*! \param parent The parent can be `nullptr` */
SceneNode::SceneNode(SceneNode* parent, float x, float y)
: Object(ObjectType::SceneNode),
updateEnabled_(true), drawEnabled_(true), parent_(nullptr),
childOrderIndex_(0), withVisitOrder_(true),
visitOrderState_(VisitOrderState::SameAsParent), visitOrderIndex_(0),
position_(x, y), anchorPoint_(0.0f, 0.0f), scaleFactor_(1.0f, 1.0f), rotation_(0.0f),
color_(Colorf::White), layer_(0), absPosition_(0.0f, 0.0f), absScaleFactor_(1.0f, 1.0f),
absRotation_(0.0f), absColor_(Colorf::White), absLayer_(0),
worldMatrix_(Matrix4x4f::Identity), localMatrix_(Matrix4x4f::Identity),
shouldDeleteChildrenOnDestruction_(true), dirtyBits_(0xFF), lastFrameUpdated_(0)
{
setParent(parent);
}
/*! \param parent The parent can be `nullptr` */
SceneNode::SceneNode(SceneNode* parent, Vector2f position)
: SceneNode(parent, position.X, position.Y)
{
}
/*! \param parent The parent can be `nullptr` */
SceneNode::SceneNode(SceneNode* parent)
: SceneNode(parent, 0.0f, 0.0f)
{
}
SceneNode::SceneNode()
: SceneNode(nullptr, 0.0f, 0.0f)
{
}
SceneNode::~SceneNode()
{
if (shouldDeleteChildrenOnDestruction_) {
for (SceneNode* child : children_) {
delete child;
}
} else {
for (SceneNode* child : children_) {
child->parent_ = nullptr;
}
}
setParent(nullptr);
}
SceneNode::SceneNode(SceneNode&& other) noexcept
: Object(std::move(other)), updateEnabled_(other.updateEnabled_), drawEnabled_(other.drawEnabled_), parent_(other.parent_),
children_(std::move(other.children_)), visitOrderState_(other.visitOrderState_), position_(other.position_), anchorPoint_(other.anchorPoint_),
scaleFactor_(other.scaleFactor_), rotation_(other.rotation_), color_(other.color_), layer_(other.layer_),
shouldDeleteChildrenOnDestruction_(other.shouldDeleteChildrenOnDestruction_), dirtyBits_(other.dirtyBits_), lastFrameUpdated_(other.lastFrameUpdated_)
{
swapChildPointer(this, &other);
for (SceneNode* child : children_) {
child->parent_ = this;
}
}
SceneNode& SceneNode::operator=(SceneNode&& other) noexcept
{
Object::operator=(std::move(other));
updateEnabled_ = other.updateEnabled_;
drawEnabled_ = other.drawEnabled_;
parent_ = other.parent_;
children_ = std::move(other.children_);
visitOrderState_ = other.visitOrderState_;
position_ = other.position_;
anchorPoint_ = other.anchorPoint_;
scaleFactor_ = other.scaleFactor_;
rotation_ = other.rotation_;
color_ = other.color_;
layer_ = other.layer_;
shouldDeleteChildrenOnDestruction_ = other.shouldDeleteChildrenOnDestruction_;
dirtyBits_ = other.dirtyBits_;
lastFrameUpdated_ = other.lastFrameUpdated_;
swapChildPointer(this, &other);
for (SceneNode* child : children_) {
child->parent_ = this;
}
return *this;
}
/*! \return True if the parent has been set */
bool SceneNode::setParent(SceneNode* parentNode)
{
// Can't set yourself or your parent as parent
if (parentNode == this || parentNode == parent_) {
return false;
}
if (parent_ != nullptr) {
parent_->removeChildNode(this);
}
if (parentNode != nullptr) {
parentNode->children_.push_back(this);
childOrderIndex_ = (unsigned int)parentNode->children_.size() - 1;
}
parent_ = parentNode;
dirtyBits_.set(DirtyBitPositions::TransformationBit);
dirtyBits_.set(DirtyBitPositions::AabbBit);
return true;
}
/*! \return True if the node has been added */
bool SceneNode::addChildNode(SceneNode* childNode)
{
// Can't add yourself or one of your children as a child
if (childNode == this || (childNode != nullptr && childNode->parent_ == this)) {
return false;
}
if (childNode->parent_ != nullptr) {
childNode->parent_->removeChildNode(childNode);
}
children_.push_back(childNode);
childNode->childOrderIndex_ = (unsigned int)children_.size() - 1;
childNode->parent_ = this;
return true;
}
/*! \return True if the node has been removed */
bool SceneNode::removeChildNode(SceneNode* childNode)
{
// Can't remove yourself or a `nullptr` from your children
if (childNode == this || childNode == nullptr) {
return false;
}
bool hasBeenRemoved = false;
if (!children_.empty() && // Avoid checking if this node has no children
childNode->parent_ == this) // Avoid checking if the child doesn't belong to this node
{
for (unsigned int i = 0; i < children_.size(); i++) {
if (children_[i] == childNode) {
hasBeenRemoved = removeChildNodeAt(i);
break;
}
}
}
return hasBeenRemoved;
}
/*! \return True if the node has been removed */
bool SceneNode::removeChildNodeAt(std::uint32_t index)
{
// Can't remove at an index past the number of children
if (children_.empty() || index > children_.size() - 1) {
return false;
}
children_[index]->parent_ = nullptr;
dirtyBits_.set(DirtyBitPositions::TransformationBit);
dirtyBits_.set(DirtyBitPositions::AabbBit);
children_.eraseUnordered(&children_[index]);
// The last child has been moved to this index position
if (children_.size() > index)
children_[index]->childOrderIndex_ = index;
return true;
}
/*! \return True if there were at least one node to remove */
bool SceneNode::removeAllChildrenNodes()
{
if (children_.empty()) {
return false;
}
for (unsigned int i = 0; i < children_.size(); i++) {
children_[i]->parent_ = nullptr;
dirtyBits_.set(DirtyBitPositions::TransformationBit);
dirtyBits_.set(DirtyBitPositions::AabbBit);
}
children_.clear();
return true;
}
/*! \return True if the node has been unlinked */
bool SceneNode::unlinkChildNode(SceneNode* childNode)
{
// Can't unlink yourself or a `nullptr` from your children
if (childNode == this || childNode == nullptr) {
return false;
}
bool hasBeenUnlinked = false;
if (!children_.empty() && // Avoid checking if this node has no children
childNode->parent_ == this) // Avoid checking if the child doesn't belong to this node
{
removeChildNode(childNode);
// Nephews reparenting
for (SceneNode* child : childNode->children_) {
addChildNode(child);
}
hasBeenUnlinked = true;
}
return hasBeenUnlinked;
}
/*! \return If the node has no parent then 0 is returned */
std::uint32_t SceneNode::childOrderIndex() const
{
std::uint32_t index = 0;
if (parent_ != nullptr) {
DEATH_ASSERT(parent_->children_[childOrderIndex_] == this);
index = childOrderIndex_;
}
return index;
}
/*! \return True if the two nodes have been swapped */
bool SceneNode::swapChildrenNodes(std::uint32_t firstIndex, std::uint32_t secondIndex)
{
// Check if there are at least two children and if the indices are different and valid
const std::uint32_t numChildren = std::uint32_t(children_.size());
if (numChildren < 2 || firstIndex == secondIndex ||
firstIndex > numChildren - 1 || secondIndex > numChildren - 1) {
return false;
}
std::swap(children_[firstIndex], children_[secondIndex]);
std::swap(children_[firstIndex]->childOrderIndex_, children_[secondIndex]->childOrderIndex_);
return true;
}
/*! \return True if the node has been brought one position forward */
bool SceneNode::swapNodeForward()
{
if (parent_ == nullptr) {
return false;
}
return parent_->swapChildrenNodes(childOrderIndex_, childOrderIndex_ + 1);
}
/*! \return True if the node has been brought one position back */
bool SceneNode::swapNodeBack()
{
if (parent_ == nullptr || childOrderIndex_ == 0) {
return false;
}
return parent_->swapChildrenNodes(childOrderIndex_, childOrderIndex_ - 1);
}
void SceneNode::OnUpdate(float timeMult)
{
// Early return not needed, the first call to this method is on the root node
if (updateEnabled_) {
transform();
for (unsigned int i = 0; i < (unsigned int)children_.size(); i++) {
children_[i]->OnUpdate(timeMult);
}
dirtyBits_.reset(DirtyBitPositions::TransformationBit);
dirtyBits_.reset(DirtyBitPositions::ColorBit);
// A non-drawable scenenode does not have the `updateRenderCommand()` method to reset the flags
if (_type == ObjectType::SceneNode || _type == ObjectType::ParticleSystem) {
dirtyBits_.reset(DirtyBitPositions::TransformationUploadBit);
dirtyBits_.reset(DirtyBitPositions::ColorUploadBit);
}
lastFrameUpdated_ = theApplication().GetFrameCount();
}
}
void SceneNode::OnVisit(RenderQueue& renderQueue, std::uint32_t& visitOrderIndex)
{
// Early return not needed, the first call to this method is on the root node
if (drawEnabled_) {
// Increment the index without knowing if the node is going to be rendered or not.
// It avoids both a one frame delay when the value changes and calling `DrawableNode::setVisitOrder()` from this function.
visitOrderIndex_ = (_type != ObjectType::Particle ? visitOrderIndex + 1 : visitOrderIndex);
const bool rendered = OnDraw(renderQueue);
visitOrderIndex_ = visitOrderIndex;
// Visit order index only incremented for rendered nodes
// Particles get their index incremented only once by their parent particle system
const bool incrementIndex = ((rendered && _type != ObjectType::Particle) || _type == ObjectType::ParticleSystem);
visitOrderIndex_ = incrementIndex ? visitOrderIndex++ : visitOrderIndex;
for (SceneNode* child : children_) {
child->OnVisit(renderQueue, visitOrderIndex);
}
}
}
SceneNode::SceneNode(const SceneNode& other)
: Object(other), updateEnabled_(other.updateEnabled_), drawEnabled_(other.drawEnabled_), parent_(nullptr), childOrderIndex_(0),
withVisitOrder_(true), visitOrderState_(other.visitOrderState_), visitOrderIndex_(0), position_(other.position_),
anchorPoint_(other.anchorPoint_), scaleFactor_(other.scaleFactor_), rotation_(other.rotation_), color_(other.color_),
layer_(other.layer_), absPosition_(0.0f, 0.0f), absScaleFactor_(1.0f, 1.0f), absRotation_(0.0f), absColor_(Colorf::White),
absLayer_(0), worldMatrix_(Matrix4x4f::Identity), localMatrix_(Matrix4x4f::Identity),
shouldDeleteChildrenOnDestruction_(other.shouldDeleteChildrenOnDestruction_), dirtyBits_(0xFF)
{
setParent(other.parent_);
}
/*! \note It is faster than calling `setParent()` on the first child and `removeChildNode()` on the second one */
void SceneNode::swapChildPointer(SceneNode* first, SceneNode* second)
{
DEATH_ASSERT(first->parent_ == second->parent_);
SceneNode* parent = first->parent_;
if (parent != nullptr) {
for (unsigned int i = 0; i < parent->children_.size(); i++) {
if (parent->children_[i] == second) {
parent->children_[i] = this;
childOrderIndex_ = i;
second->parent_ = nullptr;
break;
}
}
}
}
void SceneNode::transform()
{
ZoneScopedC(0x81A861);
if (parent_ != nullptr && layer_ == 0) {
absLayer_ = parent_->absLayer_;
} else {
absLayer_ = layer_;
}
switch (visitOrderState_) {
case VisitOrderState::Enabled: withVisitOrder_ = true; break;
case VisitOrderState::SameAsParent: withVisitOrder_ = (parent_ == nullptr || parent_->withVisitOrder_); break;
default: withVisitOrder_ = false; break;
}
const bool parentHasDirtyColor = (parent_ != nullptr && parent_->dirtyBits_.test(DirtyBitPositions::ColorBit));
if (parentHasDirtyColor) {
dirtyBits_.set(DirtyBitPositions::ColorBit);
}
if (dirtyBits_.test(DirtyBitPositions::ColorBit)) {
absColor_ = (parent_ != nullptr ? color_ * parent_->absColor_ : color_);
dirtyBits_.set(DirtyBitPositions::ColorUploadBit);
}
const bool parentHasDirtyTransformation = parent_ && parent_->dirtyBits_.test(DirtyBitPositions::TransformationBit);
if (parentHasDirtyTransformation) {
dirtyBits_.set(DirtyBitPositions::TransformationBit);
dirtyBits_.set(DirtyBitPositions::AabbBit);
}
if (dirtyBits_.test(DirtyBitPositions::TransformationBit)) {
// Calculating world and local matrices
localMatrix_ = Matrix4x4f::Translation(position_.X, position_.Y, 0.0f);
localMatrix_.RotateZ(rotation_);
localMatrix_.Scale(scaleFactor_.X, scaleFactor_.Y, 1.0f);
localMatrix_.Translate(-anchorPoint_.X, -anchorPoint_.Y, 0.0f);
absScaleFactor_ = scaleFactor_;
absRotation_ = rotation_;
if (parent_ != nullptr) {
worldMatrix_ = parent_->worldMatrix_ * localMatrix_;
absScaleFactor_ *= parent_->absScaleFactor_;
absRotation_ += parent_->absRotation_;
} else {
worldMatrix_ = localMatrix_;
}
absPosition_.X = worldMatrix_[3][0];
absPosition_.Y = worldMatrix_[3][1];
dirtyBits_.set(DirtyBitPositions::TransformationUploadBit);
}
}
}
|