LUA code can be executed through the LUA console GUI window.

API methods added: SetWindowSize, Log.
Gui_Panel_Refactor
Joeyrp 4 years ago
parent 1ee5fe5b1f
commit f5aea4216f

@ -50,6 +50,7 @@ set(LUNARIUM_SRC
"src/graphics/gui/logGui.cpp"
"src/graphics/gui/luaConsole.cpp"
"src/scripting/scriptManager.cpp"
"src/scripting/coreAPI.cpp"
"src/assets/types/image.cpp"
)

@ -22,6 +22,7 @@
#include <graphics/gui/logGui.h>
#include <graphics/gui/luaConsole.h>
#include <scripting/scriptManager.h>
#include <scripting/coreAPI.h>
namespace lunarium
{
@ -62,7 +63,10 @@ namespace lunarium
mpInstance->mState.SaveToFile("lunarium_state.xml");
// Shutdown subsystems
// LuaConsole::FreeInstance();
CoreAPI::FreeInstance();
ScriptManager::FreeInstance();
LuaConsole::FreeInstance();
LogGui::FreeInstance();
GUI::GetInstance().Shutdown();
GUI::FreeInstance();
@ -220,6 +224,14 @@ namespace lunarium
"Could not initialized the LUA script manager: %s", result.Description);
}
CoreAPI& capi = CoreAPI::GetInstance();
result = capi.Initialize(scriptMan);
if (Failed(result))
{
Logger::Log(LogCategory::CORE, LogLevel::WARNING,
"Could not initialized the LUA Core API: %s", result.Description);
}
mbIsInit = true;
}
@ -296,6 +308,8 @@ namespace lunarium
if (LuaConsole::GetInstance().GetNewCommand(command))
{
Logger::Log(LogCategory::CORE, LogLevel::INFO, "New LUA command: %s", command.c_str());
OpRes result = ScriptManager::RunScript(command.c_str());
// NOTE: Ignoring script result
}
}

@ -97,7 +97,7 @@ namespace lunarium
ImGui::SetScrollHereY(1.0f);
ImGui::End();
if (Core::Input().IsKeyPressed(KeyCode::RETURN) && strlen(mBuffer) > 1)
if (Core::Input().IsKeyPressed(KeyCode::RETURN, true) && strlen(mBuffer) > 1)
{
mCommandHistory.push_back(mBuffer);
memset(mBuffer, 0, LUA_CON_BUFFER_SIZE);

@ -0,0 +1,61 @@
/******************************************************************************
* File - coreAPI.cpp
* Author - Joey Pollack
* Date - 2021/09/23 (y/m/d)
* Mod Date - 2021/09/23 (y/m/d)
* Description - class with all static methods that expose core functionality to LUA
* Need an init function that takes a reference to a LUA state so that it
* can expose the static functions to LUA.
******************************************************************************/
#include "coreAPI.h"
#include "scriptManager.h"
#include <core/core.h>
#include <utils/logger.h>
namespace lunarium
{
CoreAPI* CoreAPI::mpInstance = nullptr;
CoreAPI& CoreAPI::GetInstance()
{
if (mpInstance == nullptr)
{
mpInstance = new CoreAPI;
}
return *mpInstance;
}
void CoreAPI::FreeInstance()
{
delete mpInstance;
mpInstance = nullptr;
}
OpRes CoreAPI::Initialize(ScriptManager& sman)
{
mCat = sman.mCat;
// Register methods
sman.mState["SetWindowSize"] = &CoreAPI::SetWindowSize;
sman.mState["Log"] = &CoreAPI::Log;
// return OpRes::Fail("CoreAPI::Initialize not implemented yet!");
return OpRes::OK();
}
////////////////////////////////////////////////////////////
// API
////////////////////////////////////////////////////////////
void CoreAPI::SetWindowSize(int w, int h)
{
Core::MainWindow().Resize(w, h);
}
void CoreAPI::Log(int level, const char* msg)
{
Logger::Log(mpInstance->mCat, level, msg);
}
}

@ -11,13 +11,30 @@
#ifndef CORE_API_H_
#define CORE_API_H_
#include <utils/opRes.h>
namespace lunarium
{
class ScriptManager;
class CoreAPI
{
public:
static CoreAPI& GetInstance();
static void FreeInstance();
OpRes Initialize(ScriptManager& sman);
private:
static CoreAPI* mpInstance;
uint32_t mCat;
public: // API
static void SetWindowSize(int w, int h);
static void Log(int level, const char* msg);
};
}

@ -9,6 +9,7 @@
******************************************************************************/
#include "scriptManager.h"
#include <utils/logger.h>
namespace lunarium
{
@ -31,9 +32,33 @@ namespace lunarium
OpRes ScriptManager::Initialize()
{
mCat = Logger::RegisterCategory("LUA");
mState.open_libraries(sol::lib::base, sol::lib::package);
// mState.script("print('test puppy says: bark bark bark!')");
return OpRes::OK();
}
OpRes ScriptManager::RunScript(const char* code)
{
if (!mpInstance)
{
return OpRes::Fail("ScriptManager::RunScript failed because the instance is null!");
}
// TODO: Use the state to run the script and check for errors
auto result = mpInstance->mState.script(code, [](lua_State*, sol::protected_function_result pfr) {
return pfr;
});
if (!result.valid())
{
sol::error err = result;
Logger::Log(mpInstance->mCat, LogLevel::WARNING, "LUA script has errors: %s", err.what());
return OpRes::Fail(err.what());
}
return OpRes::OK();
}
}

@ -23,10 +23,14 @@ namespace lunarium
static void FreeInstance();
OpRes Initialize();
static OpRes RunScript(const char* code);
private:
static ScriptManager* mpInstance;
sol::state mState;
uint32_t mCat;
friend class CoreAPI;
};
}

Loading…
Cancel
Save