straightforward signal and slots system for lua events
This commit is contained in:
parent
f41fd0576c
commit
f05c048f6d
|
@ -67,7 +67,7 @@ end
|
||||||
function Console.init()
|
function Console.init()
|
||||||
consoleWidget = UI.loadAndDisplay("/console/console.otui")
|
consoleWidget = UI.loadAndDisplay("/console/console.otui")
|
||||||
consoleWidget:hide()
|
consoleWidget:hide()
|
||||||
consoleWidget.onKeyPress = onKeyPress
|
connect(consoleWidget, { onKeyPress = onKeyPress })
|
||||||
|
|
||||||
commandLineEdit = consoleWidget:getChildById('commandLineEdit')
|
commandLineEdit = consoleWidget:getChildById('commandLineEdit')
|
||||||
consoleBuffer = consoleWidget:getChildById('consoleBuffer')
|
consoleBuffer = consoleWidget:getChildById('consoleBuffer')
|
||||||
|
|
|
@ -11,3 +11,16 @@ function createEnvironment()
|
||||||
setmetatable(env, { __index = _G} )
|
setmetatable(env, { __index = _G} )
|
||||||
return env
|
return env
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function connect(object, signalsAndSlots)
|
||||||
|
for signal,slot in pairs(signalsAndSlots) do
|
||||||
|
if not object[signal] then
|
||||||
|
object[signal] = slot
|
||||||
|
elseif type(object[signal]) == 'function' then
|
||||||
|
object[signal] = { object[signal], slot }
|
||||||
|
elseif type(signal) == 'table' then
|
||||||
|
table.insert(object[signal], slot)
|
||||||
|
else
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -7,6 +7,9 @@ local function onGameKeyPress(self, keyCode, keyChar, keyboardModifiers)
|
||||||
if keyCode == KeyG then
|
if keyCode == KeyG then
|
||||||
CharacterList.show()
|
CharacterList.show()
|
||||||
return true
|
return true
|
||||||
|
elseif keyCode == KeyQ then
|
||||||
|
Game.logout()
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -419,21 +419,32 @@ int LuaInterface::protectedCall(int numArgs, int requestedResults)
|
||||||
else if(isTable(funcIndex)) {
|
else if(isTable(funcIndex)) {
|
||||||
// loop through table values
|
// loop through table values
|
||||||
pushNil();
|
pushNil();
|
||||||
|
bool done = false;
|
||||||
while(next(funcIndex-1)) {
|
while(next(funcIndex-1)) {
|
||||||
if(isFunction()) {
|
if(isFunction()) {
|
||||||
// repush arguments
|
// repush arguments
|
||||||
for(int i=0;i<numArgs;++i)
|
for(int i=0;i<numArgs;++i)
|
||||||
pushValue(-(numArgs-i)-2);
|
pushValue(-numArgs-2);
|
||||||
|
|
||||||
int rets = safeCall(numArgs);
|
int rets = safeCall(numArgs);
|
||||||
pop(rets);
|
if(rets == 1) {
|
||||||
}
|
done = popBoolean();
|
||||||
// just ignore
|
if(done) {
|
||||||
else {
|
|
||||||
pop();
|
pop();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if(rets != 0)
|
||||||
|
throw LuaException("function call didn't return the expected number of results", 0);
|
||||||
|
} else {
|
||||||
|
throw LuaException("attempt to call a non function", 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pop(numArgs + 1); // pops the table of function and arguments
|
pop(numArgs + 1); // pops the table of function and arguments
|
||||||
|
|
||||||
|
if(requestedResults == -1 || requestedResults == 1) {
|
||||||
|
numRets = 1;
|
||||||
|
pushBoolean(done);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// nil values are ignored
|
// nil values are ignored
|
||||||
else if(isNil(funcIndex)) {
|
else if(isNil(funcIndex)) {
|
||||||
|
|
|
@ -62,15 +62,17 @@ void Game::processConnectionError(const boost::system::error_code& error)
|
||||||
{
|
{
|
||||||
// connection errors only have meaning if we still have a protocol
|
// connection errors only have meaning if we still have a protocol
|
||||||
if(m_protocolGame) {
|
if(m_protocolGame) {
|
||||||
|
if(error != asio::error::eof)
|
||||||
g_lua.callGlobalField("Game", "onConnectionError", error.message());
|
g_lua.callGlobalField("Game", "onConnectionError", error.message());
|
||||||
|
|
||||||
if(m_online)
|
if(m_online) {
|
||||||
processLogout();
|
processLogout();
|
||||||
|
} else {
|
||||||
// disconnect isn't needed, we are already disconnected
|
m_protocolGame->disconnect();
|
||||||
m_protocolGame.reset();
|
m_protocolGame.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Game::processLogin(const LocalPlayerPtr& localPlayer)
|
void Game::processLogin(const LocalPlayerPtr& localPlayer)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue