Skip to content

Commit

Permalink
Editable Lua variables from editor for components
Browse files Browse the repository at this point in the history
  • Loading branch information
Mormert committed Jul 26, 2023
1 parent c101740 commit 5ab639f
Show file tree
Hide file tree
Showing 8 changed files with 2,943 additions and 88 deletions.
2,872 changes: 2,872 additions & 0 deletions engine/3rdparty/ImGui/sol_ImGui.h

Large diffs are not rendered by default.

101 changes: 29 additions & 72 deletions engine/EditorResources/scripts/pretty_table.lua
Original file line number Diff line number Diff line change
@@ -1,82 +1,39 @@

luaEditor = luaEditor or {}

function luaEditor.prettyTable(node)
local cache, stack, output = {}, {}, {}
local depth = 1
local output_str = "{\n"

while true do
local size = 0

for k, v in pairs(node) do
size = size + 1
end

local cur_index = 1
function luaEditor.tableImguiRecursive(node, depth)
if type(node) == 'table' then
ImGui.BeginChild(tostring(depth), 0, 0, true)
for k, v in pairs(node) do
if (cache[node] == nil) or (cur_index >= cache[node]) then

if (string.find(output_str, "}", output_str:len())) then
output_str = output_str .. ",\n"
elseif not (string.find(output_str, "\n", output_str:len())) then
output_str = output_str .. "\n"
end

-- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
table.insert(output, output_str)
output_str = ""

local key
if (type(k) == "number" or type(k) == "boolean") then
key = "[" .. tostring(k) .. "]"
else
key = "['" .. tostring(k) .. "']"
end

if (type(v) == "number" or type(v) == "boolean") then
output_str = output_str .. string.rep('\t', depth) .. key .. " = " .. tostring(v)
elseif (type(v) == "table") then
output_str = output_str .. string.rep('\t', depth) .. key .. " = {\n"
table.insert(stack, node)
table.insert(stack, v)
cache[node] = cur_index + 1
break
else
output_str = output_str .. string.rep('\t', depth) .. key .. " = '" .. tostring(v) .. "'"
end

if (cur_index == size) then
output_str = output_str .. "\n" .. string.rep('\t', depth - 1) .. "}"
else
output_str = output_str .. ","
if type(v) == 'number' then
v, selected = ImGui.InputFloat(k, v)
node[k] = v
end
if type(v) == 'string' then
-- Kinda buggy atm
v, selected = ImGui.InputText(k, v, 100)
local terminator = 0
for i = 1, #v do
if (string.byte(v:sub(i, i)) == 0) then
terminator = i
break
end
end
else
-- close the table
if (cur_index == size) then
output_str = output_str .. "\n" .. string.rep('\t', depth - 1) .. "}"
node[k] = v:sub(1, terminator - 1)
end
if type(v) == "userdata" then
ImGui.Text(tostring(v))
end
if type(v) == "table" then
if (ImGui.TreeNode(k)) then
luaEditor.tableImguiRecursive(v, depth + 1)
ImGui.TreePop()
end
end

cur_index = cur_index + 1
end

if (size == 0) then
output_str = output_str .. "\n" .. string.rep('\t', depth - 1) .. "}"
end

