2022-08-28 08:32:00 -07:00
|
|
|
#include <algorithm>
|
2021-02-21 16:38:02 -08:00
|
|
|
#include <sstream>
|
2013-06-05 23:03:23 +02:00
|
|
|
#include "UI/OnScreenDisplay.h"
|
|
|
|
|
2020-10-01 13:05:04 +02:00
|
|
|
#include "Common/Data/Color/RGBAUtil.h"
|
2022-09-03 21:16:59 -07:00
|
|
|
#include "Common/Data/Encoding/Utf8.h"
|
2020-10-04 23:24:14 +02:00
|
|
|
#include "Common/Render/TextureAtlas.h"
|
|
|
|
#include "Common/Render/DrawBuffer.h"
|
2013-05-22 18:00:06 +02:00
|
|
|
|
2020-10-04 20:48:47 +02:00
|
|
|
#include "Common/UI/Context.h"
|
2014-12-31 16:50:23 +01:00
|
|
|
|
2020-08-15 20:53:08 +02:00
|
|
|
#include "Common/TimeUtil.h"
|
2023-06-19 15:50:36 +02:00
|
|
|
#include "Common/Net/HTTPClient.h"
|
|
|
|
#include "Core/Config.h"
|
2020-08-15 20:53:08 +02:00
|
|
|
|
2013-05-22 18:00:06 +02:00
|
|
|
OnScreenMessages osm;
|
|
|
|
|
2014-12-31 16:50:23 +01:00
|
|
|
void OnScreenMessagesView::Draw(UIContext &dc) {
|
2023-06-19 15:50:36 +02:00
|
|
|
if (!g_Config.bShowOnScreenMessages) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-22 18:00:06 +02:00
|
|
|
// First, clean out old messages.
|
2014-12-31 16:50:23 +01:00
|
|
|
osm.Lock();
|
|
|
|
osm.Clean();
|
2013-05-22 18:00:06 +02:00
|
|
|
|
|
|
|
// Get height
|
|
|
|
float w, h;
|
2016-08-07 16:49:50 -07:00
|
|
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, "Wg", &w, &h);
|
2013-05-22 18:00:06 +02:00
|
|
|
|
|
|
|
float y = 10.0f;
|
|
|
|
// Then draw them all.
|
2014-12-31 16:50:23 +01:00
|
|
|
const std::list<OnScreenMessages::Message> &messages = osm.Messages();
|
2020-10-10 19:01:40 +02:00
|
|
|
double now = time_now_d();
|
2014-12-31 16:50:23 +01:00
|
|
|
for (auto iter = messages.begin(); iter != messages.end(); ++iter) {
|
2020-10-10 19:01:40 +02:00
|
|
|
float alpha = (iter->endTime - now) * 4.0f;
|
2013-10-13 12:05:50 +02:00
|
|
|
if (alpha > 1.0) alpha = 1.0f;
|
|
|
|
if (alpha < 0.0) alpha = 0.0f;
|
2022-08-24 10:20:33 +02:00
|
|
|
dc.SetFontScale(1.0f, 1.0f);
|
2013-10-13 12:05:50 +02:00
|
|
|
// Messages that are wider than the screen are left-aligned instead of centered.
|
2022-08-24 10:20:33 +02:00
|
|
|
|
|
|
|
int align = 0;
|
2022-09-03 21:16:59 -07:00
|
|
|
// If we have newlines, we may be looking at ASCII debug output. But let's verify.
|
2022-08-24 10:20:33 +02:00
|
|
|
if (iter->text.find('\n') != 0) {
|
2022-09-03 21:16:59 -07:00
|
|
|
if (!UTF8StringHasNonASCII(iter->text.c_str()))
|
|
|
|
align |= FLAG_DYNAMIC_ASCII;
|
2022-08-24 10:20:33 +02:00
|
|
|
}
|
|
|
|
|
2013-10-13 12:05:50 +02:00
|
|
|
float tw, th;
|
2022-08-24 10:20:33 +02:00
|
|
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, iter->text.c_str(), &tw, &th, align);
|
2014-12-31 16:50:23 +01:00
|
|
|
float x = bounds_.centerX();
|
|
|
|
if (tw > bounds_.w) {
|
2022-08-24 10:20:33 +02:00
|
|
|
align |= ALIGN_TOP | ALIGN_LEFT;
|
2013-10-13 12:05:50 +02:00
|
|
|
x = 2;
|
2022-08-24 10:20:33 +02: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 12:05:50 +02:00
|
|
|
}
|
2015-05-07 11:14:04 +08:00
|
|
|
dc.SetFontStyle(dc.theme->uiFont);
|
|
|
|
dc.DrawTextShadow(iter->text.c_str(), x, y, colorAlpha(iter->color, alpha), align);
|
2022-08-24 10:20:33 +02:00
|
|
|
y += th * scale;
|
2013-05-22 18:00:06 +02:00
|
|
|
}
|
2014-12-31 16:50:23 +01:00
|
|
|
|
|
|
|
osm.Unlock();
|
2023-06-19 15:50:36 +02:00
|
|
|
|
|
|
|
// Thin bar at the top of the screen.
|
|
|
|
std::vector<float> progress = g_DownloadManager.GetCurrentProgress();
|
|
|
|
if (!progress.empty()) {
|
|
|
|
static const uint32_t colors[4] = {
|
|
|
|
0xFFFFFFFF,
|
|
|
|
0xFFCCCCCC,
|
|
|
|
0xFFAAAAAA,
|
|
|
|
0xFF777777,
|
|
|
|
};
|
|
|
|
|
|
|
|
dc.Begin();
|
|
|
|
int h = 5;
|
|
|
|
for (size_t i = 0; i < progress.size(); i++) {
|
|
|
|
float barWidth = 10 + (dc.GetBounds().w - 10) * progress[i];
|
|
|
|
Bounds bounds(0, h * i, barWidth, h);
|
|
|
|
UI::Drawable solid(colors[i & 3]);
|
|
|
|
dc.FillRect(solid, bounds);
|
|
|
|
}
|
|
|
|
dc.Flush();
|
|
|
|
}
|
2014-12-31 16:50:23 +01:00
|
|
|
}
|
|
|
|
|
2021-02-21 16:38:02 -08: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 16:50:23 +01: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 18:00:06 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 19:08:48 +02:00
|
|
|
void OnScreenMessages::Show(const std::string &text, float duration_s, uint32_t color, int icon, bool checkUnique, const char *id) {
|
2013-12-08 11:51:33 +01:00
|
|
|
double now = time_now_d();
|
2017-02-27 21:57:46 +01:00
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
2013-06-06 10:05:31 +02:00
|
|
|
if (checkUnique) {
|
|
|
|
for (auto iter = messages_.begin(); iter != messages_.end(); ++iter) {
|
2015-09-27 11:42:26 +02:00
|
|
|
if (iter->text == text || (id && iter->id && !strcmp(iter->id, id))) {
|
2013-12-08 11:51:33 +01:00
|
|
|
Message msg = *iter;
|
|
|
|
msg.endTime = now + duration_s;
|
2015-09-25 19:08:48 +02:00
|
|
|
msg.text = text;
|
|
|
|
msg.color = color;
|
2013-12-08 11:51:33 +01:00
|
|
|
messages_.erase(iter);
|
|
|
|
messages_.insert(messages_.begin(), msg);
|
2013-06-06 10:05:31 +02:00
|
|
|
return;
|
2013-12-08 11:51:33 +01:00
|
|
|
}
|
2013-06-06 10:05:31 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-22 18:00:06 +02:00
|
|
|
Message msg;
|
2015-09-25 19:08:48 +02:00
|
|
|
msg.text = text;
|
2013-05-22 18:00:06 +02:00
|
|
|
msg.color = color;
|
|
|
|
msg.endTime = now + duration_s;
|
|
|
|
msg.icon = icon;
|
2015-09-25 19:08:48 +02:00
|
|
|
msg.id = id;
|
2013-05-22 18:00:06 +02: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);
|
|
|
|
}
|
2023-06-19 15:50:36 +02:00
|
|
|
|
|
|
|
void OSDOverlayScreen::CreateViews() {
|
|
|
|
root_ = new UI::AnchorLayout();
|
|
|
|
root_->Add(new OnScreenMessagesView(new UI::AnchorLayoutParams(0.0f, 0.0f, 0.0f, 0.0f)));
|
|
|
|
}
|