diff --git a/Common/System/OSD.cpp b/Common/System/OSD.cpp index 704b4367e9..997123af86 100644 --- a/Common/System/OSD.cpp +++ b/Common/System/OSD.cpp @@ -12,12 +12,30 @@ OnScreenDisplay g_OSD; // Effectively forever. constexpr double forever_s = 10000000000.0; +OnScreenDisplay::~OnScreenDisplay() { + std::lock_guard guard(mutex_); + + double now = time_now_d(); + for (auto iter = entries_.begin(); iter != entries_.end(); ) { + if (iter->clickCallback) { + // Wasn't clicked, but let it free any data. + iter->clickCallback(false, iter->clickUserData); + iter->clickCallback = nullptr; + } + } +} + void OnScreenDisplay::Update() { std::lock_guard guard(mutex_); double now = time_now_d(); for (auto iter = entries_.begin(); iter != entries_.end(); ) { if (now >= iter->endTime) { + if (iter->clickCallback) { + // Wasn't clicked, but let it free any data. + iter->clickCallback(false, iter->clickUserData); + iter->clickCallback = nullptr; + } iter = entries_.erase(iter); } else { iter++; @@ -41,10 +59,14 @@ float OnScreenDisplay::SidebarAlpha() const { return saturatef(1.0f - ((float)timeSinceNudge - 0.1f) * 4.0f); } -void OnScreenDisplay::DismissEntry(size_t index, double now) { +void OnScreenDisplay::ClickEntry(size_t index, double now) { std::lock_guard guard(mutex_); if (index < entries_.size() && entries_[index].type != OSDType::ACHIEVEMENT_CHALLENGE_INDICATOR) { entries_[index].endTime = std::min(now + FadeoutTime(), entries_[index].endTime); + if (entries_[index].clickCallback) { + entries_[index].clickCallback(true, entries_[index].clickUserData); + entries_[index].clickCallback = nullptr; + } } } @@ -90,7 +112,7 @@ void OnScreenDisplay::Show(OSDType type, std::string_view text, std::string_view } } - Entry msg; + Entry msg{}; msg.text = text; msg.text2 = text2; msg.iconName = icon; @@ -116,7 +138,7 @@ void OnScreenDisplay::ShowAchievementUnlocked(int achievementID) { double duration_s = 5.0; - Entry msg; + Entry msg{}; msg.numericID = achievementID; msg.type = OSDType::ACHIEVEMENT_UNLOCKED; msg.startTime = now; @@ -149,7 +171,7 @@ void OnScreenDisplay::ShowAchievementProgress(int achievementID, bool show) { } // OK, let's make a new side-entry. - Entry entry; + Entry entry{}; entry.numericID = achievementID; entry.type = OSDType::ACHIEVEMENT_PROGRESS; entry.startTime = now; @@ -175,7 +197,7 @@ void OnScreenDisplay::ShowChallengeIndicator(int achievementID, bool show) { } // OK, let's make a new side-entry. - Entry entry; + Entry entry{}; entry.numericID = achievementID; entry.type = OSDType::ACHIEVEMENT_CHALLENGE_INDICATOR; entry.startTime = now; @@ -208,7 +230,7 @@ void OnScreenDisplay::ShowLeaderboardTracker(int leaderboardTrackerID, const cha } // OK, let's make a new side-entry. - Entry entry; + Entry entry{}; entry.numericID = leaderboardTrackerID; entry.type = OSDType::LEADERBOARD_TRACKER; entry.startTime = now; @@ -247,7 +269,7 @@ void OnScreenDisplay::SetProgressBar(std::string_view id, std::string_view messa } } - Entry bar; + Entry bar{}; bar.id = id; bar.type = OSDType::PROGRESS_BAR; bar.text = message; @@ -298,3 +320,11 @@ void OnScreenDisplay::ClearAchievementStuff() { } } } + +void OnScreenDisplay::SetClickCallback(const char *id, void (*callback)(bool, void *)) { + for (auto &ent : entries_) { + if (ent.id == id) { + ent.clickCallback = callback; + } + } +} diff --git a/Common/System/OSD.h b/Common/System/OSD.h index 24cb4b4ab2..8715f41f4b 100644 --- a/Common/System/OSD.h +++ b/Common/System/OSD.h @@ -35,6 +35,8 @@ enum class OSDType { // Data holder for on-screen messages. class OnScreenDisplay { public: + ~OnScreenDisplay(); + // If you specify 0.0f as duration, a duration will be chosen automatically depending on type. void Show(OSDType type, std::string_view text, float duration_s = 0.0f, const char *id = nullptr) { Show(type, text, "", duration_s, id); @@ -72,6 +74,9 @@ public: // Fades out everything related to achievements. Should be used on game shutdown. void ClearAchievementStuff(); + // Can't add an infinite number of "Show" functions, so starting to offer post-modification. + void SetClickCallback(const char *id, void (*callback)(bool, void *)); + struct Entry { OSDType type; std::string text; @@ -79,6 +84,12 @@ public: std::string iconName; int numericID; std::string id; + + // We could use std::function, but prefer to do it the oldschool way. + void (*clickCallback)(bool, void *); + void *clickUserData; + + int instanceID; double startTime; double endTime; @@ -91,7 +102,7 @@ public: std::vector Entries(); // TODO: Use something more stable than the index. - void DismissEntry(size_t index, double now); + void ClickEntry(size_t index, double now); static float FadeinTime() { return 0.1f; } static float FadeoutTime() { return 0.25f; } diff --git a/UI/DevScreens.cpp b/UI/DevScreens.cpp index 983f599214..c37343b041 100644 --- a/UI/DevScreens.cpp +++ b/UI/DevScreens.cpp @@ -870,8 +870,14 @@ void SystemInfoScreen::CreateInternalsTab(UI::ViewGroup *internals) { g_OSD.Show(OSDType::MESSAGE_INFO, "Info"); return UI::EVENT_DONE; }); + // This one is clickable internals->Add(new Choice(si->T("Success")))->OnClick.Add([&](UI::EventParams &) { - g_OSD.Show(OSDType::MESSAGE_SUCCESS, "Success"); + g_OSD.Show(OSDType::MESSAGE_SUCCESS, "Success", 0.0f, "clickable"); + g_OSD.SetClickCallback("clickable", [](bool clicked, void*) { + if (clicked) { + System_LaunchUrl(LaunchUrlType::BROWSER_URL, "https://www.google.com/"); + } + }); return UI::EVENT_DONE; }); internals->Add(new Choice(sy->T("RetroAchievements")))->OnClick.Add([&](UI::EventParams &) { diff --git a/UI/OnScreenDisplay.cpp b/UI/OnScreenDisplay.cpp index 73773584e2..2a968456c6 100644 --- a/UI/OnScreenDisplay.cpp +++ b/UI/OnScreenDisplay.cpp @@ -379,7 +379,7 @@ void OnScreenMessagesView::Draw(UIContext &dc) { edges[(size_t)pos].maxWidth = std::max(edges[(size_t)pos].maxWidth, measuredEntry.w); } - std::vector dismissZones; + std::vector clickZones; // Now, perform layout for all 8 edges. for (size_t i = 0; i < (size_t)ScreenEdgePosition::VALUE_COUNT; i++) { @@ -471,7 +471,7 @@ void OnScreenMessagesView::Draw(UIContext &dc) { case OSDType::MESSAGE_FILE_LINK: case OSDType::ACHIEVEMENT_UNLOCKED: // Save the location of the popup, for easy dismissal. - dismissZones.push_back(ClickZone{ (int)j, b }); + clickZones.push_back(ClickZone{ (int)j, b }); break; default: break; @@ -486,7 +486,7 @@ void OnScreenMessagesView::Draw(UIContext &dc) { } std::lock_guard lock(clickMutex_); - clickZones_ = dismissZones; + clickZones_ = clickZones; } std::string OnScreenMessagesView::DescribeText() const { @@ -508,7 +508,7 @@ bool OnScreenMessagesView::Dismiss(float x, float y) { double now = time_now_d(); for (auto &zone : clickZones_) { if (zone.bounds.Contains(x, y)) { - g_OSD.DismissEntry(zone.index, now); + g_OSD.ClickEntry(zone.index, now); dismissed = true; } }