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+ 
8596static  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
141175static  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+ 
198248void  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