From c8b57e8068f6bcf393f0afc6fbc02b7de5c87fc1 Mon Sep 17 00:00:00 2001 From: black-sliver <59490463+black-sliver@users.noreply.github.com> Date: Thu, 1 Feb 2024 00:36:08 +0100 Subject: [PATCH] Lua: add OnMiddleClickFunc --- doc/PACKS.md | 3 ++- src/core/luaitem.cpp | 26 +++++++++++++++++++++++--- src/core/luaitem.h | 1 + 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/doc/PACKS.md b/doc/PACKS.md index d2851ec0..e7b45366 100644 --- a/doc/PACKS.md +++ b/doc/PACKS.md @@ -152,7 +152,8 @@ a table representing an enum with the following constants: \ * `object .ItemState`: (any) object to track internal state in lua * `closure(LuaItem) .OnLeftClickFunc`: called when left-clicking * `closure(LuaItem) .OnRightClickFunc`: called when right-clicking -* **TODO**: Middle, Forward, Backward or a generalized onClick(button) +* `closure(LuaItem) .OnMiddleClickFunc`: called when middle-clicking, since 0.25.8 +* **TODO**: Forward, Backward or a generalized onClick(button) * `closure(LuaItem,string code) .CanProvideCodeFunc`: called to determine if item has code * `closure(LuaItem,string code) .ProvidesCodeFunc`: called to track progress, closure should returns 1 if code is provided (can provide && active) * `closure(LuaItem,string code) .AdvanceToCodeFunc`: called to change item's stage to provide code (not in use yet) diff --git a/src/core/luaitem.cpp b/src/core/luaitem.cpp index aa64bd64..94328269 100644 --- a/src/core/luaitem.cpp +++ b/src/core/luaitem.cpp @@ -43,6 +43,9 @@ int LuaItem::Lua_Index(lua_State *L, const char* key) { } else if (strcmp(key,"OnRightClickFunc")==0) { lua_rawgeti(L, LUA_REGISTRYINDEX, _onRightClickFunc.ref); return 1; + } else if (strcmp(key,"OnMiddleClickFunc") == 0) { + lua_rawgeti(L, LUA_REGISTRYINDEX, _onMiddleClickFunc.ref); + return 1; } else if (strcmp(key,"CanProvideCodeFunc")==0) { lua_rawgeti(L, LUA_REGISTRYINDEX, _canProvideCodeFunc.ref); return 1; @@ -131,6 +134,9 @@ bool LuaItem::Lua_NewIndex(lua_State *L, const char *key) { } else if (strcmp(key,"OnRightClickFunc")==0) { _onRightClickFunc.ref = luaL_ref(L, LUA_REGISTRYINDEX); // pop copy and store return true; + } else if (strcmp(key,"OnMiddleClickFunc") == 0) { + _onMiddleClickFunc.ref = luaL_ref(L, LUA_REGISTRYINDEX); // pop copy and store + return true; } else if (strcmp(key,"CanProvideCodeFunc")==0) { _canProvideCodeFunc.ref = luaL_ref(L, LUA_REGISTRYINDEX); // pop copy and store return true; @@ -213,10 +219,11 @@ int LuaItem::providesCode(const std::string code) const bool LuaItem::changeState(Action action) { - // for now we'll just have Left=Next send left-click, Right=Prev send right-click + // for now we'll just have Left=Next send left-click, Right=Prev send right-click and Toggle send middle-click if (action == BaseItem::Action::Secondary || action == BaseItem::Action::Prev) { - if (!_onRightClickFunc.valid()) return false; + if (!_onRightClickFunc.valid()) + return false; lua_rawgeti(_L, LUA_REGISTRYINDEX, _onRightClickFunc.ref); Lua_Push(_L); // arg1: this if (lua_pcall(_L, 1, 0, 0)) { @@ -225,9 +232,22 @@ bool LuaItem::changeState(Action action) return false; } } + else if (action == BaseItem::Action::Toggle) + { + if (!_onMiddleClickFunc.valid()) + return false; + lua_rawgeti(_L, LUA_REGISTRYINDEX, _onMiddleClickFunc.ref); + Lua_Push(_L); // arg1: this + if (lua_pcall(_L, 1, 0, 0)) { + printf("Error calling Item:onMiddleClick: %s\n", lua_tostring(_L, -1)); + lua_pop(_L, 1); + return false; + } + } else { - if (!_onLeftClickFunc.valid()) return false; + if (!_onLeftClickFunc.valid()) + return false; lua_rawgeti(_L, LUA_REGISTRYINDEX, _onLeftClickFunc.ref); Lua_Push(_L); // arg1: this if (lua_pcall(_L, 1, 0, 0)) { diff --git a/src/core/luaitem.h b/src/core/luaitem.h index 6b7b26d8..9ef3f04e 100644 --- a/src/core/luaitem.h +++ b/src/core/luaitem.h @@ -54,6 +54,7 @@ class LuaItem final : public LuaInterface, public BaseItem { LuaRef _itemState; LuaRef _onLeftClickFunc; LuaRef _onRightClickFunc; + LuaRef _onMiddleClickFunc; LuaRef _canProvideCodeFunc; LuaRef _providesCodeFunc; LuaRef _advanceToCodeFunc;