Skip to content

Commit 74fa613

Browse files
committed
Added initial version of Rive Lua structs
1 parent b6d70ca commit 74fa613

22 files changed

+865
-525
lines changed

defold-rive/src/comp_rive_databinding.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ uint32_t CompRiveCreateViewModelInstanceRuntime(RiveComponent* component, dmhash
187187
return handle;
188188
}
189189

190+
rive::ViewModelInstanceRuntime* CompRiveGetViewModelInstance(RiveComponent* component)
191+
{
192+
rive::ViewModelInstanceRuntime* vmir = FromHandle(component, component->m_CurrentViewModelInstanceRuntime);
193+
return vmir;
194+
}
195+
190196
bool CompRiveDestroyViewModelInstanceRuntime(RiveComponent* component, uint32_t handle)
191197
{
192198
rive::ViewModelInstanceRuntime* vmir = FromHandle(component, handle);

defold-rive/src/comp_rive_private.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ namespace dmRive
147147
bool CompRiveRuntimeListAddInstance(RiveComponent* component, uint32_t handle, const char* path, uint32_t instance);
148148
bool CompRiveRuntimeListRemoveInstance(RiveComponent* component, uint32_t handle, const char* path, uint32_t instance);
149149

150-
RiveSceneData* CompRiveGetRiveSceneData(RiveComponent* component);
150+
RiveSceneData* CompRiveGetRiveSceneData(RiveComponent* component);
151+
rive::ViewModelInstanceRuntime* CompRiveGetViewModelInstance(RiveComponent* component);
151152
}
152153

153154
#endif //DM_COMP_RIVE_PRIVATE_H

defold-rive/src/script_rive.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "comp_rive_private.h"
3131
#include "rive_ddf.h"
3232
#include "res_rive_data.h"
33+
#include "script_rive_private.h"
3334

3435

3536
namespace dmRive
@@ -674,17 +675,17 @@ static int RiveComp_CreateRivFromMemory(lua_State* L)
674675
{0, 0}
675676
};
676677

