Skip to content

Commit

Permalink
ImGui docking early implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
timmypidashev committed Feb 26, 2022
1 parent b920479 commit a8f48c2
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 183 deletions.
8 changes: 8 additions & 0 deletions Core/Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ namespace Iridescent {

m_Window = std::unique_ptr<Window>(Window::Create());
m_Window->SetEventCallback(BIND_EVENT_FUNCTION(OnEvent));

m_ImGuiLayer = new ImGuiLayer();
PushOverlay(m_ImGuiLayer);
}

Application::~Application()
Expand Down Expand Up @@ -56,6 +59,11 @@ namespace Iridescent {
for (Layer* layer : m_LayerStack)
layer->OnUpdate();

m_ImGuiLayer->Begin();
for (Layer* layer : m_LayerStack)
layer->OnImGuiRender();
m_ImGuiLayer->End();

m_Window->OnUpdate();
}
}
Expand Down
2 changes: 2 additions & 0 deletions Core/Application/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Layer/LayerStack.h"
#include "Events/Event.h"
#include "Events/ApplicationEvents.h"
#include "ImGui/ImGuiLayer.h"

namespace Iridescent {

Expand All @@ -28,6 +29,7 @@ namespace Iridescent {
private:
bool OnWindowClose(WindowCloseEvent& e);
std::unique_ptr<Window> m_Window;
ImGuiLayer* m_ImGuiLayer;
bool m_Running = true;
LayerStack m_LayerStack;
private:
Expand Down
5 changes: 1 addition & 4 deletions Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ set(SOURCE_FILES
Layer/LayerStack.h

# ImGui
ImGui/ImGuiBuild.cpp
ImGui/ImGuiLayer.cpp
ImGui/ImGuiLayer.h

# Platform
Platform/OpenGL/ImGuiOpenGLRenderer.h

)

# Add library sources
Expand Down
5 changes: 5 additions & 0 deletions Core/ImGui/ImGuiBuild.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "Iridpch.h"

#define IMGUI_IMPL_OPENGL_LOADER_GLAD
#include "backends/imgui_impl_opengl3.cpp"
#include "backends/imgui_impl_glfw.cpp"
174 changes: 51 additions & 123 deletions Core/ImGui/ImGuiLayer.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "Iridpch.h"
#include "ImGuiLayer.h"

#include "imgui.h"
#include "Platform/OpenGL/ImGuiOpenGLRenderer.h"
#include "GLFW/glfw3.h"
#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"
#include "Application/Application.h"
#include <GLFW/glfw3.h>

namespace Iridescent {

Expand All @@ -18,147 +18,75 @@ namespace Iridescent {
}

void ImGuiLayer::OnAttach()
{
ImGui::CreateContext();
{
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
//io.ConfigFlags |= ImGuiConfigFlags_ViewportsNoTaskBarIcons;
//io.ConfigFlags |= ImGuiConfigFlags_ViewportsNoMerge;

// Setup Dear ImGui style
ImGui::StyleColorsDark();
//ImGui::StyleColorsClassic();

ImGuiIO& io = ImGui::GetIO();
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos;
// When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
ImGuiStyle& style = ImGui::GetStyle();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}

io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB;
io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP;
io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN;
io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
io.KeyMap[ImGuiKey_Insert] = GLFW_KEY_INSERT;
io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
io.KeyMap[ImGuiKey_Space] = GLFW_KEY_SPACE;
io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
Application& app = Application::Get();
GLFWwindow* window = static_cast<GLFWwindow*>(app.GetWindow().GetNativeWindow());

// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 410");
}

void ImGuiLayer::OnDetach()
{

ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}

void ImGuiLayer::OnUpdate()
void ImGuiLayer::Begin()
{
ImGuiIO& io = ImGui::GetIO();
Application& app = Application::Get();
io.DisplaySize = ImVec2(app.GetWindow().GetWidth(), app.GetWindow().GetHeight());

float time = (float)glfwGetTime();
io.DeltaTime = m_Time > 0.0f ? (time - m_Time) : (1.0f / 60.0f);
m_Time = time;

ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();

static bool show = true;
ImGui::ShowDemoWindow(&show);

ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}

void ImGuiLayer::OnEvent(Event& event)
{
EventDispatcher dispatcher(event);
dispatcher.Dispatch<MouseButtonPressedEvent>(IRID_BIND_EVENT_FN(ImGuiLayer::OnMouseButtonPressedEvent));
dispatcher.Dispatch<MouseButtonReleasedEvent>(IRID_BIND_EVENT_FN(ImGuiLayer::OnMouseButtonReleasedEvent));
dispatcher.Dispatch<MouseMovedEvent>(IRID_BIND_EVENT_FN(ImGuiLayer::OnMouseMovedEvent));
dispatcher.Dispatch<MouseScrolledEvent>(IRID_BIND_EVENT_FN(ImGuiLayer::OnMouseScrolledEvent));
dispatcher.Dispatch<KeyPressedEvent>(IRID_BIND_EVENT_FN(ImGuiLayer::OnKeyPressedEvent));
dispatcher.Dispatch<KeyTypedEvent>(IRID_BIND_EVENT_FN(ImGuiLayer::OnKeyTypedEvent));
dispatcher.Dispatch<KeyReleasedEvent>(IRID_BIND_EVENT_FN(ImGuiLayer::OnKeyReleasedEvent));
dispatcher.Dispatch<WindowResizeEvent>(IRID_BIND_EVENT_FN(ImGuiLayer::OnWindowResizeEvent));
}

bool ImGuiLayer::OnMouseButtonPressedEvent(MouseButtonPressedEvent& e)
{
ImGuiIO& io = ImGui::GetIO();
io.MouseDown[e.GetMouseButton()] = true;

return false;
}

bool ImGuiLayer::OnMouseButtonReleasedEvent(MouseButtonReleasedEvent& e)
{
ImGuiIO& io = ImGui::GetIO();
io.MouseDown[e.GetMouseButton()] = false;

return false;
}

bool ImGuiLayer::OnMouseMovedEvent(MouseMovedEvent& e)
{
ImGuiIO& io = ImGui::GetIO();
io.MousePos = ImVec2(e.GetX(), e.GetY());

return false;
}

bool ImGuiLayer::OnMouseScrolledEvent(MouseScrolledEvent& e)
void ImGuiLayer::End()
{
ImGuiIO& io = ImGui::GetIO();
io.MouseWheelH += e.GetXOffset();
io.MouseWheel += e.GetYOffset();

return false;
}

bool ImGuiLayer::OnKeyPressedEvent(KeyPressedEvent& e)
{
ImGuiIO& io = ImGui::GetIO();
io.KeysDown[e.GetKeyCode()] = true;

io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] || io.KeysDown[GLFW_KEY_RIGHT_SUPER];
return false;
}
Application& app = Application::Get();
io.DisplaySize = ImVec2(app.GetWindow().GetWidth(), app.GetWindow().GetHeight());

bool ImGuiLayer::OnKeyReleasedEvent(KeyReleasedEvent& e)
{
ImGuiIO& io = ImGui::GetIO();
io.KeysDown[e.GetKeyCode()] = false;
// Rendering
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

return false;
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
}
}

bool ImGuiLayer::OnKeyTypedEvent(KeyTypedEvent& e)
void ImGuiLayer::OnImGuiRender()
{
ImGuiIO& io = ImGui::GetIO();
int keycode = e.GetKeyCode();
if (keycode > 0 && keycode < 0x10000)
io.AddInputCharacter((unsigned short)keycode);

return false;
static bool show = true;
ImGui::ShowDemoWindow(&show);
}

bool ImGuiLayer::OnWindowResizeEvent(WindowResizeEvent& e)
{
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(e.GetWidth(), e.GetHeight());
io.DisplayFramebufferScale = ImVec2(1.0f, 1.0f);
glViewport(0, 0, e.GetWidth(), e.GetHeight());

return false;
}
}
}
19 changes: 6 additions & 13 deletions Core/ImGui/ImGuiLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,12 @@ namespace Iridescent {
ImGuiLayer();
~ImGuiLayer();

void OnAttach();
void OnDetach();
void OnUpdate();
void OnEvent(Event& event);
private:
bool OnMouseButtonPressedEvent(MouseButtonPressedEvent& e);
bool OnMouseButtonReleasedEvent(MouseButtonReleasedEvent& e);
bool OnMouseMovedEvent(MouseMovedEvent& e);
bool OnMouseScrolledEvent(MouseScrolledEvent& e);
bool OnKeyPressedEvent(KeyPressedEvent& e);
bool OnKeyReleasedEvent(KeyReleasedEvent& e);
bool OnKeyTypedEvent(KeyTypedEvent& e);
bool OnWindowResizeEvent(WindowResizeEvent& e);
virtual void OnAttach() override;
virtual void OnDetach() override;
virtual void OnImGuiRender() override;

void Begin();
void End();
private:
float m_Time = 0.0f;
};
Expand Down
1 change: 1 addition & 0 deletions Core/Layer/Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Iridescent {
virtual void OnAttach() {}
virtual void OnDetach() {}
virtual void OnUpdate() {}
virtual void OnImGuiRender() {}
virtual void OnEvent(Event& event) {}

inline const std::string& GetName() const { return m_DebugName; }
Expand Down
6 changes: 3 additions & 3 deletions Core/Layer/LayerStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Iridescent {

LayerStack::LayerStack()
{
m_LayerInsert = m_Layers.begin();
}

LayerStack::~LayerStack()
Expand All @@ -16,7 +15,8 @@ namespace Iridescent {

void LayerStack::PushLayer(Layer* layer)
{
m_LayerInsert = m_Layers.emplace(m_LayerInsert, layer);
m_Layers.emplace(m_Layers.begin() + m_LayerInsertIndex, layer);
m_LayerInsertIndex++;
}

void LayerStack::PushOverlay(Layer* overlay)
Expand All @@ -30,7 +30,7 @@ namespace Iridescent {
if (it != m_Layers.end())
{
m_Layers.erase(it);
m_LayerInsert--;
m_LayerInsertIndex--;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Core/Layer/LayerStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Iridescent {
std::vector<Layer*>::iterator end() { return m_Layers.end(); }
private:
std::vector<Layer*> m_Layers;
std::vector<Layer*>::iterator m_LayerInsert;
unsigned int m_LayerInsertIndex = 0;
};

}
35 changes: 0 additions & 35 deletions Core/Platform/OpenGL/ImGuiOpenGLRenderer.h

This file was deleted.

Loading

0 comments on commit a8f48c2

Please sign in to comment.