1
0
mirror of synced 2025-02-11 16:22:59 +01:00
ImHex/plugins/ui/include/banners/banner_button.hpp
paxcut 2f981ef91e
fix: Banners not scaling properly with font size (#2096)
The banner windows did not scale with the fonts resulting in cropped
text when font size was made bigger than normal.
fixed by ensuring the window is big enough and then making sure text is
centered in the y-axis.
2025-01-30 20:18:44 +01:00

55 lines
2.2 KiB
C++

#pragma once
#include <hex/ui/banner.hpp>
#include <imgui.h>
#include <hex/ui/imgui_imhex_extensions.h>
namespace hex::ui {
class BannerButton : public Banner<BannerButton> {
public:
BannerButton(const char *icon, UnlocalizedString message, ImColor color, UnlocalizedString buttonText, std::function<void()> buttonCallback)
: Banner(color), m_icon(icon), m_message(std::move(message)), m_buttonText(std::move(buttonText)), m_buttonCallback(std::move(buttonCallback)) { }
void drawContent() override {
const std::string buttonText = Lang(m_buttonText);
const auto buttonSize = ImGui::CalcTextSize(buttonText.c_str());
const auto iconSize = ImGui::CalcTextSize(m_icon);
const auto textHeight = std::max(ImGui::CalcTextSize(Lang(m_message)).y, iconSize.y);
const auto textOffset = (ImGui::GetWindowHeight() - textHeight) / 2;
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + textOffset);
ImGui::TextUnformatted(m_icon);
ImGui::SameLine(0, 10_scaled);
const std::string message = Lang(m_message);
const auto messageSize = ImGui::CalcTextSize(message.c_str());
ImGuiExt::TextFormatted("{}", limitStringLength(message, message.size() * ((ImGui::GetContentRegionAvail().x - buttonSize.x - 40_scaled) / messageSize.x)));
if (ImGui::IsItemHovered()) {
ImGui::SetNextWindowSize(ImVec2(400_scaled, 0));
if (ImGui::BeginTooltip()) {
ImGuiExt::TextFormattedWrapped("{}", message);
ImGui::EndTooltip();
}
}
ImGui::SameLine();
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - buttonSize.x - 20_scaled);
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 2_scaled);
if (ImGui::SmallButton(buttonText.c_str())) {
m_buttonCallback();
this->close();
}
ImGui::PopStyleVar(1);
}
private:
const char *m_icon;
UnlocalizedString m_message;
UnlocalizedString m_buttonText;
std::function<void()> m_buttonCallback;
};
}