FuncDoodle
Loading...
Searching...
No Matches
ImUtil.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include "UI/Gui.h"
18#include <algorithm>
19#include <filesystem>
20
26 // NOLINTBEGIN
27 inline constexpr char s_DefaultLayout[] = {
28#embed "../Conf/imgui.ini"
29 , 0};
30 // NOLINTEND
31
38 inline void SetupDefaultLayout() {
39 ImGuiIO& io = ImGui::GetIO();
40 const char* userIniPath = "imgui.ini";
41
42 io.IniFilename = userIniPath;
43
44 if (!std::filesystem::exists(userIniPath)) {
45 ImGui::LoadIniSettingsFromMemory(s_DefaultLayout);
46
47 std::ofstream outFile(userIniPath);
48 if (outFile) {
49 outFile << s_DefaultLayout;
50 outFile.close();
51 }
52 }
53 }
54
60
66 inline float ButtonWidth(const char* label) {
67 ImVec2 size = ImGui::CalcTextSize(label);
68 return size.x + (ImGui::GetStyle().FramePadding.x * 2.0f);
69 }
70
77 inline void AlignButtonsRight(float totalWidth) {
78 float avail = ImGui::GetContentRegionAvail().x;
79 float startX =
80 ImGui::GetCursorPosX() + std::max(0.0f, avail - totalWidth);
81 ImGui::SetCursorPosX(startX);
82 }
83
90 inline bool EnterPressed() {
91 return ImGui::IsKeyPressed(ImGuiKey_Enter, false) ||
92 ImGui::IsKeyPressed(ImGuiKey_KeypadEnter, false);
93 }
94
101 inline bool EscPressed() {
102 return ImGui::IsKeyPressed(ImGuiKey_Escape, false);
103 }
104
113 inline ButtonRowResult ConfirmButtons(const char* primary,
114 const char* secondary, const char* tertiary = nullptr) {
115 float spacing = ImGui::GetStyle().ItemSpacing.x;
116 float primaryWidth = ButtonWidth(primary);
117 float secondaryWidth = ButtonWidth(secondary);
118 float tertiaryWidth = tertiary ? ButtonWidth(tertiary) : 0.0f;
119 int count = tertiary ? 3 : 2;
120 float totalWidth = primaryWidth + secondaryWidth + tertiaryWidth +
121 (spacing * (count - 1));
122 AlignButtonsRight(totalWidth);
123
125 if (tertiary) {
126 if (ImGui::Button(tertiary))
128 ImGui::SameLine();
129 }
130 if (ImGui::Button(secondary))
132 ImGui::SameLine();
133 if (ImGui::Button(primary))
135 return result;
136 }
137
145 return ConfirmButtons("OK", "Cancel");
146 }
147
155 return ConfirmButtons("Yes", "No");
156 }
157
165 return ConfirmButtons("Yes", "No", "Cancel");
166 }
167
175 return ConfirmButtons("OK", "Close");
176 }
177
185 return ConfirmButtons("Export", "Close");
186 }
187
192 inline bool SingleButtonRow(const char* label) {
194 return ImGui::Button(label);
195 }
196
203 inline bool OkButton() {
204 return SingleButtonRow("OK");
205 }
206
213 inline bool CloseButton() {
214 return SingleButtonRow("Close");
215 }
216
223 inline ImGuiKey GetAnyReleasedKey() {
224 for (int i = ImGuiKey_NamedKey_BEGIN; i < ImGuiKey_NamedKey_END; i++) {
225 auto key = (ImGuiKey)i;
226 if (key == ImGuiKey_LeftCtrl || key == ImGuiKey_RightCtrl ||
227 key == ImGuiKey_LeftShift || key == ImGuiKey_RightShift ||
228 key == ImGuiKey_LeftAlt || key == ImGuiKey_RightAlt ||
229 key == ImGuiKey_LeftSuper || key == ImGuiKey_RightSuper ||
230 key == ImGuiKey_MouseLeft || key == ImGuiKey_MouseRight)
231 continue;
232 if (ImGui::IsKeyReleased(key))
233 return key;
234 }
235 return ImGuiKey_None;
236 }
237
248 inline const char* GetUsername() {
249 const char* username =
250 std::getenv("USER"); // Common on Linux and macOS
251 if (!username) {
252 username = std::getenv("LOGNAME"); // Fallback for Linux and macOS
253 }
254 if (!username) {
255 username = std::getenv("USERNAME"); // Common on Windows
256 }
257 if (!username) {
258 username = "unknown";
259 }
260
261 return username;
262 }
263} // namespace FuncDoodle::ImUtil
ImGui/OpenGL/GLFW integration utilities and file dialog wrapper.
Definition ImUtil.h:25
ButtonRowResult YesNoButtons()
Renders a Yes/No button row.
Definition ImUtil.h:154
ButtonRowResult
Identifies which button was pressed in a button row.
Definition ImUtil.h:59
@ Tertiary
Definition ImUtil.h:59
@ Primary
Definition ImUtil.h:59
@ Secondary
Definition ImUtil.h:59
@ None
Definition ImUtil.h:59
bool EscPressed()
Returns true when Escape is pressed this frame.
Definition ImUtil.h:101
ImGuiKey GetAnyReleasedKey()
Returns the first non-modifier key released this frame.
Definition ImUtil.h:223
float ButtonWidth(const char *label)
Returns the width needed to render a button with the given label.
Definition ImUtil.h:66
void AlignButtonsRight(float totalWidth)
Right-aligns the next row of buttons within the current content region.
Definition ImUtil.h:77
const char * GetUsername()
Small util function for fetching the current users' username. Used for filling out the animation auth...
Definition ImUtil.h:248
constexpr char s_DefaultLayout[]
Definition ImUtil.h:27
ButtonRowResult OkCancelButtons()
Renders an OK/Cancel button row.
Definition ImUtil.h:144
ButtonRowResult ConfirmButtons(const char *primary, const char *secondary, const char *tertiary=nullptr)
Renders CONFIRM buttons for popups.
Definition ImUtil.h:113
ButtonRowResult CloseOkButtons()
Renders an OK/Close button row.
Definition ImUtil.h:174
ButtonRowResult ExportCloseButtons()
Renders an Export/Close button row.
Definition ImUtil.h:184
void SetupDefaultLayout()
Sets up the default ImGui layout in-case there's no user-defined one.
Definition ImUtil.h:38
bool CloseButton()
Renders a single Close button.
Definition ImUtil.h:213
bool OkButton()
Renders a single OK button.
Definition ImUtil.h:203
bool EnterPressed()
Returns true when Enter or keypad Enter is pressed this frame.
Definition ImUtil.h:90
ButtonRowResult YesNoCancelButtons()
Renders a Yes/No/Cancel button row.
Definition ImUtil.h:164
bool SingleButtonRow(const char *label)
Renders a single right-aligned button.
Definition ImUtil.h:192