Skip to content

Commit

Permalink
Fix crashing with modern menu and UIWidgets with no tooltips (#851)
Browse files Browse the repository at this point in the history
* Fix crashing with modern menu and UIWidgets when null is passed for tooltips

* move to shiputils
  • Loading branch information
Archez authored Nov 14, 2024
1 parent e0ed285 commit 6d76ce9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 27 deletions.
3 changes: 2 additions & 1 deletion mm/2s2h/BenGui/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ void BenMenu::DrawElement() {
info.isHidden) {
continue;
}
std::string widgetStr = std::string(info.widgetName) + std::string(info.widgetTooltip);
std::string widgetStr = std::string(info.widgetName) +
std::string(info.widgetTooltip != NULL ? info.widgetTooltip : "");
std::transform(menuSearchText.begin(), menuSearchText.end(), menuSearchText.begin(), ::tolower);
std::transform(widgetStr.begin(), widgetStr.end(), widgetStr.begin(), ::tolower);
widgetStr.erase(std::remove(widgetStr.begin(), widgetStr.end(), ' '), widgetStr.end());
Expand Down
4 changes: 3 additions & 1 deletion mm/2s2h/BenGui/SearchableMenuItems.h
Original file line number Diff line number Diff line change
Expand Up @@ -1947,7 +1947,9 @@ void SearchMenuGetItem(widgetInfo& widget) {
info.widgetType == WIDGET_SEPARATOR_TEXT || info.isHidden) {
continue;
}
std::string widgetStr = std::string(info.widgetName) + std::string(info.widgetTooltip);
std::string widgetStr =
std::string(info.widgetName) +
std::string(info.widgetTooltip != NULL ? info.widgetTooltip : "");
std::transform(menuSearchText.begin(), menuSearchText.end(), menuSearchText.begin(),
::tolower);
menuSearchText.erase(std::remove(menuSearchText.begin(), menuSearchText.end(), ' '),
Expand Down
17 changes: 9 additions & 8 deletions mm/2s2h/BenGui/UIWidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string>
#include <unordered_map>
#include <libultraship/libultra/types.h>
#include "2s2h/ShipUtils.h"

namespace UIWidgets {
// Automatically adds newlines to break up text longer than a specified number of characters
Expand Down Expand Up @@ -113,9 +114,9 @@ bool Button(const char* label, const ButtonOptions& options) {
PopStyleButton();
ImGui::EndDisabled();
if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) &&
strcmp(options.disabledTooltip, "") != 0) {
!Ship_IsCStringEmpty(options.disabledTooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.disabledTooltip).c_str());
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(options.tooltip, "") != 0) {
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.tooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.tooltip).c_str());
}
return dirty;
Expand Down Expand Up @@ -260,9 +261,9 @@ bool Checkbox(const char* _label, bool* value, const CheckboxOptions& options) {
PopStyleCheckbox();
ImGui::EndDisabled();
if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) &&
strcmp(options.disabledTooltip, "") != 0) {
!Ship_IsCStringEmpty(options.disabledTooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.disabledTooltip).c_str());
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(options.tooltip, "") != 0) {
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.tooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.tooltip).c_str());
}
return pressed;
Expand Down Expand Up @@ -369,9 +370,9 @@ bool SliderInt(const char* label, int32_t* value, int32_t min, int32_t max, cons
ImGui::EndDisabled();
ImGui::EndGroup();
if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) &&
strcmp(options.disabledTooltip, "") != 0) {
!Ship_IsCStringEmpty(options.disabledTooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.disabledTooltip).c_str());
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(options.tooltip, "") != 0) {
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.tooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.tooltip).c_str());
}
ImGui::PopID();
Expand Down Expand Up @@ -485,9 +486,9 @@ bool SliderFloat(const char* label, float* value, float min, float max, const Fl
ImGui::EndDisabled();
ImGui::EndGroup();
if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) &&
strcmp(options.disabledTooltip, "") != 0) {
!Ship_IsCStringEmpty(options.disabledTooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.disabledTooltip).c_str());
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(options.tooltip, "") != 0) {
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.tooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.tooltip).c_str());
}
ImGui::PopID();
Expand Down
33 changes: 17 additions & 16 deletions mm/2s2h/BenGui/UIWidgets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <imgui.h>
#include <libultraship/libultraship.h>
#include <unordered_map>
#include "2s2h/ShipUtils.h"

