Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/OnScreenDisplay.cpp
5659 views
1
#include <algorithm>
2
#include <sstream>
3
4
#include "UI/OnScreenDisplay.h"
5
6
#include "Common/Data/Color/RGBAUtil.h"
7
#include "Common/Data/Encoding/Utf8.h"
8
#include "Common/Render/TextureAtlas.h"
9
#include "Common/Render/DrawBuffer.h"
10
#include "Common/Math/math_util.h"
11
#include "Common/UI/IconCache.h"
12
#include "Common/StringUtils.h"
13
#include "UI/RetroAchievementScreens.h"
14
#include "UI/DebugOverlay.h"
15
16
#include "Common/UI/Context.h"
17
#include "Common/UI/Notice.h"
18
#include "Common/System/OSD.h"
19
20
#include "Common/TimeUtil.h"
21
#include "Core/Config.h"
22
23
static inline const char *DeNull(const char *ptr) {
24
return ptr ? ptr : "";
25
}
26
27
extern bool g_TakeScreenshot;
28
29
static const float g_atlasIconSize = 36.0f;
30
31
// Align only matters here for the ASCII-only flag.
32
static void MeasureOSDEntry(const UIContext &dc, const OnScreenDisplay::Entry &entry, int align, float maxWidth, float *width, float *height, float *height1) {
33
if (entry.type == OSDType::ACHIEVEMENT_UNLOCKED) {
34
const rc_client_achievement_t *achievement = rc_client_get_achievement_info(Achievements::GetClient(), entry.numericID);
35
MeasureAchievement(dc, achievement, AchievementRenderStyle::UNLOCKED, width, height);
36
*width = std::min(maxWidth, 550.0f);
37
*height1 = *height;
38
} else {
39
MeasureNotice(dc, GetNoticeLevel(entry.type), entry.text, entry.text2, entry.iconName, align, maxWidth, width, height, height1);
40
}
41
}
42
43
static void RenderOSDEntry(UIContext &dc, const OnScreenDisplay::Entry &entry, Bounds bounds, float height1, int align, float alpha, float now) {
44
if (entry.type == OSDType::ACHIEVEMENT_UNLOCKED) {
45
const rc_client_achievement_t * achievement = rc_client_get_achievement_info(Achievements::GetClient(), entry.numericID);
46
if (achievement) {
47
RenderAchievement(dc, achievement, AchievementRenderStyle::UNLOCKED, bounds, alpha, entry.startTime, time_now_d(), false);
48
}
49
return;
50
} else {
51
RenderNotice(dc, bounds, height1, GetNoticeLevel(entry.type), entry.text, entry.text2, entry.iconName, align, alpha, entry.flags, now - entry.startTime);
52
}
53
}
54
55
static void RenderOSDProgressBar(UIContext &dc, const OnScreenDisplay::Entry &entry, Bounds bounds, int align, float alpha) {
56
dc.DrawRectDropShadow(bounds, 12.0f, 0.7f * alpha);
57
58
uint32_t backgroundColor = colorAlpha(0x806050, alpha);
59
uint32_t progressBackgroundColor = colorAlpha(0xa08070, alpha);
60
61
if (entry.maxValue > entry.minValue) {
62
// Normal progress bar
63
64
UI::Drawable background = UI::Drawable(backgroundColor);
65
UI::Drawable progressBackground = UI::Drawable(progressBackgroundColor);
66
67
float ratio = (float)(entry.progress - entry.minValue) / (float)entry.maxValue;
68
69
Bounds boundLeft = bounds;
70
Bounds boundRight = bounds;
71
72
boundLeft.w *= ratio;
73
boundRight.x += ratio * boundRight.w;
74
boundRight.w *= (1.0f - ratio);
75
76
dc.FillRect(progressBackground, boundLeft);
77
dc.FillRect(background, boundRight);
78
} else {
79
// Indeterminate spinner
80
float alpha = cos(time_now_d() * 5.0) * 0.5f + 0.5f;
81
uint32_t pulse = colorBlend(backgroundColor, progressBackgroundColor, alpha);
82
UI::Drawable background = UI::Drawable(pulse);
83
dc.FillRect(background, bounds);
84
}
85
86
dc.SetFontStyle(dc.GetTheme().uiFont);
87
dc.SetFontScale(1.0f, 1.0f);
88
89
dc.DrawTextShadowRect(entry.text, bounds, colorAlpha(0xFFFFFFFF, alpha), (align & FLAG_DYNAMIC_ASCII) | ALIGN_CENTER);
90
}
91
92
static void MeasureLeaderboardTracker(UIContext &dc, const std::string &text, float *width, float *height) {
93
dc.MeasureText(dc.GetFontStyle(), 1.0f, 1.0f, text, width, height);
94
*width += 16.0f;
95
*height += 10.0f;
96
}
97
98
static void RenderLeaderboardTracker(UIContext &dc, const Bounds &bounds, const std::string &text, float alpha) {
99
// TODO: Awful color.
100
uint32_t backgroundColor = colorAlpha(0x806050, alpha);
101
UI::Drawable background = UI::Drawable(backgroundColor);
102
dc.DrawRectDropShadow(bounds, 12.0f, 0.7f * alpha);
103
dc.FillRect(background, bounds);
104
dc.SetFontStyle(dc.GetTheme().uiFont);
105
dc.SetFontScale(1.0f, 1.0f);
106
dc.DrawTextShadowRect(text, bounds.Inset(5.0f, 5.0f), colorAlpha(0xFFFFFFFF, alpha), ALIGN_VCENTER | ALIGN_HCENTER);
107
}
108
109
void OnScreenMessagesView::Draw(UIContext &dc) {
110
if (g_TakeScreenshot) {
111
return;
112
}
113
114
dc.Flush();
115
116
double now = time_now_d();
117
118
const float padding = 5.0f;
119
120
const float fadeinCoef = 1.0f / OnScreenDisplay::FadeinTime();
121
const float fadeoutCoef = 1.0f / OnScreenDisplay::FadeoutTime();
122
123
float ingameAlpha = g_OSD.IngameAlpha();
124
125
struct LayoutEdge {
126
float height;
127
float maxWidth;
128
};
129
130
struct MeasuredEntry {
131
float w;
132
float h;
133
float h1;
134
float alpha;
135
int align;
136
int align2;
137
AchievementRenderStyle style;
138
};
139
140
// Grab all the entries. Makes a copy so we can release the lock ASAP.
141
const std::vector<OnScreenDisplay::Entry> entries = g_OSD.Entries();
142
143
std::vector<MeasuredEntry> measuredEntries;
144
measuredEntries.resize(entries.size());
145
146
float typeAlpha[(size_t)OSDType::VALUE_COUNT]{};
147
ScreenEdgePosition typeEdges[(size_t)OSDType::VALUE_COUNT]{};
148
// Default to the configured position.
149
for (int i = 0; i < (size_t)OSDType::VALUE_COUNT; i++) {
150
typeAlpha[i] = ingameAlpha;
151
typeEdges[i] = (ScreenEdgePosition)g_Config.iNotificationPos;
152
}
153
154
// These types can always show, independent of whether we're in the menu or not.
155
static const OSDType fullAlphaTypes[] = {OSDType::MESSAGE_ERROR, OSDType::MESSAGE_INFO, OSDType::MESSAGE_WARNING, OSDType::MESSAGE_SUCCESS, OSDType::MESSAGE_FILE_LINK, OSDType::PROGRESS_BAR, OSDType::MESSAGE_CENTERED_ERROR, OSDType::MESSAGE_CENTERED_WARNING};
156
for (auto type : fullAlphaTypes) {
157
typeAlpha[(int)type] = 1.0f;
158
}
159
160
typeEdges[(size_t)OSDType::ACHIEVEMENT_CHALLENGE_INDICATOR] = (ScreenEdgePosition)g_Config.iAchievementsChallengePos;
161
typeEdges[(size_t)OSDType::ACHIEVEMENT_PROGRESS] = (ScreenEdgePosition)g_Config.iAchievementsProgressPos;
162
typeEdges[(size_t)OSDType::LEADERBOARD_TRACKER] = (ScreenEdgePosition)g_Config.iAchievementsLeaderboardTrackerPos;
163
typeEdges[(size_t)OSDType::LEADERBOARD_STARTED_FAILED] = (ScreenEdgePosition)g_Config.iAchievementsLeaderboardStartedOrFailedPos;
164
typeEdges[(size_t)OSDType::LEADERBOARD_SUBMITTED] = (ScreenEdgePosition)g_Config.iAchievementsLeaderboardSubmittedPos;
165
typeEdges[(size_t)OSDType::ACHIEVEMENT_UNLOCKED] = (ScreenEdgePosition)g_Config.iAchievementsUnlockedPos;
166
typeEdges[(size_t)OSDType::MESSAGE_CENTERED_WARNING] = ScreenEdgePosition::CENTER;
167
typeEdges[(size_t)OSDType::MESSAGE_CENTERED_ERROR] = ScreenEdgePosition::CENTER;
168
typeEdges[(size_t)OSDType::STATUS_ICON] = ScreenEdgePosition::TOP_LEFT;
169
typeEdges[(size_t)OSDType::PROGRESS_BAR] = ScreenEdgePosition::TOP_CENTER; // These only function at the top currently, needs fixing.
170
171
dc.SetFontStyle(dc.GetTheme().uiFont);
172
dc.SetFontScale(1.0f, 1.0f);
173
174
// Indexed by the enum ScreenEdgePosition.
175
LayoutEdge edges[(size_t)ScreenEdgePosition::VALUE_COUNT]{};
176
177
// First pass: Measure all the sides.
178
for (size_t i = 0; i < entries.size(); i++) {
179
const auto &entry = entries[i];
180
auto &measuredEntry = measuredEntries[i];
181
182
ScreenEdgePosition pos = typeEdges[(size_t)entry.type];
183
if (pos == ScreenEdgePosition::VALUE_COUNT || pos == (ScreenEdgePosition)-1) {
184
// NONE.
185
continue;
186
}
187
188
measuredEntry.align = 0;
189
measuredEntry.align2 = 0;
190
// If we have newlines, we may be looking at ASCII debug output. But let's verify.
191
if (entry.text.find('\n') != std::string::npos) {
192
if (!UTF8StringHasNonASCII(entry.text))
193
measuredEntry.align |= FLAG_DYNAMIC_ASCII;
194
}
195
if (entry.text2.find('\n') != std::string::npos) {
196
if (!UTF8StringHasNonASCII(entry.text2))
197
measuredEntry.align2 |= FLAG_DYNAMIC_ASCII;
198
}
199
200
switch (entry.type) {
201
case OSDType::ACHIEVEMENT_PROGRESS:
202
{
203
const rc_client_achievement_t *achievement = rc_client_get_achievement_info(Achievements::GetClient(), entry.numericID);
204
if (!achievement)
205
continue;
206
measuredEntry.style = AchievementRenderStyle::PROGRESS_INDICATOR;
207
MeasureAchievement(dc, achievement, measuredEntry.style, &measuredEntry.w, &measuredEntry.h);
208
break;
209
}
210
case OSDType::ACHIEVEMENT_CHALLENGE_INDICATOR:
211
{
212
const rc_client_achievement_t *achievement = rc_client_get_achievement_info(Achievements::GetClient(), entry.numericID);
213
if (!achievement)
214
continue;
215
measuredEntry.style = AchievementRenderStyle::CHALLENGE_INDICATOR;
216
MeasureAchievement(dc, achievement, measuredEntry.style, &measuredEntry.w, &measuredEntry.h);
217
break;
218
}
219
case OSDType::LEADERBOARD_TRACKER:
220
{
221
MeasureLeaderboardTracker(dc, entry.text, &measuredEntry.w, &measuredEntry.h);
222
break;
223
}
224
case OSDType::ACHIEVEMENT_UNLOCKED:
225
{
226
const rc_client_achievement_t *achievement = rc_client_get_achievement_info(Achievements::GetClient(), entry.numericID);
227
if (!achievement)
228
continue;
229
measuredEntry.style = AchievementRenderStyle::UNLOCKED;
230
MeasureAchievement(dc, achievement, AchievementRenderStyle::UNLOCKED, &measuredEntry.w, &measuredEntry.h);
231
measuredEntry.h1 = measuredEntry.h;
232
measuredEntry.w = std::min(bounds_.w, 550.0f);
233
break;
234
}
235
case OSDType::PROGRESS_BAR:
236
{
237
measuredEntry.h = 36;
238
measuredEntry.w = std::min(450.0f, bounds_.w - 50.0f);
239
break;
240
}
241
default:
242
MeasureOSDEntry(dc, entry, measuredEntry.align, bounds_.w, &measuredEntry.w, &measuredEntry.h, &measuredEntry.h1);
243
break;
244
}
245
246
float enterAlpha = saturatef((float)(now - entry.startTime) * fadeoutCoef);
247
float leaveAlpha = saturatef((float)(entry.endTime - now) * fadeoutCoef);
248
float alpha = std::min(enterAlpha, leaveAlpha);
249
measuredEntry.alpha = alpha;
250
251
edges[(size_t)pos].height += (measuredEntry.h + 4.0f) * alpha;
252
edges[(size_t)pos].maxWidth = std::max(edges[(size_t)pos].maxWidth, measuredEntry.w);
253
}
254
255
std::vector<ClickZone> clickZones;
256
257
// Now, perform layout for all 8 edges.
258
for (size_t i = 0; i < (size_t)ScreenEdgePosition::VALUE_COUNT; i++) {
259
if (edges[i].height == 0.0f) {
260
// Nothing on this side, ignore it entirely.
261
continue;
262
}
263
264
// First, compute the start position.
265
float y = padding + bounds_.y;
266
int horizAdj = 0;
267
int vertAdj = 0;
268
switch ((ScreenEdgePosition)i) {
269
case ScreenEdgePosition::TOP_LEFT: horizAdj = -1; vertAdj = -1; break;
270
case ScreenEdgePosition::CENTER_LEFT: horizAdj = -1; break;
271
case ScreenEdgePosition::BOTTOM_LEFT: horizAdj = -1; vertAdj = 1; break;
272
case ScreenEdgePosition::TOP_RIGHT: horizAdj = 1; vertAdj = -1; break;
273
case ScreenEdgePosition::CENTER_RIGHT: horizAdj = 1; break;
274
case ScreenEdgePosition::BOTTOM_RIGHT: horizAdj = 1; vertAdj = 1; break;
275
case ScreenEdgePosition::TOP_CENTER: vertAdj = -1; break;
276
case ScreenEdgePosition::BOTTOM_CENTER: vertAdj = 1; break;
277
case ScreenEdgePosition::CENTER: break;
278
default: break;
279
}
280
281
if (vertAdj == 0) {
282
// Center vertically
283
y = bounds_.centerY() - edges[i].height * 0.5f;
284
} else if (vertAdj == 1) {
285
y = bounds_.y2() - edges[i].height;
286
}
287
288
// Then, loop through the entries and those belonging here, get rendered here.
289
for (size_t j = 0; j < (size_t)entries.size(); j++) {
290
auto &entry = entries[j];
291
if (typeEdges[(size_t)entry.type] != (ScreenEdgePosition)i) { // yes, i
292
continue;
293
}
294
auto &measuredEntry = measuredEntries[j];
295
float alpha = measuredEntry.alpha * typeAlpha[(size_t)entry.type];
296
297
Bounds b(bounds_.x + padding, y, measuredEntry.w, measuredEntry.h);
298
299
if (horizAdj == 0) {
300
// Centered
301
b.x = bounds_.centerX() - b.w * 0.5f;
302
} else if (horizAdj == 1) {
303
// Right-aligned
304
b.x = bounds_.x2() - (measuredEntry.w + padding);
305
}
306
307
switch (entry.type) {
308
case OSDType::ACHIEVEMENT_PROGRESS:
309
case OSDType::ACHIEVEMENT_CHALLENGE_INDICATOR:
310
{
311
const rc_client_achievement_t *achievement = rc_client_get_achievement_info(Achievements::GetClient(), entry.numericID);
312
if (achievement) {
313
RenderAchievement(dc, achievement, measuredEntry.style, b, alpha, entry.startTime, now, false);
314
}
315
break;
316
}
317
case OSDType::LEADERBOARD_TRACKER:
318
RenderLeaderboardTracker(dc, b, entry.text, alpha);
319
break;
320
case OSDType::PROGRESS_BAR:
321
RenderOSDProgressBar(dc, entry, b, 0, alpha);
322
break;
323
default:
324
{
325
// Scale down if height doesn't fit.
326
float scale = 1.0f;
327
if (measuredEntry.h > bounds_.y2() - y) {
328
// Scale down!
329
scale = std::max(0.15f, (bounds_.y2() - y) / measuredEntry.h);
330
dc.SetFontScale(scale, scale);
331
b.w *= scale;
332
b.h *= scale;
333
}
334
335
float alpha = Clamp((float)(entry.endTime - now) * 4.0f, 0.0f, 1.0f);
336
RenderOSDEntry(dc, entry, b, measuredEntry.h1, measuredEntry.align, alpha, now);
337
338
switch (entry.type) {
339
case OSDType::MESSAGE_INFO:
340
case OSDType::MESSAGE_SUCCESS:
341
case OSDType::MESSAGE_WARNING:
342
case OSDType::MESSAGE_ERROR:
343
case OSDType::MESSAGE_CENTERED_ERROR:
344
case OSDType::MESSAGE_CENTERED_WARNING:
345
case OSDType::MESSAGE_ERROR_DUMP:
346
case OSDType::MESSAGE_FILE_LINK:
347
case OSDType::ACHIEVEMENT_UNLOCKED:
348
// Save the location of the popup, for easy dismissal.
349
clickZones.push_back(ClickZone{ (int)j, b });
350
break;
351
default:
352
break;
353
}
354
break;
355
}
356
}
357
358
y += (measuredEntry.h + 4.0f) * measuredEntry.alpha;
359
}
360
}
361
362
std::lock_guard<std::mutex> lock(clickMutex_);
363
clickZones_ = clickZones;
364
}
365
366
std::string OnScreenMessagesView::DescribeText() const {
367
std::stringstream ss;
368
const auto &entries = g_OSD.Entries();
369
for (auto iter = entries.begin(); iter != entries.end(); ++iter) {
370
if (iter != entries.begin()) {
371
ss << "\n";
372
}
373
ss << iter->text;
374
}
375
return ss.str();
376
}
377
378
// Asynchronous!
379
bool OnScreenMessagesView::Dismiss(float x, float y) {
380
bool dismissed = false;
381
std::lock_guard<std::mutex> lock(clickMutex_);
382
double now = time_now_d();
383
for (auto &zone : clickZones_) {
384
if (zone.bounds.Contains(x, y)) {
385
g_OSD.ClickEntry(zone.index, now);
386
dismissed = true;
387
}
388
}
389
return dismissed;
390
}
391
392
bool OSDOverlayScreen::UnsyncTouch(const TouchInput &touch) {
393
// Don't really need to forward.
394
// UIScreen::UnsyncTouch(touch);
395
if ((touch.flags & TouchInputFlags::DOWN) && osmView_) {
396
return osmView_->Dismiss(touch.x, touch.y);
397
} else {
398
return false;
399
}
400
}
401
402
void OSDOverlayScreen::CreateViews() {
403
root_ = new UI::AnchorLayout();
404
root_->SetTag("OSDOverlayScreen");
405
osmView_ = root_->Add(new OnScreenMessagesView(new UI::AnchorLayoutParams(0.0f, 0.0f, 0.0f, 0.0f)));
406
}
407
408
void OSDOverlayScreen::DrawForeground(UIContext &ui) {
409
DebugOverlay debugOverlay = (DebugOverlay)g_Config.iDebugOverlay;
410
411
// Special case control for now, since it uses the control mapper that's owned by EmuScreen.
412
if (debugOverlay != DebugOverlay::OFF && debugOverlay != DebugOverlay::CONTROL) {
413
UIContext *uiContext = screenManager()->getUIContext();
414
DrawDebugOverlay(uiContext, uiContext->GetLayoutBounds(), debugOverlay);
415
}
416
}
417
418
void OSDOverlayScreen::update() {
419
// Partial version of UIScreen::update() but doesn't do event processing to avoid duplicate event processing.
420
DeviceOrientation orientation = GetDeviceOrientation();
421
if (orientation != lastOrientation_) {
422
RecreateViews();
423
lastOrientation_ = orientation;
424
}
425
426
DoRecreateViews();
427
}
428
429
void NoticeView::GetContentDimensionsBySpec(const UIContext &dc, UI::MeasureSpec horiz, UI::MeasureSpec vert, float &w, float &h) const {
430
float layoutWidth = layoutParams_->width;
431
if (layoutWidth < 0) {
432
// If there's no size, let's grow as big as we want.
433
layoutWidth = horiz.size;
434
}
435
ApplyBoundBySpec(layoutWidth, horiz);
436
const int align = wrapText_ ? FLAG_WRAP_TEXT : 0;
437
MeasureNotice(dc, level_, text_, detailsText_, iconName_, align, layoutWidth, &w, &h, &height1_);
438
// Layout hack! Some weird problems with the layout that I can't figure out right now..
439
if (squishy_) {
440
w = 50.0;
441
}
442
}
443
444
void NoticeView::Draw(UIContext &dc) {
445
dc.PushScissor(bounds_);
446
const int align = wrapText_ ? FLAG_WRAP_TEXT : 0;
447
RenderNotice(dc, bounds_, height1_, level_, text_, detailsText_, iconName_, align, 1.0f, OSDMessageFlags::None, 0.0f);
448
dc.PopScissor();
449
}
450
451