Преглед на файлове

fix(lua): Allow binding keys to command strings in Lua API

- Modified 'bind_key' Lua binding in EditorCore to accept sol::object.
- Implemented logic to handle string command names directly (for 'bind_key("C-f", "forward-char")').
- Maintained support for function callbacks.
- Resolves critical bug where baked-in defaults failed to load because they used string bindings.
Bernardo Magri преди 1 месец
родител
ревизия
821a318495
променени са 1 файла, в които са добавени 54 реда и са изтрити 43 реда
  1. 54 43
      src/lua_api.cpp

+ 54 - 43
src/lua_api.cpp

@@ -647,54 +647,65 @@ void LuaApi::register_types() {
 
         
         // Key binding (method on EditorCore)
-        "bind_key", [this](EditorCore& core, std::string key, sol::function callback, sol::optional<std::string> description) {
-            // Create a unique command name for the Lua function
-            std::string command_name = "lua_cmd_" + key; // Use key itself as part of name
-
-            // Register a C++ CommandFunction that wraps the Lua callback
-            core.command_system().register_command(command_name, 
-                [callback, this](CommandContext& context) -> CommandResult { // Added 'this' to capture
-                    try {
-                        // Pass args from context to Lua function
-                        sol::table args_table = get_lua_state().create_table(); // Use get_lua_state()
-                        const auto& args = context.get_args(); // Use getter
-                        for (size_t i = 0; i < args.size(); ++i) {
-                            args_table[i + 1] = args[i];
-                        }
-                        auto result = callback(args_table);
-                        if (result.valid()) {
-                            if (result.get_type() == sol::type::table) {
-                                sol::table res_table = result;
-                                CommandStatus status = CommandStatus::Success;
-                                std::string message = "";
-                                
-                                if (res_table["success"].valid()) {
-                                    status = res_table["success"].get<bool>() ? CommandStatus::Success : CommandStatus::Failure;
-                                }
-                                if (res_table["message"].valid()) {
-                                    message = res_table["message"];
+        "bind_key", [this](EditorCore& core, std::string key, sol::object callback_or_cmd, sol::optional<std::string> description) {
+            if (callback_or_cmd.is<std::string>()) {
+                // Bind directly to an existing command name
+                std::string cmd_name = callback_or_cmd.as<std::string>();
+                core.keybinding_manager().bind(KeySequence(key), cmd_name, description.value_or(""));
+            } 
+            else if (callback_or_cmd.is<sol::function>()) {
+                // Create a unique command name for the Lua function
+                std::string command_name = "lua_cmd_" + key; // Use key itself as part of name
+                sol::function callback = callback_or_cmd.as<sol::function>();
+
+                // Register a C++ CommandFunction that wraps the Lua callback
+                core.command_system().register_command(command_name, 
+                    [callback, this](CommandContext& context) -> CommandResult { // Added 'this' to capture
+                        try {
+                            // Pass args from context to Lua function
+                            sol::table args_table = get_lua_state().create_table(); // Use get_lua_state()
+                            const auto& args = context.get_args(); // Use getter
+                            for (size_t i = 0; i < args.size(); ++i) {
+                                args_table[i + 1] = args[i];
+                            }
+                            auto result = callback(args_table);
+                            if (result.valid()) {
+                                if (result.get_type() == sol::type::table) {
+                                    sol::table res_table = result;
+                                    CommandStatus status = CommandStatus::Success;
+                                    std::string message = "";
+                                    
+                                    if (res_table["success"].valid()) {
+                                        status = res_table["success"].get<bool>() ? CommandStatus::Success : CommandStatus::Failure;
+                                    }
+                                    if (res_table["message"].valid()) {
+                                        message = res_table["message"];
+                                    }
+                                    return CommandResult{status, message};
+                                } else if (result.get_type() == sol::type::string) {
+                                    std::string message = result.get<std::string>();
+                                    return CommandResult{CommandStatus::Success, message};
+                                } else {
+                                    return CommandResult{CommandStatus::Success, ""};
                                 }
-                                return CommandResult{status, message};
-                            } else if (result.get_type() == sol::type::string) {
-                                std::string message = result.get<std::string>();
-                                return CommandResult{CommandStatus::Success, message};
                             } else {
                                 return CommandResult{CommandStatus::Success, ""};
                             }
-                        } else {
-                            return CommandResult{CommandStatus::Success, ""};
+                        } catch (const sol::error& e) {
+                            return CommandResult{CommandStatus::Failure, "Lua error in key binding callback: " + std::string(e.what())};
                         }
-                    } catch (const sol::error& e) {
-                        return CommandResult{CommandStatus::Failure, "Lua error in key binding callback: " + std::string(e.what())};
-                    }
-                },
-                description.value_or(""), // Use original description
-                false,       // Not interactive directly via spec, Lua callback handles interactivity
-                ""           // No interactive spec for this wrapper command
-            );
-
-            // Now bind the key to the newly registered C++ command's name
-            core.keybinding_manager().bind(KeySequence(key), command_name, description.value_or(""));
+                    },
+                    description.value_or(""), // Use original description
+                    false,       // Not interactive directly via spec, Lua callback handles interactivity
+                    ""           // No interactive spec for this wrapper command
+                );
+
+                // Now bind the key to the newly registered C++ command's name
+                core.keybinding_manager().bind(KeySequence(key), command_name, description.value_or(""));
+            }
+            else {
+                spdlog::error("bind_key: Invalid argument type for callback. Expected string or function.");
+            }
         },
 
         // Register command (method on EditorCore)