build out now LuaEvent interface and fix various bugs to get things working

master
Robert Norris 2012-08-30 10:33:54 +10:00
parent 1ea2f7f4db
commit 367985d8cf
16 changed files with 178 additions and 237 deletions

View File

@ -17,7 +17,7 @@ end
Event = {
Queue = function (name, ...)
pending.insert({ name = name, event = {...} })
table.insert(pending, { name = name, event = {...} })
end,
Connect = function (name, cb)
@ -46,7 +46,7 @@ Event = {
local p = table.remove(pending, 1)
if callbacks[p.name] then
for cb,_ in pairs(callbacks[p.name]) do
do_callback(cb, p)
do_callback[p.name](cb, p)
end
end
end
@ -115,7 +115,7 @@ EventQueue = {
-- deprecated
--
Connect = function (_, cb)
Event.Connect(name, cb)
Event.Connect(on, cb)
end,
--
@ -142,7 +142,7 @@ EventQueue = {
-- deprecated
--
Disconnect = function (_, cb)
Event.Disconnect(name, cb)
Event.Disconnect(on, cb)
end,
}
end

View File

@ -13,6 +13,7 @@
#include "Pi.h"
#include "Space.h"
#include "Game.h"
#include "LuaEvent.h"
Body::Body()
{

View File

@ -15,6 +15,7 @@
#include "SystemInfoView.h"
#include "SpaceStationView.h"
#include "InfoView.h"
#include "LuaEvent.h"
#include "ObjectViewerView.h"
#include "graphics/Renderer.h"

65
src/LuaEvent.cpp Normal file
View File

@ -0,0 +1,65 @@
#include "libs.h"
#include "LuaEvent.h"
#include "LuaManager.h"
#include "LuaObject.h"
#include "LuaUtils.h"
#include "Pi.h"
namespace LuaEvent {
static void _get_method_onto_stack(lua_State *l, const char *method) {
LUA_DEBUG_START(l);
int top = lua_gettop(l);
lua_rawgeti(l, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
lua_pushstring(l, "Event");
lua_rawget(l, -2);
assert(lua_istable(l, -1));
lua_pushstring(l, method);
lua_rawget(l, -2);
assert(lua_isfunction(l, -1));
lua_insert(l, top+1);
lua_settop(l, top+1);
LUA_DEBUG_END(l, 1);
}
void Clear()
{
lua_State *l = Pi::luaManager->GetLuaState();
LUA_DEBUG_START(l);
_get_method_onto_stack(l, "_Clear");
pi_lua_protected_call(l, 0, 0);
LUA_DEBUG_END(l, 0);
}
void Emit()
{
lua_State *l = Pi::luaManager->GetLuaState();
LUA_DEBUG_START(l);
_get_method_onto_stack(l, "_Emit");
pi_lua_protected_call(l, 0, 0);
LUA_DEBUG_END(l, 0);
}
void Queue(const char *event, const ArgsBase &args)
{
lua_State *l = Pi::luaManager->GetLuaState();
LUA_DEBUG_START(l);
_get_method_onto_stack(l, "Queue");
int top = lua_gettop(l);
lua_pushstring(l, event);
args.PrepareStack();
pi_lua_protected_call(l, lua_gettop(l) - top, 0);
LUA_DEBUG_END(l, 0);
}
}

95
src/LuaEvent.h Normal file
View File

@ -0,0 +1,95 @@
#ifndef _LUAEVENT_H
#define _LUAEVENT_H
#include "LuaManager.h"
#include "LuaObject.h"
#include "DeleteEmitter.h"
#include "Pi.h"
namespace LuaEvent {
class ArgsBase {
public:
virtual ~ArgsBase() {}
virtual void PrepareStack() const = 0;
};
template <typename T0=void, typename T1=void>
class Args : public ArgsBase {
public:
Args(T0 *_arg0, T1 *_arg1) : arg0(_arg0), arg1(_arg1) { }
virtual ~Args() {}
T0 *arg0;
T1 *arg1;
inline void PrepareStack() const {
LuaObject<T0>::PushToLua(arg0);
LuaObject<T1>::PushToLua(arg1);
}
};
template <typename T0>
class Args<T0,void> : public ArgsBase {
public:
Args(T0 *_arg0) : arg0(_arg0) { }
virtual ~Args() {}
T0 *arg0;
inline void PrepareStack() const {
LuaObject<T0>::PushToLua(arg0);
}
};
template <typename T0>
class Args<T0,const char*> : public ArgsBase {
public:
Args(T0 *_arg0, const char *_arg1) : arg0(_arg0), arg1(_arg1) {}
virtual ~Args() {}
T0 *arg0;
const char *arg1;
inline void PrepareStack() const {
LuaObject<T0>::PushToLua(arg0);
lua_pushstring(Pi::luaManager->GetLuaState(), arg1);
}
};
template <>
class Args<void,void> : public ArgsBase {
public:
Args() {}
virtual ~Args() {}
inline void PrepareStack() const {}
};
void Clear();
void Emit();
void Queue(const char *event, const ArgsBase &args);
template <typename T0, typename T1>
void Queue(const char *event, T0 *arg0, T1 *arg1) {
Queue(event, Args<T0, T1>(arg0, arg1));
}
template <typename T0>
void Queue(const char *event, T0 *arg0, const char *arg1) {
Queue(event, Args<T0, const char *>(arg0, arg1));
}
template <typename T0>
void Queue(const char *event, T0 *arg0) {
Queue(event, Args<T0>(arg0));
}
inline void Queue(const char *event) {
Queue(event, Args<>());
}
}
#endif

View File

@ -1,82 +0,0 @@
#include "libs.h"
#include "LuaEventQueue.h"
#include "LuaManager.h"
#include "LuaObject.h"
#include "LuaUtils.h"
#include "Pi.h"
#include "LuaConsole.h"
#include "StringF.h"
static void _get_method_onto_stack(lua_State *l, const char *queue, const char *method) {
LUA_DEBUG_START(l);
int top = lua_gettop(l);
lua_rawgeti(l, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
lua_pushstring(l, "EventQueue");
lua_rawget(l, -2);
assert(lua_istable(l, -1));
lua_pushstring(l, queue);
lua_rawget(l, -2);
assert(lua_istable(l, -1));
lua_pushstring(l, method);
lua_rawget(l, -2);
assert(lua_isfunction(l, -1));
lua_insert(l, top+1);
lua_settop(l, top+1);
LUA_DEBUG_END(l, 1);
}
void LuaEventQueueBase::Clear()
{
lua_State *l = Pi::luaManager->GetLuaState();
LUA_DEBUG_START(l);
_get_method_onto_stack(l, m_name, "Clear");
pi_lua_protected_call(l, 0, 0);
LUA_DEBUG_END(l, 0);
}
void LuaEventQueueBase::Emit()
{
lua_State *l = Pi::luaManager->GetLuaState();
LUA_DEBUG_START(l);
_get_method_onto_stack(l, m_name, "Emit");
pi_lua_protected_call(l, 0, 0);
LUA_DEBUG_END(l, 0);
}
void LuaEventQueueBase::QueueSingleEvent(const LuaEventBase &eb)
{
lua_State *l = Pi::luaManager->GetLuaState();
LUA_DEBUG_START(l);
_get_method_onto_stack(l, m_name, "Queue");
int top = lua_gettop(l);
lua_pushnil(l);
PrepareLuaStack(l, eb);
pi_lua_protected_call(l, lua_gettop(l) - top, 0);
LUA_DEBUG_END(l, 0);
}
void LuaEventQueueBase::EmitSingleEvent(const LuaEventBase &eb)
{
lua_State *l = Pi::luaManager->GetLuaState();
LUA_DEBUG_START(l);
_get_method_onto_stack(l, m_name, "Signal");
int top = lua_gettop(l);
lua_pushnil(l);
PrepareLuaStack(l, eb);
pi_lua_protected_call(l, lua_gettop(l) - top, 0);
LUA_DEBUG_END(l, 0);
}

View File

@ -1,146 +0,0 @@
#ifndef _LUAEVENTQUEUE_H
#define _LUAEVENTQUEUE_H
#include "LuaManager.h"
#include "LuaObject.h"
#include "DeleteEmitter.h"
class LuaEventBase {
public:
virtual ~LuaEventBase() {}
};
template <typename T0, typename T1>
class LuaEvent : public LuaEventBase {
public:
LuaEvent(T0 *arg0, T1 *arg1) : m_arg0(arg0), m_arg1(arg1) { }
virtual ~LuaEvent() {}
T0 *m_arg0;
T1 *m_arg1;
};
template <typename T0>
class LuaEvent<T0,void> : public LuaEventBase {
public:
LuaEvent(T0 *arg0) : m_arg0(arg0) { }
virtual ~LuaEvent() {}
T0 *m_arg0;
};
template <typename T0>
class LuaEvent<T0,const char*> : public LuaEventBase {
public:
LuaEvent(T0 *arg0, const char *arg1) : m_arg0(arg0), m_arg1(arg1) {}
virtual ~LuaEvent() {}
T0 *m_arg0;
const char *m_arg1;
};
template <>
class LuaEvent<void,void> : public LuaEventBase {
public:
LuaEvent() { }
virtual ~LuaEvent() {}
};
class LuaEventQueueBase {
public:
void Clear();
void Emit();
protected:
LuaEventQueueBase(const char *name) :
m_name(name)
{}
virtual ~LuaEventQueueBase() {}
void QueueSingleEvent(const LuaEventBase &eb);
void EmitSingleEvent(const LuaEventBase &eb);
private:
virtual void PrepareLuaStack(lua_State *l, const LuaEventBase &eb) = 0;
const char *m_name;
};
template <typename T0=void, typename T1=void>
class LuaEventQueue : public LuaEventQueueBase {
public:
LuaEventQueue(const char *name) : LuaEventQueueBase(name) { }
inline void Queue(T0 *arg0, T1 *arg1) {
QueueSingleEvent(LuaEvent<T0,T1>(arg0, arg1));
}
inline void Signal(T0 *arg0, T1 *arg1) {
EmitSingleEvent(LuaEvent<T0,T1>(arg0, arg1));
}
protected:
inline void PrepareLuaStack(lua_State *l, const LuaEventBase &eb) {
const LuaEvent<T0,T1> &e = static_cast<const LuaEvent<T0,T1>&>(eb);
LuaObject<T0>::PushToLua(e.m_arg0);
LuaObject<T1>::PushToLua(e.m_arg1);
}
};
template <typename T0>
class LuaEventQueue<T0,void> : public LuaEventQueueBase {
public:
LuaEventQueue(const char *name) : LuaEventQueueBase(name) { }
inline void Queue(T0 *arg0) {
QueueSingleEvent(LuaEvent<T0,void>(arg0));
}
inline void Signal(T0 *arg0) {
EmitSingleEvent(LuaEvent<T0,void>(arg0));
}
protected:
inline void PrepareLuaStack(lua_State *l, const LuaEventBase &eb) {
const LuaEvent<T0,void> &e = static_cast<const LuaEvent<T0,void>&>(eb);
LuaObject<T0>::PushToLua(e.m_arg0);
}
};
template <typename T0>
class LuaEventQueue<T0,const char *> : public LuaEventQueueBase {
public:
LuaEventQueue(const char *name) : LuaEventQueueBase(name) { }
inline void Queue(T0 *arg0, const char *arg1) {
QueueSingleEvent(LuaEvent<T0,const char *>(arg0, arg1));
}
inline void Signal(T0 *arg0, const char *arg1) {
EmitSingleEvent(LuaEvent<T0,const char *>(arg0, arg1));
}
protected:
inline void PrepareLuaStack(lua_State *l, const LuaEventBase &eb) {
const LuaEvent<T0,const char *> &e = static_cast<const LuaEvent<T0,const char *>&>(eb);
LuaObject<T0>::PushToLua(e.m_arg0);
lua_pushstring(l, e.m_arg1);
}
};
template <>
class LuaEventQueue<void,void> : public LuaEventQueueBase {
public:
LuaEventQueue(const char *name) : LuaEventQueueBase(name) { }
inline void Queue() {
QueueSingleEvent(LuaEvent<void,void>());
}
inline void Signal() {
EmitSingleEvent(LuaEvent<void,void>());
}
protected:
inline void PrepareLuaStack(lua_State *l, const LuaEventBase &eb) { }
};
#endif

View File

@ -63,7 +63,7 @@ noinst_HEADERS = \
LuaConstants.h \
LuaEngine.h \
LuaEquipType.h \
LuaEventQueue.h \
LuaEvent.h \
LuaFormat.h \
LuaGame.h \
LuaLang.h \
@ -197,7 +197,7 @@ pioneer_SOURCES = \
LuaConstants.cpp \
LuaEngine.cpp \
LuaEquipType.cpp \
LuaEventQueue.cpp \
LuaEvent.cpp \
LuaFormat.cpp \
LuaGame.cpp \
LuaLang.cpp \

View File

@ -6,6 +6,7 @@
#include "Lang.h"
#include "Pi.h"
#include "Game.h"
#include "LuaEvent.h"
Missile::Missile(ShipType::Type type, Body *owner, Body *target): Ship(type)
{

View File

@ -41,6 +41,7 @@
#include "LuaSystemBody.h"
#include "LuaSystemPath.h"
#include "LuaTimer.h"
#include "LuaEvent.h"
#include "Missile.h"
#include "ModManager.h"
#include "ObjectViewerView.h"

View File

@ -12,6 +12,7 @@
#include "Ship.h"
#include "Pi.h"
#include "Game.h"
#include "LuaEvent.h"
#include "graphics/Graphics.h"
#include "graphics/Material.h"
#include "graphics/Renderer.h"

View File

@ -9,10 +9,10 @@
#include "SpaceStation.h"
#include "Space.h"
#include "LuaConstants.h"
#include "LuaEvent.h"
#include "KeyBindings.h"
void Ship::AIModelCoordsMatchAngVel(vector3d desiredAngVel, double softness)
{
const ShipType &stype = GetShipType();

View File

@ -3,6 +3,7 @@
#include "Planet.h"
#include "Lang.h"
#include "LuaConstants.h"
#include "LuaEvent.h"
#include "Missile.h"
#include "Projectile.h"
#include "ShipAICmd.h"
@ -865,7 +866,7 @@ void Ship::UpdateAlertState()
// clear existing alert state if there was one
if (GetAlertState() != ALERT_NONE) {
SetAlertState(ALERT_NONE);
LuaEvent("onShipAlertChanged", this, LuaConstants::GetConstantString(Pi::luaManager->GetLuaState(), "ShipAlertStatus", ALERT_NONE));
LuaEvent::Queue("onShipAlertChanged", this, LuaConstants::GetConstantString(Pi::luaManager->GetLuaState(), "ShipAlertStatus", ALERT_NONE));
}
return;
}
@ -936,7 +937,7 @@ void Ship::UpdateAlertState()
}
if (changed)
LuaEvent("onShipAlertChanged", this, LuaConstants::GetConstantString(Pi::luaManager->GetLuaState(), "ShipAlertStatus", GetAlertState()));
LuaEvent::Queue("onShipAlertChanged", this, LuaConstants::GetConstantString(Pi::luaManager->GetLuaState(), "ShipAlertStatus", GetAlertState()));
}
void Ship::UpdateFuel(const float timeStep)

View File

@ -1,6 +1,7 @@
#include "SoundMusic.h"
#include "libs.h" //for clamp
#include "Pi.h"
#include "LuaEvent.h"
#include <map>
namespace Sound {

View File

@ -20,6 +20,7 @@
#include "Lang.h"
#include "Game.h"
#include "MathUtil.h"
#include "LuaEvent.h"
Space::Space(Game *game)
: m_game(game)

View File

@ -11,6 +11,7 @@
#include "Polit.h"
#include "LmrModel.h"
#include "LuaVector.h"
#include "LuaEvent.h"
#include "Polit.h"
#include "Space.h"
#include "Lang.h"