From a5b93ccd726f5ed9c2b079b450588cc9f34f94e7 Mon Sep 17 00:00:00 2001 From: Jonathan Hugonnard Date: Sun, 23 Oct 2022 13:46:57 +0200 Subject: [PATCH 1/8] feat/loadScene : added functor in builtin/context --- libscript/include/pivot/script/Builtins.hxx | 7 +++++++ libscript/sources/Builtins.cxx | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/libscript/include/pivot/script/Builtins.hxx b/libscript/include/pivot/script/Builtins.hxx index 552fa621d..e61551782 100644 --- a/libscript/include/pivot/script/Builtins.hxx +++ b/libscript/include/pivot/script/Builtins.hxx @@ -19,6 +19,9 @@ struct BuiltinContext { /// Functor returning true if a specific key is pressed std::function isKeyPressed; + /// Functor calling the setScene from ecs::ScenemManager + std::function loadScene; + /// Mock builtin context for unit testing static BuiltinContext mock() { @@ -37,6 +40,10 @@ data::Value builtin_print(const std::vector ¶ms, const BuiltinC /// Same as above but take a ostream to write to data::Value builtin_print_stream(const std::vector ¶ms, std::ostream &stream); +/// void loadScene(String scene) +/// Loads the scene string passed as parameter into the editor +data::Value builtin_loadScene(const std::vector ¶ms, const BuiltinContext &context); + // Math built-ins /// Number cos(Number radAngle) /// Returns the cosine of the parameter (in radian) diff --git a/libscript/sources/Builtins.cxx b/libscript/sources/Builtins.cxx index 3247eba9c..7932092a7 100644 --- a/libscript/sources/Builtins.cxx +++ b/libscript/sources/Builtins.cxx @@ -48,6 +48,11 @@ data::Value builtin_print_stream(const std::vector ¶ms, std::os return data::Value(); } +data::Value builtin_loadScene(const std::vector ¶ms, const BuiltinContext &context) +{ + return context.loadScene(std::get(params.at(0))); +} + data::Value builtin_cos(const std::vector ¶ms, const BuiltinContext &) { return std::cos(std::get(params.at(0))); From ad7577918039066600d599adc8a20dbdec3c9920 Mon Sep 17 00:00:00 2001 From: Jonathan Hugonnard Date: Tue, 25 Oct 2022 18:38:06 +0200 Subject: [PATCH 2/8] feat/script/loadScene : add loadScene builtin --- libpivot/sources/engine.cxx | 6 +++++- libscript/include/pivot/script/Builtins.hxx | 2 +- libscript/sources/Builtins.cxx | 3 ++- libscript/sources/Interpreter.cxx | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/libpivot/sources/engine.cxx b/libpivot/sources/engine.cxx index 9e857c276..0c7126747 100644 --- a/libpivot/sources/engine.cxx +++ b/libpivot/sources/engine.cxx @@ -47,7 +47,11 @@ namespace pivot Engine::Engine() : m_scripting_engine( m_system_index, m_component_index, - pivot::ecs::script::interpreter::builtins::BuiltinContext{std::bind_front(&Engine::isKeyPressed, this)}), + pivot::ecs::script::interpreter::builtins::BuiltinContext{std::bind_front(&Engine::isKeyPressed, this), + [this](const std::string &scene) { + std::cout << "lambda" << std::endl; + this->loadScene(scene); + }}), m_camera(builtins::Camera(glm::vec3(0, 5, 0))) { DEBUG_FUNCTION(); diff --git a/libscript/include/pivot/script/Builtins.hxx b/libscript/include/pivot/script/Builtins.hxx index e61551782..6d4bd3dc5 100644 --- a/libscript/include/pivot/script/Builtins.hxx +++ b/libscript/include/pivot/script/Builtins.hxx @@ -25,7 +25,7 @@ struct BuiltinContext { /// Mock builtin context for unit testing static BuiltinContext mock() { - return BuiltinContext{.isKeyPressed = [](auto) { return false; }}; + return BuiltinContext{.isKeyPressed = [](auto) { return false; }, .loadScene = [](auto) {}}; } }; diff --git a/libscript/sources/Builtins.cxx b/libscript/sources/Builtins.cxx index 7932092a7..c0718f323 100644 --- a/libscript/sources/Builtins.cxx +++ b/libscript/sources/Builtins.cxx @@ -50,7 +50,8 @@ data::Value builtin_print_stream(const std::vector ¶ms, std::os data::Value builtin_loadScene(const std::vector ¶ms, const BuiltinContext &context) { - return context.loadScene(std::get(params.at(0))); + context.loadScene(std::get(params.at(0))); + return data::Value(); } data::Value builtin_cos(const std::vector ¶ms, const BuiltinContext &) diff --git a/libscript/sources/Interpreter.cxx b/libscript/sources/Interpreter.cxx index 17712940e..a592f9442 100644 --- a/libscript/sources/Interpreter.cxx +++ b/libscript/sources/Interpreter.cxx @@ -45,6 +45,7 @@ using ParameterPair = std::pair>>; /// This map will map the name of a builtin, to its callback paired with its signature const std::unordered_map> gBuiltinsCallbacks = { {"isPressed", {interpreter::builtins::builtin_isPressed, {1, {{data::BasicType::String}}}}}, + {"loadScene", {interpreter::builtins::builtin_loadScene, {1, {{data::BasicType::String}}}}}, {"cos", {interpreter::builtins::builtin_cos, {1, {{data::BasicType::Number}}}}}, {"sin", {interpreter::builtins::builtin_sin, {1, {{data::BasicType::Number}}}}}, {"print", @@ -281,7 +282,7 @@ data::Value Interpreter::executeFunction(const Node &functionCall, const Stack & validateParams(parameters, gBuiltinsCallbacks.at(callee.value).second.first, gBuiltinsCallbacks.at(callee.value).second.second, callee.value); // pair is types> - return gBuiltinsCallbacks.at(callee.value) + return gBuiltinsCallbacks.at(callee.value) .first(parameters, m_builtinContext); // return the return value of the built-in } From 3a65465419eb007d739c92b845db8ffa2874e780 Mon Sep 17 00:00:00 2001 From: Jonathan Hugonnard Date: Thu, 27 Oct 2022 15:28:37 +0200 Subject: [PATCH 3/8] feat/script/loadScene : correct SceneManager calls --- libpivot/sources/engine.cxx | 9 +++-- libscript/CMakeLists.txt | 1 + libscript/tests/test_loadScene.cxx | 58 ++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 libscript/tests/test_loadScene.cxx diff --git a/libpivot/sources/engine.cxx b/libpivot/sources/engine.cxx index 0c7126747..e3d0584f2 100644 --- a/libpivot/sources/engine.cxx +++ b/libpivot/sources/engine.cxx @@ -49,8 +49,13 @@ Engine::Engine() m_system_index, m_component_index, pivot::ecs::script::interpreter::builtins::BuiltinContext{std::bind_front(&Engine::isKeyPressed, this), [this](const std::string &scene) { - std::cout << "lambda" << std::endl; - this->loadScene(scene); + // Check that the scene is loaded + if (!this->m_scene_manager.getSceneId(scene).has_value()) { + logger.warn("loadScene") << "The scene " << scene << " doesn't exist."; + return ; + } + // Set the scene as the active one + this->m_scene_manager.setCurrentSceneId(this->m_scene_manager.getSceneId(scene).value()); }}), m_camera(builtins::Camera(glm::vec3(0, 5, 0))) { diff --git a/libscript/CMakeLists.txt b/libscript/CMakeLists.txt index 0ed4ae16c..6779db4ee 100644 --- a/libscript/CMakeLists.txt +++ b/libscript/CMakeLists.txt @@ -20,4 +20,5 @@ build_tests( tests/test_interpreter.cxx tests/test_builtins.cxx tests/test_escaped.cxx + tests/test_loadScene.cxx ) diff --git a/libscript/tests/test_loadScene.cxx b/libscript/tests/test_loadScene.cxx new file mode 100644 index 000000000..5ffdf6b2d --- /dev/null +++ b/libscript/tests/test_loadScene.cxx @@ -0,0 +1,58 @@ +#include "pivot/ecs/Core/SceneManager.hxx" +#include "pivot/script/Engine.hxx" +#include +#include +#include + +using namespace pivot::ecs; + +TEST_CASE("Scripting-Interpreter-LoadScene") +{ + std::cout << "------Interpreter LoadScene------start (lambda must be the same as in engine.cxx)" << std::endl; + + component::Index cind; + systems::Index sind; + pivot::ecs::SceneManager scenemgr; + // Register 2 scenes, and set Scene2 as current active scene + scenemgr.registerScene("Scene1"); + scenemgr.registerScene("Scene2"); + scenemgr.setCurrentSceneId(scenemgr.getSceneId("Scene2").value()); + script::Engine engine(sind, cind, + pivot::ecs::script::interpreter::builtins::BuiltinContext{ + [](auto) { return false; }, + [&scenemgr](const std::string &scene) { + // Check that the scene is loaded + if (!scenemgr.getSceneId(scene).has_value()) { + logger.warn("loadScene") << "The scene " << scene << " doesn't exist."; + return; + } + // Set the scene as the active one + scenemgr.setCurrentSceneId(scenemgr.getSceneId(scene).value()); + }}); + // std::string file = "C:/Users/Najo/eip/pivot/libscript/tests/loadScene.pvt"; + // engine.loadFile(file); + std::string fileContent = "component C\n" + "\tString str\n" + "system S(anyEntity) event Tick(Number deltaTime)\n" + "\tloadScene(\"Scene1\")\n"; + engine.loadFile(fileContent, true); + + REQUIRE(sind.getDescription("S").has_value()); + REQUIRE(cind.getDescription("C").has_value()); + + auto Cdescription = cind.getDescription("C").value(); + auto Sdescription = sind.getDescription("S").value(); + auto array1 = Cdescription.createContainer(Cdescription); + std::vector entity = {data::Record{{"str", "foo"}}}; + array1->setValueForEntity(0, entity.at(0)); + component::ArrayCombination combinations{{std::ref(*array1)}}; + event::EventWithComponent evt = { + .event = event::Event{.description = Sdescription.eventListener, .entities = {1, 2}, .payload = 0.12}}; + + Sdescription.system(Sdescription, combinations, evt); + + // Check that after execution, current active scene is Scene1 + REQUIRE(scenemgr.getCurrentSceneId() == 0); + + std::cout << "------Interpreter LoadScene------end" << std::endl; +} From cdfb9c96b427c353067236d191df49caa2b56609 Mon Sep 17 00:00:00 2001 From: Jonathan Hugonnard Date: Thu, 27 Oct 2022 21:39:32 +0200 Subject: [PATCH 4/8] clang format --- libpivot/sources/engine.cxx | 25 +++++++++++++------------ libscript/sources/Interpreter.cxx | 17 ++++++++++------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/libpivot/sources/engine.cxx b/libpivot/sources/engine.cxx index e3d0584f2..05cd849a3 100644 --- a/libpivot/sources/engine.cxx +++ b/libpivot/sources/engine.cxx @@ -45,18 +45,19 @@ using namespace pivot::ecs; namespace pivot { Engine::Engine() - : m_scripting_engine( - m_system_index, m_component_index, - pivot::ecs::script::interpreter::builtins::BuiltinContext{std::bind_front(&Engine::isKeyPressed, this), - [this](const std::string &scene) { - // Check that the scene is loaded - if (!this->m_scene_manager.getSceneId(scene).has_value()) { - logger.warn("loadScene") << "The scene " << scene << " doesn't exist."; - return ; - } - // Set the scene as the active one - this->m_scene_manager.setCurrentSceneId(this->m_scene_manager.getSceneId(scene).value()); - }}), + : m_scripting_engine(m_system_index, m_component_index, + pivot::ecs::script::interpreter::builtins::BuiltinContext{ + std::bind_front(&Engine::isKeyPressed, this), + [this](const std::string &scene) { + // Check that the scene is loaded + if (!this->m_scene_manager.getSceneId(scene).has_value()) { + logger.warn("loadScene") << "The scene " << scene << " doesn't exist."; + return; + } + // Set the scene as the active one + this->m_scene_manager.setCurrentSceneId( + this->m_scene_manager.getSceneId(scene).value()); + }}), m_camera(builtins::Camera(glm::vec3(0, 5, 0))) { DEBUG_FUNCTION(); diff --git a/libscript/sources/Interpreter.cxx b/libscript/sources/Interpreter.cxx index a592f9442..c98f3d9bc 100644 --- a/libscript/sources/Interpreter.cxx +++ b/libscript/sources/Interpreter.cxx @@ -11,11 +11,10 @@ namespace pivot::ecs::script::interpreter { -const std::map gVariableTypes{{"Vector3", data::BasicType::Vec3}, - {"Number", data::BasicType::Number}, - {"Boolean", data::BasicType::Boolean}, - {"Color", data::BasicType::Number}, - {"String", data::BasicType::String}}; +const std::map gVariableTypes{ + {"Vector3", data::BasicType::Vec3}, {"Vector2", data::BasicType::Vec2}, {"Asset", data::BasicType::Asset}, + {"Number", data::BasicType::Number}, {"Boolean", data::BasicType::Boolean}, {"Color", data::BasicType::Color}, + {"String", data::BasicType::String}}; // Map builtin binary (two operands) operators, to their operator enum const std::map> gOperatorCallbacks = { {"*", interpreter::builtins::builtin_operator}, @@ -45,9 +44,13 @@ using ParameterPair = std::pair>>; /// This map will map the name of a builtin, to its callback paired with its signature const std::unordered_map> gBuiltinsCallbacks = { {"isPressed", {interpreter::builtins::builtin_isPressed, {1, {{data::BasicType::String}}}}}, - {"loadScene", {interpreter::builtins::builtin_loadScene, {1, {{data::BasicType::String}}}}}, {"cos", {interpreter::builtins::builtin_cos, {1, {{data::BasicType::Number}}}}}, {"sin", {interpreter::builtins::builtin_sin, {1, {{data::BasicType::Number}}}}}, + {"toString", + {interpreter::builtins::builtin_toString, + {std::numeric_limits::max(), + {{data::BasicType::String, data::BasicType::Number, data::BasicType::Integer, data::BasicType::Boolean, + data::BasicType::Asset, data::BasicType::Vec3, data::BasicType::Vec2, data::BasicType::Color}}}}}, {"print", {interpreter::builtins::builtin_print, {std::numeric_limits::max(), @@ -282,7 +285,7 @@ data::Value Interpreter::executeFunction(const Node &functionCall, const Stack & validateParams(parameters, gBuiltinsCallbacks.at(callee.value).second.first, gBuiltinsCallbacks.at(callee.value).second.second, callee.value); // pair is types> - return gBuiltinsCallbacks.at(callee.value) + return gBuiltinsCallbacks.at(callee.value) .first(parameters, m_builtinContext); // return the return value of the built-in } From a406f09d750fdd2fd060604854f91a5625b6ac1a Mon Sep 17 00:00:00 2001 From: Jonathan Hugonnard Date: Fri, 28 Oct 2022 02:49:53 +0200 Subject: [PATCH 5/8] erratum --- libscript/sources/Interpreter.cxx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/libscript/sources/Interpreter.cxx b/libscript/sources/Interpreter.cxx index c98f3d9bc..a17f76b14 100644 --- a/libscript/sources/Interpreter.cxx +++ b/libscript/sources/Interpreter.cxx @@ -44,13 +44,9 @@ using ParameterPair = std::pair>>; /// This map will map the name of a builtin, to its callback paired with its signature const std::unordered_map> gBuiltinsCallbacks = { {"isPressed", {interpreter::builtins::builtin_isPressed, {1, {{data::BasicType::String}}}}}, + {"loadScene", {interpreter::builtins::builtin_loadScene, {1, {{data::BasicType::String}}}}}, {"cos", {interpreter::builtins::builtin_cos, {1, {{data::BasicType::Number}}}}}, {"sin", {interpreter::builtins::builtin_sin, {1, {{data::BasicType::Number}}}}}, - {"toString", - {interpreter::builtins::builtin_toString, - {std::numeric_limits::max(), - {{data::BasicType::String, data::BasicType::Number, data::BasicType::Integer, data::BasicType::Boolean, - data::BasicType::Asset, data::BasicType::Vec3, data::BasicType::Vec2, data::BasicType::Color}}}}}, {"print", {interpreter::builtins::builtin_print, {std::numeric_limits::max(), From 09b3d9ec21b9305b45040a897fd60f3ee871c50f Mon Sep 17 00:00:00 2001 From: Jonathan Hugonnard Date: Tue, 15 Nov 2022 20:38:27 +0100 Subject: [PATCH 6/8] fix(script) : missplay + L + ratio --- libpivot/sources/engine.cxx | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/libpivot/sources/engine.cxx b/libpivot/sources/engine.cxx index b5a20dbf7..92eb55e81 100644 --- a/libpivot/sources/engine.cxx +++ b/libpivot/sources/engine.cxx @@ -68,17 +68,17 @@ namespace pivot Engine::Engine() : m_scripting_engine(m_system_index, m_component_index, pivot::ecs::script::interpreter::builtins::BuiltinContext{ - .loadScene = std::bind_front(&Engine::isKeyPressed, this), - [this](const std::string &scene) { - // Check that the scene is loaded - if (!this->m_scene_manager.getSceneId(scene).has_value()) { - logger.warn("loadScene") << "The scene " << scene << " doesn't exist."; - return; - } - // Set the scene as the active one - this->m_scene_manager.setCurrentSceneId( - this->m_scene_manager.getSceneId(scene).value()); - }), + .loadScene = + [this](const std::string &scene) { + // Check that the scene is loaded + if (!this->m_scene_manager.getSceneId(scene).has_value()) { + logger.warn("loadScene") << "The scene " << scene << " doesn't exist."; + return; + } + // Set the scene as the active one + this->m_scene_manager.setCurrentSceneId( + this->m_scene_manager.getSceneId(scene).value()); + }, .isKeyPressed = std::bind_front(&Engine::isKeyPressed, this), .selectCamera = std::bind_front(&Engine::setCurrentCamera, this), }), From aaa25bece90c7740225cc34f4d916b6d8fa001fe Mon Sep 17 00:00:00 2001 From: Jonathan Hugonnard Date: Tue, 15 Nov 2022 20:44:26 +0100 Subject: [PATCH 7/8] format(script) : clang + didnt ask + dont care --- libscript/include/pivot/script/Builtins.hxx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libscript/include/pivot/script/Builtins.hxx b/libscript/include/pivot/script/Builtins.hxx index 545f606c6..d34e2cad4 100644 --- a/libscript/include/pivot/script/Builtins.hxx +++ b/libscript/include/pivot/script/Builtins.hxx @@ -28,7 +28,8 @@ struct BuiltinContext { /// Mock builtin context for unit testing static BuiltinContext mock() { - return BuiltinContext{.isKeyPressed = [](auto) { return false; }, .loadScene = [](auto) {}, .selectCamera = [](auto) {}}; + return BuiltinContext{ + .isKeyPressed = [](auto) { return false; }, .loadScene = [](auto) {}, .selectCamera = [](auto) {}}; } }; From 53b80e359f5a9b9e2d0bc3103c6dcf1904fbc71b Mon Sep 17 00:00:00 2001 From: Jonathan Hugonnard Date: Tue, 15 Nov 2022 20:55:34 +0100 Subject: [PATCH 8/8] typo(script) : typo --- libpivot/sources/engine.cxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libpivot/sources/engine.cxx b/libpivot/sources/engine.cxx index 92eb55e81..136091f7b 100644 --- a/libpivot/sources/engine.cxx +++ b/libpivot/sources/engine.cxx @@ -76,8 +76,7 @@ Engine::Engine() return; } // Set the scene as the active one - this->m_scene_manager.setCurrentSceneId( - this->m_scene_manager.getSceneId(scene).value()); + this->changeCurrentScene(this->m_scene_manager.getSceneId(scene).value()); }, .isKeyPressed = std::bind_front(&Engine::isKeyPressed, this), .selectCamera = std::bind_front(&Engine::setCurrentCamera, this),