From 267b0e81b1043a6170869b9f1afbe65d6f1c3fdf Mon Sep 17 00:00:00 2001 From: Razakhel Date: Sun, 19 Nov 2023 10:54:32 +0100 Subject: [PATCH] [Examples/FullDemo] The mesh's script source can be edited from the demo --- examples/fullDemo.cpp | 54 +++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/examples/fullDemo.cpp b/examples/fullDemo.cpp index 2958cc17..0ad750ab 100644 --- a/examples/fullDemo.cpp +++ b/examples/fullDemo.cpp @@ -163,21 +163,22 @@ int main() { // Scripting // /////////////// - world.addSystem(); + constexpr std::string_view luaScriptSource = R"( +local rotAngle = Degreesf.new(20) + +function setup() + -- 'this' always represents the entity containing the script + this:getTransform().rotation = Quaternionf.new(-rotAngle, Axis.Y) +end - auto& luaScript = mesh.addComponent(R"( - local rotAngle = Degreesf.new(20) +function update(timeInfo) + local angle = rotAngle * math.sin(timeInfo.globalTime) * timeInfo.deltaTime + this:getTransform():rotate(Quaternionf.new(angle, Axis.Y)) +end)"; - function setup() - -- 'this' always represents the entity containing the script - this:getTransform().rotation = Quaternionf.new(-rotAngle, Axis.Y) - end + world.addSystem(); - function update(timeInfo) - local angle = rotAngle * math.sin(timeInfo.globalTime) * timeInfo.deltaTime - this:getTransform():rotate(Quaternionf.new(angle, Axis.Y)) - end - )"); + auto& luaScript = mesh.addComponent(luaScriptSource.data()); /////////// // Audio // @@ -215,6 +216,35 @@ int main() { DemoUtils::insertOverlayCullingOption(window, overlay); DemoUtils::insertOverlayVerticalSyncOption(window, overlay); + constexpr float overlayScriptWidth = 600.f; + constexpr float overlayScriptSourceHeight = 225.f; + constexpr float overlayScriptHeight = overlayScriptSourceHeight + 60.f; + + Raz::OverlayWindow& overlayScript = window.getOverlay().addWindow("Edit script", + Raz::Vec2f(overlayScriptWidth, overlayScriptHeight), + Raz::Vec2f(static_cast(window.getWidth()) - overlayScriptWidth, 0.f)); + overlayScript.disable(); + + Raz::OverlayTextArea& scriptTextArea = overlayScript.addTextArea("Lua script", + [] (const std::string&) noexcept {}, + luaScriptSource.data() + 1, + overlayScriptSourceHeight); + + overlayScript.addButton("Apply", [&] () { + try { + // Running a dummy script, checking that it's syntactically correct + Raz::LuaScript testScript(scriptTextArea.getText()); + testScript.registerEntity(mesh, "this"); + testScript.update({}); + + luaScript.loadCode(scriptTextArea.getText()); + } catch (const std::exception& ex) { + Raz::Logger::error("Failed to reload the Lua script:\n" + std::string(ex.what())); + } + }); + + overlay.addButton("Edit script", [&overlayScript] () noexcept { overlayScript.enable(!overlayScript.isEnabled()); }); + overlay.addSeparator(); #if defined(RAZ_USE_AUDIO)