From 7522304360f549a59ea88352bb802b1b92a30766 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 16 Nov 2021 23:52:31 +1000 Subject: [PATCH] HostInterface: Add keyed OSD messages --- src/core/host_interface.cpp | 15 ++++--- src/core/host_interface.h | 5 ++- .../regtest_host_interface.cpp | 7 ++++ .../regtest_host_interface.h | 2 + src/frontend-common/common_host_interface.cpp | 39 ++++++++++++++++++- src/frontend-common/common_host_interface.h | 3 ++ 6 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/core/host_interface.cpp b/src/core/host_interface.cpp index efb9bbf0c..29d1ad674 100644 --- a/src/core/host_interface.cpp +++ b/src/core/host_interface.cpp @@ -229,11 +229,6 @@ bool HostInterface::ConfirmFormattedMessage(const char* format, ...) return ConfirmMessage(message.c_str()); } -void HostInterface::AddOSDMessage(std::string message, float duration /* = 2.0f */) -{ - Log_InfoPrintf("OSD: %s", message.c_str()); -} - void HostInterface::AddFormattedOSDMessage(float duration, const char* format, ...) { std::va_list ap; @@ -244,6 +239,16 @@ void HostInterface::AddFormattedOSDMessage(float duration, const char* format, . AddOSDMessage(std::move(message), duration); } +void HostInterface::AddKeyedFormattedOSDMessage(std::string key, float duration, const char* format, ...) +{ + std::va_list ap; + va_start(ap, format); + std::string message = StringUtil::StdStringFromFormatV(format, ap); + va_end(ap); + + AddKeyedOSDMessage(std::move(key), std::move(message), duration); +} + std::string HostInterface::GetBIOSDirectory() { std::string dir = GetStringSettingValue("BIOS", "SearchDirectory", ""); diff --git a/src/core/host_interface.h b/src/core/host_interface.h index bf606b99e..6af55504e 100644 --- a/src/core/host_interface.h +++ b/src/core/host_interface.h @@ -70,8 +70,11 @@ public: bool ConfirmFormattedMessage(const char* format, ...) printflike(2, 3); /// Adds OSD messages, duration is in seconds. - virtual void AddOSDMessage(std::string message, float duration = 2.0f); + virtual void AddOSDMessage(std::string message, float duration = 2.0f) = 0; + virtual void AddKeyedOSDMessage(std::string key, std::string message, float duration = 2.0f) = 0; + virtual void RemoveKeyedOSDMessage(std::string key) = 0; void AddFormattedOSDMessage(float duration, const char* format, ...) printflike(3, 4); + void AddKeyedFormattedOSDMessage(std::string key, float duration, const char* format, ...) printflike(3, 4); /// Returns the base user directory path. ALWAYS_INLINE const std::string& GetUserDirectory() const { return m_user_directory; } diff --git a/src/duckstation-regtest/regtest_host_interface.cpp b/src/duckstation-regtest/regtest_host_interface.cpp index 8e8bce643..f4b58ff14 100644 --- a/src/duckstation-regtest/regtest_host_interface.cpp +++ b/src/duckstation-regtest/regtest_host_interface.cpp @@ -77,6 +77,13 @@ void RegTestHostInterface::AddOSDMessage(std::string message, float duration /*= Log_InfoPrintf("OSD: %s", message.c_str()); } +void RegTestHostInterface::AddKeyedOSDMessage(std::string key, std::string message, float duration /* = 2.0f */) +{ + Log_InfoPrintf("OSD: %s", message.c_str()); +} + +void RegTestHostInterface::RemoveKeyedOSDMessage(std::string key) {} + void RegTestHostInterface::DisplayLoadingScreen(const char* message, int progress_min /*= -1*/, int progress_max /*= -1*/, int progress_value /*= -1*/) { diff --git a/src/duckstation-regtest/regtest_host_interface.h b/src/duckstation-regtest/regtest_host_interface.h index cb5626b68..e0a52beb3 100644 --- a/src/duckstation-regtest/regtest_host_interface.h +++ b/src/duckstation-regtest/regtest_host_interface.h @@ -17,6 +17,8 @@ public: bool ConfirmMessage(const char* message) override; void AddOSDMessage(std::string message, float duration = 2.0f) override; + void AddKeyedOSDMessage(std::string key, std::string message, float duration = 2.0f) override; + void RemoveKeyedOSDMessage(std::string key) override; void DisplayLoadingScreen(const char* message, int progress_min = -1, int progress_max = -1, int progress_value = -1) override; void GetGameInfo(const char* path, CDImage* image, std::string* code, std::string* title) override; diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index 1c32a6d32..3b420cb17 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -1389,6 +1389,27 @@ void CommonHostInterface::AddOSDMessage(std::string message, float duration /*= m_osd_posted_messages.push_back(std::move(msg)); } +void CommonHostInterface::AddKeyedOSDMessage(std::string key, std::string message, float duration /*= 2.0f*/) +{ + OSDMessage msg; + msg.key = std::move(key); + msg.text = std::move(message); + msg.duration = duration; + + std::unique_lock lock(m_osd_messages_lock); + m_osd_posted_messages.push_back(std::move(msg)); +} + +void CommonHostInterface::RemoveKeyedOSDMessage(std::string key) +{ + OSDMessage msg; + msg.key = std::move(key); + msg.duration = 0.0f; + + std::unique_lock lock(m_osd_messages_lock); + m_osd_posted_messages.push_back(std::move(msg)); +} + void CommonHostInterface::ClearOSDMessages() { { @@ -1425,7 +1446,23 @@ void CommonHostInterface::AcquirePendingOSDMessages() break; if (g_settings.display_show_osd_messages) - m_osd_active_messages.push_back(std::move(m_osd_posted_messages.front())); + { + OSDMessage& new_msg = m_osd_posted_messages.front(); + std::deque::iterator iter; + if (!new_msg.key.empty() && (iter = std::find_if(m_osd_active_messages.begin(), m_osd_active_messages.end(), + [&new_msg](const OSDMessage& other) { + return new_msg.key == other.key; + })) != m_osd_active_messages.end()) + { + iter->text = std::move(new_msg.text); + iter->duration = new_msg.duration; + iter->time = new_msg.time; + } + else + { + m_osd_active_messages.push_back(std::move(new_msg)); + } + } m_osd_posted_messages.pop_front(); diff --git a/src/frontend-common/common_host_interface.h b/src/frontend-common/common_host_interface.h index 202a3627e..23f42df62 100644 --- a/src/frontend-common/common_host_interface.h +++ b/src/frontend-common/common_host_interface.h @@ -223,6 +223,8 @@ public: /// Adds OSD messages, duration is in seconds. void AddOSDMessage(std::string message, float duration = 2.0f) override; + void AddKeyedOSDMessage(std::string key, std::string message, float duration = 2.0f) override; + void RemoveKeyedOSDMessage(std::string key) override; void ClearOSDMessages(); /// async message queue bookeeping for. Should be called on UI thread. @@ -353,6 +355,7 @@ protected: struct OSDMessage { + std::string key; std::string text; Common::Timer time; float duration;