Skip to content

Commit 16f3637

Browse files
bodymovinbodymovin
andcommitted
feature: add support for mapping view models to artboards in lists (#11524) 41002bf174
* feature: add support for mapping view models to artboards in an artboard list Co-authored-by: hernan <hernan@rive.app>
1 parent 03de9c3 commit 16f3637

12 files changed

Lines changed: 239 additions & 4 deletions

.rive_head

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e59db089ed75af8000a358c7a352d42d565b235d
1+
41002bf174cb409ba82c12b6803add48e969b6fb
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "ArtboardListMapRule",
3+
"key": {
4+
"int": 648,
5+
"string": "artboardlistmaprule"
6+
},
7+
"extends": "component.json",
8+
"properties": {
9+
"artboardId": {
10+
"type": "Id",
11+
"typeRuntime": "uint",
12+
"initialValue": "Core.missingId",
13+
"initialValueRuntime": "-1",
14+
"key": {
15+
"int": 934,
16+
"string": "artboardid"
17+
},
18+
"description": "Artboard id to map"
19+
},
20+
"viewModelId": {
21+
"type": "Id",
22+
"typeRuntime": "uint",
23+
"initialValue": "Core.missingId",
24+
"initialValueRuntime": "-1",
25+
"key": {
26+
"int": 935,
27+
"string": "viewmodelid"
28+
},
29+
"description": "iew model id to map"
30+
}
31+
}
32+
}

