diff --git a/Navigator/src/Navigator/Application.cpp b/Navigator/src/Navigator/Application.cpp index 84fa7c1..ef99c8b 100644 --- a/Navigator/src/Navigator/Application.cpp +++ b/Navigator/src/Navigator/Application.cpp @@ -13,6 +13,9 @@ namespace Navigator { m_window->SetEventCallback(BIND_EVENT_FUNCTION(Application::OnEvent)); PushLayer(new Layer()); + + m_imgui_layer = new ImGuiLayer(); + PushOverlay(m_imgui_layer); } Application::~Application() {} @@ -20,6 +23,7 @@ namespace Navigator { void Application::OnEvent(Event& event) { + NAV_TRACE("Found event: {0}", event); EventDispatcher dispatch(event); dispatch.Dispatch(BIND_EVENT_FUNCTION(Application::OnWindowCloseEvent)); for(auto iter = m_stack.end(); iter != m_stack.begin(); ) @@ -53,10 +57,14 @@ namespace Navigator { { while(m_runFlag) { - NAV_TRACE("Doing a run."); - for(auto layer : m_stack) - NAV_TRACE("Layer with name {0} found!", layer->GetName()); + for(auto layer : m_stack) + layer->OnUpdate(); + + m_imgui_layer->Begin(); + for(auto layer : m_stack) + layer->OnImGuiRender(); + m_imgui_layer->End(); m_window->OnUpdate(); } } diff --git a/Navigator/src/Navigator/Application.h b/Navigator/src/Navigator/Application.h index 7f60d0c..af398de 100644 --- a/Navigator/src/Navigator/Application.h +++ b/Navigator/src/Navigator/Application.h @@ -1,12 +1,13 @@ #ifndef APPLICATION_H #define APPLICATION_H -#include "NavCore.h" +#include "Navigator/NavCore.h" #include "Events/Event.h" -#include "Events/AppEvent.h" -#include "LayerStack.h" -#include "Layer.h" -#include "Window.h" +#include "Navigator/Events/AppEvent.h" +#include "Navigator/LayerStack.h" +#include "Navigator/Layer.h" +#include "Navigator/Window.h" +#include "Navigator/ImGui/ImGuiLayer.h" namespace Navigator { @@ -24,13 +25,14 @@ namespace Navigator { inline static Application& Get() { return *s_instance; } - inline void GetWindow() { return; } + inline Window& GetWindow() { return *m_window; } private: bool OnWindowCloseEvent(WindowCloseEvent& event); LayerStack m_stack; std::unique_ptr m_window; + ImGuiLayer* m_imgui_layer; bool m_runFlag; static Application* s_instance; diff --git a/Navigator/src/Navigator/ImGui/ImGuiBuild.cpp b/Navigator/src/Navigator/ImGui/ImGuiBuild.cpp new file mode 100644 index 0000000..f4b50e5 --- /dev/null +++ b/Navigator/src/Navigator/ImGui/ImGuiBuild.cpp @@ -0,0 +1,4 @@ + + +#include "backends/imgui_impl_opengl3.cpp" +#include "backends/imgui_impl_glfw.cpp" \ No newline at end of file diff --git a/Navigator/src/Navigator/ImGui/ImGuiLayer.cpp b/Navigator/src/Navigator/ImGui/ImGuiLayer.cpp new file mode 100644 index 0000000..7754c4a --- /dev/null +++ b/Navigator/src/Navigator/ImGui/ImGuiLayer.cpp @@ -0,0 +1,90 @@ +#include "ImGuiLayer.h" +#include "Navigator/Application.h" +#include "Navigator/NavCore.h" + +#include "imgui.h" +#include "backends/imgui_impl_opengl3.h" +#include "backends/imgui_impl_glfw.h" + +#include +#include + +namespace Navigator { + + ImGuiLayer::ImGuiLayer() : + Layer("ImGuiLayer") + { + } + + ImGuiLayer::~ImGuiLayer() + { + } + + void ImGuiLayer::OnAttach() + { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; + + ImGui::StyleColorsDark(); + + ImGuiStyle& style = ImGui::GetStyle(); + + if(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + style.WindowRounding = 0.0f; + style.Colors[ImGuiCol_WindowBg].w = 1.0f; + } + + Application& app = Application::Get(); + GLFWwindow* window = static_cast(app.GetWindow().GetNativeWindow()); + + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init("#version 410"); + } + + void ImGuiLayer::OnDetach() + { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + } + + void ImGuiLayer::Begin() + { + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + + ImGui::NewFrame(); + } + + void ImGuiLayer::End() + { + Application& app = Application::Get(); + ImGuiIO& io = ImGui::GetIO(); + + io.DisplaySize = ImVec2((float)app.GetWindow().GetWidth(), (float)app.GetWindow().GetHeight()); + + + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + if(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + GLFWwindow* backup_current_context = glfwGetCurrentContext(); + ImGui::UpdatePlatformWindows(); + ImGui::RenderPlatformWindowsDefault(); + glfwMakeContextCurrent(backup_current_context); + } + } + + void ImGuiLayer::OnImGuiRender() + { + static bool show = true; + ImGui::ShowDemoWindow(&show); + } +} \ No newline at end of file diff --git a/Navigator/src/Navigator/ImGui/ImGuiLayer.h b/Navigator/src/Navigator/ImGui/ImGuiLayer.h new file mode 100644 index 0000000..ce52bbc --- /dev/null +++ b/Navigator/src/Navigator/ImGui/ImGuiLayer.h @@ -0,0 +1,27 @@ +#ifndef IMGUI_LAYER_H +#define IMGUI_LAYER_H + +#include "Navigator/Layer.h" + +namespace Navigator { + + class ImGuiLayer : public Layer + { + public: + ImGuiLayer(); + ~ImGuiLayer(); + + virtual void OnAttach() override; + virtual void OnDetach() override; + virtual void OnImGuiRender() override; + + void Begin(); + void End(); + + private: + float m_time; + }; + +} + +#endif \ No newline at end of file diff --git a/Navigator/src/Navigator/Layer.h b/Navigator/src/Navigator/Layer.h index 82d3b4d..f6cb7d0 100644 --- a/Navigator/src/Navigator/Layer.h +++ b/Navigator/src/Navigator/Layer.h @@ -14,6 +14,7 @@ namespace Navigator { virtual void OnAttach() {} virtual void OnDetach() {} virtual void OnImGuiRender() {} + virtual void OnUpdate() {} virtual void OnEvent(Event& event) {} inline const std::string& GetName() { return m_name; } diff --git a/navigator.sublime-project b/navigator.sublime-project index 7f374ad..2f7eedd 100644 --- a/navigator.sublime-project +++ b/navigator.sublime-project @@ -7,6 +7,6 @@ ], "settings": { - "SublimeLinter.linters.g++.args": ["-c","-Wall","-fsyntax-only","-std=c++17","-include${folder}/Navigator/src/navpch.h","-INavigator/src/", "-INavigator/src/Navigator/","-INavigator/vendor/spdlog/include", "-INavigator/vendor/glfw/include/","-INavigator/vendor/glad/include"] + "SublimeLinter.linters.g++.args": ["-c","-Wall","-fsyntax-only","-std=c++17","-include${folder}/Navigator/src/navpch.h","-INavigator/src/", "-INavigator/src/Navigator/","-INavigator/vendor/spdlog/include", "-INavigator/vendor/glfw/include/","-INavigator/vendor/glad/include","-INavigator/vendor/imgui"] } } \ No newline at end of file