From 72e45f0062d727cedd576d1e1251f6722454a119 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 12 Nov 2018 08:04:11 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lua/modules/time/je_lua_timer.cpp | 140 +++++++++++++++++++++++++++++++--- 1 file changed, 129 insertions(+), 11 deletions(-) (limited to 'src/lua/modules/time/je_lua_timer.cpp') diff --git a/src/lua/modules/time/je_lua_timer.cpp b/src/lua/modules/time/je_lua_timer.cpp index e57c666..c79c16b 100644 --- a/src/lua/modules/time/je_lua_timer.cpp +++ b/src/lua/modules/time/je_lua_timer.cpp @@ -2,28 +2,146 @@ #include "lua/common/je_lua_common.h" #include "je_lua_timer.h" +using namespace JinEngine::Time; + namespace JinEngine { namespace Lua { -/* - typedef Shared& TimerRef; - LUA_IMPLEMENT inline TimerRef checkTimer(lua_State* L) + typedef Shared& SharedTimer; + + class Callback + { + public: + Callback() + { + } + ~Callback() + { + delete func; + delete param; + } + + Reference* func; + Reference* param; + }; + +#define TIMER_CALLBACK \ +[=](void* data)->void { \ + Callback* cbk = static_cast(data); \ + cbk->func->push(); \ + cbk->param->push(); \ + luax_call(L, 1, 0); \ +} + +#define FINISH_CALLBACK \ +[=](void* data) { \ + Callback* cbk = static_cast(data); \ + delete cbk; \ +} + + LUA_IMPLEMENT inline SharedTimer checkTimer(lua_State* L) { Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_TIME_TIMER); return proxy->getShared(); } -*/ + + // timer:every(time, callback, parameter) LUA_IMPLEMENT int l_every(lua_State* L) { - //TimerRef shared = checkTimer(L); - //Timer* timer = shared.getObject(); - //int n = luax_checkinteger(L, 1); - //int shared = luax_ref(L, 2); - //timer->every(n, [](void* data)->void { - // - //}, ); + SharedTimer shared = checkTimer(L); + Timer* timer = shared.getObject(); + float s = luax_checknumber(L, 2); + Callback* callback = new Callback(); + callback->func = new Reference(L, 3); + callback->param = new Reference(L, 4); + Timer::Handler* handler = timer->every(s, TIMER_CALLBACK, callback, FINISH_CALLBACK); + Proxy* proxy = luax_newinstance(L, JIN_TIME_HANDLER); + proxy->bind(new Shared(handler, JIN_TIME_HANDLER)); + return 1; + } + + // timer:after(time, callback, parameter) + LUA_IMPLEMENT int l_after(lua_State* L) + { + SharedTimer shared = checkTimer(L); + Timer* timer = shared.getObject(); + float s = luax_checknumber(L, 2); + Callback* callback = new Callback(); + callback->func = new Reference(L, 3); + callback->param = new Reference(L, 4); + Timer::Handler* handler = timer->after(s, TIMER_CALLBACK, callback, FINISH_CALLBACK); + Proxy* proxy = luax_newinstance(L, JIN_TIME_HANDLER); + proxy->bind(new Shared(handler, JIN_TIME_HANDLER)); + return 1; + } + + // timer:repeat(time, callback, parameter) + LUA_IMPLEMENT int l_repeat(lua_State* L) + { + SharedTimer shared = checkTimer(L); + Timer* timer = shared.getObject(); + float s = luax_checknumber(L, 2); + int count = luax_checkinteger(L, 3); + Callback* callback = new Callback(); + callback->func = new Reference(L, 4); + callback->param = new Reference(L, 5); + Timer::Handler* handler = timer->repeat(s, count, TIMER_CALLBACK, callback, FINISH_CALLBACK); + Proxy* proxy = luax_newinstance(L, JIN_TIME_HANDLER); + proxy->bind(new Shared(handler, JIN_TIME_HANDLER)); + return 1; + } + + LUA_IMPLEMENT int l_update(lua_State* L) + { + SharedTimer shared = checkTimer(L); + Timer* timer = shared.getObject(); + float s = luax_checknumber(L, 2); + timer->update(s); + return 0; + } + + LUA_IMPLEMENT int l_cancel(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_TIME_TIMER); + Timer* timer = p->getObject(); + Proxy* ph = (Proxy*)luax_checktype(L, 2, JIN_TIME_HANDLER); + Timer::Handler* handler = ph->getObject(); + timer->cancel(handler); + return 0; + } + + LUA_IMPLEMENT int l_cancelAll(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_TIME_TIMER); + Timer* timer = p->getObject(); + timer->cancelAll(); + return 0; + } + + LUA_IMPLEMENT int l_gc(lua_State* L) + { + Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_TIME_TIMER); + p->release(); + return 0; + } + + LUA_IMPLEMENT const luaL_Reg f[] = { + { "__gc", l_gc }, + { "every", l_every }, + { "after", l_after }, + { "duplicate", l_repeat }, + { "update", l_update }, + { "cancel", l_cancel }, + { "cancelAll", l_cancelAll }, + { 0, 0 } + }; + + LUA_EXPORT int luaopen_Timer(lua_State* L) + { + luax_newtype(L, JIN_TIME_TIMER, f); + return 0; } } -- cgit v1.1-26-g67d0