Adds extra/custom ImGui methods

Slightly shrink default font size
Asset folder always open in asset browser
master
Joey Pollack 3 years ago
parent f8cb13d856
commit ce3dd3f984

@ -76,6 +76,7 @@ set(LUNARIUM_SRC
"src/graphics/opengl/glText.cpp" "src/graphics/opengl/glText.cpp"
"src/graphics/opengl/glShader.cpp" "src/graphics/opengl/glShader.cpp"
"src/gui/gui.cpp" "src/gui/gui.cpp"
"src/gui/imgui_ext.cpp"
"src/gui/panel.cpp" "src/gui/panel.cpp"
"src/gui/panel_manager.cpp" "src/gui/panel_manager.cpp"
"src/gui/console.cpp" "src/gui/console.cpp"

@ -63,7 +63,7 @@ namespace lunarium
// NOTE: Generating a font file for now. Late might look into loading directly from memory // NOTE: Generating a font file for now. Late might look into loading directly from memory
DataManager::GenerateFontFileAt(Font::F_ROBOTO, "robo.ttf"); DataManager::GenerateFontFileAt(Font::F_ROBOTO, "robo.ttf");
//io.Fonts->AddFontFromFileTTF("open.ttf", 16.0f); //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); mFonts[GuiFont::FONT_ROBO_SMALL] = io.Fonts->AddFontFromFileTTF("robo.ttf", 12.0f);
//io.Fonts->AddFontFromFileTTF("Karla-Regular.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("Karla-Regular.ttf", 16.0f);

@ -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 <dearimgui/imgui.h>
#include <dearimgui/imgui_internal.h>
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);
}
}

@ -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 <core/common_defs.h>
#include <glm/glm.hpp>
#include <string>
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_

@ -9,102 +9,36 @@
#include "component_guis.h" #include "component_guis.h"
#include <editor/editor.h> #include <editor/editor.h>
#include <utils/logger.h> #include <utils/logger.h>
#include <gui/imgui_ext.h>
#include <dearimgui/imgui.h> #include <dearimgui/imgui.h>
#include <dearimgui/imgui_internal.h> #include <dearimgui/imgui_internal.h>
namespace lunarium { namespace editor 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) 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; const int BUFFER_SIZE = 256;
static char buffer[BUFFER_SIZE] = ""; static char buffer[BUFFER_SIZE] = "";
ImGui::Text("Tag:"); ImGui::Text("Tag Info");
ImGui::SameLine(); ImGui::SameLine();
strcpy(buffer, comp.Info.c_str()); strcpy(buffer, comp.Info.c_str());
ImGui::InputText("##Tag", buffer, BUFFER_SIZE); ImGui::InputText("##Tag", buffer, BUFFER_SIZE);
comp.Info = buffer; 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 /// https://github.com/TheCherno/Hazel/blob/master/Hazelnut/src/Panels/SceneHierarchyPanel.cpp
void CompGui::RenderTransformComp(TransformComponent& comp) 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); glm::vec3 rotation = glm::degrees(comp.Rotation);
DrawVec3Control("Rotation", rotation); ImGuiExt::Vec3Control("Rotation", rotation, 0.0f, 85.0f);
comp.Rotation = glm::radians(rotation); comp.Rotation = glm::radians(rotation);
DrawVec3Control("Scale", comp.Scale, 1.0f); ImGuiExt::Vec3Control("Scale", comp.Scale, 1.0f, 85.0f);
} }
}} }}

@ -33,6 +33,7 @@ namespace editor
void AssetBrowser::SetAssetDirectory(std::filesystem::path dir) void AssetBrowser::SetAssetDirectory(std::filesystem::path dir)
{ {
mAssetDirectory = dir; mAssetDirectory = dir;
mSelectedDir = dir;
} }
@ -58,6 +59,7 @@ namespace editor
ImGui::SetCursorPosY(row_height); ImGui::SetCursorPosY(row_height);
if (ImGui::BeginChild("Directory Tree", ImVec2(wind_size.x * 0.15f, wind_size.y), true, ImGuiWindowFlags_NoCollapse)) if (ImGui::BeginChild("Directory Tree", ImVec2(wind_size.x * 0.15f, wind_size.y), true, ImGuiWindowFlags_NoCollapse))
{ {
ImGui::SetNextItemOpen(true);
DoDirTree(mAssetDirectory); DoDirTree(mAssetDirectory);
mSyncTree = false; mSyncTree = false;
} }

@ -9,6 +9,7 @@
#include "properties_view.h" #include "properties_view.h"
#include <dearimgui/imgui.h> #include <dearimgui/imgui.h>
#include <gui/imgui_ext.h>
#include <editor/editor.h> #include <editor/editor.h>
#include <world/entity.h> #include <world/entity.h>
#include <world/components.h> #include <world/components.h>
@ -114,7 +115,10 @@ namespace lunarium { namespace editor
void PropertiesView::ShowEntity() 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 // TODO: iterate through components
if (mpSelectedEntity->HasComponent<TagComponent>()) if (mpSelectedEntity->HasComponent<TagComponent>())
@ -126,15 +130,19 @@ namespace lunarium { namespace editor
if (mpSelectedEntity->HasComponent<TransformComponent>()) if (mpSelectedEntity->HasComponent<TransformComponent>())
{ {
ImGui::PopStyleVar();
CompGui::RenderTransformComp(mpSelectedEntity->GetComponent<TransformComponent>()); CompGui::RenderTransformComp(mpSelectedEntity->GetComponent<TransformComponent>());
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, spacing_y));
ImGui::Separator(); ImGui::Separator();
} }
// After all components rendered // After all components rendered
if (ImGui::Button("Add Component")) if (ImGuiExt::ButtonCentered("Add Component"))
{ {
OpenPopup(Popup::ADD_COMP).LogIfFailed(Editor::LogCat); OpenPopup(Popup::ADD_COMP).LogIfFailed(Editor::LogCat);
} }
ImGui::PopStyleVar();
} }
void PropertiesView::ShowAsset() void PropertiesView::ShowAsset()

@ -152,6 +152,9 @@ namespace lunarium
} }
} }
// TODO: ADD CODE TO DESERIALIZE ANY NEW COMPONENTS
// TODO: Load children // TODO: Load children
auto& children = node["children"]; auto& children = node["children"];
for (auto iter = children.begin(); iter != children.end(); iter++) for (auto iter = children.begin(); iter != children.end(); iter++)

Loading…
Cancel
Save