From ce3dd3f984fdece7fa3e42180b0dfcefebc0e27c Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Thu, 7 Jul 2022 15:41:21 -0400 Subject: [PATCH] Adds extra/custom ImGui methods Slightly shrink default font size Asset folder always open in asset browser --- CMakeLists.txt | 1 + src/gui/gui.cpp | 2 +- src/gui/imgui_ext.cpp | 106 ++++++++++++++++++ src/gui/imgui_ext.h | 31 +++++ src/run_modes/editor/component_guis.cpp | 88 ++------------- src/run_modes/editor/panels/asset_browser.cpp | 2 + .../editor/panels/properties_view.cpp | 12 +- src/world/entity.cpp | 3 + 8 files changed, 165 insertions(+), 80 deletions(-) create mode 100644 src/gui/imgui_ext.cpp create mode 100644 src/gui/imgui_ext.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 738113e..fdf7eef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,7 @@ set(LUNARIUM_SRC "src/graphics/opengl/glText.cpp" "src/graphics/opengl/glShader.cpp" "src/gui/gui.cpp" +"src/gui/imgui_ext.cpp" "src/gui/panel.cpp" "src/gui/panel_manager.cpp" "src/gui/console.cpp" diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 29f1277..759e865 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -63,7 +63,7 @@ namespace lunarium // NOTE: Generating a font file for now. Late might look into loading directly from memory DataManager::GenerateFontFileAt(Font::F_ROBOTO, "robo.ttf"); //io.Fonts->AddFontFromFileTTF("open.ttf", 16.0f); - mFonts[GuiFont::FONT_ROBO] = io.Fonts->AddFontFromFileTTF("robo.ttf", 18.0f); + mFonts[GuiFont::FONT_ROBO] = io.Fonts->AddFontFromFileTTF("robo.ttf", 16.0f); mFonts[GuiFont::FONT_ROBO_SMALL] = io.Fonts->AddFontFromFileTTF("robo.ttf", 12.0f); //io.Fonts->AddFontFromFileTTF("Karla-Regular.ttf", 16.0f); diff --git a/src/gui/imgui_ext.cpp b/src/gui/imgui_ext.cpp new file mode 100644 index 0000000..9bbe9e7 --- /dev/null +++ b/src/gui/imgui_ext.cpp @@ -0,0 +1,106 @@ +/****************************************************************************** +* File - imgui_ext.cpp +* Author - Joey Pollack +* Date - 2022/07/07 (y/m/d) +* Mod Date - 2022/07/07 (y/m/d) +* Description - Adds new and custom methods to the imgui api +******************************************************************************/ + +#include "imgui_ext.h" + +#include +#include + +namespace lunarium +{ + /// This function was taken from the Hazel engine written by Cherno! + /// https://github.com/TheCherno/Hazel/blob/master/Hazelnut/src/Panels/SceneHierarchyPanel.cpp + void ImGuiExt::Vec3Control(const std::string& label, glm::vec3& values, float resetValue, float columnWidth) + { + ImGui::PushID(label.c_str()); + + ImGui::Columns(2); + ImGui::SetColumnWidth(0, columnWidth); + ImGui::Text(label.c_str()); + ImGui::NextColumn(); + + ImGui::PushMultiItemsWidths(3, ImGui::CalcItemWidth()); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2{ 0, 0 }); + + float lineHeight = GImGui->Font->FontSize + GImGui->Style.FramePadding.y * 2.0f; + ImVec2 buttonSize = { lineHeight + 3.0f, lineHeight }; + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.8f, 0.1f, 0.15f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.9f, 0.2f, 0.2f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.8f, 0.1f, 0.15f, 1.0f }); + //ImGui::PushFont(boldFont); + if (ImGui::Button("X", buttonSize)) + values.x = resetValue; + //ImGui::PopFont(); + ImGui::PopStyleColor(3); + + ImGui::SameLine(); + ImGui::DragFloat("##X", &values.x, 0.1f, 0.0f, 0.0f, "%.2f"); + ImGui::PopItemWidth(); + ImGui::SameLine(); + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.2f, 0.7f, 0.2f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.3f, 0.8f, 0.3f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.2f, 0.7f, 0.2f, 1.0f }); + //ImGui::PushFont(boldFont); + if (ImGui::Button("Y", buttonSize)) + values.y = resetValue; + //ImGui::PopFont(); + ImGui::PopStyleColor(3); + + ImGui::SameLine(); + ImGui::DragFloat("##Y", &values.y, 0.1f, 0.0f, 0.0f, "%.2f"); + ImGui::PopItemWidth(); + ImGui::SameLine(); + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.1f, 0.25f, 0.8f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.2f, 0.35f, 0.9f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.1f, 0.25f, 0.8f, 1.0f }); + //ImGui::PushFont(boldFont); + if (ImGui::Button("Z", buttonSize)) + values.z = resetValue; + //ImGui::PopFont(); + ImGui::PopStyleColor(3); + + ImGui::SameLine(); + ImGui::DragFloat("##Z", &values.z, 0.1f, 0.0f, 0.0f, "%.2f"); + ImGui::PopItemWidth(); + + ImGui::PopStyleVar(); + + ImGui::Columns(1); + + ImGui::PopID(); + } + + /// Only works if this is the only item on the line + void ImGuiExt::TextCentered(const std::string text) + { + auto windowWidth = ImGui::GetWindowSize().x; + auto textWidth = ImGui::CalcTextSize(text.c_str()).x; + + ImGui::SetCursorPosX((windowWidth - textWidth) * 0.5f); + ImGui::Text(text.c_str()); + } + + // Not recommended but the only way to do it for now + // https://github.com/ocornut/imgui/discussions/3862 + bool ImGuiExt::ButtonCentered(const char* label, float alignment) + { + ImGuiStyle& style = ImGui::GetStyle(); + + float size = ImGui::CalcTextSize(label).x + style.FramePadding.x * 2.0f; + float avail = ImGui::GetContentRegionAvail().x; + + float off = (avail - size) * alignment; + if (off > 0.0f) + ImGui::SetCursorPosX(ImGui::GetCursorPosX() + off); + + return ImGui::Button(label); + } +} \ No newline at end of file diff --git a/src/gui/imgui_ext.h b/src/gui/imgui_ext.h new file mode 100644 index 0000000..3dab4bb --- /dev/null +++ b/src/gui/imgui_ext.h @@ -0,0 +1,31 @@ +/****************************************************************************** +* File - imgui_ext.h +* Author - Joey Pollack +* Date - 2022/07/07 (y/m/d) +* Mod Date - 2022/07/07 (y/m/d) +* Description - Adds new and custom methods to the imgui api +******************************************************************************/ + +#ifndef LUNARIUM_IMGUI_EXT_H_ +#define LUNARIUM_IMGUI_EXT_H_ + +#include +#include +#include + +namespace lunarium +{ + class ImGuiExt + { + public: + + /// This function was taken from the Hazel engine written by Cherno! + /// https://github.com/TheCherno/Hazel/blob/master/Hazelnut/src/Panels/SceneHierarchyPanel.cpp + static void Vec3Control(const std::string& label, glm::vec3& values, float resetValue = 0.0f, float columnWidth = 100.0f); + static void TextCentered(const std::string text); + static bool ButtonCentered(const char* label, float alignment = 0.5f); + }; +} + + +#endif // LUNARIUM_IMGUI_EXT_H_ \ No newline at end of file diff --git a/src/run_modes/editor/component_guis.cpp b/src/run_modes/editor/component_guis.cpp index a11e193..8894d02 100644 --- a/src/run_modes/editor/component_guis.cpp +++ b/src/run_modes/editor/component_guis.cpp @@ -9,102 +9,36 @@ #include "component_guis.h" #include #include +#include #include #include namespace lunarium { namespace editor { - ///////////////////////////////////////////////////////////////////// - // HELPERS - ///////////////////////////////////////////////////////////////////// - - /// This function was taken from the Hazel engine written by Cherno! - /// https://github.com/TheCherno/Hazel/blob/master/Hazelnut/src/Panels/SceneHierarchyPanel.cpp - static void DrawVec3Control(const std::string& label, glm::vec3& values, float resetValue = 0.0f, float columnWidth = 100.0f) - { - ImGuiIO& io = ImGui::GetIO(); - auto boldFont = io.Fonts->Fonts[0]; - - ImGui::PushID(label.c_str()); - - ImGui::Columns(2); - ImGui::SetColumnWidth(0, columnWidth); - ImGui::Text(label.c_str()); - ImGui::NextColumn(); - - ImGui::PushMultiItemsWidths(3, ImGui::CalcItemWidth()); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2{ 0, 0 }); - - float lineHeight = GImGui->Font->FontSize + GImGui->Style.FramePadding.y * 2.0f; - ImVec2 buttonSize = { lineHeight + 3.0f, lineHeight }; - - ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.8f, 0.1f, 0.15f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.9f, 0.2f, 0.2f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.8f, 0.1f, 0.15f, 1.0f }); - ImGui::PushFont(boldFont); - if (ImGui::Button("X", buttonSize)) - values.x = resetValue; - ImGui::PopFont(); - ImGui::PopStyleColor(3); - - ImGui::SameLine(); - ImGui::DragFloat("##X", &values.x, 0.1f, 0.0f, 0.0f, "%.2f"); - ImGui::PopItemWidth(); - ImGui::SameLine(); - - ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.2f, 0.7f, 0.2f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.3f, 0.8f, 0.3f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.2f, 0.7f, 0.2f, 1.0f }); - ImGui::PushFont(boldFont); - if (ImGui::Button("Y", buttonSize)) - values.y = resetValue; - ImGui::PopFont(); - ImGui::PopStyleColor(3); - - ImGui::SameLine(); - ImGui::DragFloat("##Y", &values.y, 0.1f, 0.0f, 0.0f, "%.2f"); - ImGui::PopItemWidth(); - ImGui::SameLine(); - - ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.1f, 0.25f, 0.8f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.2f, 0.35f, 0.9f, 1.0f }); - ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.1f, 0.25f, 0.8f, 1.0f }); - ImGui::PushFont(boldFont); - if (ImGui::Button("Z", buttonSize)) - values.z = resetValue; - ImGui::PopFont(); - ImGui::PopStyleColor(3); - - ImGui::SameLine(); - ImGui::DragFloat("##Z", &values.z, 0.1f, 0.0f, 0.0f, "%.2f"); - ImGui::PopItemWidth(); - - ImGui::PopStyleVar(); - - ImGui::Columns(1); - - ImGui::PopID(); - } - void CompGui::RenderTagComp(TagComponent& comp) { + ImGuiExt::TextCentered("Tag Component"); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(3.0f, ImGui::GetStyle().ItemSpacing.y)); + const int BUFFER_SIZE = 256; static char buffer[BUFFER_SIZE] = ""; - ImGui::Text("Tag:"); + ImGui::Text("Tag Info"); ImGui::SameLine(); strcpy(buffer, comp.Info.c_str()); ImGui::InputText("##Tag", buffer, BUFFER_SIZE); comp.Info = buffer; + ImGui::PopStyleVar(); + } - /// This code also taken from the Hazel engine + /// This code taken from the Hazel engine /// https://github.com/TheCherno/Hazel/blob/master/Hazelnut/src/Panels/SceneHierarchyPanel.cpp void CompGui::RenderTransformComp(TransformComponent& comp) { - DrawVec3Control("Translation", comp.Position); + ImGuiExt::Vec3Control("Translation", comp.Position, 0.0f, 85.0f); glm::vec3 rotation = glm::degrees(comp.Rotation); - DrawVec3Control("Rotation", rotation); + ImGuiExt::Vec3Control("Rotation", rotation, 0.0f, 85.0f); comp.Rotation = glm::radians(rotation); - DrawVec3Control("Scale", comp.Scale, 1.0f); + ImGuiExt::Vec3Control("Scale", comp.Scale, 1.0f, 85.0f); } }} \ No newline at end of file diff --git a/src/run_modes/editor/panels/asset_browser.cpp b/src/run_modes/editor/panels/asset_browser.cpp index e072a94..04e8438 100644 --- a/src/run_modes/editor/panels/asset_browser.cpp +++ b/src/run_modes/editor/panels/asset_browser.cpp @@ -33,6 +33,7 @@ namespace editor void AssetBrowser::SetAssetDirectory(std::filesystem::path dir) { mAssetDirectory = dir; + mSelectedDir = dir; } @@ -58,6 +59,7 @@ namespace editor ImGui::SetCursorPosY(row_height); if (ImGui::BeginChild("Directory Tree", ImVec2(wind_size.x * 0.15f, wind_size.y), true, ImGuiWindowFlags_NoCollapse)) { + ImGui::SetNextItemOpen(true); DoDirTree(mAssetDirectory); mSyncTree = false; } diff --git a/src/run_modes/editor/panels/properties_view.cpp b/src/run_modes/editor/panels/properties_view.cpp index a748594..1925853 100644 --- a/src/run_modes/editor/panels/properties_view.cpp +++ b/src/run_modes/editor/panels/properties_view.cpp @@ -9,6 +9,7 @@ #include "properties_view.h" #include +#include #include #include #include @@ -114,7 +115,10 @@ namespace lunarium { namespace editor void PropertiesView::ShowEntity() { - ImGui::Text("Components"); + ImGuiExt::TextCentered("Components"); + float spacing_y = 12.0f; + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, spacing_y)); + ImGui::Separator(); // TODO: iterate through components if (mpSelectedEntity->HasComponent()) @@ -126,15 +130,19 @@ namespace lunarium { namespace editor if (mpSelectedEntity->HasComponent()) { + ImGui::PopStyleVar(); CompGui::RenderTransformComp(mpSelectedEntity->GetComponent()); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, spacing_y)); ImGui::Separator(); } // After all components rendered - if (ImGui::Button("Add Component")) + if (ImGuiExt::ButtonCentered("Add Component")) { OpenPopup(Popup::ADD_COMP).LogIfFailed(Editor::LogCat); } + + ImGui::PopStyleVar(); } void PropertiesView::ShowAsset() diff --git a/src/world/entity.cpp b/src/world/entity.cpp index 8d5df0c..9f8e8f8 100644 --- a/src/world/entity.cpp +++ b/src/world/entity.cpp @@ -152,6 +152,9 @@ namespace lunarium } } + + // TODO: ADD CODE TO DESERIALIZE ANY NEW COMPONENTS + // TODO: Load children auto& children = node["children"]; for (auto iter = children.begin(); iter != children.end(); iter++)