mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-30 19:24:23 +01:00
Clean up the OSD click callback code
This commit is contained in:
1 parent
4ccd41110b
commit
a92ff89d5f
9 files changed
+43
-95
No files matched your search
+6
-23
@@ -12,29 +12,12 @@ OnScreenDisplay g_OSD;
|
||||
// Effectively forever.
|
||||
constexpr double forever_s = 10000000000.0;
|
||||
|
||||
OnScreenDisplay::~OnScreenDisplay() {
|
||||
std::lock_guard<std::mutex> guard(mutex_);
|
||||
|
||||
double now = time_now_d();
|
||||
for (auto &iter : entries_) {
|
||||
if (iter.clickCallback) {
|
||||
// Wasn't clicked, but let it free any data.
|
||||
iter.clickCallback(false, iter.clickUserData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnScreenDisplay::Update() {
|
||||
std::lock_guard<std::mutex> 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++;
|
||||
@@ -61,9 +44,10 @@ float OnScreenDisplay::IngameAlpha() const {
|
||||
void OnScreenDisplay::ClickEntry(size_t index, double now) {
|
||||
std::lock_guard<std::mutex> 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();
|
||||
} else {
|
||||
entries_[index].endTime = std::min(now + FadeoutTime(), entries_[index].endTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -333,18 +317,17 @@ void OnScreenDisplay::ClearAchievementStuff() {
|
||||
}
|
||||
}
|
||||
|
||||
void OnScreenDisplay::SetClickCallback(const char *id, void (*callback)(bool, void *), void *userdata) {
|
||||
void OnScreenDisplay::SetClickCallback(std::string_view id, std::function<void()> callback) {
|
||||
_dbg_assert_(callback != nullptr);
|
||||
for (auto &ent : entries_) {
|
||||
// protect against dupes.
|
||||
if (ent.id == id && !ent.clickCallback) {
|
||||
if (ent.id == id) {
|
||||
ent.clickCallback = callback;
|
||||
ent.clickUserData = userdata;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnScreenDisplay::SetFlags(const char *id, OSDMessageFlags flags) {
|
||||
void OnScreenDisplay::SetFlags(std::string_view id, OSDMessageFlags flags) {
|
||||
for (auto &ent : entries_) {
|
||||
// protect against dupes.
|
||||
if (ent.id == id) {
|
||||
|
||||
+4
-7
@@ -4,6 +4,7 @@
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
|
||||
#include "Common/Common.h"
|
||||
|
||||
@@ -49,8 +50,6 @@ ENUM_CLASS_BITOPS(OSDMessageFlags);
|
||||
// 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);
|
||||
@@ -90,8 +89,8 @@ public:
|
||||
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 *), void *userdata);
|
||||
void SetFlags(const char *id, OSDMessageFlags flag);
|
||||
void SetFlags(std::string_view id, OSDMessageFlags flag);
|
||||
void SetClickCallback(std::string_view id, std::function<void()> callback);
|
||||
|
||||
struct Entry {
|
||||
OSDType type;
|
||||
@@ -102,9 +101,7 @@ public:
|
||||
std::string id;
|
||||
OSDMessageFlags flags;
|
||||
|
||||
// We could use std::function, but prefer to do it the oldschool way.
|
||||
void (*clickCallback)(bool, void *);
|
||||
void *clickUserData;
|
||||
std::function<void()> clickCallback;
|
||||
|
||||
int instanceID;
|
||||
double startTime;
|
||||
|
||||
Reference in new issue
Block a user