Cleanup of ImGui code (#2576)

* Change sliders to have +/- buttons by default

* Remove unnecessary margin setting

* Add +/- buttons to remaining sliders and fix some broken UI

* Update EnhancementComboBox to not need array size

* Remove redundant widgets from randomizer

* Miscellaneous fixes

* Fix Switch build break

* Consolidate disabled styling in widgets

* Simplify widget logic

* Merge with most recent PRs

* Fix audio fanfare and sound effect sliders mixup

* Fix sliders being one button too wide

* Fix sliders being too long

* Fix sliders being too long on console
This commit is contained in:
Josh Bodner
2023-04-01 13:10:49 -07:00
committed by GitHub
parent 2aa7c71eac
commit 21d82e7c4c
14 changed files with 654 additions and 839 deletions
+146 -210
View File
@@ -51,22 +51,6 @@ namespace UIWidgets {
return WrappedText(text.c_str(), charactersPerLine);
}
void LoadPickersColors(ImVec4& ColorArray, const char* cvarname, const ImVec4& default_colors, bool has_alpha)
{
Color_RGBA8 defaultColors;
defaultColors.r = default_colors.x;
defaultColors.g = default_colors.y;
defaultColors.b = default_colors.z;
defaultColors.a = default_colors.w;
Color_RGBA8 cvarColor = CVarGetColor(cvarname, defaultColors);
ColorArray.x = cvarColor.r / 255.0;
ColorArray.y = cvarColor.g / 255.0;
ColorArray.z = cvarColor.b / 255.0;
ColorArray.w = cvarColor.a / 255.0;
}
void SetLastItemHoverText(const std::string& text) {
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
@@ -118,13 +102,9 @@ namespace UIWidgets {
}
void PaddedSeparator(bool padTop, bool padBottom, float extraVerticalTopPadding, float extraVerticalBottomPadding) {
if (padTop)
Spacer(0);
if (padTop) Spacer(0);
ImGui::Separator();
if (padBottom)
Spacer(0);
if (padBottom) Spacer(0);
}
void RenderCross(ImDrawList* draw_list, ImVec2 pos, ImU32 col, float sz) {
@@ -143,8 +123,9 @@ namespace UIWidgets {
bool CustomCheckbox(const char* label, bool* v, bool disabled, CheckboxGraphics disabledGraphic) {
ImGuiWindow* window = ImGui::GetCurrentWindow();
if (window->SkipItems)
if (window->SkipItems) {
return false;
}
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
@@ -187,21 +168,37 @@ namespace UIWidgets {
}
ImVec2 label_pos = ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y);
if (g.LogEnabled)
if (g.LogEnabled) {
ImGui::LogRenderedText(&label_pos, mixed_value ? "[~]" : *v ? "[x]" : "[ ]");
if (label_size.x > 0.0f)
}
if (label_size.x > 0.0f) {
ImGui::RenderText(label_pos, label);
}
IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));
return pressed;
}
void ReEnableComponent(const char* disabledTooltipText) {
// End of disable region of previous component
ImGui::PopStyleVar(1);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(disabledTooltipText, "") != 0) {
ImGui::SetTooltip("%s", disabledTooltipText);
}
ImGui::PopItemFlag();
}
void DisableComponent(const float alpha) {
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, alpha);
}
bool EnhancementCheckbox(const char* text, const char* cvarName, bool disabled, const char* disabledTooltipText, CheckboxGraphics disabledGraphic, bool defaultValue) {
bool changed = false;
if (disabled) {
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
DisableComponent(ImGui::GetStyle().Alpha * 0.5f);
}
bool val = (bool)CVarGetInteger(cvarName, defaultValue);
if (CustomCheckbox(text, &val, disabled, disabledGraphic)) {
CVarSetInteger(cvarName, val);
@@ -210,60 +207,39 @@ namespace UIWidgets {
}
if (disabled) {
ImGui::PopStyleVar(1);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(disabledTooltipText, "") != 0) {
ImGui::SetTooltip("%s", disabledTooltipText);
}
ImGui::PopItemFlag();
ReEnableComponent(disabledTooltipText);
}
return changed;
}
bool PaddedEnhancementCheckbox(const char* text, const char* cvarName, bool padTop, bool padBottom, bool disabled, const char* disabledTooltipText, CheckboxGraphics disabledGraphic, bool defaultValue) {
bool changed = false;
ImGui::BeginGroup();
if (padTop) Spacer(0);
if (EnhancementCheckbox(text, cvarName, disabled, disabledTooltipText, disabledGraphic, defaultValue)) {
changed = true;
}
bool changed = EnhancementCheckbox(text, cvarName, disabled, disabledTooltipText, disabledGraphic, defaultValue);
if (padBottom) Spacer(0);
ImGui::EndGroup();
return changed;
}
void EnhancementCombo(const std::string& name, const char* cvarName, const std::vector<std::string>& items, int defaultValue) {
if (ImGui::BeginCombo(name.c_str(), items[static_cast<int>(CVarGetInteger(cvarName, defaultValue))].c_str())) {
for (int settingIndex = 0; settingIndex < (int) items.size(); settingIndex++) {
if (ImGui::Selectable(items[settingIndex].c_str())) {
CVarSetInteger(cvarName, settingIndex);
SohImGui::RequestCvarSaveOnNextTick();
}
}
ImGui::EndCombo();
}
}
bool EnhancementCombobox(const char* name, const char* ComboArray[], size_t arraySize, uint8_t FirstTimeValue, bool disabled, const char* disabledTooltipText, uint8_t disabledValue) {
bool EnhancementCombobox(const char* cvarName, std::span<const char*, std::dynamic_extent> comboArray, uint8_t defaultIndex, bool disabled, const char* disabledTooltipText, uint8_t disabledValue) {
bool changed = false;
if (FirstTimeValue <= 0) {
FirstTimeValue = 0;
if (defaultIndex <= 0) {
defaultIndex = 0;
}
if (disabled) {
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
DisableComponent(ImGui::GetStyle().Alpha * 0.5f);
}
uint8_t selected = CVarGetInteger(name, FirstTimeValue);
uint8_t DefaultValue = selected;
std::string comboName = std::string("##") + std::string(name);
if (ImGui::BeginCombo(comboName.c_str(), ComboArray[DefaultValue])) {
for (uint8_t i = 0; i < arraySize; i++) {
if (strlen(ComboArray[i]) > 1) {
if (ImGui::Selectable(ComboArray[i], i == selected)) {
CVarSetInteger(name, i);
uint8_t selected = CVarGetInteger(cvarName, defaultIndex);
std::string comboName = std::string("##") + std::string(cvarName);
if (ImGui::BeginCombo(comboName.c_str(), comboArray[selected])) {
for (uint8_t i = 0; i < comboArray.size(); i++) {
if (strlen(comboArray[i]) > 1) {
if (ImGui::Selectable(comboArray[i], i == selected)) {
CVarSetInteger(cvarName, i);
selected = i;
changed = true;
SohImGui::RequestCvarSaveOnNextTick();
@@ -274,14 +250,10 @@ namespace UIWidgets {
}
if (disabled) {
ImGui::PopStyleVar(1);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(disabledTooltipText, "") != 0) {
ImGui::SetTooltip("%s", disabledTooltipText);
}
ImGui::PopItemFlag();
ReEnableComponent(disabledTooltipText);
if (disabledValue >= 0 && selected != disabledValue) {
CVarSetInteger(name, disabledValue);
CVarSetInteger(cvarName, disabledValue);
changed = true;
SohImGui::RequestCvarSaveOnNextTick();
}
@@ -290,120 +262,89 @@ namespace UIWidgets {
return changed;
}
bool LabeledRightAlignedEnhancementCombobox(const char* label, const char* cvarName, std::span<const char*, std::dynamic_extent> comboArray, uint8_t defaultIndex, bool disabled, const char* disabledTooltipText, uint8_t disabledValue) {
ImGui::Text(label);
s32 currentValue = CVarGetInteger(cvarName, defaultIndex);
#ifdef __WIIU__
ImGui::SameLine(ImGui::GetContentRegionAvail().x - (ImGui::CalcTextSize(comboArray[currentValue]).x + 40.0f));
ImGui::PushItemWidth(ImGui::CalcTextSize(comboArray[currentValue]).x + 60.0f);
#else
ImGui::SameLine(ImGui::GetContentRegionAvail().x - (ImGui::CalcTextSize(comboArray[currentValue]).x + 20.0f));
ImGui::PushItemWidth(ImGui::CalcTextSize(comboArray[currentValue]).x + 30.0f);
#endif
bool changed = EnhancementCombobox(cvarName, comboArray, defaultIndex, disabled, disabledTooltipText, disabledValue);
ImGui::PopItemWidth();
return changed;
}
void PaddedText(const char* text, bool padTop, bool padBottom) {
if (padTop)
Spacer(0);
if (padTop) Spacer(0);
ImGui::Text("%s", text);
if (padBottom)
Spacer(0);
}
void DisableComponentSwitch(const char* disabledTooltipText, const float alpha) {
// End of disable region of previous component
ImGui::PopStyleVar(1);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(disabledTooltipText, "") != 0) {
ImGui::SetTooltip("%s", disabledTooltipText);
}
ImGui::PopItemFlag();
// Start of disable region of next component
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, alpha);
if (padBottom) Spacer(0);
}
bool EnhancementSliderInt(const char* text, const char* id, const char* cvarName, int min, int max, const char* format, int defaultValue, bool PlusMinusButton, bool disabled, const char* disabledTooltipText) {
bool changed = false;
int val = CVarGetInteger(cvarName, defaultValue);
float alpha;
if (disabled) {
alpha = ImGui::GetStyle().Alpha * 0.5f;
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, alpha);
DisableComponent(ImGui::GetStyle().Alpha * 0.5f);
}
ImGui::Text(text, val);
Spacer(0);
if (disabled) {
DisableComponentSwitch(disabledTooltipText, alpha);
}
if(PlusMinusButton) {
std::string MinusBTNName = " - ##";
MinusBTNName += cvarName;
ImGui::BeginGroup();
if (PlusMinusButton) {
std::string MinusBTNName = " - ##" + std::string(cvarName);
if (ImGui::Button(MinusBTNName.c_str())) {
val--;
CVarSetInteger(cvarName, val);
SohImGui::RequestCvarSaveOnNextTick();
changed = true;
}
ImGui::SameLine();
ImGui::SetCursorPosX(ImGui::GetCursorPosX() - 7.0f);
}
if (disabled) {
DisableComponentSwitch(disabledTooltipText, alpha);
}
}
if (PlusMinusButton) {
#ifdef __SWITCH__
ImGui::PushItemWidth(ImGui::GetWindowSize().x - 110.0f);
#elif defined(__WIIU__)
ImGui::PushItemWidth(ImGui::GetWindowSize().x - 79.0f * 2);
#else
ImGui::PushItemWidth(ImGui::GetWindowSize().x - 79.0f);
#endif
}
if (ImGui::SliderInt(id, &val, min, max, format))
ImGui::PushItemWidth(std::min((ImGui::GetContentRegionAvail().x - (PlusMinusButton ? sliderButtonWidth : 0.0f)), maxSliderWidth));
if (ImGui::SliderInt(id, &val, min, max, format, ImGuiSliderFlags_AlwaysClamp))
{
CVarSetInteger(cvarName, val);
SohImGui::RequestCvarSaveOnNextTick();
changed = true;
}
ImGui::PopItemWidth();
if (PlusMinusButton) {
ImGui::PopItemWidth();
}
if(PlusMinusButton) {
if (disabled) {
DisableComponentSwitch(disabledTooltipText, alpha);
}
std::string PlusBTNName = " + ##";
PlusBTNName += cvarName;
std::string PlusBTNName = " + ##" + std::string(cvarName);
ImGui::SameLine();
ImGui::SetCursorPosX(ImGui::GetCursorPosX() - 7.0f);
if (ImGui::Button(PlusBTNName.c_str())) {
val++;
CVarSetInteger(cvarName, val);
SohImGui::RequestCvarSaveOnNextTick();
changed = true;
}
}
ImGui::EndGroup();
if (disabled) {
ImGui::PopStyleVar(1);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(disabledTooltipText, "") != 0) {
ImGui::SetTooltip("%s", disabledTooltipText);
}
ImGui::PopItemFlag();
ReEnableComponent(disabledTooltipText);
}
if (val < min)
{
if (val < min) {
val = min;
CVarSetInteger(cvarName, val);
SohImGui::RequestCvarSaveOnNextTick();
changed = true;
}
if (val > max)
{
if (val > max) {
val = max;
changed = true;
}
if (changed) {
CVarSetInteger(cvarName, val);
SohImGui::RequestCvarSaveOnNextTick();
changed = true;
}
return changed;
@@ -414,8 +355,7 @@ namespace UIWidgets {
float val = CVarGetFloat(cvarName, defaultValue);
if (disabled) {
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
DisableComponent(ImGui::GetStyle().Alpha * 0.5f);
}
if (!isPercentage) {
@@ -424,93 +364,90 @@ namespace UIWidgets {
ImGui::Text(text, static_cast<int>(100 * val));
}
Spacer(0);
if(PlusMinusButton) {
std::string MinusBTNName = " - ##";
MinusBTNName += cvarName;
ImGui::BeginGroup();
if (PlusMinusButton) {
std::string MinusBTNName = " - ##" + std::string(cvarName);
if (ImGui::Button(MinusBTNName.c_str())) {
if (!isPercentage) {
val -= 0.1f;
} else {
if (isPercentage) {
val -= 0.01f;
} else {
val -= 0.1f;
}
CVarSetFloat(cvarName, val);
SohImGui::RequestCvarSaveOnNextTick();
changed = true;
}
ImGui::SameLine();
ImGui::SetCursorPosX(ImGui::GetCursorPosX() - 7.0f);
}
if (PlusMinusButton) {
#ifdef __SWITCH__
ImGui::PushItemWidth(ImGui::GetWindowSize().x - 110.0f);
#elif defined(__WIIU__)
ImGui::PushItemWidth(ImGui::GetWindowSize().x - 79.0f * 2);
#else
ImGui::PushItemWidth(ImGui::GetWindowSize().x - 79.0f);
#endif
}
if (ImGui::SliderFloat(id, &val, min, max, format)) {
ImGui::PushItemWidth(std::min((ImGui::GetContentRegionAvail().x - (PlusMinusButton ? sliderButtonWidth : 0.0f)), maxSliderWidth));
if (ImGui::SliderFloat(id, &val, min, max, format, ImGuiSliderFlags_AlwaysClamp)) {
if (isPercentage) {
CVarSetFloat(cvarName, roundf(val * 100) / 100);
} else {
CVarSetFloat(cvarName, val);
val = roundf(val * 100) / 100;
}
SohImGui::RequestCvarSaveOnNextTick();
changed = true;
}
ImGui::PopItemWidth();
if (PlusMinusButton) {
ImGui::PopItemWidth();
}
if(PlusMinusButton) {
std::string PlusBTNName = " + ##";
PlusBTNName += cvarName;
std::string PlusBTNName = " + ##" + std::string(cvarName);
ImGui::SameLine();
ImGui::SetCursorPosX(ImGui::GetCursorPosX() - 7.0f);
if (ImGui::Button(PlusBTNName.c_str())) {
if (!isPercentage) {
val += 0.1f;
} else {
if (isPercentage) {
val += 0.01f;
} else {
val += 0.1f;
}
CVarSetFloat(cvarName, val);
SohImGui::RequestCvarSaveOnNextTick();
changed = true;
}
}
ImGui::EndGroup();
if (disabled) {
ImGui::PopStyleVar(1);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(disabledTooltipText, "") != 0) {
ImGui::SetTooltip("%s", disabledTooltipText);
}
ImGui::PopItemFlag();
ReEnableComponent(disabledTooltipText);
}
if (val < min) {
val = min;
CVarSetFloat(cvarName, val);
SohImGui::RequestCvarSaveOnNextTick();
changed = true;
}
if (val > max) {
val = max;
changed = true;
}
if (changed) {
CVarSetFloat(cvarName, val);
SohImGui::RequestCvarSaveOnNextTick();
changed = true;
}
return changed;
}
void PaddedEnhancementSliderInt(const char* text, const char* id, const char* cvarName, int min, int max, const char* format, int defaultValue, bool PlusMinusButton, bool padTop, bool padBottom, bool disabled, const char* disabledTooltipText) {
if (padTop)
Spacer(0);
bool PaddedEnhancementSliderInt(const char* text, const char* id, const char* cvarName, int min, int max, const char* format, int defaultValue, bool PlusMinusButton, bool padTop, bool padBottom, bool disabled, const char* disabledTooltipText) {
bool changed = false;
ImGui::BeginGroup();
if (padTop) Spacer(0);
EnhancementSliderInt(text, id, cvarName, min, max, format, defaultValue, PlusMinusButton, disabled, disabledTooltipText);
changed = EnhancementSliderInt(text, id, cvarName, min, max, format, defaultValue, PlusMinusButton, disabled, disabledTooltipText);
if (padBottom)
Spacer(0);
if (padBottom) Spacer(0);
ImGui::EndGroup();
return changed;
}
bool PaddedEnhancementSliderFloat(const char* text, const char* id, const char* cvarName, float min, float max, const char* format, float defaultValue, bool isPercentage, bool PlusMinusButton, bool padTop, bool padBottom, bool disabled, const char* disabledTooltipText) {
bool changed = false;
ImGui::BeginGroup();
if (padTop) Spacer(0);
changed = EnhancementSliderFloat(text, id, cvarName, min, max, format, defaultValue, isPercentage, PlusMinusButton, disabled, disabledTooltipText);
if (padBottom) Spacer(0);
ImGui::EndGroup();
return changed;
}
bool EnhancementRadioButton(const char* text, const char* cvarName, int id) {
@@ -524,9 +461,7 @@ namespace UIWidgets {
EnhancementRadioButton("German", "gLanguages", LANGUAGE_GER);
EnhancementRadioButton("French", "gLanguages", LANGUAGE_FRA);
*/
std::string make_invisible = "##";
make_invisible += text;
make_invisible += cvarName;
std::string make_invisible = "##" + std::string(text) + std::string(cvarName);
bool ret = false;
int val = CVarGetInteger(cvarName, 0);
@@ -543,12 +478,8 @@ namespace UIWidgets {
bool DrawResetColorButton(const char* cvarName, ImVec4* colors, ImVec4 defaultcolors, bool has_alpha) {
bool changed = false;
std::string Cvar_RBM = cvarName;
Cvar_RBM += "RBM";
std::string MakeInvisible = "Reset";
MakeInvisible += "##";
MakeInvisible += cvarName;
MakeInvisible += "Reset";
std::string Cvar_RBM = std::string(cvarName) + "RBM";
std::string MakeInvisible = "Reset##" + std::string(cvarName) + "Reset";
if (ImGui::Button(MakeInvisible.c_str())) {
colors->x = defaultcolors.x;
colors->y = defaultcolors.y;
@@ -573,13 +504,8 @@ namespace UIWidgets {
bool DrawRandomizeColorButton(const char* cvarName, ImVec4* colors) {
bool changed = false;
Color_RGBA8 NewColors = {0,0,0,255};
std::string Cvar_RBM = cvarName;
Cvar_RBM += "RBM";
std::string MakeInvisible = "##";
MakeInvisible += cvarName;
MakeInvisible += "Random";
std::string FullName = "Random";
FullName += MakeInvisible;
std::string Cvar_RBM = std::string(cvarName) + "RBM";
std::string FullName = "Random##" + std::string(cvarName) + "Random";
if (ImGui::Button(FullName.c_str())) {
#if defined(__SWITCH__) || defined(__WIIU__)
srand(time(NULL));
@@ -601,8 +527,7 @@ namespace UIWidgets {
}
void DrawLockColorCheckbox(const char* cvarName) {
std::string Cvar_Lock = cvarName;
Cvar_Lock += "Lock";
std::string Cvar_Lock = std::string(cvarName) + "Lock";
s32 lock = CVarGetInteger(Cvar_Lock.c_str(), 0);
std::string FullName = "Lock##" + Cvar_Lock;
EnhancementCheckbox(FullName.c_str(), Cvar_Lock.c_str());
@@ -610,17 +535,28 @@ namespace UIWidgets {
}
void RainbowColor(const char* cvarName, ImVec4* colors) {
std::string Cvar_RBM = cvarName;
Cvar_RBM += "RBM";
std::string MakeInvisible = "Rainbow";
MakeInvisible += "##";
MakeInvisible += cvarName;
MakeInvisible += "Rainbow";
std::string Cvar_RBM = std::string(cvarName) + "RBM";
std::string MakeInvisible = "Rainbow##" + std::string(cvarName) + "Rainbow";
EnhancementCheckbox(MakeInvisible.c_str(), Cvar_RBM.c_str());
Tooltip("Cycles through colors on a timer\nOverwrites previously chosen color");
}
void LoadPickersColors(ImVec4& ColorArray, const char* cvarname, const ImVec4& default_colors, bool has_alpha) {
Color_RGBA8 defaultColors;
defaultColors.r = default_colors.x;
defaultColors.g = default_colors.y;
defaultColors.b = default_colors.z;
defaultColors.a = default_colors.w;
Color_RGBA8 cvarColor = CVarGetColor(cvarname, defaultColors);
ColorArray.x = cvarColor.r / 255.0;
ColorArray.y = cvarColor.g / 255.0;
ColorArray.z = cvarColor.b / 255.0;
ColorArray.w = cvarColor.a / 255.0;
}
bool EnhancementColor(const char* text, const char* cvarName, ImVec4 ColorRGBA, ImVec4 default_colors, bool allow_rainbow, bool has_alpha, bool TitleSameLine) {
bool changed = false;
LoadPickersColors(ColorRGBA, cvarName, default_colors, has_alpha);