Skip to content

Commit 01e1500

Browse files
bodymovinbodymovin
andcommitted
feature: expose root viewmodel to scripting via context (#11539) 0eadaaea47
Co-authored-by: hernan <hernan@rive.app>
1 parent 6c29365 commit 01e1500

8 files changed

Lines changed: 54 additions & 1 deletion

File tree

.rive_head

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
147fe75b6ba2592e3c8531b518433e87ab399b4c
1+
0eadaaea4797dfb4e6887ba05dd12774c3a7af97

include/rive/data_bind/data_context.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ class DataContext
3434
void viewModelInstance(rcp<ViewModelInstance> value);
3535
void advanced();
3636
rcp<ViewModelInstance> viewModelInstance() { return m_ViewModelInstance; };
37+
rcp<ViewModelInstance> rootViewModelInstance()
38+
{
39+
if (m_Parent)
40+
{
41+
return m_Parent->rootViewModelInstance();
42+
}
43+
return m_ViewModelInstance;
44+
};
3745

3846
ViewModelInstanceValue* viewModelValue()
3947
{

include/rive/lua/rive_lua_libs.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ enum class LuaAtoms : int16_t
212212
// Scripted Context
213213
markNeedsUpdate,
214214
viewModel,
215+
rootViewModel,
215216

216217
// Animation
217218
duration,

src/lua/lua_scripted_context.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ static int context_namecall(lua_State* L)
4242
}
4343
return 0;
4444
}
45+
case (int)LuaAtoms::rootViewModel:
46+
{
47+
auto scriptedObject = scriptedContext->scriptedObject();
48+
auto dataContext = scriptedObject->dataContext();
49+
if (dataContext)
50+
{
51+
auto viewModelInstance =
52+
dataContext->rootViewModelInstance();
53+
if (viewModelInstance)
54+
{
55+
lua_newrive<ScriptedViewModel>(
56+
L,
57+
L,
58+
ref_rcp(viewModelInstance->viewModel()),
59+
viewModelInstance);
60+
return 1;
61+
}
62+
}
63+
return 0;
64+
}
4565
default:
4666
break;
4767
}

src/lua/rive_lua_libs.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ std::unordered_map<std::string, int16_t> atoms = {
144144
{"isClosed", (int16_t)LuaAtoms::isClosed},
145145
{"markNeedsUpdate", (int16_t)LuaAtoms::markNeedsUpdate},
146146
{"viewModel", (int16_t)LuaAtoms::viewModel},
147+
{"rootViewModel", (int16_t)LuaAtoms::rootViewModel},
147148
{"duration", (int16_t)LuaAtoms::duration},
148149
{"setTime", (int16_t)LuaAtoms::setTime},
149150
{"setTimeFrames", (int16_t)LuaAtoms::setTimeFrames},
1.32 KB
Binary file not shown.

tests/unit_tests/runtime/scripting/scripting_context_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,26 @@ TEST_CASE("script has access to the data bound view model", "[silver]")
205205

206206
CHECK(silver.matches("viewmodel_from_context"));
207207
}
208+
209+
TEST_CASE("script has access to the data root view model", "[silver]")
210+
{
211+
rive::SerializingFactory silver;
212+
auto file = ReadRiveFile("assets/scripting_root_viewmodel.riv", &silver);
213+
auto artboard = file->artboardNamed("parent");
214+
215+
silver.frameSize(artboard->width(), artboard->height());
216+
REQUIRE(artboard != nullptr);
217+
auto stateMachine = artboard->stateMachineAt(0);
218+
219+
auto vmi = file->createDefaultViewModelInstance(artboard.get());
220+
stateMachine->bindViewModelInstance(vmi);
221+
stateMachine->advanceAndApply(0.1f);
222+
223+
auto renderer = silver.makeRenderer();
224+
artboard->draw(renderer.get());
225+
silver.addFrame();
226+
stateMachine->advanceAndApply(0.1f);
227+
artboard->draw(renderer.get());
228+
229+
CHECK(silver.matches("scripting_root_viewmodel"));
230+
}
543 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)