2022-08-28 15:32:00 +00:00
|
|
|
#include <algorithm>
|
2021-02-22 00:38:02 +00:00
|
|
|
#include <sstream>
|
2013-06-05 21:03:23 +00:00
|
|
|
#include "UI/OnScreenDisplay.h"
|
|
|
|
|
2020-10-01 11:05:04 +00:00
|
|
|
#include "Common/Data/Color/RGBAUtil.h"
|
2020-10-04 21:24:14 +00:00
|
|
|
#include "Common/Render/TextureAtlas.h"
|
|
|
|
#include "Common/Render/DrawBuffer.h"
|
2013-05-22 16:00:06 +00:00
|
|
|
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/UI/Context.h"
|
2014-12-31 15:50:23 +00:00
|
|
|
|
2020-08-15 18:53:08 +00:00
|
|
|
#include "Common/TimeUtil.h"
|
|
|
|
|
2013-05-22 16:00:06 +00:00
|
|
|
OnScreenMessages osm;
|
|
|
|
|
2014-12-31 15:50:23 +00:00
|
|
|
void OnScreenMessagesView::Draw(UIContext &dc) {
|
2013-05-22 16:00:06 +00:00
|
|
|
// First, clean out old messages.
|
2014-12-31 15:50:23 +00:00
|
|
|
osm.Lock();
|
|
|
|
osm.Clean();
|
2013-05-22 16:00:06 +00:00
|
|
|
|
|
|
|
// Get height
|
|
|
|
float w, h;
|
2016-08-07 23:49:50 +00:00
|
|
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, "Wg", &w, &h);
|
2013-05-22 16:00:06 +00:00
|
|
|
|
|
|
|
float y = 10.0f;
|
|
|
|
// Then draw them all.
|
2014-12-31 15:50:23 +00:00
|
|
|
const std::list<OnScreenMessages::Message> &messages = osm.Messages();
|
2020-10-10 17:01:40 +00:00
|
|
|
double now = time_now_d();
|
2014-12-31 15:50:23 +00:00
|
|
|
for (auto iter = messages.begin(); iter != messages.end(); ++iter) {
|
2020-10-10 17:01:40 +00:00
|
|
|
float alpha = (iter->endTime - now) * 4.0f;
|
2013-10-13 10:05:50 +00:00
|
|
|
if (alpha > 1.0) alpha = 1.0f;
|
|
|
|
if (alpha < 0.0) alpha = 0.0f;
|
2022-08-24 08:20:33 +00:00
|
|
|
dc.SetFontScale(1.0f, 1.0f);
|
2013-10-13 10:05:50 +00:00
|
|
|
// Messages that are wider than the screen are left-aligned instead of centered.
|
2022-08-24 08:20:33 +00:00
|
|
|
|
|
|
|
int align = 0;
|
|
|
|
// Simple heuristic: Draw messages that contain line breaks with ASCII_FONT (likely debug output)
|
|
|
|
if (iter->text.find('\n') != 0) {
|
|
|
|
align |= FLAG_DYNAMIC_ASCII;
|
|
|
|
}
|
|
|
|
|
2013-10-13 10:05:50 +00:00
|
|
|
float tw, th;
|
2022-08-24 08:20:33 +00:00
|
|
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, iter->text.c_str(), &tw, &th, align);
|
2014-12-31 15:50:23 +00:00
|
|
|
float x = bounds_.centerX();
|
|
|
|
if (tw > bounds_.w) {
|
2022-08-24 08:20:33 +00:00
|
|
|
align |= ALIGN_TOP | ALIGN_LEFT;
|
2013-10-13 10:05:50 +00:00
|
|
|
x = 2;
|
2022-08-24 08:20:33 +00:00
|
|
|
} else {
|
|
|
|
align |= ALIGN_TOP | ALIGN_HCENTER;
|
|
|
|
}
|
|
|
|
float scale = 1.0f;
|
|
|
|
if (th > bounds_.h - y) {
|
|
|
|
// Scale down!
|
|
|
|
scale = std::max(0.15f, (bounds_.h - y) / th);
|
|
|
|
dc.SetFontScale(scale, scale);
|
2013-10-13 10:05:50 +00:00
|
|
|
}
|
2015-05-07 03:14:04 +00:00
|
|
|
dc.SetFontStyle(dc.theme->uiFont);
|
|
|
|
dc.DrawTextShadow(iter->text.c_str(), x, y, colorAlpha(iter->color, alpha), align);
|
2022-08-24 08:20:33 +00:00
|
|
|
y += th * scale;
|
2013-05-22 16:00:06 +00:00
|
|
|
}
|
2014-12-31 15:50:23 +00:00
|
|
|
|
|
|
|
osm.Unlock();
|
|
|
|
}
|
|
|
|
|
2021-02-22 00:38:02 +00:00
|
|
|
std::string OnScreenMessagesView::DescribeText() const {
|
|
|
|
std::stringstream ss;
|
|
|
|
const auto &messages = osm.Messages();
|
|
|
|
for (auto iter = messages.begin(); iter != messages.end(); ++iter) {
|
|
|
|
if (iter != messages.begin()) {
|
|
|
|
ss << "\n";
|
|
|
|
}
|
|
|
|
ss << iter->text;
|
|
|
|
}
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2014-12-31 15:50:23 +00:00
|
|
|
void OnScreenMessages::Clean() {
|
|
|
|
restart:
|
|
|
|
double now = time_now_d();
|
|
|
|
for (auto iter = messages_.begin(); iter != messages_.end(); iter++) {
|
|
|
|
if (iter->endTime < now) {
|
|
|
|
messages_.erase(iter);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 16:00:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 17:08:48 +00:00
|
|
|
void OnScreenMessages::Show(const std::string &text, float duration_s, uint32_t color, int icon, bool checkUnique, const char *id) {
|
2013-12-08 10:51:33 +00:00
|
|
|
double now = time_now_d();
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
2013-06-06 08:05:31 +00:00
|
|
|
if (checkUnique) {
|
|
|
|
for (auto iter = messages_.begin(); iter != messages_.end(); ++iter) {
|
2015-09-27 09:42:26 +00:00
|
|
|
if (iter->text == text || (id && iter->id && !strcmp(iter->id, id))) {
|
2013-12-08 10:51:33 +00:00
|
|
|
Message msg = *iter;
|
|
|
|
msg.endTime = now + duration_s;
|
2015-09-25 17:08:48 +00:00
|
|
|
msg.text = text;
|
|
|
|
msg.color = color;
|
2013-12-08 10:51:33 +00:00
|
|
|
messages_.erase(iter);
|
|
|
|
messages_.insert(messages_.begin(), msg);
|
2013-06-06 08:05:31 +00:00
|
|
|
return;
|
2013-12-08 10:51:33 +00:00
|
|
|
}
|
2013-06-06 08:05:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-22 16:00:06 +00:00
|
|
|
Message msg;
|
2015-09-25 17:08:48 +00:00
|
|
|
msg.text = text;
|
2013-05-22 16:00:06 +00:00
|
|
|
msg.color = color;
|
|
|
|
msg.endTime = now + duration_s;
|
|
|
|
msg.icon = icon;
|
2015-09-25 17:08:48 +00:00
|
|
|
msg.id = id;
|
2013-05-22 16:00:06 +00:00
|
|
|
messages_.insert(messages_.begin(), msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnScreenMessages::ShowOnOff(const std::string &message, bool b, float duration_s, uint32_t color, int icon) {
|
|
|
|
Show(message + (b ? ": on" : ": off"), duration_s, color, icon);
|
|
|
|
}
|