2022-08-28 15:32:00 +00:00
|
|
|
#include <algorithm>
|
2021-02-22 00:38:02 +00:00
|
|
|
#include <sstream>
|
2023-06-20 12:40:46 +00:00
|
|
|
|
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"
|
2022-09-04 04:16:59 +00:00
|
|
|
#include "Common/Data/Encoding/Utf8.h"
|
2020-10-04 21:24:14 +00:00
|
|
|
#include "Common/Render/TextureAtlas.h"
|
|
|
|
#include "Common/Render/DrawBuffer.h"
|
2023-06-20 16:39:30 +00:00
|
|
|
#include "Common/Math/math_util.h"
|
2013-05-22 16:00:06 +00:00
|
|
|
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/UI/Context.h"
|
2023-06-20 12:40:46 +00:00
|
|
|
#include "Common/System/System.h"
|
2014-12-31 15:50:23 +00:00
|
|
|
|
2020-08-15 18:53:08 +00:00
|
|
|
#include "Common/TimeUtil.h"
|
2023-06-19 13:50:36 +00:00
|
|
|
#include "Common/Net/HTTPClient.h"
|
|
|
|
#include "Core/Config.h"
|
2020-08-15 18:53:08 +00:00
|
|
|
|
2023-06-20 13:07:01 +00:00
|
|
|
static uint32_t GetOSDBackgroundColor(OSDType type) {
|
|
|
|
// Colors from Infima
|
|
|
|
switch (type) {
|
|
|
|
case OSDType::MESSAGE_ERROR:
|
2023-06-20 15:27:34 +00:00
|
|
|
case OSDType::MESSAGE_ERROR_DUMP: return 0x3530d5; // danger-darker
|
|
|
|
case OSDType::MESSAGE_WARNING: return 0x009ed9; // warning-darker
|
|
|
|
case OSDType::MESSAGE_INFO: return 0x706760; // gray-700
|
2023-06-20 13:07:01 +00:00
|
|
|
case OSDType::MESSAGE_SUCCESS: return 0x008b00;
|
|
|
|
default: return 0x606770;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 15:27:34 +00:00
|
|
|
ImageID GetOSDIcon(OSDType type) {
|
|
|
|
switch (type) {
|
|
|
|
case OSDType::MESSAGE_INFO: return ImageID::invalid(); // return ImageID("I_INFO");
|
|
|
|
case OSDType::MESSAGE_ERROR: return ImageID("I_CROSS");
|
|
|
|
case OSDType::MESSAGE_WARNING: return ImageID("I_WARNING");
|
|
|
|
case OSDType::MESSAGE_SUCCESS: return ImageID("I_CHECKEDBOX");
|
|
|
|
default: return ImageID::invalid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const float iconSize = 36.0f;
|
|
|
|
|
|
|
|
// Align only matters here for the ASCII-only flag.
|
|
|
|
static void MeasureOSDEntry(UIContext &dc, const OnScreenDisplay::Entry &entry, int align, float *width, float *height) {
|
|
|
|
dc.MeasureText(dc.theme->uiFont, 1.0f, 1.0f, entry.text.c_str(), width, height, align);
|
|
|
|
|
|
|
|
if (!GetOSDIcon(entry.type).isInvalid()) {
|
|
|
|
*width += iconSize + 5.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
*width += 12.0f;
|
|
|
|
*height = std::max(*height, iconSize + 5.0f);
|
|
|
|
}
|
|
|
|
|
2023-06-20 16:39:30 +00:00
|
|
|
static void RenderOSDEntry(UIContext &dc, const OnScreenDisplay::Entry &entry, Bounds bounds, int align, float alpha) {
|
2023-06-20 15:27:34 +00:00
|
|
|
UI::Drawable background = UI::Drawable(colorAlpha(GetOSDBackgroundColor(entry.type), alpha));
|
|
|
|
|
|
|
|
uint32_t foreGround = whiteAlpha(alpha);
|
|
|
|
|
|
|
|
Bounds shadowBounds = bounds.Expand(10.0f);
|
|
|
|
|
2023-06-20 16:39:30 +00:00
|
|
|
dc.Draw()->DrawImage4Grid(dc.theme->dropShadow4Grid, shadowBounds.x, shadowBounds.y + 4.0f, shadowBounds.x2(), shadowBounds.y2(), alphaMul(0xFF000000, 0.9f * alpha), 1.0f);
|
2023-06-20 15:27:34 +00:00
|
|
|
|
|
|
|
dc.FillRect(background, bounds);
|
|
|
|
dc.SetFontStyle(dc.theme->uiFont);
|
|
|
|
|
|
|
|
ImageID iconID = GetOSDIcon(entry.type);
|
|
|
|
|
|
|
|
if (iconID.isValid()) {
|
|
|
|
dc.DrawImageVGradient(iconID, foreGround, foreGround, Bounds(bounds.x + 2.5f, bounds.y + 2.5f, iconSize, iconSize));
|
|
|
|
|
|
|
|
// Make room
|
|
|
|
bounds.x += iconSize + 5.0f;
|
|
|
|
bounds.w -= iconSize + 5.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.DrawTextShadowRect(entry.text.c_str(), bounds, colorAlpha(0xFFFFFFFF, alpha), (align & FLAG_DYNAMIC_ASCII) | ALIGN_CENTER);
|
|
|
|
}
|
|
|
|
|
2014-12-31 15:50:23 +00:00
|
|
|
void OnScreenMessagesView::Draw(UIContext &dc) {
|
2023-06-19 13:50:36 +00:00
|
|
|
if (!g_Config.bShowOnScreenMessages) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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.
|
2023-06-20 13:07:01 +00:00
|
|
|
const std::vector<OnScreenDisplay::Entry> entries = g_OSD.Entries();
|
2020-10-10 17:01:40 +00:00
|
|
|
double now = time_now_d();
|
2023-06-20 13:07:01 +00:00
|
|
|
for (auto iter = entries.begin(); iter != entries.end(); ++iter) {
|
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;
|
2022-09-04 04:16:59 +00:00
|
|
|
// If we have newlines, we may be looking at ASCII debug output. But let's verify.
|
2022-08-24 08:20:33 +00:00
|
|
|
if (iter->text.find('\n') != 0) {
|
2022-09-04 04:16:59 +00:00
|
|
|
if (!UTF8StringHasNonASCII(iter->text.c_str()))
|
|
|
|
align |= FLAG_DYNAMIC_ASCII;
|
2022-08-24 08:20:33 +00:00
|
|
|
}
|
|
|
|
|
2013-10-13 10:05:50 +00:00
|
|
|
float tw, th;
|
2023-06-20 15:27:34 +00:00
|
|
|
MeasureOSDEntry(dc, *iter, align, &tw, &th);
|
|
|
|
|
|
|
|
Bounds b(0.0f, y, tw, th);
|
|
|
|
|
2014-12-31 15:50:23 +00:00
|
|
|
if (tw > bounds_.w) {
|
2023-06-20 15:27:34 +00:00
|
|
|
// Left-aligned
|
|
|
|
b.x = 2;
|
2022-08-24 08:20:33 +00:00
|
|
|
} else {
|
2023-06-20 15:27:34 +00:00
|
|
|
// Centered
|
|
|
|
b.x = (bounds_.w - b.w) * 0.5f;
|
2022-08-24 08:20:33 +00:00
|
|
|
}
|
2023-06-20 15:27:34 +00:00
|
|
|
|
|
|
|
// Scale down if height doesn't fit.
|
2022-08-24 08:20:33 +00:00
|
|
|
float scale = 1.0f;
|
|
|
|
if (th > bounds_.h - y) {
|
|
|
|
// Scale down!
|
|
|
|
scale = std::max(0.15f, (bounds_.h - y) / th);
|
|
|
|
dc.SetFontScale(scale, scale);
|
2023-06-20 15:27:34 +00:00
|
|
|
b.w *= scale;
|
|
|
|
b.h *= scale;
|
2013-10-13 10:05:50 +00:00
|
|
|
}
|
2023-06-20 16:39:30 +00:00
|
|
|
|
|
|
|
float alpha = Clamp((float)(iter->endTime - now) * 4.0f, 0.0f, 1.0f);
|
|
|
|
RenderOSDEntry(dc, *iter, b, align, alpha);
|
|
|
|
y += (b.h * scale + 4.0f) * alpha; // including alpha here gets us smooth animations.
|
2013-05-22 16:00:06 +00:00
|
|
|
}
|
2014-12-31 15:50:23 +00:00
|
|
|
|
2023-06-19 13:50:36 +00: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 15:50:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 00:38:02 +00:00
|
|
|
std::string OnScreenMessagesView::DescribeText() const {
|
|
|
|
std::stringstream ss;
|
2023-06-20 13:07:01 +00:00
|
|
|
const auto &entries = g_OSD.Entries();
|
|
|
|
for (auto iter = entries.begin(); iter != entries.end(); ++iter) {
|
|
|
|
if (iter != entries.begin()) {
|
2021-02-22 00:38:02 +00:00
|
|
|
ss << "\n";
|
|
|
|
}
|
|
|
|
ss << iter->text;
|
|
|
|
}
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2023-06-19 13:50:36 +00:00
|
|
|
void OSDOverlayScreen::CreateViews() {
|
|
|
|
root_ = new UI::AnchorLayout();
|
|
|
|
root_->Add(new OnScreenMessagesView(new UI::AnchorLayoutParams(0.0f, 0.0f, 0.0f, 0.0f)));
|
|
|
|
}
|