What is Dear imgui?

Dear imgui is a immediate mode graphical user interface developed by Omar Cornut and sponsored by companies like Blizzard, Google and more. This kind of user interface is not like a normal WinForms project where you can drag and drop buttons and add event callbacks to them. Each window, button, slider etc. is writen in code and look something like this:

if (!begin("Settings")) { end(); } else { ImGui::BeginChild("item view", ImVec2(0, -ImGui::GetFrameHeightWithSpacing())); // Leave room for 1 line below us ImGui::Text("Settings"); ImGui::Separator(); if (ImGui::BeginTabBar("##Tabs", ImGuiTabBarFlags_None)) { if (ImGui::BeginTabItem("Standard settings")) { ImGui::TextWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "); ImGui::EndTabItem(); } if (ImGui::BeginTabItem("Render settings")) { ImGui::ColorPicker3("Background color", MainManager::getInstance().getProgramSettings()->backgroundColor.e); ImGui::EndTabItem(); } ImGui::EndTabBar(); } ImGui::EndChild(); end(); }

In this example a new window is created with two tabs and some information on it. For examples of how this could look depending on the settings you are using can be found in the Gallery.

Most of these examples are using the default branch of Dear imgui, but there is also the so called Docking branch. This adds the feature to merge imgui windows with eachother. This way we can create applications that look like this:

Setup

I will be using GLFW for window creation and management, but Dear imgui also supports SDL2, Win32, Glut and OSX.

Clone or download the docking branch. Add these files to your project:

3. Make sure to add the impl files to your project. I added the files for GLFW and openGL, but if you are using one of the other bindings you’ll have to add their implementation files. They can be found under imgui/examples/.

4. Add includes to imgui.h and your implementation headers to your main and compile your project to test if everything works. If you are having trouble you can ask questions down below.

5. setup your platform for window creation. A good tutorial for GLFW can be found here. Make sure you can create a window!

Imgui windows

Now everything is working we start by making a basic window class. This will be the parent class to all the different windows in your application.

class ImGuiWindow { public: ImGuiWindow(); ~ImGuiWindow(); virtual void update(); void show(); protected: virtual bool begin(std::string name); void end(); bool show; };

This base class will be extended by all other windows. The constructor sets the boolean show to true. The begin and end functions are used to create and end the window. These will be called in the update function. The window has to be updated as much as possible, preferably 60 times per second. Otherwise it will not be responsive.

bool ImGuiWindow::begin(std::string name) { return ImGui::Begin(name.c_str(), &show); } void ImGuiWindow::end() { ImGui::End(); }

Now we want to create a real window with some cool buttons and such. This class looks like this:

class IGStartupWindow : public ImGuiWindow { public: void update() override; private: int counter = 0; };

The update function is where the real magic happens:

void IGStartupWindow::update() { if (show) { if (!begin("Startup")) { end(); } else { ImGui::Text("Press me:"); if (ImGui::Button("button")) { //do something } end(); } } }

If the window should show it opens with the caption “Startup”. If the creation fails we call end to keep the internal dear imgui window state correct. Else we go on and create some text and a button. This is just an easy example, but this can be expanded to with all kind of sliders, buttons and colorpickers.

Window management

Now we can create all sorts of windows, we need some way to keep track of them, so we can update them.

class ImGuiUIManager { public: ImGuiUIManager(Window* window); ~ImGuiUIManager(); void update(); void render(); void addWindow(ImGuiWindow newWin); private: std::vector<ImGuiWindow> windows; void ShowDockSpace(bool* p_open); void setstyle(); void updateWindows(); void deleteWindows(); };

This class keeps track of all the windows. It makes sure all the windows have the same style using the setstyle helperfunction. make sure to switch the window parameter of the constructor to the kind of window you are using, for me that will be a GLFWwindow.

ImGuiUIManager::ImGuiUIManager(Window* window) { 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; setstyle(); initWindows(); ImGui_ImplGlfw_InitForOpenGL(window->getWindow(), true); const char* glsl_version = "#version 410"; ImGui_ImplOpenGL3_Init(glsl_version); } void ImGuiUIManager::update() { bool show = true; ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); ImGuiIO& io = ImGui::GetIO(); ShowDockSpace(&show); updateWindows(); ImGui::Render(); if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { GLFWwindow* backup_current_context = glfwGetCurrentContext(); ImGui::UpdatePlatformWindows(); ImGui::RenderPlatformWindowsDefault(); glfwMakeContextCurrent(backup_current_context); } } void ImGuiUIManager::render() { ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); }

The showdockspace function comes right from the docking demo.

void ImGuiUIManager::ShowDockSpace(bool* p_open) { static bool opt_fullscreen_persistant = true; bool opt_fullscreen = opt_fullscreen_persistant; static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_None; // We are using the ImGuiWindowFlags_NoDocking flag to make the parent window not dockable into, // because it would be confusing to have two docking targets within each others. ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking; if (opt_fullscreen) { ImGuiViewport* viewport = ImGui::GetMainViewport(); ImGui::SetNextWindowPos(viewport->Pos); ImGui::SetNextWindowSize(viewport->Size); ImGui::SetNextWindowViewport(viewport->ID); ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; } // When using ImGuiDockNodeFlags_PassthruCentralNode, DockSpace() will render our background // and handle the pass-thru hole, so we ask Begin() to not render a background. if (dockspace_flags & ImGuiDockNodeFlags_PassthruCentralNode) window_flags |= ImGuiWindowFlags_NoBackground; // Important: note that we proceed even if Begin() returns false (aka window is collapsed). // This is because we want to keep our DockSpace() active. If a DockSpace() is inactive, // all active windows docked into it will lose their parent and become undocked. // We cannot preserve the docking relationship between an active window and an inactive docking, otherwise // any change of dockspace/settings would lead to windows being stuck in limbo and never being visible. ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); ImGui::Begin("###DockSpace", p_open, window_flags); ImGui::PopStyleVar(); if (opt_fullscreen) ImGui::PopStyleVar(2); // DockSpace ImGuiIO& io = ImGui::GetIO(); if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable) { ImGuiID dockspace_id = ImGui::GetID("MyDockSpace"); ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags); } else { //ShowDockingDisabledMessage(); } ImGui::End(); }

And for the helperfunctions:

void ImGuiUIManager::setstyle() { ImGuiStyle* style = &ImGui::GetStyle(); ImVec4* colors = style->Colors; colors[ImGuiCol_Text] = ImVec4(1.000f, 1.000f, 1.000f, 1.000f); colors[ImGuiCol_TextDisabled] = ImVec4(0.500f, 0.500f, 0.500f, 1.000f); colors[ImGuiCol_WindowBg] = ImVec4(0.180f, 0.180f, 0.180f, 1.000f); colors[ImGuiCol_ChildBg] = ImVec4(0.280f, 0.280f, 0.280f, 0.000f); colors[ImGuiCol_PopupBg] = ImVec4(0.313f, 0.313f, 0.313f, 1.000f); colors[ImGuiCol_Border] = ImVec4(0.266f, 0.266f, 0.266f, 1.000f); colors[ImGuiCol_BorderShadow] = ImVec4(0.000f, 0.000f, 0.000f, 0.000f); colors[ImGuiCol_FrameBg] = ImVec4(0.160f, 0.160f, 0.160f, 1.000f); colors[ImGuiCol_FrameBgHovered] = ImVec4(0.200f, 0.200f, 0.200f, 1.000f); colors[ImGuiCol_FrameBgActive] = ImVec4(0.280f, 0.280f, 0.280f, 1.000f); colors[ImGuiCol_TitleBg] = ImVec4(0.148f, 0.148f, 0.148f, 1.000f); colors[ImGuiCol_TitleBgActive] = ImVec4(0.148f, 0.148f, 0.148f, 1.000f); colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.148f, 0.148f, 0.148f, 1.000f); colors[ImGuiCol_MenuBarBg] = ImVec4(0.195f, 0.195f, 0.195f, 1.000f); colors[ImGuiCol_ScrollbarBg] = ImVec4(0.160f, 0.160f, 0.160f, 1.000f); colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.277f, 0.277f, 0.277f, 1.000f); colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.300f, 0.300f, 0.300f, 1.000f); colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(1.000f, 0.391f, 0.000f, 1.000f); colors[ImGuiCol_CheckMark] = ImVec4(1.000f, 1.000f, 1.000f, 1.000f); colors[ImGuiCol_SliderGrab] = ImVec4(0.391f, 0.391f, 0.391f, 1.000f); colors[ImGuiCol_SliderGrabActive] = ImVec4(1.000f, 0.391f, 0.000f, 1.000f); colors[ImGuiCol_Button] = ImVec4(1.000f, 1.000f, 1.000f, 0.000f); colors[ImGuiCol_ButtonHovered] = ImVec4(1.000f, 1.000f, 1.000f, 0.156f); colors[ImGuiCol_ButtonActive] = ImVec4(1.000f, 1.000f, 1.000f, 0.391f); colors[ImGuiCol_Header] = ImVec4(0.313f, 0.313f, 0.313f, 1.000f); colors[ImGuiCol_HeaderHovered] = ImVec4(0.469f, 0.469f, 0.469f, 1.000f); colors[ImGuiCol_HeaderActive] = ImVec4(0.469f, 0.469f, 0.469f, 1.000f); colors[ImGuiCol_Separator] = colors[ImGuiCol_Border]; colors[ImGuiCol_SeparatorHovered] = ImVec4(0.391f, 0.391f, 0.391f, 1.000f); colors[ImGuiCol_SeparatorActive] = ImVec4(1.000f, 0.391f, 0.000f, 1.000f); colors[ImGuiCol_ResizeGrip] = ImVec4(1.000f, 1.000f, 1.000f, 0.250f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(1.000f, 1.000f, 1.000f, 0.670f); colors[ImGuiCol_ResizeGripActive] = ImVec4(1.000f, 0.391f, 0.000f, 1.000f); colors[ImGuiCol_Tab] = ImVec4(0.098f, 0.098f, 0.098f, 1.000f); colors[ImGuiCol_TabHovered] = ImVec4(0.352f, 0.352f, 0.352f, 1.000f); colors[ImGuiCol_TabActive] = ImVec4(0.195f, 0.195f, 0.195f, 1.000f); colors[ImGuiCol_TabUnfocused] = ImVec4(0.098f, 0.098f, 0.098f, 1.000f); colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.195f, 0.195f, 0.195f, 1.000f); colors[ImGuiCol_DockingPreview] = ImVec4(1.000f, 0.391f, 0.000f, 0.781f); colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.180f, 0.180f, 0.180f, 1.000f); colors[ImGuiCol_PlotLines] = ImVec4(0.469f, 0.469f, 0.469f, 1.000f); colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.000f, 0.391f, 0.000f, 1.000f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.586f, 0.586f, 0.586f, 1.000f); colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.000f, 0.391f, 0.000f, 1.000f); colors[ImGuiCol_TextSelectedBg] = ImVec4(1.000f, 1.000f, 1.000f, 0.156f); colors[ImGuiCol_DragDropTarget] = ImVec4(1.000f, 0.391f, 0.000f, 1.000f); colors[ImGuiCol_NavHighlight] = ImVec4(1.000f, 0.391f, 0.000f, 1.000f); colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.000f, 0.391f, 0.000f, 1.000f); colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.000f, 0.000f, 0.000f, 0.586f); colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.000f, 0.000f, 0.000f, 0.586f); style->ChildRounding = 4.0f; style->FrameBorderSize = 1.0f; style->FrameRounding = 2.0f; style->GrabMinSize = 7.0f; style->PopupRounding = 2.0f; style->ScrollbarRounding = 12.0f; style->ScrollbarSize = 13.0f; style->TabBorderSize = 1.0f; style->TabRounding = 0.0f; style->WindowRounding = 4.0f; } void ImGuiUIManager::updateWindows() { for (auto i = 0; i < windows.size(); i++) { windows[i].update(); } } void addWindow(ImGuiWindow newWin) { windows.push_back(window); }

I chose for a dark theme with orange tints, but there are loads to be found online, or you can create one yourself.

And thats all we need to create desktopapplication using Dear imgui. Create your own windows and submit them to the windowmanager. Make sure to call the update and render functions about 60 times per second and your set to go. Feel free to share your creations and questions down below!

Ruurd

Usefull links

Dear Imgui github page: https://github.com/ocornut/imgui/tree/docking

Dear Imgui gallery: https://github.com/ocornut/imgui/issues/2847

GLFW window creation: https://learnopengl.com/Getting-started/OpenGL