1616
1717typedef NS_ENUM (NSInteger , VPLuaSVGAViewCallback) {
1818 kVPLuaSVGAViewCallbackOnFinished = 1 ,
19- kVPLuaSVGAViewCallbackOnStep ,
20- kVPLuaSVGAViewCallbackOnClick
19+ kVPLuaSVGAViewCallbackOnStep
2120};
2221
23- static char *callbackSVGAViewKeys[] = { " " , " onFinished" , " onStep" , " onClick " };
22+ static char *callbackSVGAViewKeys[] = { " " , " onFinished" , " onStep" };
2423
25- #define META_TABLE_SVGAView " UI.SVGAView"
2624
2725@interface VPLuaSVGAView () <VPUPSVGAPlayerDelegate>
2826
@@ -76,7 +74,7 @@ static int lvNewSVGAView(lua_State *L) {
7674 userData->object = CFBridgingRetain (svgaView);// 脚本对象引用native对象
7775 svgaView.lv_userData = userData;// native对象引用脚本对象
7876
79- luaL_getmetatable (L, META_TABLE_SVGAView ); // 获取svgaView对应的类方法列表
77+ luaL_getmetatable (L, META_TABLE_UIView ); // 获取svgaView对应的类方法列表
8078 lua_setmetatable (L, -2 ); // 设置刚才创建的lua对象的方法列表是类svgaView的方法列表
8179 }
8280 LuaViewCore* view = LV_LUASTATE_VIEW (L);// 获取当前LuaView对应的LuaViewCore
@@ -181,50 +179,46 @@ static int svga (lua_State *L) {
181179 return 0 ;
182180}
183181
184- static int svgaCallback (lua_State *L) {
185- return lv_setCallbackByKey (L, nil , NO );
182+ static int svgaCallback (lua_State *L) {
183+ LVUserDataInfo *data = (LVUserDataInfo *)lua_touserdata (L, 1 );
184+ if (LVIsType (data, View) && lua_type (L, 2 ) == LUA_TTABLE) {
185+ lua_pushvalue (L, 2 );
186+ lua_pushnil (L);
187+
188+ while (lua_next (L, -2 )) {
189+ if (lua_type (L, -2 ) != LUA_TSTRING) {
190+ continue ;
191+ }
192+ const char * key = lua_tostring (L, -2 );
193+ int idx = 0 ;
194+ for (int i = 0 ; i < sizeof (callbackSVGAViewKeys) / sizeof (callbackSVGAViewKeys[0 ]); ++i) {
195+ if (strcmp (key, callbackSVGAViewKeys[i]) == 0 ) {
196+ idx = i;
197+ break ;
198+ }
199+ }
200+
201+ if (idx != 0 ) {
202+ lua_pushvalue (L, 1 );
203+ if (lua_type (L, -2 ) == LUA_TFUNCTION) {
204+ lua_pushvalue (L, -2 );
205+ } else {
206+ lua_pushnil (L);
207+ }
208+ lv_udataRef (L, idx);
209+ lua_pop (L, 2 );
210+ } else {
211+ lua_pop (L, 1 );
212+ }
213+ }
214+ lua_pop (L, 1 );
215+ }
216+
217+ lv_pushUserdata (L, data);
218+
219+ return 1 ;
186220}
187221
188- // static int svgaCallback(lua_State *L) {
189- // LVUserDataInfo *data = (LVUserDataInfo *)lua_touserdata(L, 1);
190- // if (LVIsType(data, View) && lua_type(L, 2) == LUA_TTABLE) {
191- // lua_pushvalue(L, 2);
192- // lua_pushnil(L);
193- //
194- // while (lua_next(L, -2)) {
195- // if (lua_type(L, -2) != LUA_TSTRING) {
196- // continue;
197- // }
198- // const char* key = lua_tostring(L, -2);
199- // int idx = 0;
200- // for (int i = 0; i < sizeof(callbackSVGAViewKeys) / sizeof(callbackSVGAViewKeys[0]); ++i) {
201- // if (strcmp(key, callbackSVGAViewKeys[i]) == 0) {
202- // idx = i;
203- // break;
204- // }
205- // }
206- //
207- // if (idx != 0) {
208- // lua_pushvalue(L, 1);
209- // if (lua_type(L, -2) == LUA_TFUNCTION) {
210- // lua_pushvalue(L, -2);
211- // } else {
212- // lua_pushnil(L);
213- // }
214- // lv_udataRef(L, idx);
215- // lua_pop(L, 2);
216- // } else {
217- // lua_pop(L, 1);
218- // }
219- // }
220- // lua_pop(L, 1);
221- // }
222- //
223- // lv_pushUserdata(L, data);
224- //
225- // return 1;
226- // }
227-
228222static int startAnimation (lua_State *L) {
229223 LVUserDataInfo * user = (LVUserDataInfo *)lua_touserdata (L, 1 );// 获取第一个参数(self,lua的userdata, 对象自身)
230224 if ( user ){
@@ -335,20 +329,12 @@ static int setCallback(lua_State *L, int idx) {
335329 return 1 ;
336330}
337331
338- // static int onFinished (lua_State *L) {
339- // return setCallback(L, kVPLuaSVGAViewCallbackOnFinished);
340- // }
341- //
342- // static int onStep (lua_State *L) {
343- // return setCallback(L, kVPLuaSVGAViewCallbackOnStep);
344- // }
345-
346332static int onFinished (lua_State *L) {
347- return lv_setCallbackByKey (L, callbackSVGAViewKeys[ kVPLuaSVGAViewCallbackOnFinished ], NO );
333+ return setCallback (L, kVPLuaSVGAViewCallbackOnFinished );
348334}
349335
350336static int onStep (lua_State *L) {
351- return lv_setCallbackByKey (L, callbackSVGAViewKeys[ kVPLuaSVGAViewCallbackOnStep ], NO );
337+ return setCallback (L, kVPLuaSVGAViewCallbackOnStep );
352338}
353339
354340/*
@@ -360,6 +346,7 @@ +(int) lvClassDefine:(lua_State *)L globalName:(NSString*) globalName{
360346
361347 // lua SVGAView构造方法创建的对象对应的方法列表
362348 const struct luaL_Reg memberFunctions [] = {
349+
363350 {" loops" , loops},
364351 {" readyToPlay" , readyToPlay},
365352 {" fps" , fps},
@@ -378,31 +365,22 @@ +(int) lvClassDefine:(lua_State *)L globalName:(NSString*) globalName{
378365 };
379366
380367 // 创建SVGAView类的方法列表
381- lv_createClassMetaTable (L, META_TABLE_SVGAView );
368+ lv_createClassMetaTable (L, META_TABLE_UIView );
382369
383370 luaL_openlib (L, NULL , [LVBaseView baseMemberFunctions ], 0 ); // 继承基类View的所有方法列表
384371 luaL_openlib (L, NULL , memberFunctions, 0 ); // 当前类SVGAView特有的方法列表
385-
386- const char * keys[] = { " addView" , NULL };// 列出需要移除的多余API
387- lv_luaTableRemoveKeys (L, keys );// 移除冗余API 兼容安卓
388-
389372 return 1 ;
390373}
391374
392375- (void )svgaPlayerDidFinishedAnimation : (id <VPUPSVGAPlayerProtocol>)player {
393376 dispatch_async (dispatch_get_main_queue (), ^{
394- [self lv_callLuaCallback: @" OnFinish " key2: nil argN: 1 ];
377+ [self callback: kVPLuaSVGAViewCallbackOnFinished ];
395378 });
396379}
397380
398381- (void )svgaPlayerDidAnimatedToFrame : (NSInteger )frame {
399382 dispatch_async (dispatch_get_main_queue (), ^{
400- lua_State* l = self.lv_luaviewCore .l ;
401- if ( l ){
402- lua_checkstack32 (l);
403- lua_pushnumber (l, frame);
404- [self lv_callLuaCallback: @" onStep" key2: nil argN: 1 ];
405- }
383+ [self callback: kVPLuaSVGAViewCallbackOnStep progress: frame];
406384 });
407385}
408386
0 commit comments