Skip to content

Commit

Permalink
impr: Re-render SVG textures on scaling changes
Browse files Browse the repository at this point in the history
This change also future-proofs the scaling event so it can be fired
when users change the scaling mode in the settings, since both user
scaling and window content scaling changes require
re-rasterisation.
  • Loading branch information
csnover committed Jul 18, 2024
1 parent a02dcaf commit a94fbe4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/libimhex/include/hex/api/event_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ namespace hex {
EVENT_DEF(EventAbnormalTermination, int);
EVENT_DEF(EventThemeChanged);
EVENT_DEF(EventOSThemeChanged);
EVENT_DEF(EventDPIChanged, float);
EVENT_DEF(EventScaleChanged);
EVENT_DEF(EventWindowFocused, bool);

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/libimhex/source/ui/imgui_imhex_extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace ImGuiExt {

namespace {

void adjustSVGScale(const lunasvg::Document *document, int &width, int &height, int scale) {
void adjustSVGScale(const lunasvg::Document *document, int &width, int &height, float scale) {
if (document->width() == 0 || document->height() == 0)
return;

Expand Down
4 changes: 2 additions & 2 deletions main/gui/source/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ namespace hex {
return;

ImHexApi::System::impl::setContentScale(newScale);
EventDPIChanged::post(newScale);
EventScaleChanged::post();

auto win = static_cast<Window *>(hex::glfw::GetWindowUserPointer(window));
win->fullFrame();
Expand Down Expand Up @@ -1040,7 +1040,7 @@ namespace hex {
plugin.setImGuiContext(ImGui::GetCurrentContext());

RequestInitThemeHandlers::post();
EventDPIChanged::post(ImHexApi::System::getContentScale());
EventScaleChanged::post();
}

void Window::exitGLFW() {
Expand Down
8 changes: 6 additions & 2 deletions plugins/builtin/source/content/out_of_box_experience.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,12 @@ namespace hex::plugin::builtin {
ImHexApi::System::setWindowResizable(false);

const auto imageTheme = ThemeManager::getImageTheme();
const auto scale = ImHexApi::System::getContentScale();
s_imhexBanner = ImGuiExt::Texture::fromSVG(romfs::get(hex::format("assets/{}/banner.svg", imageTheme)).span<std::byte>(), 300_scaled, 0, scale);
auto loadBanner = [=] {
const auto scale = ImHexApi::System::getContentScale();
s_imhexBanner = ImGuiExt::Texture::fromSVG(romfs::get(hex::format("assets/{}/banner.svg", imageTheme)).span<std::byte>(), 300_scaled, 0, scale);
};
EventScaleChanged::subscribe(loadBanner);
loadBanner();
s_compassTexture = ImGuiExt::Texture::fromImage(romfs::get("assets/common/compass.png").span<std::byte>());
s_globeTexture = ImGuiExt::Texture::fromImage(romfs::get("assets/common/globe.png").span<std::byte>());
s_screenshotDescriptions = nlohmann::json::parse(romfs::get("assets/screenshot_descriptions.json").string());
Expand Down
25 changes: 18 additions & 7 deletions plugins/builtin/source/content/welcome_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,15 @@ namespace hex::plugin::builtin {
}
}

static auto changeTextureSvg(const std::string &path, float width) {
const auto scale = ImHexApi::System::getContentScale();
return ImGuiExt::Texture::fromSVG(romfs::get(path).span(), width, 0, scale, ImGuiExt::Texture::Filter::Linear);
};

static void loadBannerTexture() {
s_bannerTexture = changeTextureSvg(hex::format("assets/{}/banner.svg", ThemeManager::getImageTheme()), 300_scaled);
}

/**
* @brief Registers the event handlers related to the welcome screen
* should only be called once, at startup
Expand Down Expand Up @@ -518,19 +527,21 @@ namespace hex::plugin::builtin {
ImHexApi::System::setTargetFPS(static_cast<float>(value.get<int>(14)));
});

EventScaleChanged::subscribe([]() {
// FIXME: The condition is a hack to wait until the
// RequestChangeTheme event has fired because it is too hard to me
// to do appropriate state management with way the event manager is
// designed and with global variables everywhere
if (s_bannerTexture)
loadBannerTexture();
});
RequestChangeTheme::subscribe([](const std::string &theme) {
auto changeTexture = [&](const std::string &path) {
return ImGuiExt::Texture::fromImage(romfs::get(path).span(), ImGuiExt::Texture::Filter::Nearest);
};

auto changeTextureSvg = [&](const std::string &path, float width) {
// UI scaling is already baked into the width
const auto scale = ImHexApi::System::getContentScale();
return ImGuiExt::Texture::fromSVG(romfs::get(path).span(), width, 0, scale, ImGuiExt::Texture::Filter::Linear);
};

ThemeManager::changeTheme(theme);
s_bannerTexture = changeTextureSvg(hex::format("assets/{}/banner.svg", ThemeManager::getImageTheme()), 300_scaled);
loadBannerTexture();
s_nightlyTexture = changeTextureSvg(hex::format("assets/{}/nightly.svg", "common"), 35_scaled);
s_backdropTexture = changeTexture(hex::format("assets/{}/backdrop.png", ThemeManager::getImageTheme()));

Expand Down
4 changes: 2 additions & 2 deletions plugins/fonts/source/font_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace hex::fonts {
m_config.SizePixels = ImHexApi::Fonts::DefaultFontSize;
m_config.MergeMode = false;

EventDPIChanged::subscribe(this, [this](float) {
EventScaleChanged::subscribe(this, [this]() {
if (scaleAndBuild()) {
ImGui_ImplOpenGL3_DestroyFontsTexture();
ImGui_ImplOpenGL3_CreateFontsTexture();
Expand All @@ -62,7 +62,7 @@ namespace hex::fonts {

~FontAtlas() {
IM_DELETE(m_fontAtlas);
EventDPIChanged::unsubscribe(this);
EventScaleChanged::unsubscribe(this);
}

bool scaleAndBuild() {
Expand Down

0 comments on commit a94fbe4

Please sign in to comment.