sculpto
gui.cpp
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file gui.cpp
3 * \brief Application graphics user interface layer class implementation module.
4 * Handles user GUI events and widgets render.
5 *
6 * \author Sabitov Kirill
7 * \date 28 June 2022
8 *********************************************************************/
9
10#include "sclpch.h"
11
12#include <imgui.h>
13#include <backends/imgui_impl_opengl3.h>
14#include <backends/imgui_impl_win32.h>
15
16#include "gui.h"
17#include "platform/opengl/gl.h"
18#include "../application/application.h"
19#include "../render/render_context.h"
20
21bool scl::gui::IsDockspace { true };
22
24{
25 // Setup ImGui context
26 IMGUI_CHECKVERSION();
27 ImGui::CreateContext();
28 ImGui::StyleColorsClassic();
29
30 // Setting ImGui backend flags
31 ImGuiIO &io = ImGui::GetIO(); (void)io;
32 io.ConfigWindowsMoveFromTitleBarOnly = true;
33 io.ConfigFlags |= ImGuiConfigFlags_NavNoCaptureKeyboard; // Disable Keyboard Controls
34 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
35 io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
36 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable && // Enable Multi-Viewport / Platform Windows / Not OpenGL
38
39 // Configuring ImGui styles
40 SetUpTheme();
41
42 // Initialise platform depended ImGui context
43#ifdef SCL_PLATFORM_WINDOWS
44 const application &app = application::Get();
45 ImGui_ImplWin32_Init(app.GetWindow().GetHandle());
46#else
47# error Other platforms currently dont support GUI
48#endif
49
50 // Initialise ImGui render api dependent context
51 switch (render_context::GetApi())
52 {
53 case render_context_api::OpenGL: ImGui_ImplOpenGL3_Init("#version 450 core"); break;
54 case render_context_api::DirectX: SCL_CORE_ASSERT(0, "DirectX currently dont support GUI"); return;
55 default: SCL_CORE_ASSERT(0, "Other platforms currently dont support GUI"); return;
56 }
57
58 // Set up event handlers
59 event_dispatcher::AddEventListner<keyboard_event>([&io](keyboard_event &) { return io.WantCaptureKeyboard; });
60 event_dispatcher::AddEventListner<mouse_button_event>([&io](mouse_button_event &) { return io.WantCaptureMouse; });
61 event_dispatcher::AddEventListner<mouse_move_event>([&io](mouse_move_event &) { return io.WantCaptureMouse; });
62 event_dispatcher::AddEventListner<mouse_wheel_event>([&io](mouse_wheel_event &) { return io.WantCaptureMouse; });
63
64 SCL_CORE_SUCCES("GUI system initialised.");
65}
66
68{
69 // Deinitialise render context dependent ImGui Implementation
70 switch (render_context::GetApi())
71 {
72 case render_context_api::OpenGL: ImGui_ImplOpenGL3_Shutdown(); break;
73 case render_context_api::DirectX: SCL_CORE_ASSERT(0, "DirectX currently dont support GUI"); return;
74 default: SCL_CORE_ASSERT(0, "Other render APIs currently dont support GUI"); return;
75 }
76
77 // Deinitialise ImGui render api dependent context
78#ifdef SCL_PLATFORM_WINDOWS
79 ImGui_ImplWin32_Shutdown();
80#else
81# error Other platforms currently dont support GUI
82#endif
83
84 // Deinitialise ImGui core conext
85 ImGui::DestroyContext();
86 SCL_CORE_SUCCES("GUI system deinitialised.");
87}
88
90{
91 ImGui_ImplOpenGL3_NewFrame();
92 ImGui_ImplWin32_NewFrame();
93 ImGui::NewFrame();
94
95 if (IsDockspace) DrawDockspace();
96}
97
99{
100 ImGuiIO &io = ImGui::GetIO();
101 const window::data &win_data = application::Get().GetWindow().GetWindowData();
102 io.DisplaySize = ImVec2((float)win_data.Width, (float)win_data.Height);
103
104 ImGui::Render();
105 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
106
107 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
108 {
109#ifdef SCL_PLATFORM_WINDOWS
110 ImGui::UpdatePlatformWindows();
111 ImGui::RenderPlatformWindowsDefault();
112#else
113# error Other platforms currently dont support GUI
114#endif
115 }
116}
117
118void scl::gui::DrawDockspace()
119{
120 ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_None;
121 ImGuiWindowFlags window_flags =
122 ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus |
123 ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
124
125 const ImGuiViewport *viewport = ImGui::GetMainViewport();
126 ImGui::SetNextWindowPos(viewport->WorkPos);
127 ImGui::SetNextWindowSize(viewport->WorkSize);
128 ImGui::SetNextWindowViewport(viewport->ID);
129 ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
130 ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
131 ImGui::Begin("DockSpace Demo", &IsDockspace, window_flags);
132 ImGui::PopStyleVar(2);
133
134 ImGuiIO &io = ImGui::GetIO();
135 if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable)
136 {
137 ImGuiID dockspace_id = ImGui::GetID("MyDockSpace");
138 ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
139 }
140 ImGui::End();
141}
142
143void scl::gui::SetUpTheme()
144{
145 auto& io = ImGui::GetIO();
146 float fontSize = 15.0f;
147 io.Fonts->AddFontFromFileTTF("assets/fonts/Quicksand-Regular.ttf", fontSize);
148 io.FontDefault = io.Fonts->AddFontFromFileTTF("assets/fonts/Quicksand-Regular.ttf", fontSize);
149
150 auto &style = ImGui::GetStyle();
151 style.FrameRounding = 4.0f;
152 style.WindowBorderSize = 0.0f;
153 style.WindowRounding = 4.0f;
154 style.PopupBorderSize = 0.0f;
155 style.GrabRounding = 4.0f;
156 style.ChildRounding = 4.0f;
157
158 ImVec4 *colors = style.Colors;
159 colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
160 colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
161 colors[ImGuiCol_WindowBg] = ImVec4(0.10f, 0.10f, 0.10f, 1.00f);
162 colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
163 colors[ImGuiCol_PopupBg] = ImVec4(0.19f, 0.19f, 0.19f, 0.92f);
164 colors[ImGuiCol_Border] = ImVec4(0.19f, 0.19f, 0.19f, 0.29f);
165 colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.24f);
166 colors[ImGuiCol_FrameBg] = ImVec4(0.05f, 0.05f, 0.05f, 0.54f);
167 colors[ImGuiCol_FrameBgHovered] = ImVec4(0.19f, 0.19f, 0.19f, 0.54f);
168 colors[ImGuiCol_FrameBgActive] = ImVec4(0.20f, 0.22f, 0.23f, 1.00f);
169 colors[ImGuiCol_TitleBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
170 colors[ImGuiCol_TitleBgActive] = ImVec4(0.06f, 0.06f, 0.06f, 1.00f);
171 colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
172 colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
173 colors[ImGuiCol_ScrollbarBg] = ImVec4(0.05f, 0.05f, 0.05f, 0.54f);
174 colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.34f, 0.34f, 0.34f, 0.54f);
175 colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.40f, 0.40f, 0.54f);
176 colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.56f, 0.56f, 0.56f, 0.54f);
177 colors[ImGuiCol_CheckMark] = ImVec4(0.33f, 0.67f, 0.86f, 1.00f);
178 colors[ImGuiCol_SliderGrab] = ImVec4(0.34f, 0.34f, 0.34f, 0.54f);
179 colors[ImGuiCol_SliderGrabActive] = ImVec4(0.56f, 0.56f, 0.56f, 0.54f);
180 colors[ImGuiCol_Button] = ImVec4(0.05f, 0.05f, 0.05f, 0.54f);
181 colors[ImGuiCol_ButtonHovered] = ImVec4(0.19f, 0.19f, 0.19f, 0.54f);
182 colors[ImGuiCol_ButtonActive] = ImVec4(0.20f, 0.22f, 0.23f, 1.00f);
183 colors[ImGuiCol_Header] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
184 colors[ImGuiCol_HeaderHovered] = ImVec4(0.00f, 0.00f, 0.00f, 0.36f);
185 colors[ImGuiCol_HeaderActive] = ImVec4(0.20f, 0.22f, 0.23f, 0.33f);
186 colors[ImGuiCol_Separator] = ImVec4(0.28f, 0.28f, 0.28f, 0.29f);
187 colors[ImGuiCol_SeparatorHovered] = ImVec4(0.44f, 0.44f, 0.44f, 0.29f);
188 colors[ImGuiCol_SeparatorActive] = ImVec4(0.40f, 0.44f, 0.47f, 1.00f);
189 colors[ImGuiCol_ResizeGrip] = ImVec4(0.28f, 0.28f, 0.28f, 0.29f);
190 colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.44f, 0.44f, 0.44f, 0.29f);
191 colors[ImGuiCol_ResizeGripActive] = ImVec4(0.40f, 0.44f, 0.47f, 1.00f);
192 colors[ImGuiCol_Tab] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
193 colors[ImGuiCol_TabHovered] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
194 colors[ImGuiCol_TabActive] = ImVec4(0.20f, 0.20f, 0.20f, 0.36f);
195 colors[ImGuiCol_TabUnfocused] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
196 colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
197 colors[ImGuiCol_DockingPreview] = ImVec4(0.33f, 0.67f, 0.86f, 1.00f);
198 colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.09f, 0.08f, 0.09f, 1.00f);
199 colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
200 colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
201 colors[ImGuiCol_PlotHistogram] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
202 colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
203 colors[ImGuiCol_TableHeaderBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
204 colors[ImGuiCol_TableBorderStrong] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
205 colors[ImGuiCol_TableBorderLight] = ImVec4(0.28f, 0.28f, 0.28f, 0.29f);
206 colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
207 colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f);
208 colors[ImGuiCol_TextSelectedBg] = ImVec4(0.20f, 0.22f, 0.23f, 1.00f);
209 colors[ImGuiCol_DragDropTarget] = ImVec4(0.33f, 0.67f, 0.86f, 1.00f);
210 colors[ImGuiCol_NavHighlight] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
211 colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 0.00f, 0.00f, 0.70f);
212 colors[ImGuiCol_NavWindowingDimBg] = ImVec4(1.00f, 0.00f, 0.00f, 0.20f);
213 colors[ImGuiCol_ModalWindowDimBg] = ImVec4(1.00f, 0.00f, 0.00f, 0.35f);
214 colors[ImGuiCol_PlotLines] = ImVec4(0.51f, 0.42f, 0.42f, 1.00f);
215}
#define SCL_CORE_ASSERT(expr,...)
Definition: assert.h:69
static void SubmitUpdate()
Definition: gui.cpp:98
static void Init()
Definition: gui.cpp:23
static void BeginUpdate()
Definition: gui.cpp:89
static bool IsDockspace
Definition: gui.h:20
static void Close()
Definition: gui.cpp:67
static render_context_api GetApi()
#define SCL_CORE_SUCCES(...)
Definition: log.h:42
Sculpto library prehompiled header. Defines common definitions, includes commonly used modules.