This commit is contained in:
Christian Kaiser 2025-08-02 15:00:48 -05:00 committed by GitHub
commit 7a44256a59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 47 additions and 17 deletions

View File

@ -29,11 +29,16 @@ struct Plugin {
class PluginCommand : public Command { class PluginCommand : public Command {
public: public:
PluginCommand(const std::string& id, const std::string& title, int onclickRef, int onenabledRef) PluginCommand(const std::string& id,
const std::string& title,
int onclickRef,
int onenabledRef,
int oncheckedRef)
: Command(id.c_str(), CmdUIOnlyFlag) : Command(id.c_str(), CmdUIOnlyFlag)
, m_title(title) , m_title(title)
, m_onclickRef(onclickRef) , m_onclickRef(onclickRef)
, m_onenabledRef(onenabledRef) , m_onenabledRef(onenabledRef)
, m_oncheckedRef(oncheckedRef)
{ {
} }
@ -72,28 +77,44 @@ protected:
bool onEnabled(Context* context) override bool onEnabled(Context* context) override
{ {
if (m_onenabledRef) { if (m_onenabledRef) {
script::Engine* engine = App::instance()->scriptEngine(); return callScriptRef(m_onenabledRef);
lua_State* L = engine->luaState();
lua_rawgeti(L, LUA_REGISTRYINDEX, m_onenabledRef);
if (lua_pcall(L, 0, 1, 0)) {
if (const char* s = lua_tostring(L, -1)) {
Console().printf("Error: %s", s);
return false;
}
}
else {
bool ret = lua_toboolean(L, -1);
lua_pop(L, 1);
return ret;
}
} }
return true; return true;
} }
bool onChecked(Context* context) override
{
if (m_oncheckedRef) {
return callScriptRef(m_oncheckedRef);
}
return false;
}
private:
bool callScriptRef(int ref)
{
ASSERT(ref);
script::Engine* engine = App::instance()->scriptEngine();
lua_State* L = engine->luaState();
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
if (lua_pcall(L, 0, 1, 0)) {
if (const char* s = lua_tostring(L, -1)) {
Console().printf("Error: %s", s);
return false;
}
}
else {
bool ret = lua_toboolean(L, -1);
lua_pop(L, 1);
return ret;
}
}
std::string m_title; std::string m_title;
int m_onclickRef; int m_onclickRef;
int m_onenabledRef; int m_onenabledRef;
int m_oncheckedRef;
}; };
void deleteCommandIfExistent(Extension* ext, const std::string& id) void deleteCommandIfExistent(Extension* ext, const std::string& id)
@ -126,6 +147,7 @@ int Plugin_newCommand(lua_State* L)
if (lua_istable(L, 2)) { if (lua_istable(L, 2)) {
std::string id, title, group; std::string id, title, group;
int onenabledRef = 0; int onenabledRef = 0;
int oncheckedRef = 0;
lua_getfield(L, 2, "id"); lua_getfield(L, 2, "id");
if (const char* s = lua_tostring(L, -1)) { if (const char* s = lua_tostring(L, -1)) {
@ -156,6 +178,14 @@ int Plugin_newCommand(lua_State* L)
lua_pop(L, 1); lua_pop(L, 1);
} }
type = lua_getfield(L, 2, "onchecked");
if (type == LUA_TFUNCTION) {
oncheckedRef = luaL_ref(L, LUA_REGISTRYINDEX); // does a pop
}
else {
lua_pop(L, 1);
}
type = lua_getfield(L, 2, "onclick"); type = lua_getfield(L, 2, "onclick");
if (type == LUA_TFUNCTION) { if (type == LUA_TFUNCTION) {
int onclickRef = luaL_ref(L, LUA_REGISTRYINDEX); int onclickRef = luaL_ref(L, LUA_REGISTRYINDEX);
@ -164,7 +194,7 @@ int Plugin_newCommand(lua_State* L)
// overwriting a previous registered command) // overwriting a previous registered command)
deleteCommandIfExistent(plugin->ext, id); deleteCommandIfExistent(plugin->ext, id);
auto cmd = new PluginCommand(id, title, onclickRef, onenabledRef); auto cmd = new PluginCommand(id, title, onclickRef, onenabledRef, oncheckedRef);
Commands::instance()->add(cmd); Commands::instance()->add(cmd);
plugin->ext->addCommand(id); plugin->ext->addCommand(id);