From e02a4850e0893914a5cf45450d8f6761d9db94c0 Mon Sep 17 00:00:00 2001 From: Light Date: Wed, 11 Aug 2021 17:40:01 +0430 Subject: [PATCH] UserInterface Maintenance --- .../Engine/UserInterface/UserInterface.cpp | 105 +++++++++++++++++- .../src/Engine/UserInterface/UserInterface.h | 7 ++ .../GraphicsAPI/DirectX/dxUserInterface.cpp | 89 +-------------- .../GraphicsAPI/DirectX/dxUserInterface.h | 7 +- .../GraphicsAPI/OpenGL/glUserInterface.cpp | 88 +-------------- .../GraphicsAPI/OpenGL/glUserInterface.h | 7 +- 6 files changed, 121 insertions(+), 182 deletions(-) diff --git a/Engine/src/Engine/UserInterface/UserInterface.cpp b/Engine/src/Engine/UserInterface/UserInterface.cpp index 952c987..e756d03 100644 --- a/Engine/src/Engine/UserInterface/UserInterface.cpp +++ b/Engine/src/Engine/UserInterface/UserInterface.cpp @@ -14,24 +14,125 @@ #include "Graphics/GraphicsContext.h" +#include "Input/KeyCodes.h" + #include namespace Light { Scope UserInterface::Create(GLFWwindow* windowHandle, Ref sharedContext) { + Scope scopeUserInterface = nullptr; + + switch (GraphicsContext::GetGraphicsAPI()) { case GraphicsAPI::OpenGL: - return CreateScope(windowHandle); + scopeUserInterface = CreateScope(); + break; case GraphicsAPI::DirectX: LT_WIN( - return CreateScope(windowHandle, std::dynamic_pointer_cast(sharedContext));) + scopeUserInterface = CreateScope();) + break; default: LT_ENGINE_ASSERT(false, "UserInterface::Create: invalid/unsupported 'GraphicsAPI' {}", GraphicsContext::GetGraphicsAPI()); return nullptr; } + + scopeUserInterface->Init(windowHandle, sharedContext); + return std::move(scopeUserInterface); + } + + + void UserInterface::Init(GLFWwindow* windowHandle, Ref sharedContext) + { + // create context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + + // configure io + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; + + io.ConfigFlags |= ImGuiBackendFlags_PlatformHasViewports; + io.ConfigFlags |= ImGuiBackendFlags_RendererHasViewports; + + // #todo: handle this in a better way + if (std::filesystem::exists("user_gui_layout.ini")) + io.IniFilename = "user_gui_layout.ini"; + else + io.IniFilename = "default_gui_layout.ini"; + + // style + ImGui::StyleColorsDark(); + + PlatformImplementation(windowHandle, sharedContext); + + // keyboard map + io.KeyMap[ImGuiKey_Tab] = Key::Tab; + io.KeyMap[ImGuiKey_LeftArrow] = Key::LeftArrow; + io.KeyMap[ImGuiKey_RightArrow] = Key::RightArrow; + io.KeyMap[ImGuiKey_UpArrow] = Key::UpArrow; + io.KeyMap[ImGuiKey_DownArrow] = Key::DownArrow; + io.KeyMap[ImGuiKey_PageUp] = Key::PageUp; + io.KeyMap[ImGuiKey_PageDown] = Key::PageDown; + io.KeyMap[ImGuiKey_Home] = Key::Home; + io.KeyMap[ImGuiKey_End] = Key::End; + io.KeyMap[ImGuiKey_Insert] = Key::Insert; + io.KeyMap[ImGuiKey_Delete] = Key::Delete; + io.KeyMap[ImGuiKey_Backspace] = Key::BackSpace; + io.KeyMap[ImGuiKey_Space] = Key::Space; + io.KeyMap[ImGuiKey_Enter] = Key::Enter; + io.KeyMap[ImGuiKey_Escape] = Key::Escape; + io.KeyMap[ImGuiKey_KeyPadEnter] = Key::Enter; + io.KeyMap[ImGuiKey_A] = Key::A; + io.KeyMap[ImGuiKey_C] = Key::C; + io.KeyMap[ImGuiKey_V] = Key::V; + io.KeyMap[ImGuiKey_X] = Key::X; + io.KeyMap[ImGuiKey_Y] = Key::Y; + io.KeyMap[ImGuiKey_Z] = Key::Z; + + io.Fonts->AddFontFromFileTTF("res/Fonts/OpenSans/OpenSans-Bold.ttf", 18.0f); + io.FontDefault = io.Fonts->AddFontFromFileTTF("res/Fonts/OpenSans/OpenSans-Regular.ttf", 18.0f); + + SetDarkThemeColors(); + } + + void UserInterface::SetDarkThemeColors() + { + auto& colors = ImGui::GetStyle().Colors; + colors[ImGuiCol_WindowBg] = ImVec4{ 0.1f, 0.105f, 0.11f, 1.0f }; + + // Headers + colors[ImGuiCol_Header] = ImVec4{ 0.2f, 0.205f, 0.21f, 1.0f }; + colors[ImGuiCol_HeaderHovered] = ImVec4{ 0.3f, 0.305f, 0.31f, 1.0f }; + colors[ImGuiCol_HeaderActive] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; + + // Buttons + colors[ImGuiCol_Button] = ImVec4{ 0.2f, 0.205f, 0.21f, 1.0f }; + colors[ImGuiCol_ButtonHovered] = ImVec4{ 0.3f, 0.305f, 0.31f, 1.0f }; + colors[ImGuiCol_ButtonActive] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; + + // Frame BG + colors[ImGuiCol_FrameBg] = ImVec4{ 0.2f, 0.205f, 0.21f, 1.0f }; + colors[ImGuiCol_FrameBgHovered] = ImVec4{ 0.3f, 0.305f, 0.31f, 1.0f }; + colors[ImGuiCol_FrameBgActive] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; + + // Tabs + colors[ImGuiCol_Tab] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; + colors[ImGuiCol_TabHovered] = ImVec4{ 0.38f, 0.3805f, 0.381f, 1.0f }; + colors[ImGuiCol_TabActive] = ImVec4{ 0.28f, 0.2805f, 0.281f, 1.0f }; + colors[ImGuiCol_TabUnfocused] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; + colors[ImGuiCol_TabUnfocusedActive] = ImVec4{ 0.2f, 0.205f, 0.21f, 1.0f }; + + // Title + colors[ImGuiCol_TitleBg] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; + colors[ImGuiCol_TitleBgActive] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; + colors[ImGuiCol_TitleBgCollapsed] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; } } \ No newline at end of file diff --git a/Engine/src/Engine/UserInterface/UserInterface.h b/Engine/src/Engine/UserInterface/UserInterface.h index 137eea9..4a7f9d4 100644 --- a/Engine/src/Engine/UserInterface/UserInterface.h +++ b/Engine/src/Engine/UserInterface/UserInterface.h @@ -21,6 +21,10 @@ namespace Light { virtual ~UserInterface() = default; + void Init(GLFWwindow* windowHandle, Ref sharedContext); + + virtual void PlatformImplementation(GLFWwindow* windowHandle, Ref sharedContext) = 0; + virtual void Begin() = 0; virtual void End() = 0; @@ -28,6 +32,9 @@ namespace Light { protected: UserInterface() = default; + + private: + void SetDarkThemeColors(); }; } \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.cpp b/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.cpp index 04b2c36..00b6cbd 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.cpp +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.cpp @@ -14,63 +14,13 @@ namespace Light { - dxUserInterface::dxUserInterface(GLFWwindow* windowHandle, Ref sharedContext) + void dxUserInterface::PlatformImplementation(GLFWwindow* windowHandle, Ref sharedContext) { - // create context - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - - // configure io ImGuiIO& io = ImGui::GetIO(); - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + Ref context = std::dynamic_pointer_cast(sharedContext); - io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; - io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; - - io.ConfigFlags |= ImGuiBackendFlags_PlatformHasViewports; - io.ConfigFlags |= ImGuiBackendFlags_RendererHasViewports; - - // #todo: handle this in a better way - if(std::filesystem::exists("user_gui_layout.ini")) - io.IniFilename = "user_gui_layout.ini"; - else - io.IniFilename = "default_gui_layout.ini"; - - // style - ImGui::StyleColorsDark(); - - // init ImGui_ImplWin32_Init(glfwGetWin32Window(windowHandle)); - ImGui_ImplDX11_Init(sharedContext->GetDevice().Get(), sharedContext->GetDeviceContext().Get()); - - // keyboard map - io.KeyMap[ImGuiKey_Tab] = Key::Tab; - io.KeyMap[ImGuiKey_LeftArrow] = Key::LeftArrow; - io.KeyMap[ImGuiKey_RightArrow] = Key::RightArrow; - io.KeyMap[ImGuiKey_UpArrow] = Key::UpArrow; - io.KeyMap[ImGuiKey_DownArrow] = Key::DownArrow; - io.KeyMap[ImGuiKey_PageUp] = Key::PageUp; - io.KeyMap[ImGuiKey_PageDown] = Key::PageDown; - io.KeyMap[ImGuiKey_Home] = Key::Home; - io.KeyMap[ImGuiKey_End] = Key::End; - io.KeyMap[ImGuiKey_Insert] = Key::Insert; - io.KeyMap[ImGuiKey_Delete] = Key::Delete; - io.KeyMap[ImGuiKey_Backspace] = Key::BackSpace; - io.KeyMap[ImGuiKey_Space] = Key::Space; - io.KeyMap[ImGuiKey_Enter] = Key::Enter; - io.KeyMap[ImGuiKey_Escape] = Key::Escape; - io.KeyMap[ImGuiKey_KeyPadEnter] = Key::Enter; - io.KeyMap[ImGuiKey_A] = Key::A; - io.KeyMap[ImGuiKey_C] = Key::C; - io.KeyMap[ImGuiKey_V] = Key::V; - io.KeyMap[ImGuiKey_X] = Key::X; - io.KeyMap[ImGuiKey_Y] = Key::Y; - io.KeyMap[ImGuiKey_Z] = Key::Z; - - io.Fonts->AddFontFromFileTTF("res/Fonts/OpenSans/OpenSans-Bold.ttf", 18.0f); - io.FontDefault = io.Fonts->AddFontFromFileTTF("res/Fonts/OpenSans/OpenSans-Regular.ttf", 18.0f); - - SetDarkThemeColors(); + ImGui_ImplDX11_Init(context->GetDevice().Get(), context->GetDeviceContext().Get()); } dxUserInterface::~dxUserInterface() @@ -113,37 +63,4 @@ namespace Light { LT_ENGINE_INFO("________________________________________"); } - void dxUserInterface::SetDarkThemeColors() - { - auto& colors = ImGui::GetStyle().Colors; - colors[ImGuiCol_WindowBg] = ImVec4{ 0.1f, 0.105f, 0.11f, 1.0f }; - - // Headers - colors[ImGuiCol_Header] = ImVec4{ 0.2f, 0.205f, 0.21f, 1.0f }; - colors[ImGuiCol_HeaderHovered] = ImVec4{ 0.3f, 0.305f, 0.31f, 1.0f }; - colors[ImGuiCol_HeaderActive] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - - // Buttons - colors[ImGuiCol_Button] = ImVec4{ 0.2f, 0.205f, 0.21f, 1.0f }; - colors[ImGuiCol_ButtonHovered] = ImVec4{ 0.3f, 0.305f, 0.31f, 1.0f }; - colors[ImGuiCol_ButtonActive] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - - // Frame BG - colors[ImGuiCol_FrameBg] = ImVec4{ 0.2f, 0.205f, 0.21f, 1.0f }; - colors[ImGuiCol_FrameBgHovered] = ImVec4{ 0.3f, 0.305f, 0.31f, 1.0f }; - colors[ImGuiCol_FrameBgActive] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - - // Tabs - colors[ImGuiCol_Tab] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - colors[ImGuiCol_TabHovered] = ImVec4{ 0.38f, 0.3805f, 0.381f, 1.0f }; - colors[ImGuiCol_TabActive] = ImVec4{ 0.28f, 0.2805f, 0.281f, 1.0f }; - colors[ImGuiCol_TabUnfocused] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - colors[ImGuiCol_TabUnfocusedActive] = ImVec4{ 0.2f, 0.205f, 0.21f, 1.0f }; - - // Title - colors[ImGuiCol_TitleBg] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - colors[ImGuiCol_TitleBgActive] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - colors[ImGuiCol_TitleBgCollapsed] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - } - } \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.h b/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.h index c510f3d..fed72a4 100644 --- a/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.h +++ b/Engine/src/Platform/GraphicsAPI/DirectX/dxUserInterface.h @@ -16,16 +16,15 @@ namespace Light { class dxUserInterface : public UserInterface { public: - dxUserInterface(GLFWwindow* windowHandle, Ref sharedContext); + dxUserInterface() = default; ~dxUserInterface(); + void PlatformImplementation(GLFWwindow* windowHandle, Ref sharedContext) override; + void Begin() override; void End() override; void LogDebugData() override; - - private: - void SetDarkThemeColors(); }; } \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.cpp b/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.cpp index 775bb51..faee8ac 100644 --- a/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.cpp +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.cpp @@ -12,63 +12,12 @@ namespace Light { - glUserInterface::glUserInterface(GLFWwindow* windowHandle) - : m_WindowHandle(windowHandle) + void glUserInterface::PlatformImplementation(GLFWwindow* windowHandle, Ref sharedContext) { - // create context - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); + m_WindowHandle = windowHandle; - // configure io - ImGuiIO& io = ImGui::GetIO(); - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; - io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; - io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; - - io.ConfigFlags |= ImGuiBackendFlags_PlatformHasViewports; - io.ConfigFlags |= ImGuiBackendFlags_RendererHasViewports; - - // #todo: handle this in a better way - if (std::filesystem::exists("user_gui_layout.ini")) - io.IniFilename = "user_gui_layout.ini"; - else - io.IniFilename = "default_gui_layout.ini"; - - // style color - ImGui::StyleColorsDark(); - - // init ImGui_ImplGlfw_InitForOpenGL(windowHandle, false); ImGui_ImplOpenGL3_Init(); - - // keyboard map - io.KeyMap[ImGuiKey_Tab] = Key::Tab; - io.KeyMap[ImGuiKey_LeftArrow] = Key::LeftArrow; - io.KeyMap[ImGuiKey_RightArrow] = Key::RightArrow; - io.KeyMap[ImGuiKey_UpArrow] = Key::UpArrow; - io.KeyMap[ImGuiKey_DownArrow] = Key::DownArrow; - io.KeyMap[ImGuiKey_PageUp] = Key::PageUp; - io.KeyMap[ImGuiKey_PageDown] = Key::PageDown; - io.KeyMap[ImGuiKey_Home] = Key::Home; - io.KeyMap[ImGuiKey_End] = Key::End; - io.KeyMap[ImGuiKey_Insert] = Key::Insert; - io.KeyMap[ImGuiKey_Delete] = Key::Delete; - io.KeyMap[ImGuiKey_Backspace] = Key::BackSpace; - io.KeyMap[ImGuiKey_Space] = Key::Space; - io.KeyMap[ImGuiKey_Enter] = Key::Enter; - io.KeyMap[ImGuiKey_Escape] = Key::Escape; - io.KeyMap[ImGuiKey_KeyPadEnter] = Key::Enter; - io.KeyMap[ImGuiKey_A] = Key::A; - io.KeyMap[ImGuiKey_C] = Key::C; - io.KeyMap[ImGuiKey_V] = Key::V; - io.KeyMap[ImGuiKey_X] = Key::X; - io.KeyMap[ImGuiKey_Y] = Key::Y; - io.KeyMap[ImGuiKey_Z] = Key::Z; - - io.Fonts->AddFontFromFileTTF("res/Fonts/OpenSans/OpenSans-Bold.ttf", 18.0f); - io.FontDefault = io.Fonts->AddFontFromFileTTF("res/Fonts/OpenSans/OpenSans-Regular.ttf", 18.0f); - - SetDarkThemeColors(); } glUserInterface::~glUserInterface() @@ -112,37 +61,4 @@ namespace Light { LT_ENGINE_INFO("________________________________________"); } - void glUserInterface::SetDarkThemeColors() - { - auto& colors = ImGui::GetStyle().Colors; - colors[ImGuiCol_WindowBg] = ImVec4{ 0.1f, 0.105f, 0.11f, 1.0f }; - - // Headers - colors[ImGuiCol_Header] = ImVec4{ 0.2f, 0.205f, 0.21f, 1.0f }; - colors[ImGuiCol_HeaderHovered] = ImVec4{ 0.3f, 0.305f, 0.31f, 1.0f }; - colors[ImGuiCol_HeaderActive] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - - // Buttons - colors[ImGuiCol_Button] = ImVec4{ 0.2f, 0.205f, 0.21f, 1.0f }; - colors[ImGuiCol_ButtonHovered] = ImVec4{ 0.3f, 0.305f, 0.31f, 1.0f }; - colors[ImGuiCol_ButtonActive] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - - // Frame BG - colors[ImGuiCol_FrameBg] = ImVec4{ 0.2f, 0.205f, 0.21f, 1.0f }; - colors[ImGuiCol_FrameBgHovered] = ImVec4{ 0.3f, 0.305f, 0.31f, 1.0f }; - colors[ImGuiCol_FrameBgActive] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - - // Tabs - colors[ImGuiCol_Tab] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - colors[ImGuiCol_TabHovered] = ImVec4{ 0.38f, 0.3805f, 0.381f, 1.0f }; - colors[ImGuiCol_TabActive] = ImVec4{ 0.28f, 0.2805f, 0.281f, 1.0f }; - colors[ImGuiCol_TabUnfocused] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - colors[ImGuiCol_TabUnfocusedActive] = ImVec4{ 0.2f, 0.205f, 0.21f, 1.0f }; - - // Title - colors[ImGuiCol_TitleBg] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - colors[ImGuiCol_TitleBgActive] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - colors[ImGuiCol_TitleBgCollapsed] = ImVec4{ 0.15f, 0.1505f, 0.151f, 1.0f }; - } - } \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.h b/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.h index 7e32a8f..b9e9046 100644 --- a/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.h +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glUserInterface.h @@ -14,16 +14,15 @@ namespace Light { GLFWwindow* m_WindowHandle; public: - glUserInterface(GLFWwindow* windowHandle); + glUserInterface() = default; ~glUserInterface(); + void PlatformImplementation(GLFWwindow* windowHandle, Ref sharedContext) override; + void Begin() override; void End() override; void LogDebugData() override; - - private: - void SetDarkThemeColors(); }; } \ No newline at end of file