Skip to content

Commit

Permalink
Lua: add OnMiddleClickFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Feb 28, 2024
1 parent 40b141d commit 36d6603
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion doc/PACKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ a table representing an enum with the following constants: \
* `object .ItemState`: (any) object to track internal state in Lua. Keys have to be strings for Get and Set below to work.
* `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)
Expand Down
26 changes: 23 additions & 3 deletions src/core/luaitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions src/core/luaitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class LuaItem final : public LuaInterface<LuaItem>, public BaseItem {
LuaRef _itemState;
LuaRef _onLeftClickFunc;
LuaRef _onRightClickFunc;
LuaRef _onMiddleClickFunc;
LuaRef _canProvideCodeFunc;
LuaRef _providesCodeFunc;
LuaRef _advanceToCodeFunc;
Expand Down

0 comments on commit 36d6603

Please sign in to comment.