lua_api.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. #include "lumacs/editor_core.hpp"
  3. #include "lumacs/theme.hpp"
  4. #include "lumacs/keybinding.hpp"
  5. #include <sol/sol.hpp>
  6. #include <functional>
  7. #include <map>
  8. #include <string>
  9. namespace lumacs {
  10. class EditorCore; // Forward declaration
  11. /// Lua scripting API for Lumacs
  12. class LuaApi {
  13. public:
  14. explicit LuaApi(); // Default constructor
  15. ~LuaApi() = default;
  16. /// Set the EditorCore instance after construction
  17. void set_core(EditorCore& core);
  18. /// Get the Lua state
  19. [[nodiscard]] sol::state& state() { return lua_; }
  20. /// Load and execute a Lua file
  21. bool load_file(const std::filesystem::path& path);
  22. /// Execute Lua code
  23. bool execute(std::string_view code);
  24. /// Load init.lua from standard locations
  25. bool load_init_file();
  26. /// Bind a key to a Lua function (uses new keybinding system)
  27. void bind_key(std::string key, sol::function callback, std::string description = "");
  28. /// Process key using the new keybinding system
  29. KeyResult process_key(const std::string& key);
  30. /// Get all key bindings (for debugging) - Legacy method
  31. [[nodiscard]] std::map<std::string, sol::function> key_bindings() const {
  32. return legacy_key_bindings_;
  33. }
  34. // Legacy methods for backward compatibility
  35. [[nodiscard]] bool has_key_binding(const std::string& key) const;
  36. bool execute_key_binding(const std::string& key);
  37. private:
  38. sol::state lua_;
  39. EditorCore* core_ = nullptr; // Changed to pointer
  40. std::map<std::string, sol::function> legacy_key_bindings_; // For backward compatibility
  41. void setup_api();
  42. void register_types();
  43. void register_functions();
  44. };
  45. } // namespace lumacs