From 299ee05cd9b67c0ac3b11631fd50a0747b558d54 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 1 Dec 2019 21:33:56 +1000 Subject: [PATCH] HostInterface: Move OSD messages to base class --- src/core/host_interface.cpp | 59 +++++++++++++++++++++++++- src/core/host_interface.h | 20 ++++++++- src/duckstation/sdl_host_interface.cpp | 57 ------------------------- src/duckstation/sdl_host_interface.h | 15 ------- 4 files changed, 76 insertions(+), 75 deletions(-) diff --git a/src/core/host_interface.cpp b/src/core/host_interface.cpp index 64a654fa1..b2298dec2 100644 --- a/src/core/host_interface.cpp +++ b/src/core/host_interface.cpp @@ -7,6 +7,7 @@ #include "host_display.h" #include "system.h" #include +#include Log_SetChannel(HostInterface); #ifdef _WIN32 @@ -82,7 +83,6 @@ void HostInterface::ResetSystem() AddOSDMessage("System reset."); } - void HostInterface::DestroySystem() { m_system.reset(); @@ -100,6 +100,63 @@ void HostInterface::ReportMessage(const char* message) Log_InfoPrintf(message); } +void HostInterface::AddOSDMessage(const char* message, float duration /*= 2.0f*/) +{ + OSDMessage msg; + msg.text = message; + msg.duration = duration; + + std::unique_lock lock(m_osd_messages_lock); + m_osd_messages.push_back(std::move(msg)); +} + +void HostInterface::DrawOSDMessages() +{ + constexpr ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings | + ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoNav | + ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoFocusOnAppearing; + + std::unique_lock lock(m_osd_messages_lock); + const float scale = ImGui::GetIO().DisplayFramebufferScale.x; + + auto iter = m_osd_messages.begin(); + float position_x = 10.0f * scale; + float position_y = (10.0f + (m_settings.display_fullscreen ? 0.0f : 20.0f)) * scale; + u32 index = 0; + while (iter != m_osd_messages.end()) + { + const OSDMessage& msg = *iter; + const double time = msg.time.GetTimeSeconds(); + const float time_remaining = static_cast(msg.duration - time); + if (time_remaining <= 0.0f) + { + iter = m_osd_messages.erase(iter); + continue; + } + + const float opacity = std::min(time_remaining, 1.0f); + ImGui::SetNextWindowPos(ImVec2(position_x, position_y)); + ImGui::SetNextWindowSize(ImVec2(0.0f, 0.0f)); + ImGui::PushStyleVar(ImGuiStyleVar_Alpha, opacity); + + if (ImGui::Begin(SmallString::FromFormat("osd_%u", index++), nullptr, window_flags)) + { + ImGui::TextUnformatted(msg.text.c_str()); + position_y += ImGui::GetWindowSize().y + (4.0f * scale); + } + + ImGui::End(); + ImGui::PopStyleVar(); + ++iter; + } +} + +void HostInterface::ClearImGuiFocus() +{ + ImGui::SetWindowFocus(nullptr); +} + std::optional> HostInterface::GetBIOSImage(ConsoleRegion region) { // Try the other default filenames in the directory of the configured BIOS. diff --git a/src/core/host_interface.h b/src/core/host_interface.h index 5ac6f04f0..131d16bc2 100644 --- a/src/core/host_interface.h +++ b/src/core/host_interface.h @@ -3,8 +3,11 @@ #include "settings.h" #include "types.h" #include +#include #include +#include #include +#include #include class AudioStream; @@ -38,8 +41,8 @@ public: virtual void ReportError(const char* message); virtual void ReportMessage(const char* message); - // Adds OSD messages, duration is in seconds. - virtual void AddOSDMessage(const char* message, float duration = 2.0f) = 0; + /// Adds OSD messages, duration is in seconds. + void AddOSDMessage(const char* message, float duration = 2.0f); /// Loads the BIOS image for the specified region. virtual std::optional> GetBIOSImage(ConsoleRegion region); @@ -50,6 +53,13 @@ public: protected: using ThrottleClock = std::chrono::steady_clock; + struct OSDMessage + { + std::string text; + Timer time; + float duration; + }; + /// Connects controllers. TODO: Clean this up later... virtual void ConnectControllers(); @@ -58,6 +68,9 @@ protected: void UpdateSpeedLimiterState(); + void DrawOSDMessages(); + void ClearImGuiFocus(); + void UpdatePerformanceCounters(); void ResetPerformanceCounters(); @@ -79,6 +92,9 @@ protected: u32 m_last_global_tick_counter = 0; Timer m_fps_timer; + std::deque m_osd_messages; + std::mutex m_osd_messages_lock; + bool m_paused = false; bool m_speed_limiter_temp_disabled = false; bool m_speed_limiter_enabled = false; diff --git a/src/duckstation/sdl_host_interface.cpp b/src/duckstation/sdl_host_interface.cpp index c6360b55a..2eadb9ec6 100644 --- a/src/duckstation/sdl_host_interface.cpp +++ b/src/duckstation/sdl_host_interface.cpp @@ -608,11 +608,6 @@ void SDLHostInterface::HandleSDLKeyEvent(const SDL_Event* event) } } -void SDLHostInterface::ClearImGuiFocus() -{ - ImGui::SetWindowFocus(nullptr); -} - void SDLHostInterface::DrawImGui() { DrawMainMenuBar(); @@ -1274,58 +1269,6 @@ bool SDLHostInterface::DrawFileChooser(const char* label, std::string* path, con return result; } -void SDLHostInterface::AddOSDMessage(const char* message, float duration /*= 2.0f*/) -{ - OSDMessage msg; - msg.text = message; - msg.duration = duration; - - std::unique_lock lock(m_osd_messages_lock); - m_osd_messages.push_back(std::move(msg)); -} - -void SDLHostInterface::DrawOSDMessages() -{ - constexpr ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | - ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings | - ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoNav | - ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoFocusOnAppearing; - - std::unique_lock lock(m_osd_messages_lock); - const float scale = ImGui::GetIO().DisplayFramebufferScale.x; - - auto iter = m_osd_messages.begin(); - float position_x = 10.0f * scale; - float position_y = (10.0f + (m_settings.display_fullscreen ? 0.0f : 20.0f)) * scale; - u32 index = 0; - while (iter != m_osd_messages.end()) - { - const OSDMessage& msg = *iter; - const double time = msg.time.GetTimeSeconds(); - const float time_remaining = static_cast(msg.duration - time); - if (time_remaining <= 0.0f) - { - iter = m_osd_messages.erase(iter); - continue; - } - - const float opacity = std::min(time_remaining, 1.0f); - ImGui::SetNextWindowPos(ImVec2(position_x, position_y)); - ImGui::SetNextWindowSize(ImVec2(0.0f, 0.0f)); - ImGui::PushStyleVar(ImGuiStyleVar_Alpha, opacity); - - if (ImGui::Begin(SmallString::FromFormat("osd_%u", index++), nullptr, window_flags)) - { - ImGui::TextUnformatted(msg.text); - position_y += ImGui::GetWindowSize().y + (4.0f * scale); - } - - ImGui::End(); - ImGui::PopStyleVar(); - ++iter; - } -} - void SDLHostInterface::DoPowerOff() { Assert(m_system); diff --git a/src/duckstation/sdl_host_interface.h b/src/duckstation/sdl_host_interface.h index c2866c95c..03b41fcb0 100644 --- a/src/duckstation/sdl_host_interface.h +++ b/src/duckstation/sdl_host_interface.h @@ -31,9 +31,6 @@ public: void ReportError(const char* message) override; void ReportMessage(const char* message) override; - // Adds OSD messages, duration is in seconds. - void AddOSDMessage(const char* message, float duration = 2.0f) override; - void Run(); protected: @@ -43,13 +40,6 @@ private: static constexpr u32 NUM_QUICK_SAVE_STATES = 10; static constexpr char RESUME_SAVESTATE_FILENAME[] = "savestate_resume.bin"; - struct OSDMessage - { - String text; - Timer time; - float duration; - }; - bool HasSystem() const { return static_cast(m_system); } #ifdef WIN32 @@ -91,7 +81,6 @@ private: void HandleSDLEvent(const SDL_Event* event); void HandleSDLKeyEvent(const SDL_Event* event); - void ClearImGuiFocus(); void DrawMainMenuBar(); void DrawQuickSettingsMenu(); @@ -99,7 +88,6 @@ private: void DrawPoweredOffWindow(); void DrawSettingsWindow(); void DrawAboutWindow(); - void DrawOSDMessages(); void DrawDebugWindows(); bool DrawFileChooser(const char* label, std::string* path, const char* filter = nullptr); @@ -108,9 +96,6 @@ private: std::string m_settings_filename; - std::deque m_osd_messages; - std::mutex m_osd_messages_lock; - std::map m_sdl_controllers; std::shared_ptr m_controller;