677-
extern void ScriptInitializeFile(lua_State* L, dmResource::HFactory factory);
678-
extern void ScriptInitializeStructs(lua_State* L, dmResource::HFactory factory);
679-
extern void ScriptInitializeDataBinding(lua_State* L, dmResource::HFactory factory);
680-
681678
void ScriptRegister(lua_State* L, dmResource::HFactory factory)
682679
{
680+
// deprecated
683681
luaL_register(L, "rive", RIVE_FUNCTIONS);
684682
ScriptInitializeDataBinding(L, factory);
685683
lua_pop(L, 1);
684+
686685
ScriptInitializeFile(L, factory);
687-
ScriptInitializeStructs(L, factory);
686+
ScriptInitializeArtboard(L, factory);
687+
ScriptInitializeViewModel(L, factory);
688+
ScriptInitializeViewModelProperty(L, factory);
688689

689690
g_Factory = factory;
690691
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright 2021 The Defold Foundation
2+
// Licensed under the Defold License version 1.0 (the "License"); you may not use
3+
// this file except in compliance with the License.
4+
//
5+
// You may obtain a copy of the License, together with FAQs at
6+
// https://www.defold.com/license
7+
//
8+
// Unless required by applicable law or agreed to in writing, software distributed
9+
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10+
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
// specific language governing permissions and limitations under the License.
12+
13+
14+
#if !defined(DM_RIVE_UNSUPPORTED)
15+
16+
#include <stdint.h>
17+
18+
#include <dmsdk/dlib/dstrings.h>
19+
#include <dmsdk/gameobject/script.h>
20+
#include <dmsdk/gamesys/script.h>
21+
#include <dmsdk/script/script.h>
22+
#include <dmsdk/resource/resource.h>
23+
24+
#include "comp_rive.h"
25+
#include "comp_rive_private.h"
26+
#include "res_rive_data.h"
27+
#include "script_rive_private.h"
28+
#include <rive/data_bind/data_values/data_type.hpp>
29+
30+
#include <rive/file.hpp>
31+
#include <rive/animation/linear_animation_instance.hpp>
32+
#include <rive/animation/loop.hpp>
33+
#include <rive/animation/state_machine.hpp>
34+
#include <rive/animation/state_machine_instance.hpp>
35+
#include <rive/custom_property.hpp>
36+
#include <rive/custom_property_boolean.hpp>
37+
#include <rive/custom_property_number.hpp>
38+
#include <rive/custom_property_string.hpp>
39+
#include <rive/math/mat2d.hpp>
40+
#include <rive/nested_artboard.hpp> // Artboard
41+
#include <rive/renderer.hpp>
42+
#include <rive/text/text.hpp>
43+
44+
45+
namespace dmRive
46+
{
47+
48+
static dmResource::HFactory g_Factory = 0;
49+
50+
static const char* SCRIPT_TYPE_NAME_ARTBOARD = "Artboard";
51+
static uint32_t TYPE_HASH_ARTBOARD = 0;
52+
53+
54+
// *********************************************************************************
55+
56+
static bool IsArtboard(lua_State* L, int index)
57+
{
58+
return dmScript::GetUserType(L, index) == TYPE_HASH_ARTBOARD;
59+
}
60+
61+
static Artboard* ToArtboard(lua_State* L, int index)
62+
{
63+
return (Artboard*)dmScript::ToUserType(L, index, TYPE_HASH_ARTBOARD);
64+
}
65+
66+
static int Artboard_tostring(lua_State* L)
67+
{
68+
Artboard* artboard = ToArtboard(L, 1);
69+
if (!artboard)
70+
{
71+
lua_pushstring(L, "no valid pointer!");
72+
return 1;
73+
}
74+
lua_pushfstring(L, "rive.artboard(%p : '%s')", artboard, artboard->m_Instance ? artboard->m_Instance->name().c_str() : "null");
75+
return 1;
76+
}
77+
78+
static int Artboard_eq(lua_State* L)
79+
{
80+
Artboard* a1 = ToArtboard(L, 1);
81+
Artboard* a2 = ToArtboard(L, 2);
82+
lua_pushboolean(L, a1 && a2 && a1 == a2);
83+
return 1;
84+
}
85+
86+
static int Artboard_gc(lua_State* L)
87+
{
88+
Artboard* artboard = ToArtboard(L, 1);
89+
delete artboard->m_Instance;
90+
dmResource::Release(g_Factory, artboard->m_Resource);
91+
return 0;
92+
}
93+
94+
static const luaL_reg Artboard_methods[] =
95+
{
96+
{0,0}
97+
};
98+
99+
static const luaL_reg Artboard_meta[] =
100+
{
101+
{"__tostring", Artboard_tostring},
102+
{"__eq", Artboard_eq},
103+
{"__gc", Artboard_gc},
104+
{0,0}
105+
};
106+
107+
void PushArtboard(lua_State* L, RiveSceneData* resource, rive::ArtboardInstance* instance)
108+
{
109+
Artboard* artboard = (Artboard*)lua_newuserdata(L, sizeof(Artboard));
110+
artboard->m_Resource = resource;
111+
artboard->m_Instance = instance;
112+
luaL_getmetatable(L, SCRIPT_TYPE_NAME_ARTBOARD);
113+
lua_setmetatable(L, -2);
114+
dmResource::IncRef(g_Factory, resource);
115+
}
116+
117+
Artboard* CheckArtboard(lua_State* L, int index)
118+
{
119+
Artboard* artboard = (Artboard*)dmScript::CheckUserType(L, index, TYPE_HASH_ARTBOARD, 0);
120+
return artboard;
121+
}
122+
123+
void ScriptInitializeArtboard(lua_State* L, dmResource::HFactory factory)
124+
{
125+
int top = lua_gettop(L);
126+
127+
g_Factory = factory;
128+
129+
TYPE_HASH_ARTBOARD = dmScript::RegisterUserType(L, SCRIPT_TYPE_NAME_ARTBOARD, Artboard_methods, Artboard_meta);
130+
131+
assert(top == lua_gettop(L));
132+
}
133+
134+
}
135+
136+
#endif // DM_RIVE_UNSUPPORTED

defold-rive/src/script_rive_file.cpp

Lines changed: 84 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "comp_rive.h"
2525
#include "comp_rive_private.h"
2626
#include "res_rive_data.h"
27+
#include "script_rive_private.h"
2728
#include <rive/data_bind/data_values/data_type.hpp>
2829

2930
#include <rive/file.hpp>
@@ -67,21 +68,31 @@ static bool IsTypeRivc(HResourceFactory factory, dmhash_t path_hash)
6768

6869
// *********************************************************************************
6970

70-
struct RiveFile
71-
{
72-
RiveSceneData* m_Resource;
73-
};
7471

75-
static bool IsRiveFile(lua_State* L, int index)
72+
bool IsRiveFile(lua_State* L, int index)
7673
{
7774
return dmScript::GetUserType(L, index) == TYPE_HASH_RIVE_FILE;
7875
}
7976

80-
static RiveFile* ToRiveFile(lua_State* L, int index)
77+
RiveFile* ToRiveFile(lua_State* L, int index)
8178
{
8279
return (RiveFile*)dmScript::ToUserType(L, index, TYPE_HASH_RIVE_FILE);
8380
}
8481

82+
void PushRiveFile(lua_State* L, RiveSceneData* resource)
83+
{
84+
RiveFile* file = (RiveFile*)lua_newuserdata(L, sizeof(RiveFile));
85+
file->m_Resource = resource;
86+
luaL_getmetatable(L, SCRIPT_TYPE_NAME_RIVE_FILE);
87+
lua_setmetatable(L, -2);
88+
}
89+
90+
RiveFile* CheckRiveFile(lua_State* L, int index)
91+
{
92+
RiveFile* file = (RiveFile*)dmScript::CheckUserType(L, index, TYPE_HASH_RIVE_FILE, 0);
93+
return file;
94+
}
95+
8596
static int RiveFile_tostring(lua_State* L)
8697
{
8798
RiveFile* file = ToRiveFile(L, 1);
@@ -90,7 +101,7 @@ static int RiveFile_tostring(lua_State* L)
90101
lua_pushstring(L, "no valid pointer!");
91102
return 1;
92103
}
93-
lua_pushfstring(L, "%s.file(%p : %s)", "rive", file, dmHashReverseSafe64(file->m_Resource->m_PathHash));
104+
lua_pushfstring(L, "%s.file(%p : '%s')", "rive", file, dmHashReverseSafe64(file->m_Resource->m_PathHash));
94105
return 1;
95106
}
96107

