From 6016a4acf70a081e8afe1cf6d3ec7b55c705cbd5 Mon Sep 17 00:00:00 2001 From: Maxence Pellerin Date: Mon, 17 Jan 2022 18:52:37 +0100 Subject: [PATCH 1/6] feat(index): component + system --- CMakeLists.txt | 9 +- include/ImGuiCore/ComponentEditor.hxx | 41 ++----- include/ImGuiCore/EntityModule.hxx | 3 + include/ImGuiCore/SystemsEditor.hxx | 22 +--- include/ImGuiCore/TypeTemplate/Template.hxx | 37 ++++++ include/Systems/PhysicsSystem.hxx | 7 +- source/ImGuiCore/ComponentEditor.cxx | 120 +++++------------- source/ImGuiCore/Editor.cxx | 4 +- source/ImGuiCore/EntityModule.cxx | 7 +- source/ImGuiCore/SystemsEditor.cxx | 19 ++- source/Systems/PhysicsSystem.cxx | 9 ++ source/main.cxx | 129 +++++++++++--------- 12 files changed, 198 insertions(+), 209 deletions(-) create mode 100644 include/ImGuiCore/TypeTemplate/Template.hxx diff --git a/CMakeLists.txt b/CMakeLists.txt index 891cb6b..67f44e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,12 @@ project(user-interface) include(FetchContent) -set(CMAKE_CXX_STANDARD 20) +if(MSVC) + # see https://gitlab.kitware.com/cmake/cmake/-/issues/22606 + set(CMAKE_CXX_STANDARD 23) +else() + set(CMAKE_CXX_STANDARD 20) +endif() set(CMAKE_CXX_STANDARD_REQUIRED ON) message(STATUS "Looking for lld") @@ -23,7 +28,7 @@ if (LOCAL_PIVOT_DIRECTORY STREQUAL "") FetchContent_Declare( pivot GIT_REPOSITORY https://github.com/EIP-Pivot/pivot.git - GIT_TAG v0.1.0-a.2 + GIT_TAG 2278a7a236e0c4b6470a2300258b98c4c013b3c5 ) if (NOT pivot_POPULATED) message(STATUS "Populating pivot") diff --git a/include/ImGuiCore/ComponentEditor.hxx b/include/ImGuiCore/ComponentEditor.hxx index 1f19a45..6cfc742 100644 --- a/include/ImGuiCore/ComponentEditor.hxx +++ b/include/ImGuiCore/ComponentEditor.hxx @@ -12,6 +12,8 @@ #include #include +#include + #include #include @@ -20,6 +22,8 @@ extern SceneManager gSceneManager; using ObjectVector = std::vector>; +using namespace pivot::ecs::component; + class ComponentEditor { public: @@ -29,36 +33,10 @@ public: void setVectorObject(LevelId scene); std::unordered_map &getVectorObject(); ObjectVector getObject(); - - template - void addComponent(Entity entity, T component) - { - if (!gSceneManager.getCurrentLevel().isRegister()) gSceneManager.getCurrentLevel().RegisterComponent(); - gSceneManager.getCurrentLevel().AddComponent(entity, component); - } private: - template - void addComponent(T component) - { - if (!gSceneManager.getCurrentLevel().isRegister()) - gSceneManager.getCurrentLevel().RegisterComponent(); - gSceneManager.getCurrentLevel().AddComponent(currentEntity, component); - } - template - void displayComponent(std::string name) - { - if (gSceneManager.getCurrentLevel().hasComponent(currentEntity)) { - if (ImGui::TreeNode(name.c_str())) { - ImGui::TreePop(); - createComponent(gSceneManager.getCurrentLevel().GetComponent(currentEntity)); - } - } - } - + void addComponent(const Description &description); + void displayComponent(); void createPopUp(); - void createComponent(RigidBody &rigidBody); - void createComponent(Gravity &gravity); - void createComponent(RenderObject &renderObject); private : Entity currentEntity; @@ -68,9 +46,4 @@ private : std::unordered_map sceneObject; std::array textures = {"rouge", "vert", "bleu", "cyan", "orange", "jaune", "blanc", "violet"}; std::array models = {"cube", "plane"}; -}; - -template <> -void ComponentEditor::addComponent(Entity entity, RenderObject renderObject); -template <> -void ComponentEditor::addComponent(RenderObject renderObject); \ No newline at end of file +}; \ No newline at end of file diff --git a/include/ImGuiCore/EntityModule.hxx b/include/ImGuiCore/EntityModule.hxx index c8190e2..8fe86c2 100644 --- a/include/ImGuiCore/EntityModule.hxx +++ b/include/ImGuiCore/EntityModule.hxx @@ -6,6 +6,9 @@ #include +#include +#include + class EntityModule { public: diff --git a/include/ImGuiCore/SystemsEditor.hxx b/include/ImGuiCore/SystemsEditor.hxx index e143953..ba135ca 100644 --- a/include/ImGuiCore/SystemsEditor.hxx +++ b/include/ImGuiCore/SystemsEditor.hxx @@ -6,32 +6,18 @@ #include "Systems/PhysicsSystem.hxx" #include +#include extern SceneManager gSceneManager; +using namespace pivot::ecs::systems; + class SystemsEditor { public: void create(); - void addSystem(); - - template - void addSystem() - { - auto system = gSceneManager.getCurrentLevel().RegisterSystem(); - gSceneManager.getCurrentLevel().SetSystemSignature(system->getSignature()); - system->Init(); - } private: - template - void displaySystem(std::string name) - { - if (gSceneManager.getCurrentLevel().hasSystem()) { - if (ImGui::TreeNode(name.c_str())) { - ImGui::TreePop(); - } - } - } + void displaySystem(); void createPopUp(); }; diff --git a/include/ImGuiCore/TypeTemplate/Template.hxx b/include/ImGuiCore/TypeTemplate/Template.hxx new file mode 100644 index 0000000..6e23dcb --- /dev/null +++ b/include/ImGuiCore/TypeTemplate/Template.hxx @@ -0,0 +1,37 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include + +using namespace pivot::ecs::component; + +template +void draw(T &value, std::string name); + +#ifndef PIVOT_PROPERTY_DRAW_IMPLEMENTATION +#define PIVOT_PROPERTY_DRAW_IMPLEMENTATION + +template <> +void draw(glm::vec3 &value, std::string name) +{ + ImGui::InputFloat3(name.c_str(), glm::value_ptr(value)); +} + +template <> +void draw(std::string &value, std::string name) +{ + ImGui::InputText(name.c_str(), &value); +} + +template <> +void draw(int &value, std::string name) +{ + // ImGui::InputText("Name", &value); +} + +#endif \ No newline at end of file diff --git a/include/Systems/PhysicsSystem.hxx b/include/Systems/PhysicsSystem.hxx index 74dd36e..85e99f0 100644 --- a/include/Systems/PhysicsSystem.hxx +++ b/include/Systems/PhysicsSystem.hxx @@ -2,10 +2,15 @@ #include "pivot/ecs/Core/System.hxx" +#include +#include + class PhysicsSystem : public System { public: void Update(float dt); Signature getSignature(); -}; \ No newline at end of file +}; + +void physicsSystem(std::vector components); \ No newline at end of file diff --git a/source/ImGuiCore/ComponentEditor.cxx b/source/ImGuiCore/ComponentEditor.cxx index 9623c50..92cad6b 100644 --- a/source/ImGuiCore/ComponentEditor.cxx +++ b/source/ImGuiCore/ComponentEditor.cxx @@ -1,4 +1,6 @@ #include "ImGuiCore/ComponentEditor.hxx" +#include "ImGuiCore/TypeTemplate/Template.hxx" +#include #include void ComponentEditor::create(Entity entity) @@ -6,10 +8,7 @@ void ComponentEditor::create(Entity entity) currentEntity = entity; ImGui::Begin("Component editor"); createPopUp(); - ImGui::InputText("Name", &gSceneManager.getCurrentLevel().getEntityName(currentEntity)); - displayComponent("Rigid body"); - displayComponent("Gravity"); - displayComponent("Render Object"); + displayComponent(); if (ImGui::Button("Add Component")) { ImGui::OpenPopup("AddComponent"); } ImGui::End(); } @@ -29,99 +28,44 @@ ObjectVector ComponentEditor::getObject() { return sceneObject[gSceneManager.get void ComponentEditor::createPopUp() { + auto &cm = gSceneManager.getCurrentLevel().getComponentManager(); if (ImGui::BeginPopup("AddComponent")) { - if (!gSceneManager.getCurrentLevel().hasComponent(currentEntity)) { - if (ImGui::MenuItem("Rigid Body")) { addComponent(RigidBody()); } - } - if (!gSceneManager.getCurrentLevel().hasComponent(currentEntity)) { - if (ImGui::MenuItem("Gravity")) { addComponent(Gravity()); } - } - if (!gSceneManager.getCurrentLevel().hasComponent(currentEntity)) { - if (ImGui::MenuItem("Render object")) { - addComponent({ - .meshID = "cube", - .objectInformation = - { - .transform = Transform(glm::vec3(), glm::vec3(), glm::vec3(1.0f)), - .textureIndex = "blanc", - .materialIndex = "white", - }, - }); - } - } - ImGui::EndPopup(); - } - if (ImGui::BeginPopup("Textures")) { - for (std::string texture: textures) { - if (ImGui::MenuItem(texture.c_str())) { - gSceneManager.getCurrentLevel() - .GetComponent(currentEntity) - .objectInformation.textureIndex = texture; - } - } - ImGui::EndPopup(); - } - if (ImGui::BeginPopup("Models")) { - for (std::string model: models) { - if (ImGui::MenuItem(model.c_str())) { - gSceneManager.getCurrentLevel().GetComponent(currentEntity).meshID = model; + for (const auto &[name, description]: GlobalIndex::getSingleton()) { + if (cm.GetComponent(currentEntity, cm.GetComponentId(name).value()) == std::nullopt) { + if (ImGui::MenuItem(name.c_str())) { addComponent(description); } } } ImGui::EndPopup(); } } -void ComponentEditor::createComponent(RigidBody &rigidBody) +void ComponentEditor::displayComponent() { - ImGui::Indent(); - ImGui::InputFloat3("Acceleration", glm::value_ptr(rigidBody.acceleration)); - ImGui::InputFloat3("Velocity", glm::value_ptr(rigidBody.velocity)); - ImGui::Unindent(); -} - -void ComponentEditor::createComponent(Gravity &gravity) -{ - ImGui::Indent(); - ImGui::InputFloat3("Force", glm::value_ptr(gravity.force)); - ImGui::Unindent(); -} - -void ComponentEditor::createComponent(RenderObject &renderObject) -{ - ImGui::Indent(); - if (ImGui::InputFloat3("Tr", glm::value_ptr(matrixTranslation))) - renderObject.objectInformation.transform.setPosition(matrixTranslation); - if (ImGui::InputFloat3("Rt", glm::value_ptr(matrixRotation))) - renderObject.objectInformation.transform.setRotation(matrixRotation); - if (ImGui::InputFloat3("Sc", glm::value_ptr(matrixScale))) - renderObject.objectInformation.transform.setScale(matrixScale); - if (ImGui::Selectable(gSceneManager.getCurrentLevel() - .GetComponent(currentEntity) - .objectInformation.textureIndex.c_str(), - false, 0, ImVec2(50, 15))) - ImGui::OpenPopup("Textures"); - if (ImGui::Selectable(gSceneManager.getCurrentLevel().GetComponent(currentEntity).meshID.c_str(), - false, 0, ImVec2(50, 15))) - ImGui::OpenPopup("Models"); - ImGui::Unindent(); + auto &cm = gSceneManager.getCurrentLevel().getComponentManager(); + for (const auto &[description, component]: cm.GetAllComponents(currentEntity)) { + if (ImGui::TreeNode(description.name.c_str())) { + ImGui::TreePop(); + ImGui::Indent(); + for (const auto &property: description.properties) { + std::visit([property](auto &&arg) { draw(arg, property.name); }, + description.getProperty(component, property.name)); + } + ImGui::Unindent(); + } + } } -template <> -void ComponentEditor::addComponent(RenderObject renderObject) +void ComponentEditor::addComponent(const Description &description) { - if (!gSceneManager.getCurrentLevel().isRegister()) - gSceneManager.getCurrentLevel().RegisterComponent(); - gSceneManager.getCurrentLevel().AddComponent(currentEntity, renderObject); - sceneObject[gSceneManager.getCurrentLevelId()].push_back( - gSceneManager.getCurrentLevel().GetComponent(currentEntity)); + auto &cm = gSceneManager.getCurrentLevel().getComponentManager(); + auto id = cm.GetComponentId(description.name).value(); + std::map properties; + for (const auto &property: description.properties) { + switch (property.type) { + case Description::Property::Type::String: properties.insert({property.name, std::string("")}); break; + case Description::Property::Type::Vec3: properties.insert({property.name, glm::vec3(0.0f)}); break; + case Description::Property::Type::Number: properties.insert({property.name, 0}); break; + } + } + cm.AddComponent(currentEntity, description.create(properties), id); } - -template <> -void ComponentEditor::addComponent(Entity entity, RenderObject renderObject) -{ - if (!gSceneManager.getCurrentLevel().isRegister()) - gSceneManager.getCurrentLevel().RegisterComponent(); - gSceneManager.getCurrentLevel().AddComponent(entity, renderObject); - sceneObject[gSceneManager.getCurrentLevelId()].push_back( - gSceneManager.getCurrentLevel().GetComponent(entity)); -} \ No newline at end of file diff --git a/source/ImGuiCore/Editor.cxx b/source/ImGuiCore/Editor.cxx index d31e41a..e99cc8a 100644 --- a/source/ImGuiCore/Editor.cxx +++ b/source/ImGuiCore/Editor.cxx @@ -72,7 +72,7 @@ LevelId Editor::addScene() { LevelId newScene = gSceneManager.registerLevel(); gSceneManager.setCurrentLevelId(newScene); - gSceneManager.getCurrentLevel().Init(); + // gSceneManager.getCurrentLevel().Init(); return newScene; } @@ -80,7 +80,7 @@ LevelId Editor::addScene(std::string name) { LevelId newScene = gSceneManager.registerLevel(name); gSceneManager.setCurrentLevelId(newScene); - gSceneManager.getCurrentLevel().Init(); + // gSceneManager.getCurrentLevel().Init(); return newScene; } diff --git a/source/ImGuiCore/EntityModule.cxx b/source/ImGuiCore/EntityModule.cxx index ed6ba82..cf64626 100644 --- a/source/ImGuiCore/EntityModule.cxx +++ b/source/ImGuiCore/EntityModule.cxx @@ -1,10 +1,13 @@ #include "ImGuiCore/EntityModule.hxx" #include +#include extern SceneManager gSceneManager; void EntityModule::create() { + auto &componentManager = gSceneManager.getCurrentLevel().getComponentManager(); + auto tagId = componentManager.GetComponentId("Tag").value(); if (gSceneManager.getCurrentLevelId() != currentScene) { _hasSelected = false; entitySelected = -1; @@ -12,8 +15,8 @@ void EntityModule::create() currentScene = gSceneManager.getCurrentLevelId(); ImGui::Begin("Entity"); createPopUp(); - for (auto const &[entity,_]: gSceneManager.getCurrentLevel().getEntities()) { - if (ImGui::Selectable(gSceneManager.getCurrentLevel().GetComponent(entity).name.c_str(), + for (auto const &[entity, _]: gSceneManager.getCurrentLevel().getEntities()) { + if (ImGui::Selectable(std::any_cast(componentManager.GetComponent(entity, tagId).value()).name.c_str(), entitySelected == entity)) { _hasSelected = true; entitySelected = entity; diff --git a/source/ImGuiCore/SystemsEditor.cxx b/source/ImGuiCore/SystemsEditor.cxx index d5a1339..d68dafa 100644 --- a/source/ImGuiCore/SystemsEditor.cxx +++ b/source/ImGuiCore/SystemsEditor.cxx @@ -3,7 +3,7 @@ void SystemsEditor::create() { ImGui::Begin("Systems"); - displaySystem("Physics system"); + displaySystem(); if (ImGui::Button("Add System")) { ImGui::OpenPopup("AddSystem"); } createPopUp(); ImGui::End(); @@ -11,10 +11,21 @@ void SystemsEditor::create() void SystemsEditor::createPopUp() { + auto &sm = gSceneManager.getCurrentLevel().getSystemManager(); if (ImGui::BeginPopup("AddSystem")) { - if (!gSceneManager.getCurrentLevel().hasSystem()) { - if (ImGui::MenuItem("Physics system")) { addSystem(); } + for (const auto &[name, description]: GlobalIndex::getSingleton()) { + auto used = sm.getSystemUsed(); + if (std::find(used.begin(), used.end(), name) == used.end()) + if (ImGui::MenuItem("Physics system")) sm.useSystem(name); } ImGui::EndPopup(); } -} \ No newline at end of file +} + +void SystemsEditor::displaySystem() +{ + auto &sm = gSceneManager.getCurrentLevel().getSystemManager(); + for (const auto &system: sm.getSystemUsed()) { + if (ImGui::TreeNode(system.c_str())) ImGui::TreePop(); + } +} diff --git a/source/Systems/PhysicsSystem.cxx b/source/Systems/PhysicsSystem.cxx index 4bbace8..f1d7e7b 100644 --- a/source/Systems/PhysicsSystem.cxx +++ b/source/Systems/PhysicsSystem.cxx @@ -36,3 +36,12 @@ Signature PhysicsSystem::getSignature() signature.set(gSceneManager.getCurrentLevel().GetComponentType()); return signature; } + +void physicsSystem(std::vector components) +{ + std::cout << "I'm a system with components:\n"; + for (const auto &component: components) std::cout << "\t" << component.type().name() << "\n"; + auto tag = std::any_cast>(components[1]); + std::cout << tag.get().name << std::endl; + tag.get().name = "oui"; +} diff --git a/source/main.cxx b/source/main.cxx index 3a51b91..cb0d523 100644 --- a/source/main.cxx +++ b/source/main.cxx @@ -15,6 +15,9 @@ #include #include +#include +#include + #include // #include "Scene.hxx" @@ -41,69 +44,79 @@ class Application : public VulkanApplication public: Application(): VulkanApplication(), editor(Editor()), camera(editor.getCamera()){}; - void addRandomObject(std::string object) - { - std::array textures = {"rouge", "vert", "bleu", "cyan", "orange", "jaune", "blanc", "violet"}; - std::random_device generator; - std::uniform_real_distribution randPositionY(0.0f, 50.0f); - std::uniform_real_distribution randPositionXZ(-50.0f, 50.0f); - std::uniform_real_distribution randRotation(0.0f, 3.0f); - std::uniform_real_distribution randColor(0.0f, 1.0f); - std::uniform_real_distribution randGravity(-10.0f, -1.0f); - std::uniform_real_distribution randVelocityY(10.0f, 200.0f); - std::uniform_real_distribution randVelocityXZ(-200.0f, 200.0f); - std::uniform_real_distribution randScale(0.5f, 1.0f); - std::uniform_int_distribution randTexture(0, textures.size() - 1); - auto newEntity = entity.addEntity(); - if (newEntity == 1997) - gSceneManager.getCurrentLevel().GetComponent(newEntity).name = - "Best Entity = " + std::to_string(newEntity); - else - gSceneManager.getCurrentLevel().GetComponent(newEntity).name = "Entity " + std::to_string(newEntity); - componentEditor.addComponent(newEntity, { - .force = glm::vec3(0.0f, randGravity(generator), 0.0f), - }); - componentEditor.addComponent( - newEntity, - { - .velocity = glm::vec3(randVelocityXZ(generator), randVelocityY(generator), randVelocityXZ(generator)), - .acceleration = glm::vec3(0.0f, 0.0f, 0.0f), - }); - glm::vec3 position = glm::vec3(randPositionXZ(generator), randPositionY(generator), randPositionXZ(generator)); - glm::vec3 rotation = glm::vec3(randRotation(generator), randRotation(generator), randRotation(generator)); - glm::vec3 scale = glm::vec3(randScale(generator)); - componentEditor.addComponent(newEntity, - { - .meshID = object, - .objectInformation = - { - .transform = Transform(position, rotation, scale), - .textureIndex = textures[randTexture(generator)], - .materialIndex = "white", - }, - }); - } - - void DemoScene() - { - editor.addScene("Demo"); - systemsEditor.addSystem(); - - std::vector entities(MAX_OBJECT - 1); - - for (auto &_entity: entities) { addRandomObject("cube"); } - } + // void addRandomObject(std::string object) + // { + // std::array textures = {"rouge", "vert", "bleu", "cyan", "orange", "jaune", "blanc", "violet"}; + // std::random_device generator; + // std::uniform_real_distribution randPositionY(0.0f, 50.0f); + // std::uniform_real_distribution randPositionXZ(-50.0f, 50.0f); + // std::uniform_real_distribution randRotation(0.0f, 3.0f); + // std::uniform_real_distribution randColor(0.0f, 1.0f); + // std::uniform_real_distribution randGravity(-10.0f, -1.0f); + // std::uniform_real_distribution randVelocityY(10.0f, 200.0f); + // std::uniform_real_distribution randVelocityXZ(-200.0f, 200.0f); + // std::uniform_real_distribution randScale(0.5f, 1.0f); + // std::uniform_int_distribution randTexture(0, textures.size() - 1); + // auto newEntity = entity.addEntity(); + // if (newEntity == 1997) + // gSceneManager.getCurrentLevel().GetComponent(newEntity).name = + // "Best Entity = " + std::to_string(newEntity); + // else + // gSceneManager.getCurrentLevel().GetComponent(newEntity).name = "Entity " + std::to_string(newEntity); + // componentEditor.addComponent(newEntity, { + // .force = glm::vec3(0.0f, randGravity(generator), 0.0f), + // }); + // componentEditor.addComponent( + // newEntity, + // { + // .velocity = glm::vec3(randVelocityXZ(generator), randVelocityY(generator), randVelocityXZ(generator)), + // .acceleration = glm::vec3(0.0f, 0.0f, 0.0f), + // }); + // glm::vec3 position = glm::vec3(randPositionXZ(generator), randPositionY(generator), randPositionXZ(generator)); + // glm::vec3 rotation = glm::vec3(randRotation(generator), randRotation(generator), randRotation(generator)); + // glm::vec3 scale = glm::vec3(randScale(generator)); + // componentEditor.addComponent(newEntity, + // { + // .meshID = object, + // .objectInformation = + // { + // .transform = Transform(position, rotation, scale), + // .textureIndex = textures[randTexture(generator)], + // .materialIndex = "white", + // }, + // }); + // } + + // void DemoScene() + // { + // editor.addScene("Demo"); + // systemsEditor.addSystem(); + + // std::vector entities(MAX_OBJECT - 1); + + // for (auto &_entity: entities) { addRandomObject("cube"); } + // } void loadScene() { LevelId defaultScene = editor.addScene("Default"); - DemoScene(); + // DemoScene(); gSceneManager.setCurrentLevelId(defaultScene); } void init() { - gSceneManager.Init(); + pivot::ecs::systems::Description description{ + .name = "Physics System", + .arguments = + { + "Gravity", + "RigidBody", + }, + .system = &physicsSystem, + }; + pivot::ecs::systems::GlobalIndex::getSingleton().registerSystem(description); + loadScene(); window.captureCursor(true); @@ -230,10 +243,10 @@ class Application : public VulkanApplication entity.hasSelected() ? componentEditor.create(entity.getEntitySelected()) : componentEditor.create(); systemsEditor.create(); - if (entity.hasSelected() && - gSceneManager.getCurrentLevel().hasComponent(entity.getEntitySelected())) { - editor.DisplayGuizmo(entity.getEntitySelected()); - } + // if (entity.hasSelected() && + // gSceneManager.getCurrentLevel().hasComponent(entity.getEntitySelected())) { + // editor.DisplayGuizmo(entity.getEntitySelected()); + // } } else { gSceneManager.getCurrentLevel().Update(dt); } From 2c0a122a207bd0b31b225c8cec4217e258638a06 Mon Sep 17 00:00:00 2001 From: Maxence Pellerin Date: Mon, 17 Jan 2022 19:47:34 +0100 Subject: [PATCH 2/6] feat(system): physicsSystem --- CMakeLists.txt | 2 +- include/Systems/PhysicsSystem.hxx | 2 +- source/Systems/PhysicsSystem.cxx | 15 +++++++++------ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 67f44e0..60abb6b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ if (LOCAL_PIVOT_DIRECTORY STREQUAL "") FetchContent_Declare( pivot GIT_REPOSITORY https://github.com/EIP-Pivot/pivot.git - GIT_TAG 2278a7a236e0c4b6470a2300258b98c4c013b3c5 + GIT_TAG 8b261ff4b663c27c931fd8b9bf1d59e644dc9326 ) if (NOT pivot_POPULATED) message(STATUS "Populating pivot") diff --git a/include/Systems/PhysicsSystem.hxx b/include/Systems/PhysicsSystem.hxx index 85e99f0..cc8c72b 100644 --- a/include/Systems/PhysicsSystem.hxx +++ b/include/Systems/PhysicsSystem.hxx @@ -13,4 +13,4 @@ public: Signature getSignature(); }; -void physicsSystem(std::vector components); \ No newline at end of file +void physicsSystem(std::vector> components); \ No newline at end of file diff --git a/source/Systems/PhysicsSystem.cxx b/source/Systems/PhysicsSystem.cxx index f1d7e7b..c5272ba 100644 --- a/source/Systems/PhysicsSystem.cxx +++ b/source/Systems/PhysicsSystem.cxx @@ -37,11 +37,14 @@ Signature PhysicsSystem::getSignature() return signature; } -void physicsSystem(std::vector components) +void physicsSystem(std::vector> entities) { - std::cout << "I'm a system with components:\n"; - for (const auto &component: components) std::cout << "\t" << component.type().name() << "\n"; - auto tag = std::any_cast>(components[1]); - std::cout << tag.get().name << std::endl; - tag.get().name = "oui"; + for (const auto &entity: entities) { + auto gravity = std::any_cast>(entity[0]); + auto rigidBody = std::any_cast>(entity[1]); + rigidBody.get().velocity += gravity.get().force * 0.16f; + } + // for (const auto &component: components) std::cout << "\t" << component.type().name() << "\n"; + // auto gravity = std::any_cast>(components[0]); + // auto rigidBody = std::any_cast>(components[1]); } From b0d14a0897131f6a951e63ada0dad88155ed23e6 Mon Sep 17 00:00:00 2001 From: Maxence Pellerin Date: Tue, 1 Feb 2022 19:45:28 +0100 Subject: [PATCH 3/6] feat(value): render data value --- CMakeLists.txt | 2 +- include/ImGuiCore/Editor.hxx | 2 +- include/ImGuiCore/TypeTemplate/Template.hxx | 28 +++--- .../ImGuiCore/TypeTemplate/createValue.hxx | 37 ++++++++ include/Systems/PhysicsSystem.hxx | 15 +--- source/ImGuiCore/ComponentEditor.cxx | 22 ++--- source/ImGuiCore/Editor.cxx | 26 +++--- source/ImGuiCore/EntityModule.cxx | 4 +- source/Systems/PhysicsSystem.cxx | 48 +---------- source/main.cxx | 86 ++++--------------- 10 files changed, 104 insertions(+), 166 deletions(-) create mode 100644 include/ImGuiCore/TypeTemplate/createValue.hxx diff --git a/CMakeLists.txt b/CMakeLists.txt index 60abb6b..257e668 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ if (LOCAL_PIVOT_DIRECTORY STREQUAL "") FetchContent_Declare( pivot GIT_REPOSITORY https://github.com/EIP-Pivot/pivot.git - GIT_TAG 8b261ff4b663c27c931fd8b9bf1d59e644dc9326 + GIT_TAG 8a2816866838ef557290cf2b48dcfa17fffb1539 ) if (NOT pivot_POPULATED) message(STATUS "Populating pivot") diff --git a/include/ImGuiCore/Editor.hxx b/include/ImGuiCore/Editor.hxx index d800262..9734b1e 100644 --- a/include/ImGuiCore/Editor.hxx +++ b/include/ImGuiCore/Editor.hxx @@ -5,7 +5,7 @@ #include #include -#include +#include class Editor { diff --git a/include/ImGuiCore/TypeTemplate/Template.hxx b/include/ImGuiCore/TypeTemplate/Template.hxx index 6e23dcb..f1990c8 100644 --- a/include/ImGuiCore/TypeTemplate/Template.hxx +++ b/include/ImGuiCore/TypeTemplate/Template.hxx @@ -3,35 +3,41 @@ #include #include #include +#include -#include #include +#include #include using namespace pivot::ecs::component; +using namespace pivot::ecs::data; -template -void draw(T &value, std::string name); - -#ifndef PIVOT_PROPERTY_DRAW_IMPLEMENTATION -#define PIVOT_PROPERTY_DRAW_IMPLEMENTATION - -template <> void draw(glm::vec3 &value, std::string name) { ImGui::InputFloat3(name.c_str(), glm::value_ptr(value)); } -template <> void draw(std::string &value, std::string name) { ImGui::InputText(name.c_str(), &value); } -template <> void draw(int &value, std::string name) { // ImGui::InputText("Name", &value); } -#endif \ No newline at end of file +void draw(bool &value, std::string name) +{ + // ImGui::InputText("Name", &value); +} + +void draw(double &value, std::string name) +{ + // ImGui::InputText("Name", &value); +} + +void draw(Record &values, std::string name) +{ + for (auto &[name, value]: values) std::visit([&name](auto &&arg) { draw(arg, name); }, value); +} \ No newline at end of file diff --git a/include/ImGuiCore/TypeTemplate/createValue.hxx b/include/ImGuiCore/TypeTemplate/createValue.hxx new file mode 100644 index 0000000..83731a8 --- /dev/null +++ b/include/ImGuiCore/TypeTemplate/createValue.hxx @@ -0,0 +1,37 @@ +#pragma once + +#include +#include +#include +#include + +#include +#include +#include + +using namespace pivot::ecs::component; +using namespace pivot::ecs::data; + +Value createValue(BasicType type) +{ + switch (type) { + case BasicType::String: return Value{""}; + case BasicType::Number: return Value{0.0}; + case BasicType::Integer: return Value{0}; + case BasicType::Boolean: return Value{false}; + case BasicType::Vec3: return Value{glm::vec3(0.0f)}; + } + throw std::runtime_error("Ilegal BasicType value."); +} + +Value createValue(RecordType types) +{ + Value value{Record{}}; + for (auto &[name, type]: types) + std::visit( + [&value, &name](auto &&arg) mutable { + std::get(value).insert({name, createValue(arg)}); + }, + type); + return value; +} \ No newline at end of file diff --git a/include/Systems/PhysicsSystem.hxx b/include/Systems/PhysicsSystem.hxx index cc8c72b..6f36eec 100644 --- a/include/Systems/PhysicsSystem.hxx +++ b/include/Systems/PhysicsSystem.hxx @@ -1,16 +1,7 @@ #pragma once -#include "pivot/ecs/Core/System.hxx" +#include -#include -#include +using namespace pivot::ecs; -class PhysicsSystem : public System -{ -public: - void Update(float dt); - - Signature getSignature(); -}; - -void physicsSystem(std::vector> components); \ No newline at end of file +void physicsSystem(const systems::Description &, const systems::Description::systemArgs &, const event::Event &); \ No newline at end of file diff --git a/source/ImGuiCore/ComponentEditor.cxx b/source/ImGuiCore/ComponentEditor.cxx index 92cad6b..be78433 100644 --- a/source/ImGuiCore/ComponentEditor.cxx +++ b/source/ImGuiCore/ComponentEditor.cxx @@ -1,5 +1,6 @@ #include "ImGuiCore/ComponentEditor.hxx" #include "ImGuiCore/TypeTemplate/Template.hxx" +#include "ImGuiCore/TypeTemplate/createValue.hxx" #include #include @@ -42,14 +43,13 @@ void ComponentEditor::createPopUp() void ComponentEditor::displayComponent() { auto &cm = gSceneManager.getCurrentLevel().getComponentManager(); - for (const auto &[description, component]: cm.GetAllComponents(currentEntity)) { + for (auto [description, component]: cm.GetAllComponents(currentEntity)) { if (ImGui::TreeNode(description.name.c_str())) { ImGui::TreePop(); ImGui::Indent(); - for (const auto &property: description.properties) { - std::visit([property](auto &&arg) { draw(arg, property.name); }, - description.getProperty(component, property.name)); - } + std::visit([](auto &&arg) { + draw(arg, "oui"); + }, component); ImGui::Unindent(); } } @@ -59,13 +59,7 @@ void ComponentEditor::addComponent(const Description &description) { auto &cm = gSceneManager.getCurrentLevel().getComponentManager(); auto id = cm.GetComponentId(description.name).value(); - std::map properties; - for (const auto &property: description.properties) { - switch (property.type) { - case Description::Property::Type::String: properties.insert({property.name, std::string("")}); break; - case Description::Property::Type::Vec3: properties.insert({property.name, glm::vec3(0.0f)}); break; - case Description::Property::Type::Number: properties.insert({property.name, 0}); break; - } - } - cm.AddComponent(currentEntity, description.create(properties), id); + Value newComponent; + std::visit([&newComponent](auto &&arg) mutable { newComponent = createValue(arg); }, description.type); + cm.AddComponent(currentEntity, newComponent, id); } diff --git a/source/ImGuiCore/Editor.cxx b/source/ImGuiCore/Editor.cxx index e99cc8a..10eed71 100644 --- a/source/ImGuiCore/Editor.cxx +++ b/source/ImGuiCore/Editor.cxx @@ -49,7 +49,7 @@ void Editor::create() } else { currentGizmoMode = ImGuizmo::LOCAL; } - ImGui::Checkbox("", &useSnap); + ImGui::Checkbox("##", &useSnap); ImGui::SameLine(); switch (currentGizmoOperation) { case ImGuizmo::TRANSLATE: ImGui::InputFloat3("Snap", &snap[0]); break; @@ -92,18 +92,18 @@ void Editor::setAspectRatio(float aspect) { aspectRatio = aspect; } void Editor::DisplayGuizmo(Entity entity) { - const auto view = camera.getView(); - const auto projection = camera.getProjection(80.9f, aspectRatio); + // const auto view = camera.getView(); + // const auto projection = camera.getProjection(80.9f, aspectRatio); - const float *view_ptr = glm::value_ptr(view); - const float *projection_ptr = glm::value_ptr(projection); - float *matrix = glm::value_ptr(gSceneManager.getCurrentLevel() - .GetComponent(entity) - .objectInformation.transform.getModelMatrix()); - ImGuiIO &io = ImGui::GetIO(); - ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y); - ImGuizmo::Manipulate(view_ptr, projection_ptr, currentGizmoOperation, currentGizmoMode, matrix, NULL, - useSnap ? &snap[0] : NULL); + // const float *view_ptr = glm::value_ptr(view); + // const float *projection_ptr = glm::value_ptr(projection); + // float *matrix = glm::value_ptr(gSceneManager.getCurrentLevel() + // .GetComponent(entity) + // .objectInformation.transform.getModelMatrix()); + // ImGuiIO &io = ImGui::GetIO(); + // ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y); + // ImGuizmo::Manipulate(view_ptr, projection_ptr, currentGizmoOperation, currentGizmoMode, matrix, NULL, + // useSnap ? &snap[0] : NULL); } void Editor::createPopUp() @@ -111,7 +111,7 @@ void Editor::createPopUp() if (ImGui::BeginPopup("AddScene")) { static std::string sceneName; ImGui::SetKeyboardFocusHere(); - if (ImGui::InputText("", &sceneName, ImGuiInputTextFlags_EnterReturnsTrue)) { + if (ImGui::InputText("##", &sceneName, ImGuiInputTextFlags_EnterReturnsTrue)) { if (sceneName.empty()) addScene(); else diff --git a/source/ImGuiCore/EntityModule.cxx b/source/ImGuiCore/EntityModule.cxx index cf64626..9dd42f8 100644 --- a/source/ImGuiCore/EntityModule.cxx +++ b/source/ImGuiCore/EntityModule.cxx @@ -16,7 +16,7 @@ void EntityModule::create() ImGui::Begin("Entity"); createPopUp(); for (auto const &[entity, _]: gSceneManager.getCurrentLevel().getEntities()) { - if (ImGui::Selectable(std::any_cast(componentManager.GetComponent(entity, tagId).value()).name.c_str(), + if (ImGui::Selectable(std::get(std::get(componentManager.GetComponent(entity, tagId).value()).at("name")).c_str(), entitySelected == entity)) { _hasSelected = true; entitySelected = entity; @@ -63,7 +63,7 @@ void EntityModule::createPopUp() if (ImGui::BeginPopup("NewEntity")) { static std::string entityName; ImGui::SetKeyboardFocusHere(); - if (ImGui::InputText("", &entityName, ImGuiInputTextFlags_EnterReturnsTrue)) { + if (ImGui::InputText("##", &entityName, ImGuiInputTextFlags_EnterReturnsTrue)) { if (entityName.empty()) addEntity(); else diff --git a/source/Systems/PhysicsSystem.cxx b/source/Systems/PhysicsSystem.cxx index c5272ba..6976187 100644 --- a/source/Systems/PhysicsSystem.cxx +++ b/source/Systems/PhysicsSystem.cxx @@ -1,50 +1,8 @@ #include "Systems/PhysicsSystem.hxx" -#include "pivot/ecs/Components/Gravity.hxx" -#include "pivot/ecs/Components/RigidBody.hxx" -#include -#include +using namespace pivot::ecs; -extern SceneManager gSceneManager; - -void PhysicsSystem::Update(float dt) -{ - for (auto const &entity: mEntities) { - auto &rigidBody = gSceneManager.getCurrentLevel().GetComponent(entity); - auto &renderObject = gSceneManager.getCurrentLevel().GetComponent(entity); - - // Forces - auto const &gravity = gSceneManager.getCurrentLevel().GetComponent(entity); - - renderObject.objectInformation.transform.addPosition(rigidBody.velocity * dt); - - rigidBody.velocity += gravity.force * dt; - } -} - -Signature PhysicsSystem::getSignature() -{ - if (!gSceneManager.getCurrentLevel().isRegister()) - gSceneManager.getCurrentLevel().RegisterComponent(); - if (!gSceneManager.getCurrentLevel().isRegister()) - gSceneManager.getCurrentLevel().RegisterComponent(); - if (!gSceneManager.getCurrentLevel().isRegister()) - gSceneManager.getCurrentLevel().RegisterComponent(); - Signature signature; - signature.set(gSceneManager.getCurrentLevel().GetComponentType()); - signature.set(gSceneManager.getCurrentLevel().GetComponentType()); - signature.set(gSceneManager.getCurrentLevel().GetComponentType()); - return signature; -} - -void physicsSystem(std::vector> entities) +void physicsSystem(const systems::Description &, const systems::Description::systemArgs &, const event::Event &) { - for (const auto &entity: entities) { - auto gravity = std::any_cast>(entity[0]); - auto rigidBody = std::any_cast>(entity[1]); - rigidBody.get().velocity += gravity.get().force * 0.16f; - } - // for (const auto &component: components) std::cout << "\t" << component.type().name() << "\n"; - // auto gravity = std::any_cast>(components[0]); - // auto rigidBody = std::any_cast>(components[1]); + } diff --git a/source/main.cxx b/source/main.cxx index cb0d523..e776fc6 100644 --- a/source/main.cxx +++ b/source/main.cxx @@ -18,6 +18,9 @@ #include #include +#include +#include + #include // #include "Scene.hxx" @@ -37,82 +40,35 @@ #include "FrameLimiter.hpp" -Logger *logger = nullptr; - -class Application : public VulkanApplication +class Application : public pivot::graphics::VulkanApplication { public: Application(): VulkanApplication(), editor(Editor()), camera(editor.getCamera()){}; - // void addRandomObject(std::string object) - // { - // std::array textures = {"rouge", "vert", "bleu", "cyan", "orange", "jaune", "blanc", "violet"}; - // std::random_device generator; - // std::uniform_real_distribution randPositionY(0.0f, 50.0f); - // std::uniform_real_distribution randPositionXZ(-50.0f, 50.0f); - // std::uniform_real_distribution randRotation(0.0f, 3.0f); - // std::uniform_real_distribution randColor(0.0f, 1.0f); - // std::uniform_real_distribution randGravity(-10.0f, -1.0f); - // std::uniform_real_distribution randVelocityY(10.0f, 200.0f); - // std::uniform_real_distribution randVelocityXZ(-200.0f, 200.0f); - // std::uniform_real_distribution randScale(0.5f, 1.0f); - // std::uniform_int_distribution randTexture(0, textures.size() - 1); - // auto newEntity = entity.addEntity(); - // if (newEntity == 1997) - // gSceneManager.getCurrentLevel().GetComponent(newEntity).name = - // "Best Entity = " + std::to_string(newEntity); - // else - // gSceneManager.getCurrentLevel().GetComponent(newEntity).name = "Entity " + std::to_string(newEntity); - // componentEditor.addComponent(newEntity, { - // .force = glm::vec3(0.0f, randGravity(generator), 0.0f), - // }); - // componentEditor.addComponent( - // newEntity, - // { - // .velocity = glm::vec3(randVelocityXZ(generator), randVelocityY(generator), randVelocityXZ(generator)), - // .acceleration = glm::vec3(0.0f, 0.0f, 0.0f), - // }); - // glm::vec3 position = glm::vec3(randPositionXZ(generator), randPositionY(generator), randPositionXZ(generator)); - // glm::vec3 rotation = glm::vec3(randRotation(generator), randRotation(generator), randRotation(generator)); - // glm::vec3 scale = glm::vec3(randScale(generator)); - // componentEditor.addComponent(newEntity, - // { - // .meshID = object, - // .objectInformation = - // { - // .transform = Transform(position, rotation, scale), - // .textureIndex = textures[randTexture(generator)], - // .materialIndex = "white", - // }, - // }); - // } - - // void DemoScene() - // { - // editor.addScene("Demo"); - // systemsEditor.addSystem(); - - // std::vector entities(MAX_OBJECT - 1); - - // for (auto &_entity: entities) { addRandomObject("cube"); } - // } - + void loadScene() { LevelId defaultScene = editor.addScene("Default"); - // DemoScene(); gSceneManager.setCurrentLevelId(defaultScene); } void init() { + event::Description tick { + .name = "Tick", + .entities = {}, + .payload = pivot::ecs::data::BasicType::Number, + }; + pivot::ecs::event::GlobalIndex::getSingleton().registerEvent(tick); + pivot::ecs::systems::Description description{ .name = "Physics System", - .arguments = + .systemComponents = { "Gravity", "RigidBody", }, + .eventListener = tick, .system = &physicsSystem, }; pivot::ecs::systems::GlobalIndex::getSingleton().registerSystem(description); @@ -169,9 +125,9 @@ class Application : public VulkanApplication last = pos; ControlSystem::processMouseMovement(camera, glm::dvec2(xoffset, yoffset)); }); - load3DModels({"../assets/plane.obj", "../assets/cube.obj"}); - loadTextures({"../assets/rouge.png", "../assets/vert.png", "../assets/bleu.png", "../assets/cyan.png", - "../assets/orange.png", "../assets/jaune.png", "../assets/blanc.png", "../assets/violet.png"}); + assetStorage.loadModels("../assets/plane.obj", "../assets/cube.obj"); + assetStorage.loadTextures("../assets/rouge.png", "../assets/vert.png", "../assets/bleu.png", "../assets/cyan.png", + "../assets/orange.png", "../assets/jaune.png", "../assets/blanc.png", "../assets/violet.png"); } void processKeyboard(const Camera::Movement direction, float dt) noexcept { @@ -248,7 +204,7 @@ class Application : public VulkanApplication // editor.DisplayGuizmo(entity.getEntitySelected()); // } } else { - gSceneManager.getCurrentLevel().Update(dt); + gSceneManager.getCurrentLevel().getEventManager().sendEvent("Tick", data::Value(dt)); } UpdateCamera(dt); @@ -288,15 +244,11 @@ class Application : public VulkanApplication int main() try { - logger = new Logger(std::cout); - logger->start(); - Application app; app.init(); app.run(); return 0; } catch (std::exception &e) { - logger->err("THROW") << e.what(); - LOGGER_ENDL; + logger.err("THROW") << e.what(); return 1; } From a9f029d6a03afccea3d3528aac19120394e45a57 Mon Sep 17 00:00:00 2001 From: Maxence Pellerin Date: Sat, 12 Feb 2022 17:17:02 +0100 Subject: [PATCH 4/6] feat(system): add physic algo --- CMakeLists.txt | 2 +- include/Systems/PhysicsSystem.hxx | 2 +- source/ImGuiCore/SystemsEditor.cxx | 2 +- source/Systems/PhysicsSystem.cxx | 17 +++++++++++++++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 257e668..d093dbf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ if (LOCAL_PIVOT_DIRECTORY STREQUAL "") FetchContent_Declare( pivot GIT_REPOSITORY https://github.com/EIP-Pivot/pivot.git - GIT_TAG 8a2816866838ef557290cf2b48dcfa17fffb1539 + GIT_TAG 1250d81385ef5371ecc2b6c274dc7614234fdcbb ) if (NOT pivot_POPULATED) message(STATUS "Populating pivot") diff --git a/include/Systems/PhysicsSystem.hxx b/include/Systems/PhysicsSystem.hxx index 6f36eec..e2abfc0 100644 --- a/include/Systems/PhysicsSystem.hxx +++ b/include/Systems/PhysicsSystem.hxx @@ -4,4 +4,4 @@ using namespace pivot::ecs; -void physicsSystem(const systems::Description &, const systems::Description::systemArgs &, const event::Event &); \ No newline at end of file +void physicsSystem(const systems::Description &, systems::Description::systemArgs &, const event::Event &); \ No newline at end of file diff --git a/source/ImGuiCore/SystemsEditor.cxx b/source/ImGuiCore/SystemsEditor.cxx index d68dafa..778b921 100644 --- a/source/ImGuiCore/SystemsEditor.cxx +++ b/source/ImGuiCore/SystemsEditor.cxx @@ -16,7 +16,7 @@ void SystemsEditor::createPopUp() for (const auto &[name, description]: GlobalIndex::getSingleton()) { auto used = sm.getSystemUsed(); if (std::find(used.begin(), used.end(), name) == used.end()) - if (ImGui::MenuItem("Physics system")) sm.useSystem(name); + if (ImGui::MenuItem(name.c_str())) sm.useSystem(name); } ImGui::EndPopup(); } diff --git a/source/Systems/PhysicsSystem.cxx b/source/Systems/PhysicsSystem.cxx index 6976187..1ad83fa 100644 --- a/source/Systems/PhysicsSystem.cxx +++ b/source/Systems/PhysicsSystem.cxx @@ -2,7 +2,20 @@ using namespace pivot::ecs; -void physicsSystem(const systems::Description &, const systems::Description::systemArgs &, const event::Event &) +void physicsSystem(const systems::Description &systemDescription, systems::Description::systemArgs &entities, const event::Event &event) { - + auto dt = (float)std::get(event.payload); + for (auto combination: entities) { + auto gravity = combination[0].get(); + auto rigidBody = combination[1].get(); + + auto &force = std::get(std::get(gravity).at("force")); + force.x += 1; + + combination[0].set(gravity); + + auto &velocity = std::get(std::get(rigidBody).at("velocity")); + velocity += force * dt; + combination[1].set(rigidBody); + } } From aa59d6e38f6bea636df4343bb236437d9d5c2a4c Mon Sep 17 00:00:00 2001 From: Ersikan Date: Sun, 13 Feb 2022 13:00:32 +0100 Subject: [PATCH 5/6] refactor: better variant usage. Works on gcc --- include/ImGuiCore/TypeTemplate/Template.hxx | 20 ++++++------- .../ImGuiCore/TypeTemplate/createValue.hxx | 29 +++++++++++-------- source/ImGuiCore/ComponentEditor.cxx | 7 ++--- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/include/ImGuiCore/TypeTemplate/Template.hxx b/include/ImGuiCore/TypeTemplate/Template.hxx index f1990c8..ed78dbb 100644 --- a/include/ImGuiCore/TypeTemplate/Template.hxx +++ b/include/ImGuiCore/TypeTemplate/Template.hxx @@ -12,15 +12,10 @@ using namespace pivot::ecs::component; using namespace pivot::ecs::data; -void draw(glm::vec3 &value, std::string name) -{ - ImGui::InputFloat3(name.c_str(), glm::value_ptr(value)); -} +void draw(Value &value, std::string name); +void draw(glm::vec3 &value, std::string name) { ImGui::InputFloat3(name.c_str(), glm::value_ptr(value)); } -void draw(std::string &value, std::string name) -{ - ImGui::InputText(name.c_str(), &value); -} +void draw(std::string &value, std::string name) { ImGui::InputText(name.c_str(), &value); } void draw(int &value, std::string name) { @@ -39,5 +34,10 @@ void draw(double &value, std::string name) void draw(Record &values, std::string name) { - for (auto &[name, value]: values) std::visit([&name](auto &&arg) { draw(arg, name); }, value); -} \ No newline at end of file + for (auto &[name, value]: values) draw(value, name); +} + +void draw(Value &value, std::string name) +{ + std::visit([&name](auto &&arg) { draw(arg, name); }, static_cast(value)); +} diff --git a/include/ImGuiCore/TypeTemplate/createValue.hxx b/include/ImGuiCore/TypeTemplate/createValue.hxx index 83731a8..349f584 100644 --- a/include/ImGuiCore/TypeTemplate/createValue.hxx +++ b/include/ImGuiCore/TypeTemplate/createValue.hxx @@ -12,7 +12,15 @@ using namespace pivot::ecs::component; using namespace pivot::ecs::data; -Value createValue(BasicType type) +Value createValue(const BasicType &type); +Value createValue(const RecordType &types); + +Value createValue(const Type &type) +{ + return std::visit([](auto &t) {return createValue(t);}, static_cast(type)); +} + +Value createValue(const BasicType &type) { switch (type) { case BasicType::String: return Value{""}; @@ -21,17 +29,14 @@ Value createValue(BasicType type) case BasicType::Boolean: return Value{false}; case BasicType::Vec3: return Value{glm::vec3(0.0f)}; } - throw std::runtime_error("Ilegal BasicType value."); + throw std::runtime_error("Illegal BasicType value."); } -Value createValue(RecordType types) +Value createValue(const RecordType &types) { - Value value{Record{}}; - for (auto &[name, type]: types) - std::visit( - [&value, &name](auto &&arg) mutable { - std::get(value).insert({name, createValue(arg)}); - }, - type); - return value; -} \ No newline at end of file + Record record; + for (auto &[name, type]: types) { + record.insert({name, createValue(type)}); + } + return {record}; +} diff --git a/source/ImGuiCore/ComponentEditor.cxx b/source/ImGuiCore/ComponentEditor.cxx index be78433..e886a24 100644 --- a/source/ImGuiCore/ComponentEditor.cxx +++ b/source/ImGuiCore/ComponentEditor.cxx @@ -47,9 +47,7 @@ void ComponentEditor::displayComponent() if (ImGui::TreeNode(description.name.c_str())) { ImGui::TreePop(); ImGui::Indent(); - std::visit([](auto &&arg) { - draw(arg, "oui"); - }, component); + draw(component, "oui"); ImGui::Unindent(); } } @@ -59,7 +57,6 @@ void ComponentEditor::addComponent(const Description &description) { auto &cm = gSceneManager.getCurrentLevel().getComponentManager(); auto id = cm.GetComponentId(description.name).value(); - Value newComponent; - std::visit([&newComponent](auto &&arg) mutable { newComponent = createValue(arg); }, description.type); + Value newComponent = createValue(description.type); cm.AddComponent(currentEntity, newComponent, id); } From d3065bfe4a8a48a63114ab319710ac71517d35ff Mon Sep 17 00:00:00 2001 From: Maxence Pellerin Date: Fri, 25 Mar 2022 11:07:29 +0100 Subject: [PATCH 6/6] feat(script): add script --- CMakeLists.txt | 2 +- include/ImGuiCore/TypeTemplate/Template.hxx | 1 + scripts/components.pvt | 26 +++++++++++++++++++++ source/ImGuiCore/ComponentEditor.cxx | 4 ++++ source/Systems/PhysicsSystem.cxx | 13 ++++++++--- source/main.cxx | 8 +++++++ 6 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 scripts/components.pvt diff --git a/CMakeLists.txt b/CMakeLists.txt index d093dbf..9abd3c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ if (LOCAL_PIVOT_DIRECTORY STREQUAL "") FetchContent_Declare( pivot GIT_REPOSITORY https://github.com/EIP-Pivot/pivot.git - GIT_TAG 1250d81385ef5371ecc2b6c274dc7614234fdcbb + GIT_TAG c1f3b50a477909dfbdaf800794096d8375089656 ) if (NOT pivot_POPULATED) message(STATUS "Populating pivot") diff --git a/include/ImGuiCore/TypeTemplate/Template.hxx b/include/ImGuiCore/TypeTemplate/Template.hxx index ed78dbb..94cc7e7 100644 --- a/include/ImGuiCore/TypeTemplate/Template.hxx +++ b/include/ImGuiCore/TypeTemplate/Template.hxx @@ -29,6 +29,7 @@ void draw(bool &value, std::string name) void draw(double &value, std::string name) { + ImGui::InputDouble(name.c_str(), &value); // ImGui::InputText("Name", &value); } diff --git a/scripts/components.pvt b/scripts/components.pvt new file mode 100644 index 0000000..ca0e597 --- /dev/null +++ b/scripts/components.pvt @@ -0,0 +1,26 @@ +component Najo + Vector3 position + Number note + +component Thomas + String leopard + Number QI + +component ZAC + Number PEPETTE + Number QI + +component Position + Number pos_x + Number pos_y + Number pos_z + +component Velocity + Number vel_x + Number vel_y + Number vel_z + +system onTickPhysics(anyEntity) + anyEntity.Position.pos_x = anyEntity.Position.pos_x + anyEntity.Velocity.vel_x + anyEntity.Position.pos_y = anyEntity.Position.pos_y + anyEntity.Velocity.vel_y + anyEntity.Position.pos_z = anyEntity.Position.pos_z + anyEntity.Velocity.vel_z \ No newline at end of file diff --git a/source/ImGuiCore/ComponentEditor.cxx b/source/ImGuiCore/ComponentEditor.cxx index e886a24..2b478b2 100644 --- a/source/ImGuiCore/ComponentEditor.cxx +++ b/source/ImGuiCore/ComponentEditor.cxx @@ -44,10 +44,14 @@ void ComponentEditor::displayComponent() { auto &cm = gSceneManager.getCurrentLevel().getComponentManager(); for (auto [description, component]: cm.GetAllComponents(currentEntity)) { + auto id = cm.GetComponentId(description.name).value(); + auto &compArray = cm.GetComponentArray(id).value().get(); + ComponentRef compRef(compArray, currentEntity); if (ImGui::TreeNode(description.name.c_str())) { ImGui::TreePop(); ImGui::Indent(); draw(component, "oui"); + compRef.set(component); ImGui::Unindent(); } } diff --git a/source/Systems/PhysicsSystem.cxx b/source/Systems/PhysicsSystem.cxx index 1ad83fa..ce47035 100644 --- a/source/Systems/PhysicsSystem.cxx +++ b/source/Systems/PhysicsSystem.cxx @@ -8,14 +8,21 @@ void physicsSystem(const systems::Description &systemDescription, systems::Descr for (auto combination: entities) { auto gravity = combination[0].get(); auto rigidBody = combination[1].get(); + auto najo = combination[2].get(); + auto thomas = combination[3].get(); auto &force = std::get(std::get(gravity).at("force")); - force.x += 1; - - combination[0].set(gravity); auto &velocity = std::get(std::get(rigidBody).at("velocity")); velocity += force * dt; combination[1].set(rigidBody); + + auto ¬e = std::get(std::get(najo).at("note")); + note += 1 * dt; + combination[2].set(najo); + + auto &qi = std::get(std::get(thomas).at("QI")); + qi += note * dt; + combination[3].set(thomas); } } diff --git a/source/main.cxx b/source/main.cxx index e776fc6..5961823 100644 --- a/source/main.cxx +++ b/source/main.cxx @@ -27,6 +27,7 @@ #include "Systems/PhysicsSystem.hxx" #include #include +#include #include #include @@ -54,6 +55,7 @@ class Application : public pivot::graphics::VulkanApplication void init() { + loadResult = _engine.loadFile("../scripts/components.pvt", true); event::Description tick { .name = "Tick", .entities = {}, @@ -67,6 +69,7 @@ class Application : public pivot::graphics::VulkanApplication { "Gravity", "RigidBody", + "Najo", }, .eventListener = tick, .system = &physicsSystem, @@ -192,6 +195,9 @@ class Application : public pivot::graphics::VulkanApplication imGuiManager.newFrame(); + ImGui::Begin("Message"); + ImGui::Text("- %s", loadResult.output.c_str()); + ImGui::End(); editor.create(); if (!editor.getRun()) { editor.setAspectRatio(getAspectRatio()); @@ -229,6 +235,8 @@ class Application : public pivot::graphics::VulkanApplication } public: + pivot::ecs::script::ScriptEngine _engine; + pivot::ecs::script::LoadResult loadResult; ImGuiManager imGuiManager; Editor editor; EntityModule entity;