Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Utils.cpp
Go to the documentation of this file.
1
2
3#include "Headers/Utils.h"
4
5#include <imgui_internal.h>
6
7namespace Divide
8{
9 namespace
10 {
12 static bool g_isBoldButtonPushed = false;
13 static bool g_isNarrowLabelWidthPushed = false;
14 static const char* g_pushedTooltip = "";
15 }
16} //namespace Divide
17
18namespace ImGui
19{
20 bool InputDoubleN( const char* label, double* v, const int components, const char* display_format, const ImGuiInputTextFlags extra_flags )
21 {
22 const ImGuiWindow* window = GetCurrentWindow();
23 if ( window->SkipItems )
24 return false;
25
26 const ImGuiContext& g = *GImGui;
27 bool value_changed = false;
28 BeginGroup();
29 PushID( label );
30 PushMultiItemsWidths( components, CalcItemWidth() );
31 for ( int i = 0; i < components; i++ )
32 {
33 PushID( i );
34 value_changed |= InputDouble( "##v", &v[i], 0.0, 0.0, display_format, extra_flags );
35 SameLine( 0, g.Style.ItemInnerSpacing.x );
36 PopID();
37 PopItemWidth();
38 }
39 PopID();
40
41 TextUnformatted( label, FindRenderedTextEnd( label ) );
42 EndGroup();
43
44 return value_changed;
45 }
46
47 bool InputDouble2( const char* label, double v[2], const char* display_format, const ImGuiInputTextFlags extra_flags )
48 {
49 return InputDoubleN( label, v, 2, display_format, extra_flags );
50 }
51
52 bool InputDouble3( const char* label, double v[3], const char* display_format, const ImGuiInputTextFlags extra_flags )
53 {
54 return InputDoubleN( label, v, 3, display_format, extra_flags );
55 }
56
57 bool InputDouble4( const char* label, double v[4], const char* display_format, const ImGuiInputTextFlags extra_flags )
58 {
59 return InputDoubleN( label, v, 4, display_format, extra_flags );
60 }
61}
62
63namespace Divide
64{
65
66 ImGuiKey DivideKeyToImGuiKey( const Input::KeyCode key ) noexcept
67 {
68 switch ( key )
69 {
70 case Input::KeyCode::KC_TAB: return ImGuiKey_Tab;
71 case Input::KeyCode::KC_LEFT: return ImGuiKey_LeftArrow;
72 case Input::KeyCode::KC_RIGHT: return ImGuiKey_RightArrow;
73 case Input::KeyCode::KC_UP: return ImGuiKey_UpArrow;
74 case Input::KeyCode::KC_DOWN: return ImGuiKey_DownArrow;
75 case Input::KeyCode::KC_PGUP: return ImGuiKey_PageUp;
76 case Input::KeyCode::KC_PGDOWN: return ImGuiKey_PageDown;
77 case Input::KeyCode::KC_HOME: return ImGuiKey_Home;
78 case Input::KeyCode::KC_END: return ImGuiKey_End;
79 case Input::KeyCode::KC_INSERT: return ImGuiKey_Insert;
80 case Input::KeyCode::KC_DELETE: return ImGuiKey_Delete;
81 case Input::KeyCode::KC_BACK: return ImGuiKey_Backspace;
82 case Input::KeyCode::KC_SPACE: return ImGuiKey_Space;
83 case Input::KeyCode::KC_RETURN: return ImGuiKey_Enter;
84 case Input::KeyCode::KC_ESCAPE: return ImGuiKey_Escape;
85 case Input::KeyCode::KC_APOSTROPHE: return ImGuiKey_Apostrophe;
86 case Input::KeyCode::KC_COMMA: return ImGuiKey_Comma;
87 case Input::KeyCode::KC_MINUS: return ImGuiKey_Minus;
88 case Input::KeyCode::KC_PERIOD: return ImGuiKey_Period;
89 case Input::KeyCode::KC_SLASH: return ImGuiKey_Slash;
90 case Input::KeyCode::KC_SEMICOLON: return ImGuiKey_Semicolon;
91 case Input::KeyCode::KC_EQUALS: return ImGuiKey_Equal;
92 case Input::KeyCode::KC_LBRACKET: return ImGuiKey_LeftBracket;
93 case Input::KeyCode::KC_BACKSLASH: return ImGuiKey_Backslash;
94 case Input::KeyCode::KC_RBRACKET: return ImGuiKey_RightBracket;
95 case Input::KeyCode::KC_GRAVE: return ImGuiKey_GraveAccent;
96 case Input::KeyCode::KC_CAPITAL: return ImGuiKey_CapsLock;
97 case Input::KeyCode::KC_SCROLL: return ImGuiKey_ScrollLock;
98 case Input::KeyCode::KC_NUMLOCK: return ImGuiKey_NumLock;
99 case Input::KeyCode::KC_PRINTSCREEN: return ImGuiKey_PrintScreen;
100 case Input::KeyCode::KC_PAUSE: return ImGuiKey_Pause;
101 case Input::KeyCode::KC_NUMPAD0: return ImGuiKey_Keypad0;
102 case Input::KeyCode::KC_NUMPAD1: return ImGuiKey_Keypad1;
103 case Input::KeyCode::KC_NUMPAD2: return ImGuiKey_Keypad2;
104 case Input::KeyCode::KC_NUMPAD3: return ImGuiKey_Keypad3;
105 case Input::KeyCode::KC_NUMPAD4: return ImGuiKey_Keypad4;
106 case Input::KeyCode::KC_NUMPAD5: return ImGuiKey_Keypad5;
107 case Input::KeyCode::KC_NUMPAD6: return ImGuiKey_Keypad6;
108 case Input::KeyCode::KC_NUMPAD7: return ImGuiKey_Keypad7;
109 case Input::KeyCode::KC_NUMPAD8: return ImGuiKey_Keypad8;
110 case Input::KeyCode::KC_NUMPAD9: return ImGuiKey_Keypad9;
111 case Input::KeyCode::KC_DECIMAL: return ImGuiKey_KeypadDecimal;
112 case Input::KeyCode::KC_DIVIDE: return ImGuiKey_KeypadDivide;
113 case Input::KeyCode::KC_MULTIPLY: return ImGuiKey_KeypadMultiply;
114 case Input::KeyCode::KC_SUBTRACT: return ImGuiKey_KeypadSubtract;
115 case Input::KeyCode::KC_ADD: return ImGuiKey_KeypadAdd;
116 case Input::KeyCode::KC_NUMPADENTER: return ImGuiKey_KeypadEnter;
117 case Input::KeyCode::KC_NUMPADEQUALS: return ImGuiKey_KeypadEqual;
118 case Input::KeyCode::KC_LCONTROL: return ImGuiKey_LeftCtrl;
119 case Input::KeyCode::KC_LSHIFT: return ImGuiKey_LeftShift;
120 case Input::KeyCode::KC_LMENU: return ImGuiKey_LeftAlt;
121 case Input::KeyCode::KC_LWIN: return ImGuiKey_LeftSuper;
122 case Input::KeyCode::KC_RCONTROL: return ImGuiKey_RightCtrl;
123 case Input::KeyCode::KC_RSHIFT: return ImGuiKey_RightShift;
124 case Input::KeyCode::KC_RMENU: return ImGuiKey_RightAlt;
125 case Input::KeyCode::KC_RWIN: return ImGuiKey_RightSuper;
126 case Input::KeyCode::KC_APPS: return ImGuiKey_Menu;
127 case Input::KeyCode::KC_0: return ImGuiKey_0;
128 case Input::KeyCode::KC_1: return ImGuiKey_1;
129 case Input::KeyCode::KC_2: return ImGuiKey_2;
130 case Input::KeyCode::KC_3: return ImGuiKey_3;
131 case Input::KeyCode::KC_4: return ImGuiKey_4;
132 case Input::KeyCode::KC_5: return ImGuiKey_5;
133 case Input::KeyCode::KC_6: return ImGuiKey_6;
134 case Input::KeyCode::KC_7: return ImGuiKey_7;
135 case Input::KeyCode::KC_8: return ImGuiKey_8;
136 case Input::KeyCode::KC_9: return ImGuiKey_9;
137 case Input::KeyCode::KC_A: return ImGuiKey_A;
138 case Input::KeyCode::KC_B: return ImGuiKey_B;
139 case Input::KeyCode::KC_C: return ImGuiKey_C;
140 case Input::KeyCode::KC_D: return ImGuiKey_D;
141 case Input::KeyCode::KC_E: return ImGuiKey_E;
142 case Input::KeyCode::KC_F: return ImGuiKey_F;
143 case Input::KeyCode::KC_G: return ImGuiKey_G;
144 case Input::KeyCode::KC_H: return ImGuiKey_H;
145 case Input::KeyCode::KC_I: return ImGuiKey_I;
146 case Input::KeyCode::KC_J: return ImGuiKey_J;
147 case Input::KeyCode::KC_K: return ImGuiKey_K;
148 case Input::KeyCode::KC_L: return ImGuiKey_L;
149 case Input::KeyCode::KC_M: return ImGuiKey_M;
150 case Input::KeyCode::KC_N: return ImGuiKey_N;
151 case Input::KeyCode::KC_O: return ImGuiKey_O;
152 case Input::KeyCode::KC_P: return ImGuiKey_P;
153 case Input::KeyCode::KC_Q: return ImGuiKey_Q;
154 case Input::KeyCode::KC_R: return ImGuiKey_R;
155 case Input::KeyCode::KC_S: return ImGuiKey_S;
156 case Input::KeyCode::KC_T: return ImGuiKey_T;
157 case Input::KeyCode::KC_U: return ImGuiKey_U;
158 case Input::KeyCode::KC_V: return ImGuiKey_V;
159 case Input::KeyCode::KC_W: return ImGuiKey_W;
160 case Input::KeyCode::KC_X: return ImGuiKey_X;
161 case Input::KeyCode::KC_Y: return ImGuiKey_Y;
162 case Input::KeyCode::KC_Z: return ImGuiKey_Z;
163 case Input::KeyCode::KC_F1: return ImGuiKey_F1;
164 case Input::KeyCode::KC_F2: return ImGuiKey_F2;
165 case Input::KeyCode::KC_F3: return ImGuiKey_F3;
166 case Input::KeyCode::KC_F4: return ImGuiKey_F4;
167 case Input::KeyCode::KC_F5: return ImGuiKey_F5;
168 case Input::KeyCode::KC_F6: return ImGuiKey_F6;
169 case Input::KeyCode::KC_F7: return ImGuiKey_F7;
170 case Input::KeyCode::KC_F8: return ImGuiKey_F8;
171 case Input::KeyCode::KC_F9: return ImGuiKey_F9;
172 case Input::KeyCode::KC_F10: return ImGuiKey_F10;
173 case Input::KeyCode::KC_F11: return ImGuiKey_F11;
174 case Input::KeyCode::KC_F12: return ImGuiKey_F12;
175
176 case Input::KeyCode::KC_SYSRQ: break;//PrintScreen?
218 }
219
220 return ImGuiKey_None;
221 }
222 namespace Util
223 {
225 {
226 return GImGui->Font->FontSize + GImGui->Style.FramePadding.y * 2.f;
227 }
228
230 {
231 ImVec2 min = ImGui::GetItemRectMin();
232 const ImVec2 max = ImGui::GetItemRectMax();
233 min.y = max.y;
234 ImGui::GetWindowDrawList()->AddLine( min, max, ImGui::GetColorU32( ImGuiCol_Text ), 1.0f );
235 }
236
237 void BeginPropertyTable( const I32 numComponents, const char* label )
238 {
239 ImFont* boldFont = ImGui::GetIO().Fonts->Fonts[1];
240
241 ImGui::PushID( label );
242 ImGui::Columns( 2 );
243 ImGui::SetColumnWidth( 0, g_isNarrowLabelWidthPushed ? LabelColumnWidthNarrow : LabelColumnWidth );
244 ImGui::PushFont( boldFont );
245 ImGui::Text( label );
246 if ( ImGui::IsItemHovered( ImGuiHoveredFlags_AllowWhenDisabled ) )
247 {
248 if ( Util::IsPushedTooltip() )
249 {
250 ImGui::SetTooltip( Util::PushedToolTip() );
251 }
252 else
253 {
254 ImGui::SetTooltip( label );
255 }
256 }
257
258 ImGui::PopFont();
259 ImGui::NextColumn();
260 if ( numComponents == 1 )
261 {
262 ImGui::PushItemWidth( ImGui::CalcItemWidth() );
263 }
264 else
265 {
266 ImGui::PushMultiItemsWidths( numComponents, ImGui::CalcItemWidth() );
267 }
268 g_lastComponentWidhtPushCount = numComponents;
269 }
270
272 {
273 for ( I32 i = 0; i < g_lastComponentWidhtPushCount; ++i )
274 {
275 ImGui::PopItemWidth();
276 }
277 g_lastComponentWidhtPushCount = 0;
278 ImGui::Columns( 1 );
279 ImGui::PopID();
280 }
281
283 {
284 if ( !g_isBoldButtonPushed )
285 {
286 ImGui::PushFont( ImGui::GetIO().Fonts->Fonts[1] );
287 g_isBoldButtonPushed = true;
288 }
289 }
290
292 {
293 if ( g_isBoldButtonPushed )
294 {
295 ImGui::PopFont();
296 g_isBoldButtonPushed = false;
297 }
298 }
299
301 {
302 if ( !g_isNarrowLabelWidthPushed )
303 {
304 g_isNarrowLabelWidthPushed = true;
305 }
306 }
307
309 {
310 if ( g_isNarrowLabelWidthPushed )
311 {
312 g_isNarrowLabelWidthPushed = false;
313 }
314 }
315
316 void PushTooltip( const char* tooltip )
317 {
318 g_pushedTooltip = tooltip;
319 }
320
322 {
323 g_pushedTooltip = "";
324 }
325
326 [[nodiscard]] bool IsPushedTooltip()
327 {
328 return strlen( g_pushedTooltip ) > 0;
329 }
330
331 const char* PushedToolTip()
332 {
333 return g_pushedTooltip;
334 }
335
336 void PushButtonStyle( const bool bold,
337 const ImVec4 buttonColour,
338 const ImVec4 buttonColourHovered,
339 const ImVec4 buttonColourActive )
340 {
341 if ( bold )
342 {
343 PushBoldFont();
344 }
345 ImGui::PushStyleColor( ImGuiCol_Button, buttonColour );
346 ImGui::PushStyleColor( ImGuiCol_ButtonHovered, buttonColourHovered );
347 ImGui::PushStyleColor( ImGuiCol_ButtonActive, buttonColourActive );
348 }
349
351 {
352 PopBoldFont();
353 ImGui::PopStyleColor( 3 );
354 }
355
356
357 const char* GetFormat( ImGuiDataType dataType, const char* input, const bool hex )
358 {
359 if ( input == nullptr || strlen( input ) == 0 )
360 {
361 const auto unsignedType = [dataType]()
362 {
363 return dataType == ImGuiDataType_U8 || dataType == ImGuiDataType_U16 || dataType == ImGuiDataType_U32 || dataType == ImGuiDataType_U64;
364 };
365
366 return dataType == ImGuiDataType_Float ? "%.3f"
367 : dataType == ImGuiDataType_Double ? "%.6f"
368 : hex ? "%08X" : (unsignedType() ? "%u" : "%d");
369 }
370
371 return input;
372 }
373
375 {
376 FColour4 val = field.get<FColour4>();
377 const auto setter = [val/*by value*/, &field]( const FColour4& col )
378 {
379 if ( col != val )
380 {
381 field.set( col );
382 return true;
383 }
384 return false;
385 };
386
387 return colourInput4( parent, field._name.c_str(), val, field._readOnly, setter );
388 }
389
391 {
392 FColour3 val = field.get<FColour3>();
393 const auto setter = [val/*by value*/, &field]( const FColour3& col )
394 {
395 if ( col != val )
396 {
397 field.set( col );
398 return true;
399 }
400 return false;
401 };
402
403 return colourInput3( parent, field._name.c_str(), val, field._readOnly, setter );
404 }
405
407 {
408 const ImGuiViewport* main_viewport = ImGui::GetMainViewport();
409 const ImVec2 workPos = main_viewport->WorkPos;
410 const ImVec2 workSize = main_viewport->WorkSize;
411 const ImVec2 targetPos = workPos + ImVec2( workSize.x * 0.5f, workSize.y * 0.5f );
412
413 ImGui::SetNextWindowPos( targetPos, ImGuiCond_Always, ImVec2( 0.5f, 0.5f ) );
414 }
415
416 void OpenCenteredPopup( const char* name, const ImGuiPopupFlags popup_flags )
417 {
419 ImGui::OpenPopup( name, popup_flags );
420 }
421
422 void PrintColouredText( const std::string_view text, const ImVec4& colour )
423 {
424 ImGui::PushStyleColor( ImGuiCol_Text, colour );
425 ImGui::TextUnformatted( text.data(), text.data() + text.length());
426 ImGui::PopStyleColor();
427 }
428
429 ImGuiInputTextFlags GetDefaultFlagsForSettings( const bool readOnly, const bool hex )
430 {
431 return ImGuiInputTextFlags_EnterReturnsTrue |
432 ImGuiInputTextFlags_CharsNoBlank |
433 (hex ? ImGuiInputTextFlags_CharsHexadecimal : ImGuiInputTextFlags_CharsDecimal) |
434 (readOnly ? ImGuiInputTextFlags_ReadOnly : 0u);
435 }
436
437 ImGuiInputTextFlags GetDefaultFlagsForField( const EditorComponentField& field )
438 {
440 }
441 } //namespace Util
442} //namespace Divide
void CenterNextWindow()
Definition: Utils.cpp:406
const char * PushedToolTip()
Definition: Utils.cpp:331
void PushTooltip(const char *tooltip)
Definition: Utils.cpp:316
void PushNarrowLabelWidth()
Definition: Utils.cpp:300
void PopBoldFont()
Definition: Utils.cpp:291
void PopButtonStyle()
Definition: Utils.cpp:350
F32 GetLineHeight() noexcept
Definition: Utils.cpp:224
void PopNarrowLabelWidth()
Definition: Utils.cpp:308
void OpenCenteredPopup(const char *name, ImGui::ImGuiPopupFlags popup_flags=0)
void PushBoldFont()
Definition: Utils.cpp:282
bool colourInput4(Editor &parent, EditorComponentField &field)
Definition: Utils.cpp:374
void PopTooltip()
Definition: Utils.cpp:321
void EndPropertyTable()
Definition: Utils.cpp:271
constexpr F32 LabelColumnWidth
Definition: Utils.h:55
void PushButtonStyle(bool bold, ImVec4 buttonColour, ImVec4 buttonColourHovered, ImVec4 buttonColourActive)
Definition: Utils.cpp:336
void AddUnderLine()
Definition: Utils.cpp:229
ImGuiInputTextFlags GetDefaultFlagsForField(const EditorComponentField &field)
Definition: Utils.cpp:437
bool colourInput3(Editor &parent, EditorComponentField &field)
Definition: Utils.cpp:390
void PrintColouredText(const std::string_view text, const ImVec4 &colour)
Definition: Utils.cpp:422
bool IsPushedTooltip()
Definition: Utils.cpp:326
const char * GetFormat(ImGuiDataType dataType, const char *input, bool hex)
Definition: Utils.cpp:357
constexpr F32 LabelColumnWidthNarrow
Definition: Utils.h:56
ImGuiInputTextFlags GetDefaultFlagsForSettings(bool readOnly, bool hex)
Definition: Utils.cpp:429
void BeginPropertyTable(I32 numComponents, const char *label)
Definition: Utils.cpp:237
static const char * g_pushedTooltip
Definition: Utils.cpp:14
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
int32_t I32
ImGuiKey DivideKeyToImGuiKey(const Input::KeyCode key) noexcept
Definition: Utils.cpp:66
Project & parent
Definition: DefaultScene.h:41
bool InputDouble4(const char *label, double v[4], const char *display_format, ImGuiInputTextFlags extra_flags)
Definition: Utils.cpp:57
bool InputDouble3(const char *label, double v[3], const char *display_format, ImGuiInputTextFlags extra_flags)
Definition: Utils.cpp:52
bool InputDouble2(const char *label, double v[2], const char *display_format, ImGuiInputTextFlags extra_flags)
Definition: Utils.cpp:47
bool InputDoubleN(const char *label, double *v, int components, const char *display_format, ImGuiInputTextFlags extra_flags)
Definition: Utils.cpp:20
void set(const T &dataIn)