Skip to content

Commit fc2e56f

Browse files
authored
Add new function: rive.get_state_machine_input (#124)
* Add new function: rive.get_state_machine_input * Added a docstring for the inputs
1 parent ee36438 commit fc2e56f

File tree

6 files changed

+160
-12
lines changed

6 files changed

+160
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ bob.jar
2020
/.editor_settings
2121
*.keystore*
2222
manifest.*
23+
/main/ignore

defold-rive/api/rive.script_api

+24
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,27 @@
245245
- name: matrix
246246
type: vmath.matrix4
247247
desc: The projection matrix
248+
249+
#*****************************************************************************************************
250+
251+
- name: get_state_machine_input
252+
type: function
253+
desc: Get 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: Trigger inputs will not generate a value!
254+
255+
parameters:
256+
- name: url
257+
type: url
258+
desc: The Rive model
259+
260+
- name: name
261+
type: string
262+
desc: The name of the input
263+
264+
- name: nested_artboard
265+
type: string
266+
desc: (OPTIONAL) If specified, the input will be queried for the specified nested artboard
267+
268+
return:
269+
- name: value
270+
type: number|bool
271+
desc: The value of the input

defold-rive/src/comp_rive.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <rive/text/text.hpp>
3232
#include <rive/file.hpp>
3333
#include <rive/renderer.hpp>
34+
#include <rive/nested_artboard.hpp>
3435

3536
// Rive extension
3637
#include "comp_rive.h"
@@ -1548,6 +1549,54 @@ namespace dmRive
15481549
}
15491550
}
15501551