include/rive/artboard_component_list.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace rive
2020
{
2121
class LayoutComponent;
2222
class ScrollConstraint;
23+
class ArtboardListMapRule;
2324

2425
class ArtboardComponentList : public ArtboardComponentListBase,
2526
public ArtboardHost,
@@ -115,6 +116,7 @@ class ArtboardComponentList : public ArtboardComponentListBase,
115116
LayoutComponent* layoutParent();
116117
const Mat2D& listTransform() override;
117118
void listItemTransforms(std::vector<Mat2D*>& transforms) override;
119+
void addMapRule(ArtboardListMapRule*);
118120

119121
private:
120122
void updateArtboardsWorldTransform();
@@ -159,6 +161,7 @@ class ArtboardComponentList : public ArtboardComponentListBase,
159161
int m_visibleEndIndex = -1;
160162
std::unordered_map<ArtboardInstance*, ArtboardComponentListOverride*>
161163
m_artboardOverridesMap;
164+
std::unordered_map<int, int> m_artboardMapRules;
162165
void attachArtboardOverride(ArtboardInstance*,
163166
rcp<ViewModelInstanceListItem>);
164167
void clearArtboardOverride(ArtboardInstance*);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef _RIVE_ARTBOARD_LIST_MAP_RULE_HPP_
2+
#define _RIVE_ARTBOARD_LIST_MAP_RULE_HPP_
3+
#include "rive/generated/artboard_list_map_rule_base.hpp"
4+
#include <stdio.h>
5+
namespace rive
6+
{
7+
class ArtboardListMapRule : public ArtboardListMapRuleBase
8+
{
9+
public:
10+
StatusCode onAddedDirty(CoreContext* context) override;
11+
};
12+
} // namespace rive
13+
14+
#endif
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#ifndef _RIVE_ARTBOARD_LIST_MAP_RULE_BASE_HPP_
2+
#define _RIVE_ARTBOARD_LIST_MAP_RULE_BASE_HPP_
3+
#include "rive/component.hpp"
4+
#include "rive/core/field_types/core_uint_type.hpp"
5+
namespace rive
6+
{
7+
class ArtboardListMapRuleBase : public Component
8+
{
9+
protected:
10+
typedef Component Super;
11+
12+
public:
13+
static const uint16_t typeKey = 648;
14+
15+
/// Helper to quickly determine if a core object extends another without
16+
/// RTTI at runtime.
17+
bool isTypeOf(uint16_t typeKey) const override
18+
{
19+
switch (typeKey)
20+
{
21+
case ArtboardListMapRuleBase::typeKey:
22+
case ComponentBase::typeKey:
23+
return true;
24+
default:
25+
return false;
26+
}
27+
}
28+
29+
uint16_t coreType() const override { return typeKey; }
30+
31+
static const uint16_t artboardIdPropertyKey = 934;
32+
static const uint16_t viewModelIdPropertyKey = 935;
33+
34+
protected:
35+
uint32_t m_ArtboardId = -1;
36+
uint32_t m_ViewModelId = -1;
37+
38+
public:
39+
inline uint32_t artboardId() const { return m_ArtboardId; }
40+
void artboardId(uint32_t value)
41+
{
42+
if (m_ArtboardId == value)
43+
{
44+
return;
45+
}
46+
m_ArtboardId = value;
47+
artboardIdChanged();
48+
}
49+
50+
inline uint32_t viewModelId() const { return m_ViewModelId; }
51+
void viewModelId(uint32_t value)
52+
{
53+
if (m_ViewModelId == value)
54+
{
55+
return;
56+
}
57+
m_ViewModelId = value;
58+
viewModelIdChanged();
59+
}
60+
61+
Core* clone() const override;
62+
void copy(const ArtboardListMapRuleBase& object)
63+
{
64+
m_ArtboardId = object.m_ArtboardId;
65+
m_ViewModelId = object.m_ViewModelId;
66+
Component::copy(object);
67+
}
68+
69+
bool deserialize(uint16_t propertyKey, BinaryReader& reader) override
70+
{
71+
switch (propertyKey)
72+
{
73+
case artboardIdPropertyKey:
74+
m_ArtboardId = CoreUintType::deserialize(reader);
75+
return true;
76+
case viewModelIdPropertyKey:
77+
m_ViewModelId = CoreUintType::deserialize(reader);
78+
return true;
79+
}
80+
return Component::deserialize(propertyKey, reader);
81+
}
82+
83+
protected:
84+
virtual void artboardIdChanged() {}
85+
virtual void viewModelIdChanged() {}
86+
};
87+
} // namespace rive
88+
89+
#endif

include/rive/generated/core_registry.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
#include "rive/animation/transition_viewmodel_condition.hpp"
9191
#include "rive/artboard.hpp"
9292
#include "rive/artboard_component_list.hpp"
93+
#include "rive/artboard_list_map_rule.hpp"
9394
#include "rive/assets/asset.hpp"
9495
#include "rive/assets/audio_asset.hpp"
9596
#include "rive/assets/drawable_asset.hpp"
@@ -805,6 +806,8 @@ class CoreRegistry
805806
return new Text();
806807
case TextValueRunBase::typeKey:
807808
return new TextValueRun();
809+
case ArtboardListMapRuleBase::typeKey:
810+
return new ArtboardListMapRule();
808811
case CustomPropertyEnumBase::typeKey:
809812
return new CustomPropertyEnum();
810813
case FolderBase::typeKey:
@@ -1530,6 +1533,12 @@ class CoreRegistry
15301533
case TextValueRunBase::styleIdPropertyKey:
15311534
object->as<TextValueRunBase>()->styleId(value);
15321535
break;
1536+
case ArtboardListMapRuleBase::artboardIdPropertyKey:
1537+
object->as<ArtboardListMapRuleBase>()->artboardId(value);
1538+
break;
1539+
case ArtboardListMapRuleBase::viewModelIdPropertyKey:
1540+
object->as<ArtboardListMapRuleBase>()->viewModelId(value);
1541+
break;
15331542
case CustomPropertyEnumBase::propertyValuePropertyKey:
15341543
object->as<CustomPropertyEnumBase>()->propertyValue(value);
15351544
break;
@@ -2982,6 +2991,10 @@ class CoreRegistry
29822991
return object->as<TextBase>()->textRunListSource();
29832992
case TextValueRunBase::styleIdPropertyKey:
29842993
return object->as<TextValueRunBase>()->styleId();
2994+
case ArtboardListMapRuleBase::artboardIdPropertyKey:
2995+
return object->as<ArtboardListMapRuleBase>()->artboardId();
2996+
case ArtboardListMapRuleBase::viewModelIdPropertyKey:
2997+
return object->as<ArtboardListMapRuleBase>()->viewModelId();
29852998
case CustomPropertyEnumBase::propertyValuePropertyKey:
29862999
return object->as<CustomPropertyEnumBase>()->propertyValue();
29873000
case CustomPropertyEnumBase::enumIdPropertyKey:
@@ -3857,6 +3870,8 @@ class CoreRegistry
38573870
case TextBase::verticalAlignValuePropertyKey:
38583871
case TextBase::textRunListSourcePropertyKey:
38593872
case TextValueRunBase::styleIdPropertyKey:
3873+
case ArtboardListMapRuleBase::artboardIdPropertyKey:
3874+
case ArtboardListMapRuleBase::viewModelIdPropertyKey:
38603875
case CustomPropertyEnumBase::propertyValuePropertyKey:
38613876
case CustomPropertyEnumBase::enumIdPropertyKey:
38623877
case FileAssetBase::assetIdPropertyKey:
@@ -4619,6 +4634,10 @@ class CoreRegistry
46194634
return object->is<TextBase>();
46204635
case TextValueRunBase::styleIdPropertyKey:
46214636
return object->is<TextValueRunBase>();
4637+
case ArtboardListMapRuleBase::artboardIdPropertyKey:
4638+
return object->is<ArtboardListMapRuleBase>();
4639+
case ArtboardListMapRuleBase::viewModelIdPropertyKey:
4640+
return object->is<ArtboardListMapRuleBase>();
46224641
case CustomPropertyEnumBase::propertyValuePropertyKey:
46234642
return object->is<CustomPropertyEnumBase>();
46244643
case CustomPropertyEnumBase::enumIdPropertyKey:

src/artboard_component_list.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "rive/viewmodel/viewmodel_instance_symbol_list_index.hpp"
99
#include "rive/world_transform_component.hpp"
1010
#include "rive/layout/layout_data.hpp"
11+
#include "rive/artboard_list_map_rule.hpp"
1112

1213
using namespace rive;
1314

@@ -137,17 +138,34 @@ Artboard* ArtboardComponentList::findArtboard(
137138
{
138139
return nullptr;
139140
}
140-
auto artboard = m_artboardsMap.find(viewModelInstance->viewModelId());
141+
auto viewModelId = viewModelInstance->viewModelId();
142+
// Find artboard in the cached mappings
143+
auto artboard = m_artboardsMap.find(viewModelId);
141144
if (artboard != m_artboardsMap.end())
142145
{
143146
return artboard->second;
144147
}
145148
auto artboards = m_file->artboards();
149+
// Check if there is a special rule that maps the view model to a specific
150+
// artboard
151+
auto listRule = m_artboardMapRules.find(viewModelId);
152+
if (listRule != m_artboardMapRules.end())
153+
{
154+
auto artboardIndex = listRule->second;
155+
if (artboardIndex < artboards.size())
156+
{
157+
auto artboard = artboards[artboardIndex];
158+
m_artboardsMap[viewModelId] = artboard;
159+
return artboard;
160+
}
161+
}
162+
163+
// Search for the first artboard that is bound to this view model
146164
for (auto& artboard : artboards)
147165
{
148-
if (artboard->viewModelId() == viewModelInstance->viewModelId())
166+
if (artboard->viewModelId() == viewModelId)
149167
{
150-
m_artboardsMap[viewModelInstance->viewModelId()] = artboard;
168+
m_artboardsMap[viewModelId] = artboard;
151169
return artboard;
152170
}
153171
}
@@ -1081,4 +1099,9 @@ void ArtboardComponentList::listItemTransforms(std::vector<Mat2D*>& transforms)
10811099
transforms.push_back(&m_artboardTransforms[artboard]);
10821100
}
10831101
}
1102+
}
1103+
1104+
void ArtboardComponentList::addMapRule(ArtboardListMapRule* rule)
1105+
{
1106+
m_artboardMapRules[rule->viewModelId()] = rule->artboardId();
10841107
}

src/artboard_list_map_rule.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "rive/component.hpp"
2+
#include "rive/file.hpp"
3+
#include "rive/artboard_list_map_rule.hpp"
4+
#include "rive/artboard_component_list.hpp"
5+
6+
using namespace rive;
7+
8+
StatusCode ArtboardListMapRule::onAddedDirty(CoreContext* context)
9+
{
10+
StatusCode code = Super::onAddedDirty(context);
11+
if (code != StatusCode::Ok)
12+
{
13+
return code;
14+
}
15+
if (!parent()->is<ArtboardComponentList>())
16+
{
17+
return StatusCode::MissingObject;
18+
}
19+
parent()->as<ArtboardComponentList>()->addMapRule(this);
20+
return StatusCode::Ok;
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "rive/generated/artboard_list_map_rule_base.hpp"
2+
#include "rive/artboard_list_map_rule.hpp"
3+
4+
using namespace rive;
5+
6+
Core* ArtboardListMapRuleBase::clone() const
7+
{
8+
auto cloned = new ArtboardListMapRule();
9+
cloned->copy(*this);
10+
return cloned;
11+
}
1.13 KB
Binary file not shown.

0 commit comments

Comments
 (0)