Immediately apply gui_scaling changes to IGUISkin

This commit is contained in:
Gregor Parzefall 2024-05-24 17:34:32 +02:00 committed by grorp
parent b1fa430dad
commit f3c91e4f96
2 changed files with 39 additions and 17 deletions

@ -36,6 +36,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "network/networkexceptions.h" #include "network/networkexceptions.h"
#include <IGUISpriteBank.h> #include <IGUISpriteBank.h>
#include <ICameraSceneNode.h> #include <ICameraSceneNode.h>
#include <unordered_map>
#if USE_SOUND #if USE_SOUND
#include "sound/sound_openal.h" #include "sound/sound_openal.h"
@ -68,6 +69,7 @@ static void dump_start_data(const GameStartData &data)
ClientLauncher::~ClientLauncher() ClientLauncher::~ClientLauncher()
{ {
delete input; delete input;
g_settings->deregisterChangedCallback("gui_scaling", setting_changed_callback, this);
delete g_fontengine; delete g_fontengine;
g_fontengine = nullptr; g_fontengine = nullptr;
@ -126,7 +128,8 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
setAttribute(scene::ALLOW_ZWRITE_ON_TRANSPARENT, true); setAttribute(scene::ALLOW_ZWRITE_ON_TRANSPARENT, true);
guienv = m_rendering_engine->get_gui_env(); guienv = m_rendering_engine->get_gui_env();
init_guienv(guienv); config_guienv();
g_settings->registerChangedCallback("gui_scaling", setting_changed_callback, this);
g_fontengine = new FontEngine(guienv); g_fontengine = new FontEngine(guienv);
@ -326,7 +329,12 @@ void ClientLauncher::init_input()
} }
} }
void ClientLauncher::init_guienv(gui::IGUIEnvironment *guienv) void ClientLauncher::setting_changed_callback(const std::string &name, void *data)
{
static_cast<ClientLauncher*>(data)->config_guienv();
}
void ClientLauncher::config_guienv()
{ {
gui::IGUISkin *skin = guienv->getSkin(); gui::IGUISkin *skin = guienv->getSkin();
@ -345,23 +353,35 @@ void ClientLauncher::init_guienv(gui::IGUIEnvironment *guienv)
skin->setSize(gui::EGDS_CHECK_BOX_WIDTH, (s32)(17.0f * density)); skin->setSize(gui::EGDS_CHECK_BOX_WIDTH, (s32)(17.0f * density));
skin->setSize(gui::EGDS_SCROLLBAR_SIZE, (s32)(21.0f * density)); skin->setSize(gui::EGDS_SCROLLBAR_SIZE, (s32)(21.0f * density));
skin->setSize(gui::EGDS_WINDOW_BUTTON_WIDTH, (s32)(15.0f * density)); skin->setSize(gui::EGDS_WINDOW_BUTTON_WIDTH, (s32)(15.0f * density));
static u32 orig_sprite_id = skin->getIcon(gui::EGDI_CHECK_BOX_CHECKED);
static std::unordered_map<std::string, u32> sprite_ids;
if (density > 1.5f) { if (density > 1.5f) {
std::string sprite_path = porting::path_share + "/textures/base/pack/";
if (density > 3.5f)
sprite_path.append("checkbox_64.png");
else if (density > 2.0f)
sprite_path.append("checkbox_32.png");
else
sprite_path.append("checkbox_16.png");
// Texture dimensions should be a power of 2 // Texture dimensions should be a power of 2
gui::IGUISpriteBank *sprites = skin->getSpriteBank(); std::string path = porting::path_share + "/textures/base/pack/";
video::IVideoDriver *driver = m_rendering_engine->get_video_driver(); if (density > 3.5f)
video::ITexture *sprite_texture = driver->getTexture(sprite_path.c_str()); path.append("checkbox_64.png");
if (sprite_texture) { else if (density > 2.0f)
s32 sprite_id = sprites->addTextureAsSprite(sprite_texture); path.append("checkbox_32.png");
if (sprite_id != -1) else
skin->setIcon(gui::EGDI_CHECK_BOX_CHECKED, sprite_id); path.append("checkbox_16.png");
auto cached_id = sprite_ids.find(path);
if (cached_id != sprite_ids.end()) {
skin->setIcon(gui::EGDI_CHECK_BOX_CHECKED, cached_id->second);
} else {
gui::IGUISpriteBank *sprites = skin->getSpriteBank();
video::IVideoDriver *driver = m_rendering_engine->get_video_driver();
video::ITexture *texture = driver->getTexture(path.c_str());
s32 id = sprites->addTextureAsSprite(texture);
if (id != -1) {
skin->setIcon(gui::EGDI_CHECK_BOX_CHECKED, id);
sprite_ids.emplace(path, id);
}
} }
} else {
skin->setIcon(gui::EGDI_CHECK_BOX_CHECKED, orig_sprite_id);
} }
} }

@ -38,7 +38,9 @@ private:
void init_args(GameStartData &start_data, const Settings &cmd_args); void init_args(GameStartData &start_data, const Settings &cmd_args);
bool init_engine(); bool init_engine();
void init_input(); void init_input();
void init_guienv(gui::IGUIEnvironment *guienv);
static void setting_changed_callback(const std::string &name, void *data);
void config_guienv();
bool launch_game(std::string &error_message, bool reconnect_requested, bool launch_game(std::string &error_message, bool reconnect_requested,
GameStartData &start_data, const Settings &cmd_args); GameStartData &start_data, const Settings &cmd_args);