PeriDyno 1.0.0
Loading...
Searching...
No Matches
imgui_extend.cpp
Go to the documentation of this file.
1#define IMGUI_DEFINE_MATH_OPERATORS
2#include <imgui_internal.h>
3
4#include "imgui_extend.h"
5#include "Platform.h"
6#include "Math/SimpleMath.h"
7
8thread_local ImGuiContext* MyImGuiTLS;
9
13
15 // patch for thread local context...
16 GImGui = GetCurrentContext();
17 ImGuiWindow* window = GetCurrentWindow();
18 // window->DC.CursorPos = window->DC.CursorMaxPos = ImVec2(bar_rect.Min.x + window->DC.MenuBarOffset.x, bar_rect.Min.y + window->DC.MenuBarOffset.y);
19 window->DC.LayoutType = ImGuiLayoutType_Horizontal;
20}
21
23 // patch for thread local context...
24 GImGui = GetCurrentContext();
25 ImGuiWindow* window = GetCurrentWindow();
26 window->DC.LayoutType = ImGuiLayoutType_Vertical;
27}
28
29void ImGui::exampleButton(const char* label, bool *v)
30{
31 float padding = 10.0f;
32 float bounding = 1.0f;
33 ImVec2 p = ImGui::GetCursorScreenPos();
34 ImDrawList* draw_list = ImGui::GetWindowDrawList();
35 const ImVec2 label_size = ImGui::CalcTextSize(label);
36 const ImVec2 button_size = ImVec2(label_size.x + padding * 2, label_size.y + padding * 2);
37 const ImVec2 bound_size = ImVec2(button_size.x + bounding * 2, button_size.y + bounding * 2);
38 ImVec2 p_button = ImVec2(p.x + bounding, p.y + bounding);
39 ImVec2 p_label = ImVec2(p_button.x + padding, p_button.y + padding);
40
41 float radius = bound_size.y * 0.30f;
42
43 // Button
44 if (ImGui::InvisibleButton(label, bound_size))
45 *v = !*v;
46 ImVec4 col_bf4;
47 ImGuiStyle& style = ImGui::GetStyle();
48
49 // Custom color
50 if (ImGui::IsItemActivated()) col_bf4 = *v ? style.Colors[40] : style.Colors[23];
51 else if (ImGui::IsItemHovered()) col_bf4 = *v ? style.Colors[42] : style.Colors[24];
52 else col_bf4 = *v ? style.Colors[41] : style.Colors[22];
53
54 ImU32 col_bg = IM_COL32(255 * col_bf4.x, 255 * col_bf4.y, 255 * col_bf4.z, 255 * col_bf4.w);
55 ImU32 col_text = IM_COL32(255, 255, 255, 255);
56 ImU32 col_bound = IM_COL32(0,0,0,255);
57
58 // draw Rectangle
59 draw_list->AddRect(p, ImVec2(p.x + bound_size.x, p.y + bound_size.y), col_bound , radius);
60 draw_list->AddRectFilled(p_button, ImVec2(p_button.x + button_size.x, p_button.y + button_size.y), col_bg, radius);
61 draw_list->AddText(p_label, col_text, label);
62}
63
64
65
66bool ImGui::radioWithIconButton(const char* label, const char* tooltip, bool v)
67{
68 bool res = false;
69 if (v == true)
70 {
71 ImGui::Button(label);
72 if (ImGui::IsItemHovered())
73 ImGui::SetTooltip(tooltip);
74 }
75 else
76 {
77 ImGui::PushID(label);
78 ImGui::PushStyleColor(ImGuiCol_Button, ExColorsVal[ImGuiExColVal_Button_1]);
79 ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ExColorsVal[ImGuiExColVal_ButtonHovered_1]);
80 ImGui::PushStyleColor(ImGuiCol_ButtonActive, ExColorsVal[ImGuiExColVal_ButtonActive_1]);
81 if (ImGui::Button(label))
82 {
83 res = true;
84 }
85 if (ImGui::IsItemHovered())
86 ImGui::SetTooltip(tooltip);
87 ImGui::PopStyleColor(3);
88 ImGui::PopID();
89
90 }
91 return res;
92}
93
94
95bool ImGui::clickButton(const char* label, const char* tooltip)
96{
97 bool res = false;
98 ImGui::PushID(label);
99 ImGui::PushStyleColor(ImGuiCol_Button, ExColorsVal[ImGuiExColVal_Button_1]);
100 ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ExColorsVal[ImGuiExColVal_ButtonHovered_1]);
101 ImGui::PushStyleColor(ImGuiCol_ButtonActive, ExColorsVal[ImGuiExColVal_ButtonActive_1]);
102 if (ImGui::Button(label))
103 {
104 res = true;
105 }
106 if (ImGui::IsItemHovered())
107 ImGui::SetTooltip(tooltip);
108 ImGui::PopStyleColor(3);
109 ImGui::PopID();
110 return res;
111}
112
113void ImGui::toggleButton(ImTextureID texId, const char* label, bool *v)
114{
115 if (*v == true)
116 {
117
118 ImGui::PushID(label);
119 ImGui::PushStyleColor(ImGuiCol_Button, ExColorsVal[ImGuiExColVal_Button_1]);
120 ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ExColorsVal[ImGuiExColVal_ButtonHovered_1]);
121 ImGui::PushStyleColor(ImGuiCol_ButtonActive, ExColorsVal[ImGuiExColVal_ButtonActive_1]);
122 ImGui::ImageButtonWithText(texId, label);
123 if (ImGui::IsItemClicked(0))
124 {
125 *v = !*v;
126 }
127 ImGui::PopStyleColor(3);
128 ImGui::PopID();
129 }
130 else
131 {
132 if (ImGui::ImageButtonWithText(texId ,label))
133 *v = true;
134 }
135}
136
137void ImGui::toggleButton(const char* label, bool *v)
138{
139 if (*v == true)
140 {
141
142 ImGui::PushID(label);
143 ImGui::PushStyleColor(ImGuiCol_Button, ExColorsVal[ImGuiExColVal_Button_1]);
144 ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ExColorsVal[ImGuiExColVal_ButtonHovered_1]);
145 ImGui::PushStyleColor(ImGuiCol_ButtonActive, ExColorsVal[ImGuiExColVal_ButtonActive_1]);
146 ImGui::Button(label);
147 if (ImGui::IsItemClicked(0))
148 {
149 *v = !*v;
150 }
151 ImGui::PopStyleColor(3);
152 ImGui::PopID();
153 }
154 else
155 {
156 if (ImGui::Button(label))
157 *v = true;
158 }
159}
160bool ImGui::ImageButtonWithText(ImTextureID texId,const char* label,const ImVec2& imageSize, const ImVec2 &uv0, const ImVec2 &uv1, int frame_padding, const ImVec4 &bg_col, const ImVec4 &tint_col) {
161
162 // patch for thread local context...
163 GImGui = GetCurrentContext();
164 ImGuiWindow* window = GetCurrentWindow();
165
166 if (window->SkipItems)
167 return false;
168
169 ImVec2 size = imageSize;
170 if (size.x<=0 && size.y<=0) {size.x=size.y=ImGui::GetTextLineHeightWithSpacing();}
171 else {
172 if (size.x<=0) size.x=size.y;
173 else if (size.y<=0) size.y=size.x;
174 size*=window->FontWindowScale*ImGui::GetIO().FontGlobalScale;
175 }
176
177 const ImGuiStyle& style = GetStyle();
178
179 const ImGuiID id = window->GetID(label);
180 const ImVec2 textSize = ImGui::CalcTextSize(label,NULL,true);
181 const bool hasText = textSize.x>0;
182
183 const float innerSpacing = hasText ? ((frame_padding >= 0) ? (float)frame_padding : (style.ItemInnerSpacing.x)) : 0.f;
184 const ImVec2 padding = (frame_padding >= 0) ? ImVec2((float)frame_padding, (float)frame_padding) : style.FramePadding;
185 const ImVec2 totalSizeWithoutPadding(size.x+innerSpacing+textSize.x,size.y>textSize.y ? size.y : textSize.y);
186 const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + totalSizeWithoutPadding + padding*2);
187 ImVec2 start(0,0);
188 start = window->DC.CursorPos + padding;
189 if (size.y<textSize.y) start.y+=(textSize.y-size.y)*.5f;
190
191 const ImRect image_bb(start, start + size);
192 start = window->DC.CursorPos + padding;start.x+=size.x+innerSpacing;
193 if (size.y>textSize.y) start.y+=(size.y-textSize.y)*.5f;
194
195 ItemSize(bb);
196 if (!ItemAdd(bb, id))
197 return false;
198
199 bool hovered=false, held=false;
200 bool pressed = ButtonBehavior(bb, id, &hovered, &held);
201
202 // Render
203 const ImU32 col = GetColorU32((hovered && held) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button);
204 RenderFrame(bb.Min, bb.Max, col, true, ImClamp((float)ImMin(padding.x, padding.y), 0.0f, style.FrameRounding));
205 if (bg_col.w > 0.0f)
206 window->DrawList->AddRectFilled(image_bb.Min, image_bb.Max, GetColorU32(bg_col));
207
208 window->DrawList->AddImage(texId, image_bb.Min, image_bb.Max, uv0, uv1, GetColorU32(tint_col));
209
210 if (textSize.x>0) ImGui::RenderText(start,label);
211 return pressed;
212}
213
214void ImGui::beginTitle(const char* label){
215 // Avoid outputting label,push ImGui ID into stack
216 ImGui::PushID(label);
217}
218
220 // Pop ImGui ID
221 ImGui::PopID();
222}
223
224template<typename T, typename... Args>
225std::unique_ptr<T> make_unique(Args&&... args) {
226 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
227}
228
229template<typename T>
230std::shared_ptr<ImU32[]> ImGui::ToImU(T v, int size)
231{
232 auto vPtr = make_unique<ImU32[]>(size);
233 for(int i = 0; i < size; ++ i)
234 vPtr[i] = IM_COL32(v[i][0], v[i][1], v[i][2], 150);
235 return vPtr;
236}
237
239{
240 return IM_COL32((*v)[0] * 255, (*v)[1] * 255, (*v)[2] * 255, 150);
241}
242
243bool ImGui::ColorBar(char* label, float* values, ImU32* col, int length, int num_type)
244{
245 if (col == nullptr ) return false;
246 // patch for thread local context...
247 GImGui = GetCurrentContext();
248 ImGuiWindow* window = GetCurrentWindow();
249 if (window->SkipItems)
250 return false;
251
252 ImDrawList* draw_list = window->DrawList;
253 ImGuiStyle& style = GetStyle();
254 ImGuiIO& io = GetIO();
255 ImGuiID id = window->GetID(label);
256
257 const float width = CalcItemWidth();
258 //g->NextItemData.ClearFlags();
259
260 PushID(label);
261 BeginGroup();
262
263 float text_width = GetFrameHeight() * 3;
264 float text_height = GetFrameHeight();
265 float bars_width = GetFrameHeight() * 1.5;
266 float bars_height = ImMax(bars_width * 1, width - 1 * (bars_width + style.ItemInnerSpacing.x)); // Saturation/Value picking box
267 float offset_width = bars_width * 0.2;
268 ImVec2 bar_pos = window->DC.CursorPos;
269 ImVec2 real_bar_pos = bar_pos + ImVec2(0, 0.3 * text_height );
270 ImRect bb(bar_pos, bar_pos + ImVec2(bars_width + offset_width + text_width, bars_height + text_height));
271 int grid_count = length - 1;
272
273 // Set Item Size
274 ItemSize(ImVec2(bars_width + offset_width + text_width, bars_height + text_height));
275 if (!ItemAdd(bb, id))
276 {
277 EndGroup();
278 PopID();
279 return false;
280 }
281
282 for (int i = 0; i <= grid_count; ++i){
283 if (i != grid_count)
284 draw_list->AddRectFilledMultiColor(
285 ImVec2(real_bar_pos.x, real_bar_pos.y + i * (bars_height / grid_count)),
286 ImVec2(real_bar_pos.x + bars_width, real_bar_pos.y + (i + 1) * (bars_height / grid_count)),
287 col[i], col[i], col[i + 1], col[i + 1]);
288
289 if (values != nullptr){
290 char buf[20];
291 if (num_type == 0) // Decimal
292 sprintf(buf,"%-6.4f", values[i]);
293 else // Exponential
294 sprintf(buf,"%-6.2e", values[i]);
295 draw_list->AddText(ImVec2(bar_pos.x + bars_width + offset_width, bar_pos.y + i * (bars_height / grid_count)), IM_COL32(255,255,255,255),buf);
296 }
297 }
298 RenderFrameBorder(ImVec2(real_bar_pos.x, real_bar_pos.y), ImVec2(real_bar_pos.x + bars_width, real_bar_pos.y + bars_height), 0.0f);
299 EndGroup();
300 PopID();
301 return true;
302}
303
304
305ImU32 ImGui::ToHeatColor(const float v, const float v_min, const float v_max){
306 float x = dyno::clamp((v - v_min) / (v_max - v_min), float(0), float(1));
307 float r = dyno::clamp(float(-4 * abs(x - 0.75) + 2), float(0), float(1));
308 float g = dyno::clamp(float(-4 * abs(x - 0.50) + 2), float(0), float(1));
309 float b = dyno::clamp(float(-4 * abs(x) + 2), float(0), float(1));
310 return IM_COL32(r * 255, g * 255, b* 255, 150);
311}
312
313ImU32 ImGui::ToJetColor(const float v, const float v_min, const float v_max){
314 float x = dyno::clamp((v - v_min) / (v_max - v_min), float(0), float(1));
315 float r = dyno::clamp(float(-4 * abs(x - 0.75) + 1.5), float(0), float(1));
316 float g = dyno::clamp(float(-4 * abs(x - 0.50) + 1.5), float(0), float(1));
317 float b = dyno::clamp(float(-4 * abs(x - 0.25) + 1.5), float(0), float(1));
318 return IM_COL32(r * 255, g * 255, b* 255, 150);
319}
320
321void ImGui::initializeStyle(float scale)
322{
323 ImGuiStyle& style = ImGui::GetStyle();
324 style.ScaleAllSizes(scale);
325 style.WindowRounding = 7.0f;
326 style.ChildRounding = 7.0f;
327 style.FrameRounding = 7.0f;
328 style.PopupRounding = 7.0f;
329
330 ImGuiIO& io = ImGui::GetIO();
331 // Load a first font
332 //ImFont* font = io.Fonts->AddFontDefault();
333 //Default font as Qt
334
335 std::string arialPath = getAssetPath() + "font/arial.ttf";
336 io.Fonts->AddFontFromFileTTF(arialPath.c_str(), 13.0f);
337 // IconFont
338 ImFontConfig config;
339 config.MergeMode = true;
340 config.GlyphMinAdvanceX = 13.0f; // Use if you want to make the icon monospaced
341 static const ImWchar icon_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
342
343 std::string solidPath = getAssetPath() + "font/fa-solid-900.ttf";
344 io.Fonts->AddFontFromFileTTF(solidPath.c_str(), 13.0f, &config, icon_ranges);
345 io.Fonts->Build();
346
347 io.FontGlobalScale = scale;
348}
349
351{
352 ExColorsVal[ImGuiExColVal_Button_1] = ImVec4(230/255.0, 179/255.0, 0/255.0, 105/255.0);
353 ExColorsVal[ImGuiExColVal_ButtonHovered_1] = ImVec4(230/255.0, 179/255.0, 0/255.0, 255/255.0);
354 ExColorsVal[ImGuiExColVal_ButtonActive_1] = ImVec4(255/255.0, 153/255.0, 0/255.0, 255/255.0);
355 ExColorsVal[ImGuiExColVal_WindowTopBg_1] = ImVec4( 0/255.0, 0/255.0, 0/255.0, 10/255.0);
356 ExColorsVal[ImGuiExColVal_WindowMenuBg_1] = ImVec4( 36/255.0, 36/255.0, 36/255.0, 255/255.0);
357}
#define ICON_MIN_FA
#define ICON_MAX_FA
#define T(t)
thread_local ImGuiContext * MyImGuiTLS
std::unique_ptr< T > make_unique(Args &&... args)
IMGUI_API ImU32 ToJetColor(const float v, const float v_min, const float v_max)
@ ImGuiExColVal_WindowMenuBg_1
@ ImGuiExColVal_WindowTopBg_1
@ ImGuiExColVal_ButtonHovered_1
@ ImGuiExColVal_COUNT
@ ImGuiExColVal_Button_1
@ ImGuiExColVal_ButtonActive_1
std::shared_ptr< ImU32[]> ToImU(T v, int size)
IMGUI_API void exampleButton(const char *label, bool *v)
IMGUI_API bool clickButton(const char *label, const char *tooltip)
IMGUI_API bool ImageButtonWithText(ImTextureID texId, const char *label, const ImVec2 &imageSize=ImVec2(0, 0), const ImVec2 &uv0=ImVec2(0, 0), const ImVec2 &uv1=ImVec2(1, 1), int frame_padding=-1, const ImVec4 &bg_col=ImVec4(0, 0, 0, 0), const ImVec4 &tint_col=ImVec4(1, 1, 1, 1))
ImVec4 ExColorsVal[ImGuiExColVal_COUNT]
void initializeStyle(float scale)
IMGUI_API void initColorVal()
IMGUI_API bool ColorBar(char *label, float *values, ImU32 *col, int length, int num_type)
IMGUI_API void beginTitle(const char *label)
IMGUI_API void EndHorizontal()
IMGUI_API void toggleButton(ImTextureID texId, const char *label, bool *v)
IMGUI_API void endTitle()
IMGUI_API ImU32 ToHeatColor(const float v, const float v_min, const float v_max)
IMGUI_API bool radioWithIconButton(const char *label, const char *tooltip, bool v)
IMGUI_API void BeginHorizontal()
IMGUI_API ImU32 VecToImU(const dyno::Vec3f *v)
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
DYN_FUNC T clamp(const T &v, const T &lo, const T &hi)
Definition SimpleMath.h:42