mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-08-21 07:31:26 +02:00
Formalize event handlers as stackable
It seems event handlers may be invoked from event handlers, which 87b81ceb45
did not anticipate, oops.
This commit is contained in:
@@ -405,12 +405,11 @@ bool CommandInterface::HandleEvent(const GameControllerEvent &event)
|
||||
{
|
||||
auto *lsi = static_cast<LuaScriptInterface *>(this);
|
||||
auto *L = lsi->L;
|
||||
assert(!lsi->currentEventHandlerIt);
|
||||
auto eventType = int(event.index());
|
||||
auto &list = lsi->gameControllerEventHandlers[eventType];
|
||||
auto it = list.begin();
|
||||
auto end = list.end();
|
||||
lsi->currentEventHandlerIt = ⁢
|
||||
lsi->currentEventHandlerIts.push_back(&it);
|
||||
bool cont = true;
|
||||
while (it != end)
|
||||
{
|
||||
@@ -441,7 +440,8 @@ bool CommandInterface::HandleEvent(const GameControllerEvent &event)
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
lsi->currentEventHandlerIt = nullptr;
|
||||
assert(lsi->currentEventHandlerIts.back() == &it);
|
||||
lsi->currentEventHandlerIts.pop_back();
|
||||
return cont;
|
||||
}
|
||||
|
||||
@@ -463,9 +463,12 @@ void LuaScriptInterface::RemoveEventHandler(int eventType, int stackIndex)
|
||||
lua_pop(L, 1);
|
||||
if (equal)
|
||||
{
|
||||
if (currentEventHandlerIt && *currentEventHandlerIt == it)
|
||||
for (auto currentEventHandlerIt : currentEventHandlerIts)
|
||||
{
|
||||
++*currentEventHandlerIt;
|
||||
if (*currentEventHandlerIt == it)
|
||||
{
|
||||
++*currentEventHandlerIt;
|
||||
}
|
||||
}
|
||||
list.erase(it);
|
||||
break;
|
||||
|
@@ -9,6 +9,7 @@
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <list>
|
||||
#include <deque>
|
||||
|
||||
namespace http
|
||||
{
|
||||
@@ -108,7 +109,7 @@ public:
|
||||
|
||||
private:
|
||||
std::vector<std::list<LuaSmartRef>> gameControllerEventHandlers; // must come after luaState
|
||||
std::list<LuaSmartRef>::iterator *currentEventHandlerIt = nullptr;
|
||||
std::deque<std::list<LuaSmartRef>::iterator *> currentEventHandlerIts;
|
||||
|
||||
public:
|
||||
void AddEventHandler(int eventType, int stackIndex);
|
||||
|
Reference in New Issue
Block a user