Skip to content

Commit 7f227f1

Browse files
authored
Add rive.set_state_machine_input (#136)
* Add rive.set_state_machine_input * Add docs
1 parent 6925726 commit 7f227f1

17 files changed

+216
-192
lines changed

defold-rive/api/rive.script_api

+23
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,26 @@
269269
- name: value
270270
type: number|bool
271271
desc: The value of the input
272+
273+
#*****************************************************************************************************
274+
275+
- name: set_state_machine_input
276+
type: function
277+
desc: Set the input values from a state machine input, either from the current top-level artboard, or from a nested artboard inside the Rive model artboard. Note - To set input for a trigger, use a bool value.
278+
279+
parameters:
280+
- name: url
281+
type: url
282+
desc: The Rive model
283+
284+
- name: name
285+
type: string
286+
desc: The name of the input
287+
288+
- name: value
289+
type: number|bool
290+
desc: The value of the input to set
291+
292+
- name: nested_artboard
293+
type: string
294+
desc: (OPTIONAL) If specified, the input will be queried for the specified nested artboard

defold-rive/src/comp_rive.cpp

+66-8
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,7 @@ namespace dmRive
15821582
}
15831583
}
15841584

1585-
GetStateMachineInputData::Result CompRiveGetStateMachineInput(RiveComponent* component, const char* input_name, const char* nested_artboard_path, GetStateMachineInputData& out_value)
1585+
StateMachineInputData::Result CompRiveSetStateMachineInput(RiveComponent* component, const char* input_name, const char* nested_artboard_path, const StateMachineInputData& value)
15861586
{
15871587
rive::ArtboardInstance* artboard = component->m_ArtboardInstance.get();
15881588
rive::SMIInput* input_instance = 0x0;
@@ -1601,33 +1601,91 @@ namespace dmRive
16011601
}
16021602
}
16031603

1604-
out_value.m_Type = GetStateMachineInputData::TYPE_INVALID;
1604+
if (input_instance)
1605+
{
1606+
const rive::StateMachineInput* input = input_instance->input();
1607+
1608+
if (input->is<rive::StateMachineTrigger>())
1609+
{
1610+
if (value.m_Type != StateMachineInputData::TYPE_BOOL)
1611+
{
1612+
return StateMachineInputData::RESULT_TYPE_MISMATCH;
1613+
}
1614+
rive::SMITrigger* trigger = (rive::SMITrigger*)input_instance;
1615+
trigger->fire();
1616+
return StateMachineInputData::RESULT_OK;
1617+
}
1618+
else if (input->is<rive::StateMachineBool>())
1619+
{
1620+
if (value.m_Type != StateMachineInputData::TYPE_BOOL)
1621+
{
1622+
return StateMachineInputData::RESULT_TYPE_MISMATCH;
1623+
}
1624+
rive::SMIBool* v = (rive::SMIBool*)input_instance;
1625+
v->value(value.m_BoolValue);
1626+
return StateMachineInputData::RESULT_OK;
1627+
}
1628+
else if (input->is<rive::StateMachineNumber>())
1629+
{
1630+
if (value.m_Type != StateMachineInputData::TYPE_NUMBER)
1631+
{
1632+
return StateMachineInputData::RESULT_TYPE_MISMATCH;
1633+
}
1634+
rive::SMINumber* v = (rive::SMINumber*)input_instance;
1635+
v->value(value.m_NumberValue);
1636+
return StateMachineInputData::RESULT_OK;
1637+
}
1638+
}
1639+
1640+
return StateMachineInputData::RESULT_NOT_FOUND;
1641+
}
1642+
1643+
StateMachineInputData::Result CompRiveGetStateMachineInput(RiveComponent* component, const char* input_name, const char* nested_artboard_path, StateMachineInputData& out_value)
1644+
{
1645+
rive::ArtboardInstance* artboard = component->m_ArtboardInstance.get();
1646+
rive::SMIInput* input_instance = 0x0;
1647+
1648+
if (nested_artboard_path)
1649+
{
1650+
input_instance = artboard->input(input_name, nested_artboard_path);
1651+
}
1652+
else
1653+
{
1654+
dmhash_t input_hash = dmHashString64(input_name);
1655+
int index = FindStateMachineInputIndex(component, input_hash);
1656+
if (index >= 0)
1657+
{
1658+
input_instance = component->m_StateMachineInstance->input(index);
1659+
}
1660+
}
1661+
1662+
out_value.m_Type = StateMachineInputData::TYPE_INVALID;
16051663

16061664
if (input_instance)
16071665
{
16081666
const rive::StateMachineInput* input = input_instance->input();
16091667

16101668
if (input->is<rive::StateMachineTrigger>())
16111669
{
1612-
return GetStateMachineInputData::RESULT_TYPE_UNSUPPORTED;
1670+
return StateMachineInputData::RESULT_TYPE_UNSUPPORTED;
16131671
}
16141672
else if (input->is<rive::StateMachineBool>())
16151673
{
16161674
rive::SMIBool* v = (rive::SMIBool*)input_instance;
1617-
out_value.m_Type = GetStateMachineInputData::TYPE_BOOL;
1675+
out_value.m_Type = StateMachineInputData::TYPE_BOOL;
16181676
out_value.m_BoolValue = v->value();
1619-
return GetStateMachineInputData::RESULT_OK;
1677+
return StateMachineInputData::RESULT_OK;
16201678
}
16211679
else if (input->is<rive::StateMachineNumber>())
16221680
{
16231681
rive::SMINumber* v = (rive::SMINumber*)input_instance;
1624-
out_value.m_Type = GetStateMachineInputData::TYPE_NUMBER;
1682+
out_value.m_Type = StateMachineInputData::TYPE_NUMBER;
16251683
out_value.m_NumberValue = v->value();
1626-
return GetStateMachineInputData::RESULT_OK;
1684+
return StateMachineInputData::RESULT_OK;
16271685
}
16281686
}
16291687

1630-
return GetStateMachineInputData::RESULT_NOT_FOUND;
1688+
return StateMachineInputData::RESULT_NOT_FOUND;
16311689
}
16321690

