mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-08-21 15:41:35 +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 *lsi = static_cast<LuaScriptInterface *>(this);
|
||||||
auto *L = lsi->L;
|
auto *L = lsi->L;
|
||||||
assert(!lsi->currentEventHandlerIt);
|
|
||||||
auto eventType = int(event.index());
|
auto eventType = int(event.index());
|
||||||
auto &list = lsi->gameControllerEventHandlers[eventType];
|
auto &list = lsi->gameControllerEventHandlers[eventType];
|
||||||
auto it = list.begin();
|
auto it = list.begin();
|
||||||
auto end = list.end();
|
auto end = list.end();
|
||||||
lsi->currentEventHandlerIt = ⁢
|
lsi->currentEventHandlerIts.push_back(&it);
|
||||||
bool cont = true;
|
bool cont = true;
|
||||||
while (it != end)
|
while (it != end)
|
||||||
{
|
{
|
||||||
@@ -441,7 +440,8 @@ bool CommandInterface::HandleEvent(const GameControllerEvent &event)
|
|||||||
}
|
}
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
lsi->currentEventHandlerIt = nullptr;
|
assert(lsi->currentEventHandlerIts.back() == &it);
|
||||||
|
lsi->currentEventHandlerIts.pop_back();
|
||||||
return cont;
|
return cont;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -463,9 +463,12 @@ void LuaScriptInterface::RemoveEventHandler(int eventType, int stackIndex)
|
|||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
if (equal)
|
if (equal)
|
||||||
{
|
{
|
||||||
if (currentEventHandlerIt && *currentEventHandlerIt == it)
|
for (auto currentEventHandlerIt : currentEventHandlerIts)
|
||||||
{
|
{
|
||||||
++*currentEventHandlerIt;
|
if (*currentEventHandlerIt == it)
|
||||||
|
{
|
||||||
|
++*currentEventHandlerIt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
list.erase(it);
|
list.erase(it);
|
||||||
break;
|
break;
|
||||||
|
@@ -9,6 +9,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <deque>
|
||||||
|
|
||||||
namespace http
|
namespace http
|
||||||
{
|
{
|
||||||
@@ -108,7 +109,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::list<LuaSmartRef>> gameControllerEventHandlers; // must come after luaState
|
std::vector<std::list<LuaSmartRef>> gameControllerEventHandlers; // must come after luaState
|
||||||
std::list<LuaSmartRef>::iterator *currentEventHandlerIt = nullptr;
|
std::deque<std::list<LuaSmartRef>::iterator *> currentEventHandlerIts;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void AddEventHandler(int eventType, int stackIndex);
|
void AddEventHandler(int eventType, int stackIndex);
|
||||||
|
Reference in New Issue
Block a user