aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/time/je_lua_timer.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-11-12 08:04:11 +0800
committerchai <chaifix@163.com>2018-11-12 08:04:11 +0800
commit72e45f0062d727cedd576d1e1251f6722454a119 (patch)
tree736594b79e71c66a668d99d96c3ce464618e50ca /src/lua/modules/time/je_lua_timer.cpp
parent7c2f33bdf37de7acf9b0728a115377081344db1c (diff)
*修改代码结构
Diffstat (limited to 'src/lua/modules/time/je_lua_timer.cpp')
-rw-r--r--src/lua/modules/time/je_lua_timer.cpp140
1 files changed, 129 insertions, 11 deletions
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<Timer>& TimerRef;
- LUA_IMPLEMENT inline TimerRef checkTimer(lua_State* L)
+ typedef Shared<Timer>& SharedTimer;
+
+ class Callback
+ {
+ public:
+ Callback()
+ {
+ }
+ ~Callback()
+ {
+ delete func;
+ delete param;
+ }
+
+ Reference* func;
+ Reference* param;
+ };
+
+#define TIMER_CALLBACK \
+[=](void* data)->void { \
+ Callback* cbk = static_cast<Callback*>(data); \
+ cbk->func->push(); \
+ cbk->param->push(); \
+ luax_call(L, 1, 0); \
+}
+
+#define FINISH_CALLBACK \
+[=](void* data) { \
+ Callback* cbk = static_cast<Callback*>(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>();
}
-*/
+
+ // 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<Timer::Handler>(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<Timer::Handler>(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<Timer::Handler>(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<Timer>();
+ Proxy* ph = (Proxy*)luax_checktype(L, 2, JIN_TIME_HANDLER);
+ Timer::Handler* handler = ph->getObject<Timer::Handler>();
+ 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>();
+ 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;
}
}