Doxygen Samurai Engine 0.0.1
Doxygen Samurai Engine Documentation
Loading...
Searching...
No Matches
gui.cpp
Go to the documentation of this file.
1#include "gui.h"
2
3
4
5void *samurai::samuraiImgui::imguiCustomAlloc(size_t sz, void *user_data)
6{
8 SAMURAI_DEVELOPMENT_ONLY_ASSERT(allocator, "no allocator for imgui");
9
10 return allocator->allocate(sz);
11}
12
13void samurai::samuraiImgui::imguiCustomFree(void *ptr, void *user_data)
14{
16 SAMURAI_DEVELOPMENT_ONLY_ASSERT(allocator, "no allocator for imgui");
17
18 allocator->free(ptr);
19}
20
22{
23 ::ImGui::SetAllocatorFunctions(imguiCustomAlloc, imguiCustomFree, &allocator);
24}
25
26
28{
30
31 auto context = ::ImGui::CreateContext();
32 //ImGui::StyleColorsDark();
33 imguiThemes::embraceTheDarkness();
34
35 ImGuiIO &io = ::ImGui::GetIO();
36 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
37 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
38 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
39 io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
40 //io.ConfigViewportsNoAutoMerge = true;
41 //io.ConfigViewportsNoTaskBarIcon = true;
42
43 ImGuiStyle &style = ::ImGui::GetStyle();
44 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
45 {
46 //style.WindowRounding = 0.0f;
47 style.Colors[ImGuiCol_WindowBg].w = 0.f;
48 style.Colors[ImGuiCol_DockingEmptyBg].w = 0.f;
49 }
50
51#if !SAMURAI_SDL
52 ImGui_ImplGlfw_InitForOpenGL(pikaContext.wind, true);
53 ImGui_ImplOpenGL3_Init("#version 330");
54#else
55 ImGui_ImplSDL2_InitForSDLRenderer(pikaContext.wind, pikaContext.renderer );
56 ImGui_ImplSDLRenderer2_Init(pikaContext.renderer);
57#endif
58
59 pikaContext.ImGuiContext = context;
60
61
62 //https://pixtur.github.io/mkdocs-for-imgui/site/FONTS/
63 //https://github.com/juliettef/IconFontCppHeaders
64 //https://fontawesome.com/v4/icons/
65 io.Fonts->AddFontFromFileTTF(SAMURAI_RESOURCES_PATH "arial.ttf", 16);
66
67
68 ImFontConfig config;
69 config.MergeMode = true;
70 config.GlyphMinAdvanceX = 16.0f; // Use if you want to make the icon monospaced
71 static const ImWchar icon_ranges[] = {ICON_MIN_FK, ICON_MAX_FK, 0};
72 io.Fonts->AddFontFromFileTTF(SAMURAI_RESOURCES_PATH "fontawesome-webfont.ttf", 16.0f, &config, icon_ranges);
73
74 {
75 ImVector<ImWchar> ranges;
76 ImFontGlyphRangesBuilder builder;
77 builder.AddChar(0xf016);//ICON_FK_FILE_O
78 builder.AddChar(0xf114);//ICON_FK_FOLDER_O
79 builder.BuildRanges(&ranges);
80
81 io.Fonts->AddFontFromFileTTF(SAMURAI_RESOURCES_PATH "fontawesome-webfont.ttf", 150, 0, ranges.Data);
82 }
83 io.Fonts->Build();
84
85
86}
87
89{
90 ::ImGui::SetCurrentContext(pikaContext.ImGuiContext);
91}
92
94{
95 setImguiContext(pikaContext);
96#if !SAMURAI_SDL
97 ImGui_ImplOpenGL3_NewFrame();
98 ImGui_ImplGlfw_NewFrame();
99 ImGui::NewFrame();
100 ImGui::DockSpaceOverViewport(::ImGui::GetWindowViewport());
101#else
102 ImGui_ImplSDLRenderer2_NewFrame();
103 ImGui_ImplSDL2_NewFrame();
104 ImGui::NewFrame();
105#endif
106
107}
108
109
111{
112 setImguiContext(pikaContext);
113 ::ImGui::Render();
114 int display_w = 0, display_h = 0;
115
116#if !SAMURAI_SDL
117 glfwGetFramebufferSize(pikaContext.wind, &display_w, &display_h);
118 glViewport(0, 0, display_w, display_h);
119 ImGui_ImplOpenGL3_RenderDrawData(::ImGui::GetDrawData());
120#else
121 SDL_GetWindowSize(pikaContext.wind, &display_w, &display_h);
122 SDL_Rect viewport{0,0,display_w,display_h};
123 SDL_RenderSetViewport(pikaContext.renderer, &viewport);
124 ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData());
125#endif
126
127 ImGuiIO &io = ::ImGui::GetIO();
128
129 // Update and Render additional Platform Windows
130 // (Platform functions may change the current OpenGL context, so we save/restore it to make it easier to paste this code elsewhere.
131 // For this specific demo app we could also call glfwMakeContextCurrent(window) directly)
132 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
133 {
134 //GLFWwindow *backup_current_context = glfwGetCurrentContext();
135 //ImGui::UpdatePlatformWindows();
136 //ImGui::RenderPlatformWindowsDefault();
137 //glfwMakeContextCurrent(backup_current_context);
138
139 ::ImGui::UpdatePlatformWindows();
140 ::ImGui::RenderPlatformWindowsDefault();
141#if !SAMURAI_SDL
142 pikaContext.glfwMakeContextCurrentPtr(pikaContext.wind); //idea create a class with some functions
143#endif
144 }
145}
146
148{
149 ::ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(255, 0, 0, 255));
150 ::ImGui::Text(ICON_FK_TIMES_CIRCLE " ");
151 ::ImGui::PopStyleColor();
152}
153
155{
156 ::ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(255, 255, 0, 255));
157 ::ImGui::Text(ICON_FK_EXCLAMATION_TRIANGLE " ");
158 ::ImGui::PopStyleColor();
159}
160
161//https://github.com/ocornut/imgui/discussions/3862
162void samurai::samuraiImgui::alignForWidth(float width, float alignment)
163{
164 ImGuiStyle &style = ::ImGui::GetStyle();
165 float avail = ::ImGui::GetContentRegionAvail().x;
166 float off = (avail - width) * alignment;
167 if (off > 0.0f)
168 ::ImGui::SetCursorPosX(::ImGui::GetCursorPosX() + off);
169}
170
171static int sizesType = 0;
172
174{
175
176 switch (sizesType)
177 {
178 case 0:
179 ImGui::Text("%" IM_PRIu64 " (bytes)", value);
180 break;
181 case 1:
182 ImGui::Text("%f (KB)", samurai::BYTES_TO_KB(value));
183 break;
184 case 2:
185 ImGui::Text("%f (MB)", samurai::BYTES_TO_MB(value));
186 break;
187 case 3:
188 ImGui::Text("%f (GB)", samurai::BYTES_TO_GB(value));
189 break;
190 default:
191 break;
192 }
193
194}
195
197{
198 ImGui::Combo("Sizes type##samurai", &sizesType, "Bytes\0KB\0MB\0GB\0");
199}
200
201
202
static int sizesType
Definition gui.cpp:171
#define IM_PRIu64
Definition gui.h:25
void initImgui(Context &pikaContext)
Definition gui.cpp:27
void setImguiContext(Context pikaContext)
Definition gui.cpp:88
void setImguiAllocator(samurai::memory::CustomAllocator &allocator)
Definition gui.cpp:21
void displayMemorySizeToggle()
Definition gui.cpp:196
void displayMemorySizeValue(size_t value)
Definition gui.cpp:173
void imguiCustomFree(void *ptr, void *user_data)
Definition gui.cpp:13
void alignForWidth(float width, float alignment=0.5f)
Definition gui.cpp:162
void addWarningSymbol()
Definition gui.cpp:154
void addErrorSymbol()
Definition gui.cpp:147
void * imguiCustomAlloc(size_t sz, void *user_data)
Definition gui.cpp:5
void imguiEndFrame(Context pikaContext)
Definition gui.cpp:110
void imguiStartFrame(Context pikaContext)
Definition gui.cpp:93
float constexpr BYTES_TO_MB(size_t x)
Definition Sizes.h:11
float constexpr BYTES_TO_KB(size_t x)
Definition Sizes.h:10
float constexpr BYTES_TO_GB(size_t x)
Definition Sizes.h:12
samurai::memory::CustomAllocator imguiAllocator
Definition Context.h:25
ImGuiContext * ImGuiContext
Definition Context.h:14
GLFWwindow * wind
Definition Context.h:19
glfwMakeContextCurrent_t * glfwMakeContextCurrentPtr
Definition Context.h:18