Rename OSD "Message" to "Entry" since it can soon be multiple other things (progress bar, etc).

This commit is contained in:
Henrik Rydgård 2023-06-20 15:07:01 +02:00
parent 7cc8c6cea4
commit ed0c4b7a9e
3 changed files with 37 additions and 22 deletions

View File

@ -11,18 +11,18 @@ void OnScreenDisplay::Update() {
std::lock_guard<std::mutex> guard(mutex_); std::lock_guard<std::mutex> guard(mutex_);
double now = time_now_d(); double now = time_now_d();
for (auto iter = messages_.begin(); iter != messages_.end(); ) { for (auto iter = entries_.begin(); iter != entries_.end(); ) {
if (now >= iter->endTime) { if (now >= iter->endTime) {
iter = messages_.erase(iter); iter = entries_.erase(iter);
} else { } else {
iter++; iter++;
} }
} }
} }
std::vector<OnScreenDisplay::Message> OnScreenDisplay::Messages() { std::vector<OnScreenDisplay::Entry> OnScreenDisplay::Entries() {
std::lock_guard<std::mutex> guard(mutex_); std::lock_guard<std::mutex> guard(mutex_);
return messages_; return entries_; // makes a copy.
} }
void OnScreenDisplay::Show(OSDType type, const std::string &text, float duration_s, const char *id) { void OnScreenDisplay::Show(OSDType type, const std::string &text, float duration_s, const char *id) {
@ -48,23 +48,23 @@ void OnScreenDisplay::Show(OSDType type, const std::string &text, float duration
double now = time_now_d(); double now = time_now_d();
std::lock_guard<std::mutex> guard(mutex_); std::lock_guard<std::mutex> guard(mutex_);
if (id) { if (id) {
for (auto iter = messages_.begin(); iter != messages_.end(); ++iter) { for (auto iter = entries_.begin(); iter != entries_.end(); ++iter) {
if (iter->id && !strcmp(iter->id, id)) { if (iter->id && !strcmp(iter->id, id)) {
Message msg = *iter; Entry msg = *iter;
msg.endTime = now + duration_s; msg.endTime = now + duration_s;
msg.text = text; msg.text = text;
messages_.erase(iter); entries_.erase(iter);
messages_.insert(messages_.begin(), msg); entries_.insert(entries_.begin(), msg);
return; return;
} }
} }
} }
Message msg; Entry msg;
msg.text = text; msg.text = text;
msg.endTime = now + duration_s; msg.endTime = now + duration_s;
msg.id = id; msg.id = id;
messages_.insert(messages_.begin(), msg); entries_.insert(entries_.begin(), msg);
} }
void OnScreenDisplay::ShowOnOff(const std::string &message, bool on, float duration_s) { void OnScreenDisplay::ShowOnOff(const std::string &message, bool on, float duration_s) {
@ -72,7 +72,6 @@ void OnScreenDisplay::ShowOnOff(const std::string &message, bool on, float durat
Show(OSDType::MESSAGE_INFO, message + ": " + (on ? "on" : "off"), duration_s); Show(OSDType::MESSAGE_INFO, message + ": " + (on ? "on" : "off"), duration_s);
} }
const char *RequestTypeAsString(SystemRequestType type) { const char *RequestTypeAsString(SystemRequestType type) {
switch (type) { switch (type) {
case SystemRequestType::INPUT_TEXT_MODAL: return "INPUT_TEXT_MODAL"; case SystemRequestType::INPUT_TEXT_MODAL: return "INPUT_TEXT_MODAL";

View File

@ -224,26 +224,30 @@ enum class OSDType {
// PROGRESS_INDETERMINATE, // PROGRESS_INDETERMINATE,
}; };
// Data holder. This one is currently global. // Data holder for on-screen messages.
class OnScreenDisplay { class OnScreenDisplay {
public: public:
// If you specify 0 duration, a duration will be chosen automatically depending on type. // If you specify 0.0f as duration, a duration will be chosen automatically depending on type.
void Show(OSDType type, const std::string &message, float duration_s = 0.0f, const char *id = nullptr); void Show(OSDType type, const std::string &message, float duration_s = 0.0f, const char *id = nullptr);
void ShowOnOff(const std::string &message, bool on, float duration_s = 0.0f); void ShowOnOff(const std::string &message, bool on, float duration_s = 0.0f);
bool IsEmpty() const { return messages_.empty(); }
bool IsEmpty() const { return entries_.empty(); } // Shortcut to skip rendering.
// Call this every frame, cleans up old entries.
void Update(); void Update();
struct Message { struct Entry {
OSDType type;
std::string text; std::string text;
const char *id; const char *id;
double endTime; double endTime;
double duration; double duration;
float progress;
}; };
std::vector<Message> Messages(); std::vector<Entry> Entries();
private: private:
std::vector<Message> messages_; std::vector<Entry> entries_;
std::mutex mutex_; std::mutex mutex_;
}; };

View File

@ -15,6 +15,18 @@
#include "Common/Net/HTTPClient.h" #include "Common/Net/HTTPClient.h"
#include "Core/Config.h" #include "Core/Config.h"
static uint32_t GetOSDBackgroundColor(OSDType type) {
// Colors from Infima
switch (type) {
case OSDType::MESSAGE_ERROR:
case OSDType::MESSAGE_ERROR_DUMP: return 0xd53035; // danger-darker
case OSDType::MESSAGE_WARNING: return 0xd99e00; // warning-darker
case OSDType::MESSAGE_INFO: return 0x606770; // gray-700
case OSDType::MESSAGE_SUCCESS: return 0x008b00;
default: return 0x606770;
}
}
void OnScreenMessagesView::Draw(UIContext &dc) { void OnScreenMessagesView::Draw(UIContext &dc) {
if (!g_Config.bShowOnScreenMessages) { if (!g_Config.bShowOnScreenMessages) {
return; return;
@ -26,9 +38,9 @@ void OnScreenMessagesView::Draw(UIContext &dc) {
float y = 10.0f; float y = 10.0f;
// Then draw them all. // Then draw them all.
const std::vector<OnScreenDisplay::Message> messages = g_OSD.Messages(); const std::vector<OnScreenDisplay::Entry> entries = g_OSD.Entries();
double now = time_now_d(); double now = time_now_d();
for (auto iter = messages.begin(); iter != messages.end(); ++iter) { for (auto iter = entries.begin(); iter != entries.end(); ++iter) {
float alpha = (iter->endTime - now) * 4.0f; float alpha = (iter->endTime - now) * 4.0f;
if (alpha > 1.0) alpha = 1.0f; if (alpha > 1.0) alpha = 1.0f;
if (alpha < 0.0) alpha = 0.0f; if (alpha < 0.0) alpha = 0.0f;
@ -86,9 +98,9 @@ void OnScreenMessagesView::Draw(UIContext &dc) {
std::string OnScreenMessagesView::DescribeText() const { std::string OnScreenMessagesView::DescribeText() const {
std::stringstream ss; std::stringstream ss;
const auto &messages = g_OSD.Messages(); const auto &entries = g_OSD.Entries();
for (auto iter = messages.begin(); iter != messages.end(); ++iter) { for (auto iter = entries.begin(); iter != entries.end(); ++iter) {
if (iter != messages.begin()) { if (iter != entries.begin()) {
ss << "\n"; ss << "\n";
} }
ss << iter->text; ss << iter->text;