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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694 |
#include "fsINode.h"
#include <algorithm>
#include <iomanip>
#include <fsCore/src/fsNMaths.h>
#include <fsCore/src/debug/fsAssert.h>
#include <fsCore/src/fsCVariableTable.h>
#include "fsIDisplay.h"
#include "fsISerialisable.h"
std::vector<fsINode*>* fsINode::mNodesToDestroy = nullptr;
std::vector<std::pair<fsINode*, fsINode*>>* fsINode::mNodesToParent = nullptr;
//#define DEBUG_LOAD_SAVE
void fsINode::postLink()
{
postLinkDo();
}
fsBool fsINode::matrixDirtyGet() const
{
return mMtxDirty;
}
void fsINode::s32Serialise(fsS32 pVal)
{
mSerialisableController->s32Serialise(pVal);
}
fsS32 fsINode::s32Deserialise()
{
return mSerialisableController->s32Deserialise();
}
void fsINode::boolSerialise(fsBool pVal)
{
mSerialisableController->boolSerialise(pVal);
}
fsBool fsINode::boolDeserialise()
{
return mSerialisableController->boolDeserialise();
}
void fsINode::strSerialise(const fsStr& pVal)
{
mSerialisableController->strSerialise(pVal);
}
fsStr fsINode::strDeserialise()
{
return mSerialisableController->strDeserialise();
}
void fsINode::pointSerialise(const fsPoint& pVal)
{
mSerialisableController->pointSerialise(pVal);
}
fsPoint fsINode::pointDeserialise()
{
return mSerialisableController->pointDeserialise();
}
void fsINode::reset()
{
for (fsINode* child : mChildren)
{
child->reset();
}
resetDo();
}
fsBool fsINode::destroyingGet() const
{
return (mNodesToDestroy->end() != std::find(mNodesToDestroy->begin(), mNodesToDestroy->end(), this));
}
void fsINode::serialise(fsStr& pSaveStr)
{
if (!destroyingGet())
{
mSerialisableController->serialiseInit(nameGet());
serialiseDo();
for (fsINode* child : mChildren)
{
child->serialise(mSerialisableController->serialisableStrGet());
}
pSaveStr += mSerialisableController->serialiseFinalise();
}
}
void fsINode::deserialise(fsStr& pLoadStr)
{
if (mSerialisableController->deserialiseInit(nameGet(), pLoadStr))
// entity may have been destroyed during level experience and so just returns to default settings
{
deserialiseDo();
for (fsINode* child : mChildren)
{
child->deserialise(mSerialisableController->serialisableStrGet());
}
postLoad();
}
}
void fsINode::staticVectorsCreate()
{
fsAssert(( mNodesToDestroy == nullptr && mNodesToParent == nullptr ), "Already created static vectors.\n");
mNodesToDestroy = new std::vector<fsINode*>;
mNodesToParent = new std::vector<std::pair<fsINode*, fsINode*>>;
}
void fsINode::staticVectorsDestroy()
{
fsAssert(( mNodesToDestroy != nullptr && mNodesToParent != nullptr ), "Not created static vectors.\n");
delete mNodesToDestroy;
delete mNodesToParent;
mNodesToDestroy = nullptr;
mNodesToParent = nullptr;
}
void fsINode::destroy()
{
fsAssert(mNodesToDestroy != nullptr, "Not initialised static vectors.\n");
if (mNodesToDestroy->end() == std::find(mNodesToDestroy->begin(), mNodesToDestroy->end(), this))
{
auto toBeAdded = std::find_if(
mNodesToParent->begin(), mNodesToParent->end(),
[this](const std::pair<fsINode*, fsINode*> it) -> fsBool
{
return this == it.second;
});
if (toBeAdded != mNodesToParent->end())
{
mNodesToParent->erase(toBeAdded);
}
mNodesToDestroy->push_back(this);
}
}
void fsINode::flaggedNodesDestroy()
{
fsAssert(mNodesToDestroy != nullptr, "Not initialised static vectors.\n");
// Deleting a node can run arbitrary destructor/component teardown code,
// which can call destroy() on another node and push_back onto
// mNodesToDestroy. Iterating the live vector while that happens risks a
// reallocation invalidating this loop's iterator. Snapshot and clear it
// first so any such reentrant destroy() calls land in the now-empty
// vector and are picked up on the next flaggedNodesDestroy() instead.
std::vector<fsINode*> nodesToDestroy;
nodesToDestroy.swap(*mNodesToDestroy);
// A node whose ancestor is also flagged is deleted by that ancestor's
// destructor; deleting it here as well would be a double delete. Filter
// such nodes out before any deletion, while every pointer is still valid.
std::vector<fsINode*> rootsToDestroy;
rootsToDestroy.reserve(nodesToDestroy.size());
for (fsINode* node : nodesToDestroy)
{
fsBool ancestorFlagged = false;
for (fsINode* parent = node->parentGet(); parent != nullptr; parent = parent->parentGet())
{
if (nodesToDestroy.end() != std::find(nodesToDestroy.begin(), nodesToDestroy.end(), parent))
{
ancestorFlagged = true;
break;
}
}
if (!ancestorFlagged)
{
rootsToDestroy.push_back(node);
}
}
for (fsINode* node : rootsToDestroy)
{
delete node;
}
}
void fsINode::newNodesParent()
{
fsAssert(mNodesToParent != nullptr, "Not initialised static vectors.\n");
// postLinkDo() is a virtual hook that could reentrantly queue further
// reparenting, which would push_back onto mNodesToParent while it's
// still being iterated below. Snapshot and clear it first, same
// reasoning as flaggedNodesDestroy().
std::vector<std::pair<fsINode*, fsINode*>> nodesToParent;
nodesToParent.swap(*mNodesToParent);
std::for_each(std::begin(nodesToParent), std::end(nodesToParent), [](std::pair<fsINode*, fsINode*> family)
{
fsAssert(family.first->mInChildUpdate == false, "Parent node should not be in an update.\n");
family.first->childAdd(family.second);
});
std::for_each(std::begin(nodesToParent), std::end(nodesToParent), [](std::pair<fsINode*, fsINode*> family)
{
family.second->postLink();
});
}
void fsINode::initialiseFromVariableTable(const fsCVariableTable& pVars)
{
const fsStr nameVar = pVars.strGet("name");
if (!nameVar.emptyGet())
{
// fsAssert(mName.emptyGet(), "Changing the name of a node that has already been set (%s -> %s)\n",
// mName.utf8Get(), nameVar.utf8Get());
mName = nameVar;
}
fsPoint pos(pVars.s32Get("posX"), pVars.s32Get("posY"));
if (pos.x != 0 || pos.y != 0)
{
posSet(fsPoint(pos.x, pos.y));
}
initialiseFromVariableTableDo(pVars);
}
void fsINode::init()
{
for (fsINode* child : mChildren)
{
child->init();
}
initDo();
}
fsBool fsINode::screenCoordConvertToLocal(const fsVec2d& pScreenPos, fsVec2d& pLocalPos)
{
if (mMtxDirty)
{
matrixUpdate();
}
fsVec2d outPos;
if (!mRenderMtx.fsVec2DInverseTransform(pScreenPos, &outPos))
{
return false;
}
pLocalPos.x = outPos.x;
#if defined fsINVERTED_Y_AXIS
pLocalPos.y = -outPos.y;
#else
pLocalPos.y = outPos.y;
#endif
return true;
}
void fsINode::posSet(const fsVec2d& pPos)
{
#if defined fsINVERTED_Y_AXIS
if (mPos.x != pPos.x || mPos.y != -pPos.y)
{
mPos = {pPos.x, -pPos.y};
mMtxDirty = true;
}
#else
if (mPos != pPos)
{
mPos = pPos;
mMtxDirty = true;
}
#endif
}
fsVec2d fsINode::posGet() const
{
#if defined fsINVERTED_Y_AXIS
return {mPos.x, -mPos.y};
#else
return mPos;
#endif
}
fsVec2d fsINode::posRelativeToAncestorGet(const fsINode* const pAncestor) const
{
fsVec2d outVal = mPos;
fsINode* parent = mParent;<--- Variable 'parent' can be declared as pointer to const
while (parent != pAncestor)
{
fsAssert(parent != nullptr, "%s is not an ancestor of %s.\n", pAncestor->nameGet().utf8Get(),
nameGet().utf8Get());
outVal += parent->mPos;
parent = parent->mParent;
}
#if defined fsINVERTED_Y_AXIS
outVal.y = -outVal.y;
#endif
return outVal;
}
fsVec2d fsINode::screenPositionGet() const
{
fsVec2d originVec{0.f, 0.f};
fsVec2d screenPosVec;
mRenderMtx.fsVec2dTransform(originVec, &screenPosVec);
return screenPosVec;
}
void fsINode::scaleSet(fsVec2d const& pScale)
{
if (mScale.x != pScale.x || mScale.y != pScale.y)
{
mScale.x = pScale.x;
mScale.y = pScale.y;
mMtxDirty = true;
}
}
fsVec2d const& fsINode::scaleGet() const
{
return mScale;
}
void fsINode::angleSet(fsF32 pAngleRadians)
{
if (mAngle != pAngleRadians)
{
mAngle = pAngleRadians;
mTargetAngle = pAngleRadians;
mMtxDirty = true;
}
}
void fsINode::targetAngleSet(fsF32 pRadians, fsF32 pRadiansPerSec)
{
mTargetAngle = pRadians;
mRotationSpeed = pRadiansPerSec / 1000.0f;
rotationDirectionCalculate();
}
void fsINode::rotationDirectionCalculate()
{
fsF32 delta = mTargetAngle - mAngle;
mRotationDir = delta <= 0 ? -1.0f : 1.0f;
if (fsNMaths::fabsF32(delta) > fsNMaths::fsPI)
{
mRotationDir = -mRotationDir;
}
}
void fsINode::rotationUpdate(fsS32 pDeltaMs)
{
if (mAngle != mTargetAngle)
{
fsF32 inc = mRotationSpeed * mRotationDir * pDeltaMs;
if (mRotationDir > 0.0f && mTargetAngle < mAngle)
{
if (fsNMaths::fabsF32(inc) >= fsNMaths::fabsF32((mTargetAngle + 2.0f * fsNMaths::fsPI) - mAngle))
{
mAngle = mTargetAngle;
}
}
else if (mRotationDir < 0.0f && mTargetAngle > mAngle)
{
if (fsNMaths::fabsF32(inc) >= fsNMaths::fabsF32(mTargetAngle - (mAngle + 2.0f * fsNMaths::fsPI)))
{
mAngle = mTargetAngle;
}
}
else if (fsNMaths::fabsF32(inc) >= fsNMaths::fabsF32(mTargetAngle - mAngle))
{
mAngle = mTargetAngle;
}
if (mAngle != mTargetAngle)
{
mAngle += inc;
if (mAngle < 0.0f)
{
mAngle += 2.0f * fsNMaths::fsPI;
}
else if (mAngle >= 2.0f * fsNMaths::fsPI)
{
mAngle -= 2.0f * fsNMaths::fsPI;
}
}
mMtxDirty = true;
}
}
fsF32 fsINode::angleGet() const
{
return mAngle;
}
fsBool fsINode::skipMeGet() const
{
return mSkipMe;
}
void fsINode::skipMeSet(fsBool pVisible)
{
mSkipMe = pVisible;
}
void fsINode::visibleSet(fsBool pVisible)
{
mVisible = pVisible;
}
fsBool fsINode::visibleGet() const
{
if (!mVisible || (mParent != nullptr && !parentGet()->visibleGet()))
{
return false;
}
return true;
}
const fsStr& fsINode::nameGet() const
{
return mName;
}
void fsINode::nameSet(const fsStr& pName)
{
mName = pName;
}
const fsAffineMtx2d& fsINode::localMatrixGet() const
{
return mLocalMtx;
}
const fsAffineMtx2d& fsINode::renderMatrixGet() const
{
return mRenderMtx;
}
void fsINode::matrixUpdateIfDirty()
{
if (mMtxDirty)
{
matrixUpdate();
mMtxDirty = false;
}
else
{
for (fsINode* child : mChildren)
{
child->matrixUpdateIfDirty();
}
}
}
void fsINode::matrixUpdate()
{
mLocalMtx.calculate(mPos, mScale.x, mScale.y, mAngle);
if (mParent == nullptr)
{
mRenderMtx = mLocalMtx;
}
else
{
mRenderMtx = parentGet()->mRenderMtx * mLocalMtx;
}
updatedMatrixProcess();
for (fsINode* child : mChildren)
{
child->matrixUpdate();
}
}
void fsINode::update(fsS32 pDeltaMs)
{
rotationUpdate(pDeltaMs);
updateDo(pDeltaMs);
mInChildUpdate = true;
for (fsINode* child : mChildren)
{
child->update(pDeltaMs);
}
mInChildUpdate = false;
matrixUpdateIfDirty();
}
void fsINode::render()
{
if (mVisible)
{
if (!mSkipMe)
{
renderDo();
}
for (fsINode* child : mChildren)
{
child->render();
}
}
if (!mVisible && mRenderChildrenRegardless)
{
for (fsINode* child : mChildren)
{
child->render();
}
}
}
void fsINode::childAdd(fsINode* pChild)
{
if (!mInChildUpdate)
{
if (std::find(mChildren.begin(), mChildren.end(), pChild) == mChildren.end())
{
if (pChild->mParent != nullptr)
{
pChild->parentGet()->childRemove(pChild);
}
mChildren.push_back(pChild);
pChild->mParent = this;
pChild->mMtxDirty = true;
pChild->postLink();
}
}
else
{
fsAssert(mNodesToParent != nullptr, "Not initialised static vectors.\n");
mNodesToParent->push_back(std::pair<fsINode*, fsINode*>(this, pChild));
}
}
void fsINode::childRemove(fsINode* const pChild)
{
fsAssert(!mInChildUpdate, "Trying to remove a child whilst in update.\n");
auto childIt = std::find(mChildren.begin(), mChildren.end(), pChild);
if (childIt != mChildren.end())
{
mChildren.erase(childIt);
pChild->mParent = nullptr;
pChild->mMtxDirty = true;
}
else
{
fsAssert(false, "Child not found to remove.\n");
}
}
fsINode *fsINode::parentGet() const
{
return mParent;
}
fsINode *fsINode::childFind(const fsStr& pName, fsBool pCaseless) const
{
auto testName = pName;
auto myName = mName;
if (pCaseless)
{
testName.downcase();
myName.downcase();
}
if (myName == testName)
{
return const_cast<fsINode* const>(this);
}
for (auto nodePair = mNodesToParent->begin(); nodePair != mNodesToParent->end(); ++nodePair)
{
if (nodePair->first == this)
{
auto nodeName = nodePair->second->mName;
if (pCaseless)
{
nodeName.downcase();
}
if (nodeName == testName)
{
return nodePair->second;
}
}
}
fsINode* ret = nullptr;
for (auto child = mChildren.begin(); child != mChildren.end() && ret == nullptr; ++child)
{
ret = (*child)->childFind(pName, pCaseless);
}
return ret;
}
void fsINode::bringToFrontOfSiblings()
{
fsAssert(mParent != nullptr, "Trying to reorder a node with no parent.\n");
fsAssert(!parentGet()->mInChildUpdate, "Trying to reorder nodes during an update.\n");
std::vector<fsINode*>::iterator childIt = std::find(parentGet()->mChildren.begin(), parentGet()->mChildren.end(),
this);
parentGet()->mChildren.erase(childIt);
parentGet()->mChildren.push_back(this);
}
void fsINode::pushToBackOfSiblings()
{
fsAssert(mParent != nullptr, "Trying to reorder a node with no parent.\n");
fsAssert(!mInChildUpdate, "Trying to reorder nodes during an update.\n");
fsAssert(!parentGet()->mInChildUpdate, "Trying to reorder nodes during an update.\n");
std::vector<fsINode*>::iterator childIt = std::find(parentGet()->mChildren.begin(), parentGet()->mChildren.end(),
this);
parentGet()->mChildren.erase(childIt);
parentGet()->mChildren.insert(parentGet()->mChildren.begin(), this);
}
void fsINode::hierarchyDumpToLog() const
{
fsLogMsg("\n");
const fsStr logTxt = "Hierarchy of: " + mName + "\n";
fsLogMsg(logTxt.utf8Get());
hierarchyDumpToLog(0);
fsLogMsg("\n");
}
void fsINode::hierarchyDumpToLog(fsS32 pDepth) const
{
fsStr logTxt;
for (fsS32 d = 0; d < pDepth; ++d)
{
logTxt += " |";
}
logTxt += "-" + mName + "\n";
fsLogMsg(logTxt.utf8Get());
for (fsINode* child : mChildren)<--- Variable 'child' can be declared as pointer to const
{
child->hierarchyDumpToLog(pDepth + 1);
}
}
fsINode::fsINode(const fsStr& pName) :
mParent(nullptr),
mName(pName),
mInChildUpdate(false),
mVisible(true),
mRenderChildrenRegardless(false),
mSkipMe(false),
mMtxDirty(false),
mPos(0.f, 0.f),
mScale(1.f, 1.f),
mAngle(0.f),
mTargetAngle(0.f),
mRotationDir(0.f),
mRotationSpeed(0.f),
mSerialisableController(fsISerialisable::create())
{
}
fsINode::~fsINode()
{
while (!mChildren.empty())
{
fsINode* const childPtr = mChildren.back();
childRemove(childPtr);
if (mNodesToDestroy->end() == std::find(mNodesToDestroy->begin(), mNodesToDestroy->end(), childPtr))
{
delete (childPtr);
}
}
if (mParent)
{
parentGet()->childRemove(this);
}
delete mSerialisableController;<--- Class 'fsINode' does not have a copy constructor which is recommended since it has dynamic memory/resource management.<--- Class 'fsINode' does not have a operator= which is recommended since it has dynamic memory/resource management.
}
|