forked from rive-app/rive-cpp-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested_artboard.cpp
More file actions
181 lines (161 loc) · 4.69 KB
/
nested_artboard.cpp
File metadata and controls
181 lines (161 loc) · 4.69 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
#include "rive/nested_artboard.hpp"
#include "rive/artboard.hpp"
#include "rive/backboard.hpp"
#include "rive/importers/import_stack.hpp"
#include "rive/importers/backboard_importer.hpp"
#include "rive/nested_animation.hpp"
#include "rive/animation/nested_state_machine.hpp"
#include "rive/clip_result.hpp"
#include <cassert>
using namespace rive;
NestedArtboard::NestedArtboard() {}
NestedArtboard::~NestedArtboard() {}
Core* NestedArtboard::clone() const
{
NestedArtboard* nestedArtboard = static_cast<NestedArtboard*>(NestedArtboardBase::clone());
if (m_Artboard == nullptr)
{
return nestedArtboard;
}
auto ni = m_Artboard->instance();
nestedArtboard->nest(ni.release());
return nestedArtboard;
}
void NestedArtboard::nest(Artboard* artboard)
{
assert(artboard != nullptr);
m_Artboard = artboard;
if (!m_Artboard->isInstance())
{
// We're just marking the source artboard so we can later instance from
// it. No need to advance it or change any of its properties.
return;
}
m_Artboard->frameOrigin(false);
m_Artboard->opacity(renderOpacity());
m_Instance = nullptr;
if (artboard->isInstance())
{
m_Instance.reset(static_cast<ArtboardInstance*>(artboard)); // take ownership
}
m_Artboard->advance(0.0f);
}
static Mat2D makeTranslate(const Artboard* artboard)
{
return Mat2D::fromTranslate(-artboard->originX() * artboard->width(),
-artboard->originY() * artboard->height());
}
void NestedArtboard::draw(Renderer* renderer)
{
if (m_Artboard == nullptr)
{
return;
}
ClipResult clipResult = clip(renderer);
if (clipResult == ClipResult::noClip)
{
// We didn't clip, so make sure to save as we'll be doing some
// transformations.
renderer->save();
}
if (clipResult != ClipResult::emptyClip)
{
renderer->transform(worldTransform());
m_Artboard->draw(renderer);
}
renderer->restore();
}
Core* NestedArtboard::hitTest(HitInfo* hinfo, const Mat2D& xform)
{
if (m_Artboard == nullptr)
{
return nullptr;
}
hinfo->mounts.push_back(this);
auto mx = xform * worldTransform() * makeTranslate(m_Artboard);
if (auto c = m_Artboard->hitTest(hinfo, &mx))
{
return c;
}
hinfo->mounts.pop_back();
return nullptr;
}
StatusCode NestedArtboard::import(ImportStack& importStack)
{
auto backboardImporter = importStack.latest<BackboardImporter>(Backboard::typeKey);
if (backboardImporter == nullptr)
{
return StatusCode::MissingObject;
}
backboardImporter->addNestedArtboard(this);
return Super::import(importStack);
}
void NestedArtboard::addNestedAnimation(NestedAnimation* nestedAnimation)
{
m_NestedAnimations.push_back(nestedAnimation);
}
StatusCode NestedArtboard::onAddedClean(CoreContext* context)
{
// N.B. The nested instance will be null here for the source artboards.
// Instances will have a nestedInstance available. This is a good thing as
// it ensures that we only instance animations in artboard instances. It
// does require that we always use an artboard instance (not just the source
// artboard) when working with nested artboards, but in general this is good
// practice for any loaded Rive file.
assert(m_Artboard == nullptr || m_Artboard == m_Instance.get());
if (m_Instance)
{
for (auto animation : m_NestedAnimations)
{
animation->initializeAnimation(m_Instance.get());
}
}
return Super::onAddedClean(context);
}
bool NestedArtboard::advance(float elapsedSeconds)
{
if (m_Artboard == nullptr || isCollapsed())
{
return false;
}
for (auto animation : m_NestedAnimations)
{
animation->advance(elapsedSeconds);
}
return m_Artboard->advance(elapsedSeconds);
}
void NestedArtboard::update(ComponentDirt value)
{
Super::update(value);
if (hasDirt(value, ComponentDirt::RenderOpacity) && m_Artboard != nullptr)
{
m_Artboard->opacity(renderOpacity());
}
}
bool NestedArtboard::hasNestedStateMachines() const
{
for (auto animation : m_NestedAnimations)
{
if (animation->is<NestedStateMachine>())
{
return true;
}
}
return false;
}
Span<NestedAnimation*> NestedArtboard::nestedAnimations() { return m_NestedAnimations; }
bool NestedArtboard::worldToLocal(Vec2D world, Vec2D* local)
{
assert(local != nullptr);
if (m_Artboard == nullptr)
{
return false;
}
Mat2D toMountedArtboard;
if (!worldTransform().invert(&toMountedArtboard))
{
return false;
}
*local = toMountedArtboard * world;
return true;
}