diff --git a/editor/include/editor.hpp b/editor/include/editor.hpp index 4b7722d..db4373b 100755 --- a/editor/include/editor.hpp +++ b/editor/include/editor.hpp @@ -14,7 +14,6 @@ #include #include #include - #include #include @@ -148,6 +147,7 @@ namespace OpenEngine { clicked = true; } else { clicked = false; + selected_entity = scene_hierarchy.GetSelectedEntity(); } framebuffer->Unbind(); @@ -310,7 +310,7 @@ namespace OpenEngine { glm::mat4 camera_view = editor_camera.GetViewMatrix(); auto& transform_comp = selected_entity.GetComponents(); - glm::mat4 transform = transform_comp.GetTransform(); + glm::mat4 transform = GetTransformFromComp(transform_comp); bool snap = Input::IsKeyPressed(KeyCode::LeftControl); float snap_value = 0.1f; @@ -333,7 +333,8 @@ namespace OpenEngine { transform_comp.rotation += delta_rotation; transform_comp.scale = scale; } - } + } else + guizmo_operation = -1; }; void OnImGuiRender() override diff --git a/editor/include/editor_component.hpp b/editor/include/editor_component.hpp index 36a5a14..df2beb6 100644 --- a/editor/include/editor_component.hpp +++ b/editor/include/editor_component.hpp @@ -1,18 +1,16 @@ #ifndef EDITOR_COMPONENT_HPP #define EDITOR_COMPONENT_HPP -#include "open_engine/scene/components.hpp" - +#include #include +#include #include namespace OpenEngine { - /* void DrawVec3Control(const char* label, glm::vec3& values, float reset_value = 0.0f, float column_width = 100.0f, const std::array labels = {"x", "y", "z"}); - */ void TagOnImGuiRender(entt::registry& registry, entt::entity entity); @@ -21,7 +19,6 @@ namespace OpenEngine { void SpriteOnImGuiRender(entt::registry& registry, entt::entity entity); void CameraOnImGuiRender(entt::registry& registry, entt::entity entity); - } #endif // EDITOR_COMPONENT_HPP diff --git a/editor/include/panels/scene_hierarchy.hpp b/editor/include/panels/scene_hierarchy.hpp index ac0ad1f..de15b38 100644 --- a/editor/include/panels/scene_hierarchy.hpp +++ b/editor/include/panels/scene_hierarchy.hpp @@ -32,49 +32,6 @@ namespace OpenEngine { void DrawEntityNode(Entity& entity); void DrawComponents(Entity& entity); - /* - template - void DrawComponentDrawer(Entity& entity, const char* header) - { - bool component_marked_deletion = false; - ImVec2 region_available = ImGui::GetContentRegionAvail(); - ImGuiTreeNodeFlags tree_node_flags = ImGuiTreeNodeFlags_DefaultOpen - | ImGuiTreeNodeFlags_Framed - | ImGuiTreeNodeFlags_AllowOverlap; - if (entity.HasComponent()) { - ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{ 4, 4 }); - float line_height = ImGui::GetFontSize() + ImGui::GetStyle().FramePadding.y * 2.0f; - - bool opened = ImGui::TreeNodeEx((void*)typeid(T).hash_code(), tree_node_flags, "%s", header); - ImGui::SameLine(region_available.x - line_height * 0.5f); - - ImGui::PushStyleColor(ImGuiCol_Button, { 0.290f, 0.301f, 0.388f, 1.0f }); - if (ImGui::Button("...", ImVec2{ line_height, line_height })) - ImGui::OpenPopup("component_settings"); - ImGui::PopStyleColor(); - - ImGui::PopStyleVar(); - - if (ImGui::BeginPopup("component_settings")) { - if (ImGui::MenuItem("Remove component")) - component_marked_deletion = true; - - ImGui::EndPopup(); - } - - if (opened) { - entity.GetComponents().OnImGuiRender(entity); - ImGui::TreePop(); - } - - ImGui::Separator(); - - } - if (component_marked_deletion) - entity.RemoveComponents(); - } - */ - private: template static void RegisterDrawer(const std::string& name, std::function func) { diff --git a/editor/src/editor_component.cpp b/editor/src/editor_component.cpp index 5434c99..e7f16f7 100644 --- a/editor/src/editor_component.cpp +++ b/editor/src/editor_component.cpp @@ -3,6 +3,82 @@ namespace OpenEngine { + void DrawVec3Control(const char* label, glm::vec3& values, + float reset_value, float column_width, + const std::array labels) + { + ImGuiIO& io = ImGui::GetIO(); + auto bold_font = io.Fonts->Fonts[0]; + + ImGui::PushID(label); + ImVec2 item_spacing = { 15.0f, 0.0f }; + ImGui::Columns(2); + ImGui::SetColumnWidth(0, column_width); + ImGui::Text("%s", label); + if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && ImGui::IsItemHovered()) + values = glm::vec3(reset_value); + ImGui::NextColumn(); + + ImGui::PushMultiItemsWidths(3, ImGui::CalcItemWidth()); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2{ 0, 0 }); + + float line_height = ImGui::GetFontSize() + ImGui::GetStyle().FramePadding.y * 2.0f; + ImVec2 button_size = { line_height + 3.0f, line_height }; + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.953f, 0.545f, 0.659f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 1.0f, 0.8f, 0.9f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.953f, 0.545f, 0.659f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4{ 0.0f, 0.0f, 0.0f, 1.0f }); + ImGui::PushFont(bold_font); + if (ImGui::Button(labels[0], button_size)) + values.x = reset_value; + ImGui::PopFont(); + ImGui::PopStyleColor(4); + + ImGui::SameLine(); + ImGui::DragFloat("##X", &values.x, 0.1f, 0.0f, 0.0f, "%.2f"); + ImGui::PopItemWidth(); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, item_spacing); + ImGui::SameLine(); + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.650f, 0.890f, 0.631f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.9f, 1.0f, 0.9f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.650f, 0.890f, 0.631f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4{ 0.0f, 0.0f, 0.0f, 1.0f }); + ImGui::PushFont(bold_font); + if (ImGui::Button(labels[1], button_size)) + values.y = reset_value; + ImGui::PopFont(); + ImGui::PopStyleColor(4); + ImGui::PopStyleVar(); + + ImGui::SameLine(); + ImGui::DragFloat("##Y", &values.y, 0.1f, 0.0f, 0.0f, "%.2f"); + ImGui::PopItemWidth(); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, item_spacing); + ImGui::SameLine(); + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.533f, 0.698f, 0.976f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.7f, 0.9f, 1.0f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.533f, 0.698f, 0.976f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_Text, ImVec4{ 0.0f, 0.0f, 0.0f, 1.0f }); + ImGui::PushFont(bold_font); + if (ImGui::Button(labels[2], button_size)) + values.z = reset_value; + ImGui::PopFont(); + ImGui::PopStyleColor(4); + ImGui::PopStyleVar(1); + + ImGui::SameLine(); + ImGui::DragFloat("##Z", &values.z, 0.1f, 0.0f, 0.0f, "%.2f"); + ImGui::PopItemWidth(); + + ImGui::PopStyleVar(); + + ImGui::Columns(1); + ImGui::PopID(); + } + void TagOnImGuiRender(entt::registry& registry, entt::entity entity) { char buffer[256]; diff --git a/editor/src/panels/scene_hierarchy.cpp b/editor/src/panels/scene_hierarchy.cpp index 7a23b74..8245820 100644 --- a/editor/src/panels/scene_hierarchy.cpp +++ b/editor/src/panels/scene_hierarchy.cpp @@ -196,7 +196,7 @@ namespace OpenEngine { auto& reg = scene->GetRegistry(); entt::entity handle = selected_context; - if (!selected_context) + if (!selected_context || !entity) return; entity.GetComponents(); @@ -222,7 +222,6 @@ namespace OpenEngine { if (ImGui::Button("...", ImVec2{ line_height, line_height })) ImGui::OpenPopup("component_settings"); ImGui::PopStyleColor(); - ImGui::PopStyleVar(); if (ImGui::BeginPopup("component_settings")) { diff --git a/open_engine/include/open_engine/scene/components.hpp b/open_engine/include/open_engine/scene/components.hpp index a33e779..d0d7065 100644 --- a/open_engine/include/open_engine/scene/components.hpp +++ b/open_engine/include/open_engine/scene/components.hpp @@ -4,7 +4,6 @@ #include "open_engine/scene/native_scriptable_entity.hpp" #include "open_engine/scene/scene_camera.hpp" -#include #include #include #include @@ -19,10 +18,6 @@ namespace OpenEngine { - void DrawVec3Control(const char* label, glm::vec3& values, - float reset_value = 0.0f, float column_width = 100.0f, - const std::array labels = {"x", "y", "z"}); - struct TagComponent { std::string tag; @@ -43,18 +38,10 @@ namespace OpenEngine { TransformComponent(const TransformComponent&) = default; TransformComponent(const glm::vec3& position) : translation(position) {} - - glm::mat4 GetTransform() const - { - glm::mat4 transform = glm::translate(glm::mat4(1.0f), translation); - - transform *= glm::toMat4(glm::quat(rotation)); - transform *= glm::scale(glm::mat4(1.0f), scale); - - return transform; - }; }; + glm::mat4 GetTransformFromComp(TransformComponent& transform_comp); + struct SpriteRendererComponent { glm::vec4 color{ 1.0f, 1.0f, 1.0f, 1.0f }; diff --git a/open_engine/src/open_engine/scene/components.cpp b/open_engine/src/open_engine/scene/components.cpp index d65f97b..248f5dc 100644 --- a/open_engine/src/open_engine/scene/components.cpp +++ b/open_engine/src/open_engine/scene/components.cpp @@ -1,83 +1,16 @@ -#include "imgui.h" #include #include namespace OpenEngine { - void DrawVec3Control(const char* label, glm::vec3& values, - float reset_value, float column_width, - const std::array labels) + glm::mat4 GetTransformFromComp(TransformComponent& transform_comp) { - ImGuiIO& io = ImGui::GetIO(); - auto bold_font = io.Fonts->Fonts[0]; + glm::mat4 transform = glm::translate(glm::mat4(1.0f), transform_comp.translation); - ImGui::PushID(label); - ImVec2 item_spacing = { 15.0f, 0.0f }; - ImGui::Columns(2); - ImGui::SetColumnWidth(0, column_width); - ImGui::Text("%s", label); - if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && ImGui::IsItemHovered()) - values = glm::vec3(reset_value); - ImGui::NextColumn(); + transform *= glm::toMat4(glm::quat(transform_comp.rotation)); + transform *= glm::scale(glm::mat4(1.0f), transform_comp.scale); - ImGui::PushMultiItemsWidths(3, ImGui::CalcItemWidth()); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2{ 0, 0 }); - - float line_height = ImGui::GetFontSize() + ImGui::GetStyle().FramePadding.y * 2.0f; - ImVec2 button_size = { line_height + 3.0f, line_height }; - - ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.953f, 0.545f, 0.659f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 1.0f, 0.8f, 0.9f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.953f, 0.545f, 0.659f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4{ 0.0f, 0.0f, 0.0f, 1.0f }); - ImGui::PushFont(bold_font); - if (ImGui::Button(labels[0], button_size)) - values.x = reset_value; - ImGui::PopFont(); - ImGui::PopStyleColor(4); - - ImGui::SameLine(); - ImGui::DragFloat("##X", &values.x, 0.1f, 0.0f, 0.0f, "%.2f"); - ImGui::PopItemWidth(); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, item_spacing); - ImGui::SameLine(); - - ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.650f, 0.890f, 0.631f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.9f, 1.0f, 0.9f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.650f, 0.890f, 0.631f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4{ 0.0f, 0.0f, 0.0f, 1.0f }); - ImGui::PushFont(bold_font); - if (ImGui::Button(labels[1], button_size)) - values.y = reset_value; - ImGui::PopFont(); - ImGui::PopStyleColor(4); - ImGui::PopStyleVar(); - - ImGui::SameLine(); - ImGui::DragFloat("##Y", &values.y, 0.1f, 0.0f, 0.0f, "%.2f"); - ImGui::PopItemWidth(); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, item_spacing); - ImGui::SameLine(); - - ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.533f, 0.698f, 0.976f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.7f, 0.9f, 1.0f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.533f, 0.698f, 0.976f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_Text, ImVec4{ 0.0f, 0.0f, 0.0f, 1.0f }); - ImGui::PushFont(bold_font); - if (ImGui::Button(labels[2], button_size)) - values.z = reset_value; - ImGui::PopFont(); - ImGui::PopStyleColor(4); - ImGui::PopStyleVar(1); - - ImGui::SameLine(); - ImGui::DragFloat("##Z", &values.z, 0.1f, 0.0f, 0.0f, "%.2f"); - ImGui::PopItemWidth(); - - ImGui::PopStyleVar(); - - ImGui::Columns(1); - ImGui::PopID(); - } + return transform; + }; } diff --git a/open_engine/src/open_engine/scene/scene.cpp b/open_engine/src/open_engine/scene/scene.cpp index f226b43..88d5703 100644 --- a/open_engine/src/open_engine/scene/scene.cpp +++ b/open_engine/src/open_engine/scene/scene.cpp @@ -1,11 +1,9 @@ -#include "logging.hpp" #include -#include #include - -#include #include +#include +#include namespace OpenEngine { Entity Scene::CreateEntity(const std::string& name) @@ -49,7 +47,7 @@ namespace OpenEngine { if (camera_component.primary) { main_camera = &camera_component.camera; - main_transform = transform.GetTransform(); + main_transform = GetTransformFromComp(transform); break; } } @@ -61,7 +59,7 @@ namespace OpenEngine { for (const auto& entity : view) { auto [transform, sprite] = view.get(entity); - Renderer2D::DrawQuad(transform.GetTransform(), sprite.color, (int)entity); + Renderer2D::DrawQuad(GetTransformFromComp(transform), sprite.color, (int)entity); } Renderer2D::EndScene(); @@ -78,7 +76,7 @@ namespace OpenEngine { { auto [transform, sprite] = group.get(entity); - Renderer2D::DrawQuad(transform.GetTransform(), sprite.color, (int)entity); + Renderer2D::DrawQuad(GetTransformFromComp(transform), sprite.color, (int)entity); } Renderer2D::EndScene();