if (#stack > 0) then
node = stack[#stack]
stack[#stack] = nil
depth = cache[node] == nil and depth + 1 or depth - 1
else
break
end
ImGui.EndChild()
end
end

-- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
table.insert(output, output_str)
output_str = table.concat(output)

return output_str
function luaEditor.tableImGui(node)
luaEditor.tableImguiRecursive(node, 0)
end
30 changes: 18 additions & 12 deletions engine/editor/jleEditorSceneObjectsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,23 @@ jleEditorSceneObjectsWindow::update(jleGameEngine &ge)
cereal::jleImGuiCerealArchive ar1;
ar1(*selectedObjectSafePtr);

if(!gEngine->isGameKilled())
{
if(auto luaScript = selectedObjectSafePtr->getComponent<cLuaScript>())
{
if (!gEngine->isGameKilled()) {
if (auto luaScript = selectedObjectSafePtr->getComponent<cLuaScript>()) {
ImGui::Text("Lua Object:");

sol::protected_function f = gEditor->luaEnvironment()->getState()["luaEditor"]["prettyTable"];
std::string prettyTable = f(luaScript->getSelf());

ImGui::BeginChild("LuaPretty", ImVec2(0, 0), true);
ImGui::TextWrapped("%s", prettyTable.c_str());
ImGui::EndChild();
try {
sol::protected_function f =
gEditor->luaEnvironment()->getState()["luaEditor"]["tableImGui"];
if (f.valid()) {
auto res = f(luaScript->getSelf());
if (!res.valid()) {
sol::error err = res;
ImGui::Text("Lua Error: %s", err.what());
}
}
} catch (std::exception &e) {
ImGui::Text("Lua Error: %s", e.what());
}
}
}
}
Expand All @@ -232,14 +237,15 @@ jleEditorSceneObjectsWindow::update(jleGameEngine &ge)
if (ImGui::BeginMenu("Add Custom Component")) {
for (auto &&componentType : jleTypeReflectionUtils::registeredComponentsRef()) {
if (ImGui::MenuItem(componentType.first.c_str())) {
componentBeingAdded = jleTypeReflectionUtils::instantiateComponentByString(componentType.first);
componentBeingAdded =
jleTypeReflectionUtils::instantiateComponentByString(componentType.first);
openedThisFrame = true;
}
}
ImGui::EndMenu();
}

if(openedThisFrame){
if (openedThisFrame) {
ImGui::OpenPopup("Add Component Popup");
}

Expand Down
10 changes: 10 additions & 0 deletions engine/jleLuaEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "jleObject.h"
#include "jlePath.h"
#include "jleResourceRef.h"
#include "ImGui/sol_ImGui.h"
#include <glm/ext/matrix_transform.hpp>

jleLuaEnvironment::jleLuaEnvironment()
Expand All @@ -29,6 +30,8 @@ jleLuaEnvironment::setupLua(sol::state &lua)

setupLuaGLM(lua);

sol_ImGui::Init(lua);

lua.set_function("loadScript", [&](const std::string path) {
loadScript(path.c_str());
});
Expand Down Expand Up @@ -460,4 +463,11 @@ jleLuaEnvironment::loadScript(const jlePath &path)
{
// Loads script and it will be placed in resource holder
auto script = jleResourceRef<jleLuaScript>(path);
_loadedScripts.insert(std::make_pair(path, script.get()));
}

std::unordered_map<jlePath, std::shared_ptr<jleLuaScript>> &
jleLuaEnvironment::loadedScripts()
{
return _loadedScripts;
}
4 changes: 3 additions & 1 deletion engine/jleLuaEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class jleLuaEnvironment

[[nodiscard]] sol::state& getState();

std::unordered_map<jlePath, std::shared_ptr<jleLuaScript>>& loadedScripts();

private:
std::vector<std::shared_ptr<jleLuaScript>> _loadedScripts;
std::unordered_map<jlePath, std::shared_ptr<jleLuaScript>> _loadedScripts;
sol::state _luaState;
};
10 changes: 8 additions & 2 deletions engine/jleLuaScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ jleLuaScript::loadFromFile(const jlePath &path)
void
jleLuaScript::loadScript()
{
_luaEnvironment->getState().script(_sourceCode);
try {
_luaEnvironment->getState().script(_sourceCode);
faultyState = false;
} catch (std::exception &e) {
LOGE << "Loading script failed: " << e.what();
faultyState = true;
}
_luaEnvironment->loadedScripts().insert(std::make_pair(jlePath{filepath, false}, shared_from_this()));
}

void
Expand All @@ -35,4 +42,3 @@ jleLuaScript::saveToFile()
std::ofstream save{filepath};
save << _sourceCode;
}

2 changes: 2 additions & 0 deletions engine/jleLuaScriptComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ jleLuaScriptComponent::loadScript()

faultyState = false;

_luaEnvironment->loadedScripts().insert(std::make_pair(jlePath{filepath, false}, shared_from_this()));

} catch (std::exception &e) {
LOGE << "Loading script failed: " << e.what();
faultyState = true;
Expand Down
2 changes: 1 addition & 1 deletion engine/jleLuaScriptComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define JLE_LUASCRIPTCOMPONENT_H

#include "jleLuaScript.h"
class jleLuaScriptComponent : public jleLuaScript, public std::enable_shared_from_this<jleLuaScriptComponent>
class jleLuaScriptComponent : public jleLuaScript
{
public:
JLE_REGISTER_RESOURCE_TYPE(jleLuaScriptComponent, lua);
Expand Down

0 comments on commit 5ab639f

Please sign in to comment.