added more GUI helper functions

This commit is contained in:
myT 2024-07-02 01:27:56 +02:00
parent 68d32e69e9
commit aa88622348
4 changed files with 109 additions and 26 deletions

View file

@ -99,6 +99,33 @@ void ToggleBooleanWithShortcut(bool& value, ImGuiKey key)
} }
} }
void RadioButton(int* argValue, float titleWidth, const char* title, int count, ...)
{
const float x = ImGui::GetCursorPosX();
ImGui::Text(title);
ImGui::SameLine();
if(titleWidth > 0.0f)
{
ImGui::SetCursorPosX(x + titleWidth);
}
ImGui::PushID(title);
va_list args;
va_start(args, count);
for(int i = 0; i < count; i++)
{
const char* const name = va_arg(args, const char*);
const int value = va_arg(args, int);
ImGui::RadioButton(name, argValue, value);
if(i < count - 1)
{
ImGui::SameLine();
}
}
va_end(args);
ImGui::PopID();
}
struct FileDialog struct FileDialog
{ {
char folder[MAX_QPATH]; char folder[MAX_QPATH];
@ -110,10 +137,13 @@ struct FileDialog
int fileCount; int fileCount;
int selectedFileIndex; int selectedFileIndex;
bool saveMode; bool saveMode;
bool folderMode;
}; };
static FileDialog saveFile; static FileDialog saveFile;
static FileDialog openFile; static FileDialog openFile;
static FileDialog saveFolder;
static FileDialog openFolder;
static bool PathHasExtension(const char* path, const char* extension) static bool PathHasExtension(const char* path, const char* extension)
{ {
@ -149,10 +179,17 @@ static bool DoFileDialog(FileDialog& dialog)
bool success = false; bool success = false;
if(ImGui::BeginPopupModal(dialog.dialogName, NULL, ImGuiWindowFlags_AlwaysAutoResize)) if(ImGui::BeginPopupModal(dialog.dialogName, NULL, ImGuiWindowFlags_AlwaysAutoResize))
{ {
if(BeginTable("Files", 1)) if(BeginTable(dialog.folderMode ? "Folders" : "Files", 1))
{ {
for(int i = 0; i < dialog.fileCount; i++) for(int i = 0; i < dialog.fileCount; i++)
{ {
if(dialog.folderMode &&
(Q_stricmp(dialog.filePaths[i], ".") == 0 ||
Q_stricmp(dialog.filePaths[i], "..") == 0))
{
continue;
}
ImGui::TableNextRow(); ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0); ImGui::TableSetColumnIndex(0);
if(ImGui::Selectable(dialog.filePaths[i], i == dialog.selectedFileIndex, if(ImGui::Selectable(dialog.filePaths[i], i == dialog.selectedFileIndex,
@ -167,7 +204,7 @@ static bool DoFileDialog(FileDialog& dialog)
if(dialog.saveMode) if(dialog.saveMode)
{ {
if(ImGui::InputText("File path", dialog.fileName, sizeof(dialog.fileName))) if(ImGui::InputText(dialog.folderMode ? "Folder path" : "File path", dialog.fileName, sizeof(dialog.fileName)))
{ {
dialog.selectedFileIndex = -1; dialog.selectedFileIndex = -1;
} }
@ -222,38 +259,68 @@ static bool DoFileDialog(FileDialog& dialog)
return success; return success;
} }
void OpenSaveFileDialog(const char* folder, const char* extension) void SaveFileDialog_Open(const char* folder, const char* extension)
{ {
Q_assert(extension[0] == '.'); Q_assert(extension[0] == '.');
OpenFileDialog(saveFile, folder, extension); OpenFileDialog(saveFile, folder, extension);
} }
bool SaveFileDialog() bool SaveFileDialog_Do()
{ {
return DoFileDialog(saveFile); return DoFileDialog(saveFile);
} }
const char* GetSaveFileDialogPath() const char* SaveFileDialog_GetPath()
{ {
return saveFile.filePath; return saveFile.filePath;
} }
void OpenOpenFileDialog(const char* folder, const char* extension) void OpenFileDialog_Open(const char* folder, const char* extension)
{ {
Q_assert(extension[0] == '.'); Q_assert(extension[0] == '.');
OpenFileDialog(openFile, folder, extension); OpenFileDialog(openFile, folder, extension);
} }
bool OpenFileDialog() bool OpenFileDialog_Do()
{ {
return DoFileDialog(openFile); return DoFileDialog(openFile);
} }
const char* GetOpenFileDialogPath() const char* OpenFileDialog_GetPath()
{ {
return openFile.filePath; return openFile.filePath;
} }
void SaveFolderDialog_Open(const char* folder)
{
OpenFileDialog(saveFolder, folder, "/");
}
bool SaveFolderDialog_Do()
{
return DoFileDialog(saveFolder);
}
const char* SaveFolderDialog_GetPath()
{
return saveFolder.filePath;
}
void OpenFolderDialog_Open(const char* folder)
{
OpenFileDialog(openFolder, folder, "/");
}
bool OpenFolderDialog_Do()
{
return DoFileDialog(openFolder);
}
const char* OpenFolderDialog_GetPath()
{
return openFolder.filePath;
}
struct MainMenuItem struct MainMenuItem
{ {
GUI_MainMenu::Id menu; GUI_MainMenu::Id menu;
@ -399,6 +466,9 @@ static void ImGUI_ApplyTheme()
colors[ImGuiCol_TabHovered] = hover; colors[ImGuiCol_TabHovered] = hover;
colors[ImGuiCol_TabActive] = active; colors[ImGuiCol_TabActive] = active;
colors[ImGuiCol_NavHighlight] = hover; colors[ImGuiCol_NavHighlight] = hover;
colors[ImGuiCol_Separator] = hover;
colors[ImGuiCol_SeparatorHovered] = hover;
colors[ImGuiCol_SeparatorActive] = hover;
ImGuiStyle& style = ImGui::GetStyle(); ImGuiStyle& style = ImGui::GetStyle();
style.WindowPadding = ImVec2(8.00f, 8.00f); style.WindowPadding = ImVec2(8.00f, 8.00f);
@ -613,6 +683,11 @@ void CL_IMGUI_Init()
saveFile.saveMode = true; saveFile.saveMode = true;
saveFile.dialogName = "Save file"; saveFile.dialogName = "Save file";
openFile.dialogName = "Open file"; openFile.dialogName = "Open file";
saveFolder.folderMode = true;
saveFolder.saveMode = true;
saveFolder.dialogName = "Save folder";
openFolder.folderMode = true;
openFolder.dialogName = "Open folder";
} }
void CL_IMGUI_Frame() void CL_IMGUI_Frame()

View file

@ -36,12 +36,19 @@ void TableRow2(const char* item0, int item1);
void TableRow2(const char* item0, float item1, const char* format = "%g"); void TableRow2(const char* item0, float item1, const char* format = "%g");
bool IsShortcutPressed(ImGuiKey key); bool IsShortcutPressed(ImGuiKey key);
void ToggleBooleanWithShortcut(bool& value, ImGuiKey key); void ToggleBooleanWithShortcut(bool& value, ImGuiKey key);
void OpenSaveFileDialog(const char* folder, const char* extension); void RadioButton(int* value, float titleWidth, const char* title, int count, ...); // count (const char* name, int value) pairs
bool SaveFileDialog(); // true when the user clicked the save button void SaveFileDialog_Open(const char* folder, const char* extension);
const char* GetSaveFileDialogPath(); bool SaveFileDialog_Do(); // true when the user clicked the save button
void OpenOpenFileDialog(const char* folder, const char* extension); const char* SaveFileDialog_GetPath();
bool OpenFileDialog(); // true when the user clicked the open button void OpenFileDialog_Open(const char* folder, const char* extension);
const char* GetOpenFileDialogPath(); bool OpenFileDialog_Do(); // true when the user clicked the open button
const char* OpenFileDialog_GetPath();
void SaveFolderDialog_Open(const char* folder);
bool SaveFolderDialog_Do(); // true when the user clicked the save button
const char* SaveFolderDialog_GetPath();
void OpenFolderDialog_Open(const char* folder);
bool OpenFolderDialog_Do(); // true when the user clicked the open button
const char* OpenFolderDialog_GetPath();
#define MAIN_MENU_LIST(M) \ #define MAIN_MENU_LIST(M) \
M(Tools, "Tools") \ M(Tools, "Tools") \

View file

@ -180,12 +180,12 @@ void SunlightEditor::DrawGUI()
ImGui::NewLine(); ImGui::NewLine();
if(ImGui::Button("Save Config...")) if(ImGui::Button("Save Config..."))
{ {
OpenSaveFileDialog("sun", ".sun"); SaveFileDialog_Open("sun", ".sun");
} }
ImGui::SameLine(); ImGui::SameLine();
if(ImGui::Button("Open Config...")) if(ImGui::Button("Open Config..."))
{ {
OpenOpenFileDialog("sun", ".sun"); OpenFileDialog_Open("sun", ".sun");
} }
if(skyShader != NULL) if(skyShader != NULL)
{ {
@ -196,14 +196,14 @@ void SunlightEditor::DrawGUI()
} }
} }
if(SaveFileDialog()) if(SaveFileDialog_Do())
{ {
SaveSunFile(GetSaveFileDialogPath()); SaveSunFile(SaveFileDialog_GetPath());
} }
if(OpenFileDialog()) if(OpenFileDialog_Do())
{ {
LoadSunFile(GetOpenFileDialogPath()); LoadSunFile(OpenFileDialog_GetPath());
} }
} }
ImGui::End(); ImGui::End();

View file

@ -1390,12 +1390,12 @@ void VolumetricLight::DrawGUI()
ImGui::SameLine(); ImGui::SameLine();
if(fogCount > 0 && ImGui::Button("Save Config...")) if(fogCount > 0 && ImGui::Button("Save Config..."))
{ {
OpenSaveFileDialog("fogs", ".fogs"); SaveFileDialog_Open("fogs", ".fogs");
} }
ImGui::SameLine(); ImGui::SameLine();
if(ImGui::Button("Open Config...")) if(ImGui::Button("Open Config..."))
{ {
OpenOpenFileDialog("fogs", ".fogs"); OpenFileDialog_Open("fogs", ".fogs");
} }
const char* axisNames[3] = { "X", "Y", "Z" }; const char* axisNames[3] = { "X", "Y", "Z" };
@ -1467,16 +1467,17 @@ void VolumetricLight::DrawGUI()
ImGui::EndTabItem(); ImGui::EndTabItem();
} }
ImGui::EndTabBar(); ImGui::EndTabBar();
if(SaveFileDialog()) if(SaveFileDialog_Do())
{ {
SaveFogFile(GetSaveFileDialogPath()); SaveFogFile(SaveFileDialog_GetPath());
} }
if(OpenFileDialog()) if(OpenFileDialog_Do())
{ {
LoadFogFile(GetOpenFileDialogPath()); LoadFogFile(OpenFileDialog_GetPath());
} }
} }
ImGui::End(); ImGui::End();