@@ -109,33 +120,56 @@ static int RiveFile_gc(lua_State* L)
109120
return 0;
110121
}
111122

112-
static const luaL_reg RiveFile_methods[] =
123+
static int RiveFileGetArtboard(lua_State* L)
113124
{
114-
{0,0}
115-
};
125+
DM_LUA_STACK_CHECK(L, 1);
126+
RiveFile* file = CheckRiveFile(L, 1);
116127

117-
static const luaL_reg RiveFile_meta[] =
118-
{
119-
{"__tostring", RiveFile_tostring},
120-
// {"__index", RiveFile_index},
121-
// {"__newindex", RiveFile_newindex},
122-
{"__eq", RiveFile_eq},
123-
{"__gc", RiveFile_gc},
124-
{0,0}
125-
};
128+
const char* name = 0;
129+
int index = -1;
126130

127-
static void PushRiveFile(lua_State* L, RiveSceneData* resource)
128-
{
129-
RiveFile* file = (RiveFile*)lua_newuserdata(L, sizeof(RiveFile));
130-
file->m_Resource = resource;
131-
luaL_getmetatable(L, SCRIPT_TYPE_NAME_RIVE_FILE);
132-
lua_setmetatable(L, -2);
131+
switch(lua_type(L, 2))
132+
{
133+
case LUA_TSTRING: name = luaL_checkstring(L, 2); break;
134+
case LUA_TNUMBER: index = luaL_checknumber(L, 2); break;
135+
default: break;
136+
}
137+
138+
rive::ArtboardInstance* artboard = 0;
139+
if (name)
140+
{
141+
artboard = file->m_Resource->m_File->artboardNamed(name).release();
142+
if (!artboard)
143+
{
144+
return DM_LUA_ERROR("No artboard named '%s' in file '%s'", name, dmHashReverseSafe64(file->m_Resource->m_PathHash));
145+
}
146+
}
147+
else if(index >= 0)
148+
{
149+
artboard = file->m_Resource->m_File->artboardAt((size_t)index).release();
150+
if (!artboard)
151+
{
152+
return DM_LUA_ERROR("No artboard named at index %d in file '%s'", index, dmHashReverseSafe64(file->m_Resource->m_PathHash));
153+
}
154+
}
155+
else
156+
{
157+
artboard = file->m_Resource->m_File->artboardDefault().release();
158+
if (!artboard)
159+
{
160+
return DM_LUA_ERROR("No default artboard in file '%s'", dmHashReverseSafe64(file->m_Resource->m_PathHash));
161+
}
162+
}
163+
164+
PushArtboard(L, file->m_Resource, artboard);
165+
return 1;
133166
}
134167

