-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathartboard.cpp
More file actions
2500 lines (2313 loc) · 70.5 KB
/
artboard.cpp
File metadata and controls
2500 lines (2313 loc) · 70.5 KB
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "rive/artboard.hpp"
#include "rive/animation/keyframe_interpolator.hpp"
#include "rive/artboard_component_list.hpp"
#include "rive/backboard.hpp"
#include "rive/focus_data.hpp"
#include "rive/input/focus_input_traversal.hpp"
#include "rive/input/focus_manager.hpp"
#include "rive/input/focusable.hpp"
#include "rive/animation/linear_animation_instance.hpp"
#include "rive/custom_property_trigger.hpp"
#include "rive/dependency_sorter.hpp"
#include "rive/data_bind/data_bind.hpp"
#include "rive/data_bind/data_bind_context.hpp"
#include "rive/draw_rules.hpp"
#include "rive/draw_target.hpp"
#include "rive/audio_event.hpp"
#include "rive/draw_target_placement.hpp"
#include "rive/drawable.hpp"
#include "rive/animation/keyed_object.hpp"
#include "rive/factory.hpp"
#include "rive/renderer.hpp"
#include "rive/shapes/paint/shape_paint.hpp"
#include "rive/importers/import_stack.hpp"
#include "rive/importers/backboard_importer.hpp"
#include "rive/layout_component.hpp"
#include "rive/foreground_layout_drawable.hpp"
#include "rive/nested_artboard.hpp"
#include "rive/nested_artboard_leaf.hpp"
#include "rive/nested_artboard_layout.hpp"
#include "rive/animation/nested_state_machine.hpp"
#include "rive/joystick.hpp"
#include "rive/data_bind/data_bind.hpp"
#include "rive/data_bind_flags.hpp"
#include "rive/animation/nested_bool.hpp"
#include "rive/animation/nested_number.hpp"
#include "rive/animation/nested_trigger.hpp"
#include "rive/viewmodel/viewmodel_instance.hpp"
#include "rive/viewmodel/viewmodel_instance_value.hpp"
#include "rive/animation/state_machine_input_instance.hpp"
#include "rive/animation/state_machine_instance.hpp"
#include "rive/shapes/shape.hpp"
#include "rive/shapes/clipping_shape.hpp"
#include "rive/text/text_value_run.hpp"
#include "rive/event.hpp"
#include "rive/assets/audio_asset.hpp"
#include "rive/layout/layout_data.hpp"
#include "rive/profiler/profiler_macros.h"
#include "rive/scripted/scripted_object.hpp"
#include <set>
#include <unordered_map>
using namespace rive;
uint64_t Artboard::sm_frameId = 0;
Artboard::Artboard()
{
// Artboards need to override default clip value to true.
m_Clip = true;
#ifdef WITH_RIVE_TOOLS
callbackUserData = this;
#endif
}
Artboard::~Artboard()
{
// NOTE: Do NOT call cleanupFocusTree() here! The FocusManager is owned by
// StateMachineInstance which may already be destroyed before the Artboard.
// Focus cleanup should be done explicitly via cleanupFocusTree() before
// the artboard is recycled/destroyed (e.g., in ArtboardComponentList).
#ifdef WITH_RIVE_AUDIO
#ifdef EXTERNAL_RIVE_AUDIO_ENGINE
auto audioEngine = m_audioEngine;
#else
auto audioEngine = AudioEngine::RuntimeEngine(false);
#endif
if (audioEngine)
{
audioEngine->stop(this);
}
#endif
unbind();
// ViewModelInstance and ViewModelInstanceValue inherit from RefCnt.
//
// ViewModelInstance (VMI) ownership rules:
// - VMIs in the component hierarchy (parent() != nullptr) are expected to
// be owned externally and must NOT be released here.
// - VMIs not in the component hierarchy are released by the artboard, but
// AFTER hierarchy components are destroyed to avoid use-after-free. This
// applies to both source and cloned artboards (e.g., artboard instances
// created by ArtboardComponentList).
//
// ViewModelInstanceValue (VMV) ownership: always owned by their parent
// ViewModelInstance via rcp<> in m_PropertyValues. When VMI is deleted,
// its destructor clears m_PropertyValues, which unrefs and deletes VMVs.
//
// Strategy:
// 1) Identify VMI/VMV objects by pointer (safe is<>() check BEFORE any
// deletions).
// 2) Delete everything else immediately, deferring VMI unref until after
// hierarchy components are gone.
std::set<Core*> vmObjects;
std::set<ViewModelInstance*> deferredVmiUnrefs;
// First pass: identify ViewModelInstance and ViewModelInstanceValue
// objects while memory is valid (before any deletions). Precompute which
// VMIs should be released so we never dereference pointers after deletes.
auto gatherVmObjects = [&](Core* object) {
if (object == nullptr || object == this)
{
return;
}
if (object->is<ViewModelInstance>())
{
vmObjects.insert(object);
auto vmi = object->as<ViewModelInstance>();
if (vmi->parent() == nullptr)
{
deferredVmiUnrefs.insert(vmi);
}
return;
}
if (object->is<ViewModelInstanceValue>())
{
vmObjects.insert(object);
}
};
for (auto object : m_Objects)
{
gatherVmObjects(object);
}
for (auto object : m_invalidObjects)
{
gatherVmObjects(object);
}
auto isVmObject = [&](Core* object) -> bool {
return vmObjects.count(object) != 0;
};
#ifdef WITH_RIVE_SCRIPTING
lua_State* luaState = nullptr;
if (!m_ScriptedObjects.empty())
{
luaState = m_ScriptedObjects[0]->state();
}
#endif
// Second pass: delete non-VM objects.
for (auto object : m_Objects)
{
if (object == nullptr || object == this)
{
continue;
}
if (isVmObject(object))
{
continue;
}
delete object;
}
#ifdef WITH_RIVE_SCRIPTING
ScriptedObject::collectLuaGarbage(luaState);
#endif
for (auto object : m_invalidObjects)
{
if (object == nullptr)
{
continue;
}
if (isVmObject(object))
{
continue;
}
delete object;
}
// Now release deferred ViewModelInstances (both source and clone artboards)
// after hierarchy components have been destroyed. Releasing via unref()
// keeps RefCnt ownership semantics intact.
for (auto* vmi : deferredVmiUnrefs)
{
vmi->unref();
}
deleteDataBinds();
// Instances reference back to the original artboard's animations and state
// machines, so don't delete them here, they'll get cleaned up when the
// source is deleted.
// TODO: move this logic into ArtboardInstance destructor???
if (!m_IsInstance)
{
for (auto object : m_Animations)
{
delete object;
}
for (auto object : m_StateMachines)
{
delete object;
}
}
m_dirtyLayout.clear();
}
static bool canContinue(StatusCode code)
{
// We currently only cease loading on invalid object.
return code != StatusCode::InvalidObject;
}
bool Artboard::validateObjects()
{
auto size = m_Objects.size();
std::vector<bool> valid(size);
// Max iterations..
for (int cycle = 0; cycle < 100; cycle++)
{
bool changed = false;
for (size_t i = 1; i < size; i++)
{
auto object = m_Objects[i];
if (object == nullptr)
{
// objects can be null if they were not understood by this
// runtime.
continue;
}
bool wasValid = valid[i];
bool isValid = object->validate(this);
if (wasValid != isValid)
{
changed = true;
valid[i] = isValid;
}
}
if (changed)
{
// Delete invalid objects.
for (size_t i = 1; i < size; i++)
{
if (valid[i])
{
continue;
}
// Instead of immediately deleting invalid objects, we keep them
// around in case other objects are referencing them. One
// example is the backboard_importer keeping a reference on its
// m_FileAssetReferencers. So the invalid objects are taken out
// of the objects list but only deleted when the artboard is
// destroyed.
m_invalidObjects.push_back(m_Objects[i]);
m_Objects[i] = nullptr;
}
}
else
{
break;
}
}
return true;
}
StatusCode Artboard::initialize()
{
StatusCode code;
// these will be re-built in update() -- are they needed here?
m_layout = Layout(0.0f, 0.0f, width(), height());
#ifdef WITH_RIVE_LAYOUT
markLayoutDirty(this);
#endif
// onAddedDirty guarantees that all objects are now available so they can be
// looked up by index/id. This is where nodes find their parents, but they
// can't assume that their parent's parent will have resolved yet.
for (auto object : m_Objects)
{
if (object == nullptr)
{
// objects can be null if they were not understood by this runtime.
continue;
}
if (!canContinue(code = object->onAddedDirty(this)))
{
return code;
}
}
// Animations and StateMachines initialize only once on the source/origin
// Artboard. Instances will hold references to the original Animations and
// StateMachines, so running this code for instances will effectively
// initialize them twice. This can lead to unpredictable behaviour. One such
// example was that resolved objects like listener inputs were being added
// to lists twice.
if (!isInstance())
{
for (auto object : m_Animations)
{
if (!canContinue(code = object->onAddedDirty(this)))
{
return code;
}
}
for (auto object : m_StateMachines)
{
if (!canContinue(code = object->onAddedDirty(this)))
{
return code;
}
}
if (m_Animations.size() == 0 && m_StateMachines.size() == 0)
{
auto sm = new StateMachine();
sm->name("Auto Generated State Machine");
m_StateMachines.push_back(sm);
}
}
// Store a map of the drawRules to make it easier to lookup the matching
// rule for a transform component.
std::unordered_map<Core*, DrawRules*> componentDrawRules;
// onAddedClean is called when all individually referenced components have
// been found and so components can look at other components' references and
// assume that they have resolved too. This is where the whole hierarchy is
// linked up and we can traverse it to find other references (my parent's
// parent should be type X can be checked now).
for (auto object : m_Objects)
{
if (object == nullptr)
{
continue;
}
if (!canContinue(code = object->onAddedClean(this)))
{
return code;
}
if (object->is<Component>())
{
auto resettable = ResettingComponent::from(object->as<Component>());
if (resettable)
{
m_Resettables.push_back(resettable);
}
}
switch (object->coreType())
{
case DrawRulesBase::typeKey:
{
DrawRules* rules = static_cast<DrawRules*>(object);
Core* component = resolve(rules->parentId());
if (component != nullptr)
{
componentDrawRules[component] = rules;
}
else
{
fprintf(stderr,
"Artboard::initialize - Draw rule targets missing "
"component width id %d\n",
rules->parentId());
}
break;
}
case NestedArtboardBase::typeKey:
case NestedArtboardLeafBase::typeKey:
case NestedArtboardLayoutBase::typeKey:
{
m_NestedArtboards.push_back(object->as<NestedArtboard>());
m_ArtboardHosts.push_back(object->as<NestedArtboard>());
break;
}
case ArtboardComponentListBase::typeKey:
m_ComponentLists.push_back(object->as<ArtboardComponentList>());
m_ArtboardHosts.push_back(object->as<ArtboardComponentList>());
break;
case JoystickBase::typeKey:
{
Joystick* joystick = object->as<Joystick>();
if (!joystick->canApplyBeforeUpdate())
{
m_JoysticksApplyBeforeUpdate = false;
}
joystick->addDependents(this);
m_Joysticks.push_back(joystick);
break;
}
}
auto advancingComponent = AdvancingComponent::from(object);
if (advancingComponent)
{
m_advancingComponents.push_back(advancingComponent);
}
}
if (!isInstance())
{
for (auto object : m_Animations)
{
if (!canContinue(code = object->onAddedClean(this)))
{
return code;
}
}
for (auto object : m_StateMachines)
{
if (!canContinue(code = object->onAddedClean(this)))
{
return code;
}
}
}
// Multi-level references have been built up, now we can
// actually mark what's dependent on what.
for (auto object : m_Objects)
{
if (object == nullptr)
{
continue;
}
if (object->is<Component>())
{
object->as<Component>()->buildDependencies();
}
if (object->is<Drawable>() && object != this)
{
Drawable* drawable = object->as<Drawable>();
m_Drawables.push_back(drawable);
// Move the foreground drawable before its parent. We traverse the
// added list of drawables and swap their positions with the
// foreground drawable until we find the parent
if (drawable->is<ForegroundLayoutDrawable>())
{
auto parent = drawable->parent();
auto index = m_Drawables.size() - 1;
while (index >= 1)
{
auto swappingDrawable = m_Drawables[index - 1];
std::swap(m_Drawables[index - 1], m_Drawables[index]);
if (swappingDrawable == parent)
{
break;
}
index--;
}
}
for (ContainerComponent* parent = drawable; parent != nullptr;
parent = parent->parent())
{
auto itr = componentDrawRules.find(parent);
if (itr != componentDrawRules.end())
{
drawable->flattenedDrawRules = itr->second;
break;
}
}
}
else if (object->is<ClippingShape>())
{
m_clippingShapes.push_back(object->as<ClippingShape>());
}
}
// Iterate over the drawables in order to inject proxies for layouts
std::vector<LayoutComponent*> layouts;
for (int i = 0; i < m_Drawables.size(); i++)
{
auto drawable = m_Drawables[i];
LayoutComponent* currentLayout = nullptr;
bool isInCurrentLayout = true;
if (!layouts.empty())
{
currentLayout = layouts.back();
isInCurrentLayout = drawable->isChildOfLayout(currentLayout);
}
// We inject a DrawableProxy after all of the children of a
// LayoutComponent so that we can draw a stroke above and background
// below the children This also allows us to clip the children
if (currentLayout != nullptr && !isInCurrentLayout)
{
// This is the first item in the list of drawables that isn't a
// child of the layout, so we insert a proxy before it
do
{
m_Drawables.insert(m_Drawables.begin() + i,
currentLayout->proxy());
i += 1;
layouts.pop_back();
if (!layouts.empty())
{
currentLayout = layouts.back();
}
} while (!layouts.empty() &&
!drawable->isChildOfLayout(currentLayout));
}
if (drawable->is<LayoutComponent>())
{
layouts.push_back(drawable->as<LayoutComponent>());
}
}
while (!layouts.empty())
{
auto layout = layouts.back();
m_Drawables.push_back(layout->proxy());
layouts.pop_back();
}
sortDependencies();
std::vector<DrawRules*> rulesList;
// Build the rules in the right order. We use the map componentDrawRules
// to make sure we traverse the objects in the right order from parent
// to child, and add the rules accordingly.
for (auto object : m_Objects)
{
if (object == nullptr)
{
continue;
}
auto itr = componentDrawRules.find(object);
if (itr != componentDrawRules.end())
{
rulesList.emplace_back(componentDrawRules[object]);
}
}
DrawTarget root;
// Build up the draw order. Look for draw targets and build
// their dependencies.
for (auto rules : rulesList)
{
for (auto child : rules->children())
{
auto target = child->as<DrawTarget>();
root.addDependent(target);
auto dependentRules = target->drawable()->flattenedDrawRules;
if (dependentRules != nullptr)
{
// Because we don't store targets on rules, we need
// to find the targets that belong to this rule
// here.
for (auto object : m_Objects)
{
if (object != nullptr && object->is<DrawTarget>())
{
DrawTarget* dependentTarget = object->as<DrawTarget>();
if (dependentTarget->parent() == dependentRules)
{
dependentTarget->addDependent(target);
}
}
}
}
}
}
DependencySorter sorter;
std::vector<Component*> drawTargetOrder;
sorter.sort(&root, drawTargetOrder);
if (drawTargetOrder.size() > 0)
{
auto itr = drawTargetOrder.begin();
itr++;
while (itr != drawTargetOrder.end())
{
m_DrawTargets.push_back(static_cast<DrawTarget*>(*itr++));
}
}
initScriptedObjects();
return StatusCode::Ok;
}
void Artboard::sortDrawOrder()
{
m_drawOrderChangeCounter =
m_drawOrderChangeCounter == std::numeric_limits<uint8_t>::max()
? 0
: m_drawOrderChangeCounter + 1;
for (auto target : m_DrawTargets)
{
target->first = target->last = nullptr;
}
m_FirstDrawable = nullptr;
Drawable* lastDrawable = nullptr;
for (auto drawable : m_Drawables)
{
auto rules = drawable->flattenedDrawRules;
if (rules != nullptr && rules->activeTarget() != nullptr)
{
auto target = rules->activeTarget();
if (target->first == nullptr)
{
target->first = target->last = drawable;
drawable->prev = drawable->next = nullptr;
}
else
{
target->last->next = drawable;
drawable->prev = target->last;
target->last = drawable;
drawable->next = nullptr;
}
}
else
{
drawable->prev = lastDrawable;
drawable->next = nullptr;
if (lastDrawable == nullptr)
{
lastDrawable = m_FirstDrawable = drawable;
}
else
{
lastDrawable->next = drawable;
lastDrawable = drawable;
}
}
}
for (auto rule : m_DrawTargets)
{
if (rule->first == nullptr)
{
continue;
}
auto targetDrawable = rule->drawable();
switch (rule->placement())
{
case DrawTargetPlacement::before:
{
if (targetDrawable->prev != nullptr)
{
targetDrawable->prev->next = rule->first;
rule->first->prev = targetDrawable->prev;
}
if (targetDrawable == m_FirstDrawable)
{
m_FirstDrawable = rule->first;
}
targetDrawable->prev = rule->last;
rule->last->next = targetDrawable;
break;
}
case DrawTargetPlacement::after:
{
if (targetDrawable->next != nullptr)
{
targetDrawable->next->prev = rule->last;
rule->last->next = targetDrawable->next;
}
if (targetDrawable == lastDrawable)
{
lastDrawable = rule->last;
}
targetDrawable->next = rule->first;
rule->first->prev = targetDrawable;
break;
}
}
}
m_FirstDrawable = lastDrawable;
// Interleave clipping operations between drawables that share the same
// common clippings. Each clipping operation has a start and an end.
for (auto& clippingShape : m_clippingShapes)
{
clippingShape->resetDrawables();
}
Drawable* currentDrawable = m_FirstDrawable;
Drawable* nextDrawable = nullptr;
std::vector<ClippingShape*> _clippingStack;
while (currentDrawable)
{
currentDrawable->needsSaveOperation(true);
auto drawableClippingShapes = currentDrawable->clippingShapes();
// Remove all clippings that are not part of the current drawable. Since
// they are applied as a stack, if one clipping is removed, all
// subsequent clippings from the stack need to be removed as well
size_t removingIndex = _clippingStack.size();
for (size_t i = 0; i < _clippingStack.size(); ++i)
{
auto& cl = _clippingStack[i];
// Check if this clipping should stay (is in both stack and
// drawable's clippings)
bool shouldStay = std::find(drawableClippingShapes.begin(),
drawableClippingShapes.end(),
cl) != drawableClippingShapes.end();
if (!shouldStay)
{
removingIndex = i;
break;
}
}
// Remove all clippings from stack in the reverse order they were added
if (_clippingStack.size() > 0 && removingIndex < _clippingStack.size())
{
size_t i = _clippingStack.size() - 1;
while (i >= removingIndex)
{
auto& clippingShape = _clippingStack[i];
// Insert in the drawing list a clipEnd for each clipping that
// is removed
auto proxyDrawable =
clippingShape->createProxyDrawable(&clippingShape->clipEnd);
if (nextDrawable)
{
proxyDrawable->next = nextDrawable;
nextDrawable->prev = proxyDrawable;
}
else
{
fprintf(stderr,
"Error - adding clip end as first operation\n");
}
proxyDrawable->prev = currentDrawable;
currentDrawable->next = proxyDrawable;
nextDrawable = proxyDrawable;
if (i == 0)
{
break;
}
i--;
}
_clippingStack.erase(_clippingStack.begin() + removingIndex,
_clippingStack.end());
}
// Find clippings that are applied to the drawable but are not on the
// stack
for (auto& clippingShape : drawableClippingShapes)
{
auto itr = std::find(_clippingStack.begin(),
_clippingStack.end(),
clippingShape);
if (itr == _clippingStack.end())
{
auto proxyDrawable = clippingShape->createProxyDrawable(
&clippingShape->clipStart);
if (nextDrawable)
{
proxyDrawable->next = nextDrawable;
nextDrawable->prev = proxyDrawable;
}
else
{
m_FirstDrawable = proxyDrawable;
}
proxyDrawable->prev = currentDrawable;
currentDrawable->next = proxyDrawable;
nextDrawable = proxyDrawable;
_clippingStack.push_back(clippingShape);
}
}
nextDrawable = currentDrawable;
currentDrawable = currentDrawable->prev;
}
// Add closing calls to remaining clippings in the stack
if (_clippingStack.size() > 0)
{
for (int i = (int)(_clippingStack.size() - 1); i >= 0; i--)
{
auto& clippingShape = _clippingStack[i];
auto proxyDrawable =
clippingShape->createProxyDrawable(&clippingShape->clipEnd);
if (nextDrawable)
{
nextDrawable->prev = proxyDrawable;
proxyDrawable->next = nextDrawable;
}
proxyDrawable->prev = nullptr; // End of list
nextDrawable = proxyDrawable;
}
}
clearRedundantOperations();
}
// Look for drawables that are preceeding and succeeding drawables that call
// save and restore. If found, the drawable does not need to call save and
// restore itself.
void Artboard::clearRedundantOperations()
{
Drawable* currentDrawable = m_FirstDrawable;
bool prevAppliedSave = false;
// Keep a stack of clipStart operation results to apply the same operation
// to its clipEnd
std::vector<bool> appliedClippingSaveOperations;
while (currentDrawable)
{
currentDrawable->needsSaveOperation(true);
// If previous operation applied a save operation
if (prevAppliedSave)
{
// With consecutive clippings, we can skip the save and restore
// operation since the previous one has applied it
if (currentDrawable->isClipStart())
{
appliedClippingSaveOperations.push_back(false);
currentDrawable->needsSaveOperation(false);
}
else if (currentDrawable->isClipEnd())
{
// Apply or skip the clipEnd Restore operation matching its clip
// start counterpart
auto operationApplied = appliedClippingSaveOperations.back();
appliedClippingSaveOperations.pop_back();
currentDrawable->needsSaveOperation(operationApplied);
}
else
{
// Check if next is clip end, if it is, we can skip the drawable
// save/restore because it is tightly wrapped in a clipping
// operation
auto nextDrawable = currentDrawable->prev;
if (nextDrawable->isClipEnd())
{
currentDrawable->needsSaveOperation(false);
}
}
}
else if (currentDrawable->isClipStart())
{
appliedClippingSaveOperations.push_back(true);
}
else if (currentDrawable->isClipEnd())
{
// Apply or skip the clipEnd Restore operation matching its clip
// start counterpart
auto operationApplied = appliedClippingSaveOperations.back();
currentDrawable->needsSaveOperation(operationApplied);
appliedClippingSaveOperations.pop_back();
}
prevAppliedSave = currentDrawable->isClipStart() &&
(currentDrawable->willClip() || prevAppliedSave);
currentDrawable = currentDrawable->prev;
}
assert(appliedClippingSaveOperations.size() == 0);
}
void Artboard::sortDependencies()
{
DependencySorter sorter;
sorter.sort(this, m_DependencyOrder);
unsigned int graphOrder = 0;
for (auto component : m_DependencyOrder)
{
component->m_GraphOrder = graphOrder++;
}
m_Dirt |= ComponentDirt::Components;
}
void Artboard::addObject(Core* object) { m_Objects.push_back(object); }
void Artboard::addAnimation(LinearAnimation* object)
{
m_Animations.push_back(object);
}
void Artboard::addStateMachine(StateMachine* object)
{
m_StateMachines.push_back(object);
}
void Artboard::addScriptedObject(ScriptedObject* object)
{
m_ScriptedObjects.push_back(object);
}
void Artboard::initScriptedObjects()
{
if (isInstance())
{
for (auto obj : m_ScriptedObjects)
{
obj->reinit();
}
}
}
Core* Artboard::resolve(uint32_t id) const
{
if (id >= static_cast<int>(m_Objects.size()))
{
return nullptr;
}
return m_Objects[id];
}
uint32_t Artboard::idOf(Core* object) const
{
auto it = std::find(m_Objects.begin(), m_Objects.end(), object);
if (it != m_Objects.end())
{
return castTo<uint32_t>(it - m_Objects.begin());
}
else
{
return 0;
}
}
void Artboard::onComponentDirty(Component* component)
{
m_didChange = true;
m_Dirt |= ComponentDirt::Components;
/// If the order of the component is less than the current dirt
/// depth, update the dirt depth so that the update loop can break
/// out early and re-run (something up the tree is dirty).
if (component->graphOrder() < m_DirtDepth)
{
m_DirtDepth = component->graphOrder();
}
}
void Artboard::onDirty(ComponentDirt dirt)
{
m_Dirt |= ComponentDirt::Components;
}
#ifdef WITH_RIVE_LAYOUT
void Artboard::propagateSize()
{
addDirt(ComponentDirt::Path);
if (sharesLayoutWithHost())
{
m_host->markHostTransformDirty();
}
#ifdef WITH_RIVE_TOOLS
if (m_layoutChangedCallback != nullptr)
{
m_layoutChangedCallback(callbackUserData);
}
#endif
}
#endif
bool Artboard::sharesLayoutWithHost() const
{
return m_host != nullptr && m_host->isLayoutProvider();
}
void Artboard::cloneObjectDataBinds(const Core* object,
Core* clone,
Artboard* artboard) const
{
for (auto dataBind : dataBinds())
{
if (dataBind->target() == object)
{
auto dataBindClone = static_cast<DataBind*>(dataBind->clone());
dataBindClone->target(clone);
dataBindClone->file(dataBind->file());
dataBindClone->initialize();
if (dataBind->converter() != nullptr)
{
dataBindClone->converter(
dataBind->converter()->clone()->as<DataConverter>());
}
artboard->addDataBind(dataBindClone);
}
}
}
void Artboard::host(ArtboardHost* artboardHost)
{
addedToHost();
m_host = artboardHost;
#ifdef WITH_RIVE_LAYOUT
if (!sharesLayoutWithHost())
{
return;
}
Artboard* parent = parentArtboard();
if (parent != nullptr)
{
parent->markLayoutDirty(this);
parent->syncLayoutChildren();
}
#endif
}
ArtboardHost* Artboard::host() const { return m_host; }
StatusCode Artboard::onAddedClean(CoreContext* context)
{
auto code = Super::onAddedClean(context);
if (code != StatusCode::Ok)
{