1552+
GetStateMachineInputData::Result CompRiveGetStateMachineInput(RiveComponent* component, const char* input_name, const char* nested_artboard_path, GetStateMachineInputData& out_value)
1553+
{
1554+
rive::ArtboardInstance* artboard = component->m_ArtboardInstance.get();
1555+
rive::SMIInput* input_instance = 0x0;
1556+
1557+
if (nested_artboard_path)
1558+
{
1559+
input_instance = artboard->input(input_name, nested_artboard_path);
1560+
}
1561+
else
1562+
{
1563+
dmhash_t input_hash = dmHashString64(input_name);
1564+
int index = FindStateMachineInputIndex(component, input_hash);
1565+
if (index >= 0)
1566+
{
1567+
input_instance = component->m_StateMachineInstance->input(index);
1568+
}
1569+
}
1570+
1571+
out_value.m_Type = GetStateMachineInputData::TYPE_INVALID;
1572+
1573+
if (input_instance)
1574+
{
1575+
const rive::StateMachineInput* input = input_instance->input();
1576+
1577+
if (input->is<rive::StateMachineTrigger>())
1578+
{
1579+
return GetStateMachineInputData::RESULT_TYPE_UNSUPPORTED;
1580+
}
1581+
else if (input->is<rive::StateMachineBool>())
1582+
{
1583+
rive::SMIBool* v = (rive::SMIBool*)input_instance;
1584+
out_value.m_Type = GetStateMachineInputData::TYPE_BOOL;
1585+
out_value.m_BoolValue = v->value();
1586+
return GetStateMachineInputData::RESULT_OK;
1587+
}
1588+
else if (input->is<rive::StateMachineNumber>())
1589+
{
1590+
rive::SMINumber* v = (rive::SMINumber*)input_instance;
1591+
out_value.m_Type = GetStateMachineInputData::TYPE_NUMBER;
1592+
out_value.m_NumberValue = v->value();
1593+
return GetStateMachineInputData::RESULT_OK;
1594+
}
1595+
}
1596+
1597+
return GetStateMachineInputData::RESULT_NOT_FOUND;
1598+
}
1599+
15511600
static inline rive::TextValueRun* GetTextRun(rive::ArtboardInstance* artboard, const char* name, const char* nested_artboard_path)
15521601
{
15531602
if (nested_artboard_path)

defold-rive/src/comp_rive.h

+27
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ namespace dmRive
7575

7676
// For scripting
7777

78+
struct GetStateMachineInputData
79+
{
80+
union
81+
{
82+
bool m_BoolValue;
83+
float m_NumberValue;
84+
};
85+
86+
enum Result
87+
{
88+
RESULT_NOT_FOUND = -2,
89+
RESULT_TYPE_UNSUPPORTED = -1,
90+
RESULT_OK = 0,
91+
};
92+
93+
enum Type
94+
{
95+
TYPE_INVALID = -1,
96+
TYPE_BOOL = 0,
97+
TYPE_NUMBER = 1
98+
};
99+
100+
Type m_Type;
101+
};
102+
78103
// Get the game object identifier
79104
bool CompRiveGetBoneID(RiveComponent* component, dmhash_t bone_name, dmhash_t* id);
80105

@@ -88,6 +113,8 @@ namespace dmRive
88113
const char* CompRiveGetTextRun(RiveComponent* component, const char* name, const char* nested_artboard_path);
89114
bool CompRiveSetTextRun(RiveComponent* component, const char* name, const char* text_run, const char* nested_artboard_path);
90115

116+
GetStateMachineInputData::Result CompRiveGetStateMachineInput(RiveComponent* component, const char* input_name, const char* nested_artboard_path, GetStateMachineInputData& data);
117+
91118
float CompRiveGetDisplayScaleFactor();
92119

93120
// bool CompRiveSetIKTargetInstance(RiveComponent* component, dmhash_t constraint_id, float mix, dmhash_t instance_id);

defold-rive/src/script_rive.cpp

+50-12
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,14 @@ namespace dmRive
294294
dmScript::GetComponentFromLua(L, 1, dmRive::RIVE_MODEL_EXT, 0, (void**)&component, 0);
295295

296296
const char* name = luaL_checkstring(L, 2);
297+
const char* text_run = luaL_checkstring(L, 3);
297298
const char* nested_artboard_path = 0;
298299

299300
if (lua_isstring(L, 4))
300301
{
301302
nested_artboard_path = lua_tostring(L, 4);
302303
}
303304

304-
const char* text_run = luaL_checkstring(L, 3);
305-
306305
if (!CompRiveSetTextRun(component, name, text_run, nested_artboard_path))
307306
{
308307
return DM_LUA_ERROR("The text-run '%s' could not be found.", name);
@@ -326,18 +325,57 @@ namespace dmRive
326325
return 1;
327326
}
328327

328+
static int RiveComp_GetStateMachineInput(lua_State* L)
329+
{
330+
DM_LUA_STACK_CHECK(L, 1);
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+
if (lua_isstring(L, 3))
339+
{
340+
nested_artboard_path = lua_tostring(L, 3);
341+
}
342+
343+
GetStateMachineInputData data;
344+
GetStateMachineInputData::Result res = CompRiveGetStateMachineInput(component, input_name, nested_artboard_path, data);
345+
346+
if (res != GetStateMachineInputData::RESULT_OK)
347+
{
348+
if (res == GetStateMachineInputData::RESULT_TYPE_UNSUPPORTED)
349+
return DM_LUA_ERROR("The input '%s' has an unsupported type.", input_name);
350+
assert(res == GetStateMachineInputData::RESULT_NOT_FOUND);
351+
return DM_LUA_ERROR("The input '%s' could not be found (or an unknown error happened).", input_name);
352+
}
353+
354+
switch(data.m_Type)
355+
{
356+
case GetStateMachineInputData::TYPE_BOOL:
357+
lua_pushboolean(L, data.m_BoolValue);
358+
break;
359+
case GetStateMachineInputData::TYPE_NUMBER:
360+
lua_pushnumber(L, data.m_NumberValue);
361+
break;
362+
}
363+
return 1;
364+
}
365+
329366
static const luaL_reg RIVE_FUNCTIONS[] =
330367
{
331-
{"play_anim", RiveComp_PlayAnim},
332-
{"play_state_machine", RiveComp_PlayStateMachine},
333-
{"cancel", RiveComp_Cancel},
334-
{"get_go", RiveComp_GetGO},
335-
{"pointer_move", RiveComp_PointerMove},
336-
{"pointer_up", RiveComp_PointerUp},
337-
{"pointer_down", RiveComp_PointerDown},
338-
{"set_text_run", RiveComp_SetTextRun},
339-
{"get_text_run", RiveComp_GetTextRun},
340-
{"get_projection_matrix", RiveComp_GetProjectionMatrix},
368+
{"play_anim", RiveComp_PlayAnim},
369+
{"play_state_machine", RiveComp_PlayStateMachine},
370+
{"cancel", RiveComp_Cancel},
371+
{"get_go", RiveComp_GetGO},
372+
{"pointer_move", RiveComp_PointerMove},
373+
{"pointer_up", RiveComp_PointerUp},
374+
{"pointer_down", RiveComp_PointerDown},
375+
{"set_text_run", RiveComp_SetTextRun},
376+
{"get_text_run", RiveComp_GetTextRun},
377+
{"get_projection_matrix", RiveComp_GetProjectionMatrix},
378+
{"get_state_machine_input", RiveComp_GetStateMachineInput},
341379
{0, 0}
342380
};
343381

docs/index.md

+9
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ go.set("#rivemodel", "Trigger 1", true)
231231

232232
-- Set the numeric value "Number 1" to 0.8
233233
go.set("#rivemodel", "Number 1", 0.8)
234+
235+
-- Read the input value of the current state machine
236+
local v = rive.get_state_machine_input("#rivemodel", "Number 1")
237+
238+
-- Read the input value of a nested artboard from the current state machine
239+
local v = rive.get_state_machine_input("#rivemodel", "Number 1", "My_Nested_Artboard")
240+
241+
-- To go deeper into the nested hierarchy, you can add slashes between each scope
242+
local v = rive.get_state_machine_input("#rivemodel", "Number 1", "My_Nested_Artboard/My_Inner_Nested_Artboard")
234243
```
235244

236245
#### Events

0 commit comments

Comments
 (0)