Skip to content

Commit

Permalink
ImGuiManager: Fix changing global scale through FSUI
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Sep 2, 2023
1 parent 6e047cf commit a9f91a1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/core/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ bool Host::CreateGPUDevice(RenderAPI api)
return false;
}

if (!ImGuiManager::Initialize(g_settings.display_osd_scale / 100.0f))
if (!ImGuiManager::Initialize(g_settings.display_osd_scale / 100.0f, g_settings.display_show_osd_messages))
{
Log_ErrorPrintf("Failed to initialize ImGuiManager.");
g_gpu_device->Destroy();
Expand Down
9 changes: 7 additions & 2 deletions src/core/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3690,8 +3690,13 @@ void System::CheckForSettingsChanges(const Settings& old_settings)
PostProcessing::UpdateSettings();
}

if (g_gpu_device && g_settings.display_osd_scale != old_settings.display_osd_scale)
ImGuiManager::SetGlobalScale(g_settings.display_osd_scale / 100.0f);
if (g_gpu_device)
{
if (g_settings.display_osd_scale != old_settings.display_osd_scale)
ImGuiManager::SetGlobalScale(g_settings.display_osd_scale / 100.0f);
if (g_settings.display_show_osd_messages != old_settings.display_show_osd_messages)
ImGuiManager::SetShowOSDMessages(g_settings.display_show_osd_messages);
}

bool controllers_updated = false;
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
Expand Down
42 changes: 33 additions & 9 deletions src/util/imgui_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ struct OSDMessage
static std::deque<OSDMessage> s_osd_active_messages;
static std::deque<OSDMessage> s_osd_posted_messages;
static std::mutex s_osd_messages_lock;
static bool s_show_osd_messages = true;
static bool s_global_prescale_changed = false;

static std::array<ImGuiManager::SoftwareCursor, InputManager::MAX_SOFTWARE_CURSORS> s_software_cursors = {};

Expand All @@ -107,11 +109,24 @@ void ImGuiManager::SetFontRange(const u16* range)

void ImGuiManager::SetGlobalScale(float global_scale)
{
if (s_global_prescale == global_scale)
return;

s_global_prescale = global_scale;
UpdateScale();
s_global_prescale_changed = true;
}

void ImGuiManager::SetShowOSDMessages(bool enable)
{
if (s_show_osd_messages == enable)
return;

s_show_osd_messages = enable;
if (!enable)
Host::ClearOSDMessages();
}

bool ImGuiManager::Initialize(float global_scale)
bool ImGuiManager::Initialize(float global_scale, bool show_osd_messages)
{
if (!LoadFontData())
{
Expand All @@ -121,6 +136,7 @@ bool ImGuiManager::Initialize(float global_scale)

s_global_prescale = global_scale;
s_global_scale = std::max(g_gpu_device->GetWindowScale() * global_scale, 1.0f);
s_show_osd_messages = show_osd_messages;

ImGui::CreateContext();

Expand Down Expand Up @@ -181,10 +197,11 @@ void ImGuiManager::WindowResized()

ImGui::GetIO().DisplaySize = ImVec2(static_cast<float>(new_width), static_cast<float>(new_height));

UpdateScale();

// restart imgui frame on the new window size to pick it up, otherwise we draw to the old size
ImGui::EndFrame();

UpdateScale();

NewFrame();
}

Expand All @@ -196,9 +213,6 @@ void ImGuiManager::UpdateScale()
if (scale == s_global_scale && (!HasFullscreenFonts() || !ImGuiFullscreen::UpdateLayoutScale()))
return;

// This is assumed to be called mid-frame.
ImGui::EndFrame();

s_global_scale = scale;

ImGui::GetStyle() = ImGuiStyle();
Expand All @@ -211,15 +225,19 @@ void ImGuiManager::UpdateScale()

if (!g_gpu_device->UpdateImGuiFontTexture())
Panic("Failed to recreate font texture after scale+resize");

NewFrame();
}

void ImGuiManager::NewFrame()
{
ImGuiIO& io = ImGui::GetIO();
io.DeltaTime = static_cast<float>(s_last_render_time.GetTimeSecondsAndReset());

if (s_global_prescale_changed)
{
s_global_prescale_changed = false;
UpdateScale();
}

ImGui::NewFrame();

// Disable nav input on the implicit (Debug##Default) window. Otherwise we end up requesting keyboard
Expand Down Expand Up @@ -587,6 +605,9 @@ void Host::AddKeyedOSDMessage(std::string key, std::string message, float durati
else
Log_InfoPrintf("OSD: %s", message.c_str());

if (!s_show_osd_messages)
return;

OSDMessage msg;
msg.key = std::move(key);
msg.text = std::move(message);
Expand Down Expand Up @@ -622,6 +643,9 @@ void Host::AddKeyedFormattedOSDMessage(std::string key, float duration, const ch

void Host::RemoveKeyedOSDMessage(std::string key)
{
if (!s_show_osd_messages)
return;

OSDMessage msg;
msg.key = std::move(key);
msg.duration = 0.0f;
Expand Down
5 changes: 4 additions & 1 deletion src/util/imgui_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ void SetFontRange(const u16* range);
/// Changes the global scale.
void SetGlobalScale(float global_scale);

/// Changes whether OSD messages are silently dropped.
void SetShowOSDMessages(bool enable);

/// Initializes ImGui, creates fonts, etc.
bool Initialize(float global_scale);
bool Initialize(float global_scale, bool show_osd_messages);

/// Frees all ImGui resources.
void Shutdown();
Expand Down

0 comments on commit a9f91a1

Please sign in to comment.