mirror of
https://github.com/love2d/love.git
synced 2026-08-18 11:44:15 +02:00
Removed love.event.addListener/removeListener/clearListeners.
They take away too much control from the main game code, which makes it harder to implement things like gamestates without modifying library code.
This commit is contained in:
@@ -49,8 +49,6 @@ public:
|
||||
int toLua(lua_State *L);
|
||||
static Message *fromLua(lua_State *L, int n);
|
||||
|
||||
const std::string &getName() const { return name; }
|
||||
|
||||
private:
|
||||
|
||||
std::string name;
|
||||
|
||||
@@ -25,11 +25,6 @@
|
||||
|
||||
#include "sdl/Event.h"
|
||||
|
||||
// Put the Lua code directly into a raw string literal.
|
||||
static const char event_lua[] =
|
||||
#include "wrap_Event.lua"
|
||||
;
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace event
|
||||
@@ -37,50 +32,16 @@ namespace event
|
||||
|
||||
#define instance() (Module::getInstance<Event>(Module::M_EVENT))
|
||||
|
||||
static const char *listenersKey = "_listeners";
|
||||
|
||||
static int processMessage(lua_State *L, Message *m)
|
||||
{
|
||||
if (m == nullptr)
|
||||
return 0;
|
||||
|
||||
int args = m->toLua(L);
|
||||
|
||||
// Push the love.event[listenersKey][eventname] table onto the stack.
|
||||
lua_getfield(L, lua_upvalueindex(1), listenersKey);
|
||||
lua_getfield(L, -1, m->getName().c_str());
|
||||
|
||||
if (lua_istable(L, -1))
|
||||
{
|
||||
int len = (int) luax_objlen(L, -1);
|
||||
|
||||
// Each array entry in the table is a listener function we should call.
|
||||
for (int i = 1; i <= len; i++)
|
||||
{
|
||||
lua_rawgeti(L, -1, i);
|
||||
|
||||
// Push all message args (including the event name), in order.
|
||||
for (int j = 1; j <= args; j++)
|
||||
lua_pushvalue(L, -(args + 3));
|
||||
|
||||
lua_call(L, args, 0);
|
||||
}
|
||||
}
|
||||
|
||||
lua_pop(L, 2);
|
||||
|
||||
m->release();
|
||||
|
||||
// Leave the message's args on the stack.
|
||||
return args;
|
||||
}
|
||||
|
||||
static int w_poll_i(lua_State *L)
|
||||
{
|
||||
Message *m = nullptr;
|
||||
|
||||
if (instance()->poll(m))
|
||||
return processMessage(L, m);
|
||||
{
|
||||
int args = m->toLua(L);
|
||||
m->release();
|
||||
return args;
|
||||
}
|
||||
|
||||
// No pending events.
|
||||
return 0;
|
||||
@@ -88,9 +49,7 @@ static int w_poll_i(lua_State *L)
|
||||
|
||||
int w_poll(lua_State *L)
|
||||
{
|
||||
// w_poll_i needs access to the love.event table via an upvalue.
|
||||
lua_pushvalue(L, lua_upvalueindex(1));
|
||||
lua_pushcclosure(L, w_poll_i, 1);
|
||||
lua_pushcclosure(L, &w_poll_i, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -103,7 +62,14 @@ int w_pump(lua_State *)
|
||||
int w_wait(lua_State *L)
|
||||
{
|
||||
Message *m = instance()->wait();
|
||||
return processMessage(L, m);
|
||||
if (m)
|
||||
{
|
||||
int args = m->toLua(L);
|
||||
m->release();
|
||||
return args;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_push(lua_State *L)
|
||||
@@ -149,7 +115,6 @@ int w_quit(lua_State *L)
|
||||
// List of functions to wrap.
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
// addListener and removeListener are defined in wrap_Event.lua
|
||||
{ "pump", w_pump },
|
||||
{ "poll", w_poll },
|
||||
{ "wait", w_wait },
|
||||
@@ -178,16 +143,6 @@ extern "C" int luaopen_love_event(lua_State *L)
|
||||
|
||||
int ret = luax_register_module(L, w);
|
||||
|
||||
// love.event[listenersKey] = {}
|
||||
lua_newtable(L);
|
||||
lua_setfield(L, -2, listenersKey);
|
||||
|
||||
// Execute wrap_Event.lua, sending the event and listeners tables as args.
|
||||
luaL_loadbuffer(L, event_lua, sizeof(event_lua), "wrap_Event.lua");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_getfield(L, -1, listenersKey);
|
||||
lua_call(L, 2, 0);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
R"luastring"--(
|
||||
-- DO NOT REMOVE THE ABOVE LINE. It is used to load this file as a C++ string.
|
||||
-- There is a matching delimiter at the bottom of the file.
|
||||
|
||||
--[[
|
||||
Copyright (c) 2006-2015 LOVE Development Team
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
--]]
|
||||
|
||||
local event, listeners = ...
|
||||
|
||||
local type, select, error = type, select, error
|
||||
local pairs, ipairs = pairs, ipairs
|
||||
local table_insert, table_remove = table.insert, table.remove
|
||||
local max = math.max
|
||||
|
||||
local function addListenerForEvent(event, func)
|
||||
listeners[event] = listeners[event] or {}
|
||||
|
||||
-- Don't add a function to the event's array more than once.
|
||||
for i,v in ipairs(listeners[event]) do
|
||||
if v == func then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
table_insert(listeners[event], func)
|
||||
end
|
||||
|
||||
function event.addListener(func, ...)
|
||||
if type(func) ~= "function" then
|
||||
error("Invalid argument #1 to addListener (expected function)", 2)
|
||||
end
|
||||
|
||||
local t = (...)
|
||||
if type(t) == "table" then
|
||||
for i, name in ipairs(t) do
|
||||
if type(name) ~= "string" then
|
||||
error("Invalid table element #"..i.." in argument #2 to addListener (expected string)", 2)
|
||||
end
|
||||
|
||||
addListenerForEvent(name, func)
|
||||
end
|
||||
else
|
||||
local n = select("#", ...)
|
||||
for i = 1, max(n, 1) do
|
||||
local name = select(i, ...)
|
||||
|
||||
if type(name) ~= "string" then
|
||||
error("Invalid argument #"..(i+1).." to addListener (expected string)", 2)
|
||||
end
|
||||
|
||||
addListenerForEvent(name, func)
|
||||
end
|
||||
end
|
||||
|
||||
return func
|
||||
end
|
||||
|
||||
function event.removeListener(func)
|
||||
if type(func) ~= "function" then
|
||||
error("Invalid argument #1 to removeListener (expected function)", 2)
|
||||
end
|
||||
|
||||
local success = false
|
||||
|
||||
-- Remove the listener function from all event names it was subscribed to.
|
||||
for eventname, eventlisteners in pairs(listeners) do
|
||||
for i=#eventlisteners, 1, -1 do
|
||||
if eventlisteners[i] == func then
|
||||
table_remove(eventlisteners, i)
|
||||
success = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return success
|
||||
end
|
||||
|
||||
function event.clearListeners()
|
||||
for k in pairs(listeners) do
|
||||
listeners[k] = nil
|
||||
end
|
||||
end
|
||||
|
||||
-- DO NOT REMOVE THE NEXT LINE. It is used to load this file as a C++ string.
|
||||
--)luastring"--"
|
||||
Reference in New Issue
Block a user