16331691
static inline rive::TextValueRun* GetTextRun(rive::ArtboardInstance* artboard, const char* name, const char* nested_artboard_path)

defold-rive/src/comp_rive.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace dmRive
7777

7878
// For scripting
7979

80-
struct GetStateMachineInputData
80+
struct StateMachineInputData
8181
{
8282
union
8383
{
@@ -87,7 +87,8 @@ namespace dmRive
8787

8888
enum Result
8989
{
90-
RESULT_NOT_FOUND = -2,
90+
RESULT_NOT_FOUND = -3,
91+
RESULT_TYPE_MISMATCH = -2,
9192
RESULT_TYPE_UNSUPPORTED = -1,
9293
RESULT_OK = 0,
9394
};
@@ -115,7 +116,8 @@ namespace dmRive
115116
const char* CompRiveGetTextRun(RiveComponent* component, const char* name, const char* nested_artboard_path);
116117
bool CompRiveSetTextRun(RiveComponent* component, const char* name, const char* text_run, const char* nested_artboard_path);
117118

118-
GetStateMachineInputData::Result CompRiveGetStateMachineInput(RiveComponent* component, const char* input_name, const char* nested_artboard_path, GetStateMachineInputData& data);
119+
StateMachineInputData::Result CompRiveGetStateMachineInput(RiveComponent* component, const char* input_name, const char* nested_artboard_path, StateMachineInputData& data);
120+
StateMachineInputData::Result CompRiveSetStateMachineInput(RiveComponent* component, const char* input_name, const char* nested_artboard_path, const StateMachineInputData& data);
119121

120122
float CompRiveGetDisplayScaleFactor();
121123

defold-rive/src/script_rive.cpp

+56-8
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,51 @@ namespace dmRive
325325
return 1;
326326
}
327327

328+
static int RiveComp_SetStateMachineInput(lua_State* L)
329+
{
330+
DM_LUA_STACK_CHECK(L, 0);
331+
332+
RiveComponent* component = 0;
333+
dmScript::GetComponentFromLua(L, 1, dmRive::RIVE_MODEL_EXT, 0, (void**)&component, 0);
334+
335+
const char* input_name = luaL_checkstring(L, 2);
336+
const char* nested_artboard_path = 0; // optional
337+
338+
StateMachineInputData data = {};
339+
340+
if (lua_isnumber(L, 3))
341+
{
342+
data.m_Type = StateMachineInputData::TYPE_NUMBER;
343+
data.m_NumberValue = lua_tonumber(L,3);
344+
}
345+
else if (lua_isboolean(L,3))
346+
{
347+
data.m_Type = StateMachineInputData::TYPE_BOOL;
348+
data.m_BoolValue = lua_toboolean(L,3);
349+
}
350+
else
351+
{
352+
return DM_LUA_ERROR("Cannot set input '%s' with an unsupported type.", input_name);
353+
}
354+
355+
if (lua_isstring(L, 4))
356+
{
357+
nested_artboard_path = lua_tostring(L, 4);
358+
}
359+
360+
StateMachineInputData::Result res = CompRiveSetStateMachineInput(component, input_name, nested_artboard_path, data);
361+
if (res != StateMachineInputData::RESULT_OK)
362+
{
363+
if (res == StateMachineInputData::RESULT_TYPE_MISMATCH)
364+
{
365+
return DM_LUA_ERROR("Type mismatch for input '%s'.", input_name);
366+
}
367+
assert(res == StateMachineInputData::RESULT_NOT_FOUND);
368+
return DM_LUA_ERROR("The input '%s' could not be found (or an unknown error happened).", input_name);
369+
}
370+
return 0;
371+
}
372+
328373
static int RiveComp_GetStateMachineInput(lua_State* L)
329374
{
330375
DM_LUA_STACK_CHECK(L, 1);
@@ -340,26 +385,28 @@ namespace dmRive
340385
nested_artboard_path = lua_tostring(L, 3);
341386
}
342387

343-
GetStateMachineInputData data;
344-
GetStateMachineInputData::Result res = CompRiveGetStateMachineInput(component, input_name, nested_artboard_path, data);
388+
StateMachineInputData data;
389+
StateMachineInputData::Result res = CompRiveGetStateMachineInput(component, input_name, nested_artboard_path, data);
345390

346-
if (res != GetStateMachineInputData::RESULT_OK)
391+
if (res != StateMachineInputData::RESULT_OK)
347392
{
348-
if (res == GetStateMachineInputData::RESULT_TYPE_UNSUPPORTED)
393+
if (res == StateMachineInputData::RESULT_TYPE_UNSUPPORTED)
394+
{
349395
return DM_LUA_ERROR("The input '%s' has an unsupported type.", input_name);
350-
assert(res == GetStateMachineInputData::RESULT_NOT_FOUND);
396+
}
397+
assert(res == StateMachineInputData::RESULT_NOT_FOUND);
351398
return DM_LUA_ERROR("The input '%s' could not be found (or an unknown error happened).", input_name);
352399
}
353400

354401
switch(data.m_Type)
355402
{
356-
case GetStateMachineInputData::TYPE_BOOL:
403+
case StateMachineInputData::TYPE_BOOL:
357404
lua_pushboolean(L, data.m_BoolValue);
358405
break;
359-
case GetStateMachineInputData::TYPE_NUMBER:
406+
case StateMachineInputData::TYPE_NUMBER:
360407
lua_pushnumber(L, data.m_NumberValue);
361408
break;
362-
case GetStateMachineInputData::TYPE_INVALID:
409+
case StateMachineInputData::TYPE_INVALID:
363410
break;
364411
}
365412
return 1;
@@ -378,6 +425,7 @@ namespace dmRive
378425
{"get_text_run", RiveComp_GetTextRun},
379426
{"get_projection_matrix", RiveComp_GetProjectionMatrix},
380427
{"get_state_machine_input", RiveComp_GetStateMachineInput},
428+
{"set_state_machine_input", RiveComp_SetStateMachineInput},
381429
{0, 0}
382430
};
383431