namespace UIWidgets {

Expand Down Expand Up @@ -129,7 +130,7 @@ namespace UIWidgets {
PushStyleCombobox(options.color);
if (options.alignment == ComponentAlignment::Left) {
if (options.labelPosition == LabelPosition::Above) {
ImGui::Text(label);
ImGui::Text("%s", label);
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
} else if (options.labelPosition == LabelPosition::Near) {
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x - ImGui::GetStyle().ItemSpacing.x * 2);
Expand All @@ -140,7 +141,7 @@ namespace UIWidgets {
if (options.labelPosition == LabelPosition::Above) {
ImGui::NewLine();
ImGui::SameLine(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x);
ImGui::Text(label);
ImGui::Text("%s", label);
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
} else if (options.labelPosition == LabelPosition::Near) {
ImGui::SameLine(ImGui::CalcTextSize(label).x + ImGui::GetStyle().ItemSpacing.x * 2);
Expand All @@ -167,23 +168,23 @@ namespace UIWidgets {
if (options.alignment == ComponentAlignment::Left) {
if (options.labelPosition == LabelPosition::Near) {
ImGui::SameLine();
ImGui::Text(label);
ImGui::Text("%s", label);
} else if (options.labelPosition == LabelPosition::Far) {
ImGui::SameLine(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x);
ImGui::Text(label);
ImGui::Text("%s", label);
}
} else if (options.alignment == ComponentAlignment::Right) {
if (options.labelPosition == LabelPosition::Near || options.labelPosition == LabelPosition::Far) {
ImGui::SameLine(startX);
ImGui::Text(label);
ImGui::Text("%s", label);
}
}
PopStyleCombobox();
ImGui::EndDisabled();
ImGui::EndGroup();
if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(options.disabledTooltip, "") != 0) {
if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.disabledTooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.disabledTooltip).c_str());
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(options.tooltip, "") != 0) {
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.tooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.tooltip).c_str());
}
ImGui::PopID();
Expand Down Expand Up @@ -256,9 +257,9 @@ namespace UIWidgets {
PopStyleCombobox();
ImGui::EndDisabled();
ImGui::EndGroup();
if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(options.disabledTooltip, "") != 0) {
if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.disabledTooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.disabledTooltip).c_str());
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(options.tooltip, "") != 0) {
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.tooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.tooltip).c_str());
}
ImGui::PopID();
Expand All @@ -281,7 +282,7 @@ namespace UIWidgets {
PushStyleCombobox(options.color);
if (options.alignment == ComponentAlignment::Left) {
if (options.labelPosition == LabelPosition::Above) {
ImGui::Text(label);
ImGui::Text("%s", label);
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
} else if (options.labelPosition == LabelPosition::Near) {
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x - ImGui::GetStyle().ItemSpacing.x * 2);
Expand All @@ -292,7 +293,7 @@ namespace UIWidgets {
if (options.labelPosition == LabelPosition::Above) {
ImGui::NewLine();
ImGui::SameLine(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x);
ImGui::Text(label);
ImGui::Text("%s", label);
ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
} else if (options.labelPosition == LabelPosition::Near) {
ImGui::SameLine(ImGui::CalcTextSize(label).x + ImGui::GetStyle().ItemSpacing.x * 2);
Expand Down Expand Up @@ -320,23 +321,23 @@ namespace UIWidgets {
if (options.alignment == ComponentAlignment::Left) {
if (options.labelPosition == LabelPosition::Near) {
ImGui::SameLine();
ImGui::Text(label);
ImGui::Text("%s", label);
} else if (options.labelPosition == LabelPosition::Far) {
ImGui::SameLine(ImGui::GetContentRegionAvail().x - ImGui::CalcTextSize(label).x);
ImGui::Text(label);
ImGui::Text("%s", label);
}
} else if (options.alignment == ComponentAlignment::Right) {
if (options.labelPosition == LabelPosition::Near || options.labelPosition == LabelPosition::Far) {
ImGui::SameLine(startX);
ImGui::Text(label);
ImGui::Text("%s", label);
}
}
PopStyleCombobox();
ImGui::EndDisabled();
ImGui::EndGroup();
if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(options.disabledTooltip, "") != 0) {
if (options.disabled && ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.disabledTooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.disabledTooltip).c_str());
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && strcmp(options.tooltip, "") != 0) {
} else if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled) && !Ship_IsCStringEmpty(options.tooltip)) {
ImGui::SetTooltip("%s", WrappedText(options.tooltip).c_str());
}
ImGui::PopID();
Expand Down
4 changes: 3 additions & 1 deletion mm/2s2h/DeveloperTools/SaveEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,13 +559,15 @@ void DrawGeneralTab() {
if (ImGui::SliderInt("##setBank", &bankedRupees, 0, 5000, "Banked Rupees: %d")) {
HS_SET_BANK_RUPEES(bankedRupees);
}
UIWidgets::Tooltip("To recieve the rewards, set the bank to 199, 999, or 4,999 then deposit a single rupee");
UIWidgets::Tooltip("To receive the rewards, set the bank to 199, 999, or 4,999 then deposit a single rupee");
UIWidgets::PopStyleSlider();

DrawTempleClears();

UIWidgets::Checkbox("Has Tatl", (bool*)&gSaveContext.save.hasTatl, { .color = UIWidgets::Colors::Gray });
UIWidgets::Checkbox("Is Owl Save", (bool*)&gSaveContext.save.isOwlSave, { .color = UIWidgets::Colors::Gray });
UIWidgets::Checkbox("Finished Intro Sequence", (bool*)&gSaveContext.save.isFirstCycle,
{ .color = UIWidgets::Colors::Gray });
ImGui::EndGroup();

ImGui::PopItemWidth();
Expand Down
4 changes: 4 additions & 0 deletions mm/2s2h/ShipUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ extern TexturePtr gQuestIcons[14];
extern TexturePtr gBombersNotebookPhotos[24];
}

extern "C" bool Ship_IsCStringEmpty(const char* str) {
return str == NULL || str[0] == '\0';
}

// Build vertex coordinates for a quad command
// In order of top left, top right, bottom left, then bottom right
// Supports flipping the texture horizontally
Expand Down
1 change: 1 addition & 0 deletions mm/2s2h/ShipUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ void LoadGuiTextures();
extern "C" {
#endif

bool Ship_IsCStringEmpty(const char* str);
void Ship_CreateQuadVertexGroup(Vtx* vtxList, s32 xStart, s32 yStart, s32 width, s32 height, u8 flippedH);
f32 Ship_GetCharFontWidthNES(u8 character);
TexturePtr Ship_GetCharFontTextureNES(u8 character);
Expand Down

0 comments on commit 6d76ce9

Please sign in to comment.