135-
static RiveFile* CheckArtboard(lua_State* L, int index)
168+
static int RiveFileGetPath(lua_State* L)
136169
{
137-
RiveFile* file = (RiveFile*)dmScript::CheckUserType(L, index, TYPE_HASH_RIVE_FILE, 0);
138-
return file;
170+
RiveFile* file = CheckRiveFile(L, 1);
171+
lua_pushstring(L, dmHashReverseSafe64(file->m_Resource->m_PathHash));
172+
return 1;
139173
}
140174

141175
static RiveSceneData* AcquireRiveResource(lua_State* L, int index)
@@ -170,7 +204,7 @@ static RiveSceneData* AcquireRiveResource(lua_State* L, int index)
170204
return resource;
171205
}
172206

173-
static int FileGet(lua_State* L)
207+
static int RiveFileGet(lua_State* L)
174208
{
175209
int top = lua_gettop(L);
176210

@@ -186,38 +220,40 @@ static int FileGet(lua_State* L)
186220
return 1;
187221
}
188222

189-
190223
// *********************************************************************************
191224

192-
static const luaL_reg RIVE_FILE_FUNCTIONS[] =
225+
static const luaL_reg RiveFile_methods[] =
193226
{
194-
{"get_file", FileGet},
227+
{"get_path", RiveFileGetPath},
228+
{"get_artboard",RiveFileGetArtboard},
229+
{0,0}
230+
};
231+
232+
static const luaL_reg RiveFile_meta[] =
233+
{
234+
{"__tostring", RiveFile_tostring},
235+
{"__eq", RiveFile_eq},
236+
{"__gc", RiveFile_gc},
237+
{0,0}
238+
};
239+
240+
static const luaL_reg RiveFile_functions[] =
241+
{
242+
{"get_file", RiveFileGet},
195243
{0, 0}
196244
};
197245

246+
// *********************************************************************************
247+
198248
void ScriptInitializeFile(lua_State* L, dmResource::HFactory factory)
199249
{
200250
int top = lua_gettop(L);
201251

202252
g_Factory = factory;
203253

204-
struct
205-
{
206-
const char* m_Name;
207-
const luaL_reg* m_Methods;
208-
const luaL_reg* m_Metatable;
209-
uint32_t* m_TypeHash;
210-
} types[1] =
211-
{
212-
{SCRIPT_TYPE_NAME_RIVE_FILE, RiveFile_methods, RiveFile_meta, &TYPE_HASH_RIVE_FILE},
213-
};
214-
215-
for (uint32_t i = 0; i < DM_ARRAY_SIZE(types); ++i)
216-
{
217-
*types[i].m_TypeHash = dmScript::RegisterUserType(L, types[i].m_Name, types[i].m_Methods, types[i].m_Metatable);
218-
}
254+
TYPE_HASH_RIVE_FILE = dmScript::RegisterUserType(L, SCRIPT_TYPE_NAME_RIVE_FILE, RiveFile_methods, RiveFile_meta);
219255

220-
luaL_register(L, "rive", RIVE_FILE_FUNCTIONS);
256+
luaL_register(L, "rive", RiveFile_functions);
221257
lua_pop(L, 1);
222258

223259
assert(top == lua_gettop(L));

0 commit comments

Comments
 (0)