docs/index.md

+9
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ local v = rive.get_state_machine_input("#rivemodel", "Number 1", "My_Nested_Artb
240240

241241
-- To go deeper into the nested hierarchy, you can add slashes between each scope
242242
local v = rive.get_state_machine_input("#rivemodel", "Number 1", "My_Nested_Artboard/My_Inner_Nested_Artboard")
243+
244+
-- Set the input value of the current state machine
245+
rive.set_state_machine_input("#rivemodel", "Number 1", 0.5)
246+
247+
-- Set the input value of a nested artboard
248+
rive.set_state_machine_input("#rivemodel", "Number 1", 0.5, "My_Nested_Artboard")
249+
250+
-- Same as the example above, to go even deeper, separate the scopers with slashes!
251+
rive.set_state_machine_input("#rivemodel", "Number 1", 0.5, "My_Nested_Artboard/My_Inner_Nested_Artboard")
243252
```
244253

245254
#### Events

main/bones/bones.collection

+1-64
Original file line numberDiff line numberDiff line change
@@ -2,100 +2,37 @@ name: "bones"
22
instances {
33
id: "back"
44
prototype: "/main/menu/back.go"
5-
position {
6-
x: 0.0
7-
y: 0.0
8-
z: 0.0
9-
}
10-
rotation {
11-
x: 0.0
12-
y: 0.0
13-
z: 0.0
14-
w: 1.0
15-
}
16-
scale3 {
17-
x: 1.0
18-
y: 1.0
19-
z: 1.0
20-
}
215
}
226
scale_along_z: 0
237
embedded_instances {
248
id: "go"
259
data: "components {\n"
2610
" id: \"bones\"\n"
2711
" component: \"/main/bones/bones.script\"\n"
28-
" position {\n"
29-
" x: 0.0\n"
30-
" y: 0.0\n"
31-
" z: 0.0\n"
32-
" }\n"
33-
" rotation {\n"
34-
" x: 0.0\n"
35-
" y: 0.0\n"
36-
" z: 0.0\n"
37-
" w: 1.0\n"
38-
" }\n"
39-
" property_decls {\n"
40-
" }\n"
4112
"}\n"
4213
"embedded_components {\n"
4314
" id: \"rivemodel\"\n"
4415
" type: \"rivemodel\"\n"
4516
" data: \"scene: \\\"/main/bones/marty.rivescene\\\"\\n"
4617
"default_animation: \\\"Animation1\\\"\\n"
4718
"material: \\\"/defold-rive/assets/rivemodel.material\\\"\\n"
48-
"blend_mode: BLEND_MODE_ALPHA\\n"
49-
"default_state_machine: \\\"\\\"\\n"
5019
"create_go_bones: true\\n"
51-
"artboard: \\\"\\\"\\n"
20+
"artboard: \\\"New Artboard\\\"\\n"
5221
"\"\n"
53-
" position {\n"
54-
" x: 0.0\n"
55-
" y: 0.0\n"
56-
" z: 0.0\n"
57-
" }\n"
58-
" rotation {\n"
59-
" x: 0.0\n"
60-
" y: 0.0\n"
61-
" z: 0.0\n"
62-
" w: 1.0\n"
63-
" }\n"
6422
"}\n"
6523
"embedded_components {\n"
6624
" id: \"bonefactory\"\n"
6725
" type: \"factory\"\n"
6826
" data: \"prototype: \\\"/main/bones/bone.go\\\"\\n"
69-
"load_dynamically: false\\n"
70-
"dynamic_prototype: false\\n"
7127
"\"\n"
72-
" position {\n"
73-
" x: 0.0\n"
74-
" y: 0.0\n"
75-
" z: 0.0\n"
76-
" }\n"
77-
" rotation {\n"
78-
" x: 0.0\n"
79-
" y: 0.0\n"
80-
" z: 0.0\n"
81-
" w: 1.0\n"
82-
" }\n"
8328
"}\n"
8429
""
8530
position {
8631
x: 486.0
8732
y: 271.726
88-
z: 0.0
89-
}
90-
rotation {
91-
x: 0.0
92-
y: 0.0
93-
z: 0.0
94-
w: 1.0
9533
}
9634
scale3 {
9735
x: 0.5
9836
y: 0.5
99-
z: 1.0
10037
}
10138
}

0 commit comments

Comments
 (0)