[lua] Add stopPropagation function to the beforecommand event

Related to:
https://github.com/aseprite/Attachment-System/issues/121
https://github.com/aseprite/Attachment-System/issues/127
This commit is contained in:
David Capello 2023-05-18 11:49:04 -03:00
parent 961dee4354
commit d329f38075
2 changed files with 15 additions and 1 deletions

View File

@ -145,6 +145,9 @@ private:
std::vector<EventListeners> m_listeners;
};
// Used in BeforeCommand
static bool s_stopPropagationFlag = false;
class AppEvents : public Events
, private ContextObserver {
public:
@ -232,8 +235,17 @@ private:
}
void onBeforeCommand(CommandExecutionEvent& ev) {
s_stopPropagationFlag = false;
call(BeforeCommand, { { "name", ev.command()->id() },
{ "params", ev.params() } });
{ "params", ev.params() },
{ "stopPropagation",
(lua_CFunction)
[](lua_State*) -> int {
s_stopPropagationFlag = true;
return 0;
} } });
if (s_stopPropagationFlag)
ev.cancel();
}
void onAfterCommand(CommandExecutionEvent& ev) {

View File

@ -168,6 +168,8 @@ void push_value_to_lua(lua_State* L, const std::any& value) {
push_value_to_lua(L, *v);
else if (auto v = std::any_cast<std::string>(&value))
push_value_to_lua(L, *v);
else if (auto v = std::any_cast<lua_CFunction>(&value))
lua_pushcfunction(L, *v);
else if (auto v = std::any_cast<const doc::Remap*>(&value))
push_value_to_lua(L, **v);
else if (auto v = std::any_cast<const doc::Tileset*>(&value))