Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Dialog/PSPDialog.cpp
5663 views
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <algorithm>
19
#include "Common/Data/Text/I18n.h"
20
21
#include "Common/Serialize/Serializer.h"
22
#include "Common/Serialize/SerializeFuncs.h"
23
#include "Common/StringUtils.h"
24
#include "Common/Data/Encoding/Utf8.h"
25
#include "Core/Config.h"
26
#include "Core/System.h"
27
#include "Core/CoreTiming.h"
28
#include "Core/Dialog/PSPDialog.h"
29
#include "Core/HLE/sceCtrl.h"
30
#include "Core/HLE/scePower.h"
31
#include "Core/HLE/sceUtility.h"
32
#include "Core/MemMapHelpers.h"
33
#include "Core/Util/PPGeDraw.h"
34
35
#define FADE_TIME 1.0
36
37
constexpr float FONT_SCALE = 0.55f;
38
39
const char *UtilityDialogTypeToString(UtilityDialogType type) {
40
switch (type) {
41
case UtilityDialogType::NONE: return "NONE";
42
case UtilityDialogType::SAVEDATA: return "SAVEDATA";
43
case UtilityDialogType::MSG: return "MSG";
44
case UtilityDialogType::OSK: return "OSK";
45
case UtilityDialogType::NET: return "NET";
46
case UtilityDialogType::SCREENSHOT: return "SCREENSHOT";
47
case UtilityDialogType::GAMESHARING: return "GAMESHARING";
48
case UtilityDialogType::GAMEDATAINSTALL: return "GAMEDATAINSTALL";
49
case UtilityDialogType::NPSIGNIN: return "NPSIGNIN";
50
default: return "(unknown)";
51
}
52
}
53
54
const char *UtilityDialogStatusToString(PSPDialog::DialogStatus status) {
55
switch (status) {
56
case PSPDialog::SCE_UTILITY_STATUS_NONE: return "STATUS_NONE";
57
case PSPDialog::SCE_UTILITY_STATUS_INITIALIZE: return "STATUS_INITIALIZE";
58
case PSPDialog::SCE_UTILITY_STATUS_RUNNING: return "STATUS_RUNNING";
59
case PSPDialog::SCE_UTILITY_STATUS_FINISHED: return "STATUS_FINISHED";
60
case PSPDialog::SCE_UTILITY_STATUS_SHUTDOWN: return "STATUS_SHUTDOWN";
61
case PSPDialog::SCE_UTILITY_STATUS_SCREENSHOT_UNKNOWN: return "STATUS_SCREENSHOT_UNKNOWN";
62
default: return "(unknown)";
63
}
64
}
65
66
void PSPDialog::InitCommon() {
67
UpdateCommon();
68
69
if (GetCommonParam() && GetCommonParam()->language != GetPSPLanguage()) {
70
WARN_LOG(Log::sceUtility, "Game requested language %d, ignoring and using user language", GetCommonParam()->language);
71
}
72
}
73
74
void PSPDialog::UpdateCommon() {
75
okButtonImg = ImageID("I_CIRCLE");
76
cancelButtonImg = ImageID("I_CROSS");
77
okButtonFlag = CTRL_CIRCLE;
78
cancelButtonFlag = CTRL_CROSS;
79
if (GetCommonParam() && GetCommonParam()->buttonSwap == 1) {
80
okButtonImg = ImageID("I_CROSS");
81
cancelButtonImg = ImageID("I_CIRCLE");
82
okButtonFlag = CTRL_CROSS;
83
cancelButtonFlag = CTRL_CIRCLE;
84
}
85
}
86
87
PSPDialog::DialogStatus PSPDialog::GetStatus() {
88
if (pendingStatusTicks != 0 && CoreTiming::GetTicks() >= pendingStatusTicks) {
89
bool changeAllowed = true;
90
if (pendingStatus == SCE_UTILITY_STATUS_NONE && status == SCE_UTILITY_STATUS_SHUTDOWN) {
91
FinishVolatile();
92
} else if (pendingStatus == SCE_UTILITY_STATUS_RUNNING && status == SCE_UTILITY_STATUS_INITIALIZE) {
93
if (!volatileLocked_) {
94
volatileLocked_ = KernelVolatileMemLock(0, 0, 0) == 0;
95
changeAllowed = volatileLocked_;
96
}
97
}
98
if (changeAllowed) {
99
status = pendingStatus;
100
pendingStatusTicks = 0;
101
}
102
}
103
104
PSPDialog::DialogStatus retval = status;
105
if (UseAutoStatus()) {
106
if (status == SCE_UTILITY_STATUS_SHUTDOWN)
107
status = SCE_UTILITY_STATUS_NONE;
108
if (status == SCE_UTILITY_STATUS_INITIALIZE)
109
status = SCE_UTILITY_STATUS_RUNNING;
110
}
111
return retval;
112
}
113
114
void PSPDialog::ChangeStatus(DialogStatus newStatus, int delayUs) {
115
if (delayUs <= 0) {
116
if (newStatus == SCE_UTILITY_STATUS_NONE && status == SCE_UTILITY_STATUS_SHUTDOWN) {
117
FinishVolatile();
118
} else if (newStatus == SCE_UTILITY_STATUS_RUNNING && status == SCE_UTILITY_STATUS_INITIALIZE) {
119
if (!volatileLocked_) {
120
// TODO: Should probably make the status pending instead?
121
volatileLocked_ = KernelVolatileMemLock(0, 0, 0) == 0;
122
}
123
}
124
status = newStatus;
125
pendingStatus = newStatus;
126
pendingStatusTicks = 0;
127
} else {
128
pendingStatus = newStatus;
129
pendingStatusTicks = CoreTiming::GetTicks() + usToCycles(delayUs);
130
}
131
}
132
133
void PSPDialog::FinishVolatile() {
134
if (!volatileLocked_)
135
return;
136
137
if (KernelVolatileMemUnlock(0) == 0) {
138
volatileLocked_ = false;
139
// Simulate modifications to volatile memory.
140
Memory::Memset(PSP_GetVolatileMemoryStart(), 0, PSP_GetVolatileMemoryEnd() - PSP_GetVolatileMemoryStart());
141
}
142
}
143
144
int PSPDialog::FinishInit() {
145
if (ReadStatus() != SCE_UTILITY_STATUS_INITIALIZE)
146
return -1;
147
// The thread already locked.
148
volatileLocked_ = true;
149
ChangeStatus(SCE_UTILITY_STATUS_RUNNING, 0);
150
return 0;
151
}
152
153
int PSPDialog::FinishShutdown() {
154
if (ReadStatus() != SCE_UTILITY_STATUS_SHUTDOWN)
155
return -1;
156
ChangeStatus(SCE_UTILITY_STATUS_NONE, 0);
157
return 0;
158
}
159
160
void PSPDialog::ChangeStatusInit(int delayUs) {
161
ChangeStatus(SCE_UTILITY_STATUS_INITIALIZE, 0);
162
163
auto params = GetCommonParam();
164
if (params)
165
UtilityDialogInitialize(DialogType(), delayUs, params->accessThread);
166
else
167
ChangeStatus(SCE_UTILITY_STATUS_RUNNING, delayUs);
168
}
169
170
void PSPDialog::ChangeStatusShutdown(int delayUs) {
171
// If we're doing shutdown right away and skipped start, we don't run the dialog thread.
172
bool skipDialogShutdown = status == SCE_UTILITY_STATUS_NONE && pendingStatus == SCE_UTILITY_STATUS_NONE;
173
ChangeStatus(SCE_UTILITY_STATUS_SHUTDOWN, 0);
174
175
auto params = GetCommonParam();
176
if (params && !skipDialogShutdown)
177
UtilityDialogShutdown(DialogType(), delayUs, params->accessThread);
178
else
179
ChangeStatus(SCE_UTILITY_STATUS_NONE, delayUs);
180
}
181
182
void PSPDialog::StartDraw()
183
{
184
PPGeBegin();
185
PPGeDrawRect(0, 0, 480, 272, CalcFadedColor(0x20000000));
186
}
187
188
void PSPDialog::EndDraw()
189
{
190
PPGeEnd();
191
}
192
193
int PSPDialog::Shutdown(bool force)
194
{
195
if (force) {
196
ChangeStatus(SCE_UTILITY_STATUS_NONE, 0);
197
} else {
198
ChangeStatus(SCE_UTILITY_STATUS_SHUTDOWN, 0);
199
}
200
return 0;
201
}
202
203
void PSPDialog::StartFade(bool fadeIn_)
204
{
205
isFading = true;
206
fadeTimer = 0;
207
fadeIn = fadeIn_;
208
}
209
210
void PSPDialog::UpdateFade(int animSpeed) {
211
if (isFading) {
212
fadeTimer += 1.0f/30.0f * animSpeed; // Probably need a more real value of delta time
213
if (fadeTimer < FADE_TIME) {
214
if (fadeIn)
215
fadeValue = (u32) (fadeTimer / FADE_TIME * 255);
216
else
217
fadeValue = 255 - (u32) (fadeTimer / FADE_TIME * 255);
218
} else {
219
fadeValue = (fadeIn ? 255 : 0);
220
isFading = false;
221
if (!fadeIn) {
222
FinishFadeOut();
223
}
224
}
225
}
226
}
227
228
void PSPDialog::FinishFadeOut() {
229
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0);
230
}
231
232
u32 PSPDialog::CalcFadedColor(u32 inColor) const {
233
u32 alpha = inColor >> 24;
234
alpha = alpha * fadeValue / 255;
235
return (inColor & 0x00FFFFFF) | (alpha << 24);
236
}
237
238
void PSPDialog::DoState(PointerWrap &p) {
239
auto s = p.Section("PSPDialog", 1, 3);
240
if (!s)
241
return;
242
243
Do(p, status);
244
Do(p, lastButtons);
245
Do(p, buttons);
246
Do(p, fadeTimer);
247
Do(p, isFading);
248
Do(p, fadeIn);
249
Do(p, fadeValue);
250
251
// I don't think we should save these two... Let's just ignore them for now for compat.
252
int okButtonImg = 0;
253
Do(p, okButtonImg);
254
int cancelButtonImg = 0;
255
Do(p, cancelButtonImg);
256
257
Do(p, okButtonFlag);
258
Do(p, cancelButtonFlag);
259
260
if (s >= 2) {
261
Do(p, pendingStatus);
262
Do(p, pendingStatusTicks);
263
} else {
264
pendingStatusTicks = 0;
265
}
266
267
if (s >= 3) {
268
Do(p, volatileLocked_);
269
} else {
270
volatileLocked_ = false;
271
}
272
}
273
274
void PSPDialog::UpdateButtons()
275
{
276
lastButtons = __CtrlPeekButtons();
277
buttons = __CtrlReadLatch();
278
}
279
280
bool PSPDialog::IsButtonPressed(int checkButton)
281
{
282
return !isFading && (buttons & checkButton);
283
}
284
285
bool PSPDialog::IsButtonHeld(int checkButton, int &framesHeld, int framesHeldThreshold, int framesHeldRepeatRate)
286
{
287
bool btnWasHeldLastFrame = (lastButtons & checkButton) && (__CtrlPeekButtons() & checkButton);
288
if (!isFading && btnWasHeldLastFrame) {
289
framesHeld++;
290
}
291
else {
292
framesHeld = 0;
293
return false;
294
}
295
296
// It's considered held for dialog purposes after 30 frames (~0.5 seconds),
297
// and set to repeat every 10 frames, by default.
298
if (framesHeld >= framesHeldThreshold && ((framesHeld % framesHeldRepeatRate) == 0))
299
return true;
300
301
return false;
302
}
303
304
PPGeStyle PSPDialog::FadedStyle(PPGeAlign align, float scale) {
305
PPGeStyle textStyle;
306
textStyle.align = align;
307
textStyle.scale = scale;
308
textStyle.color = CalcFadedColor(textStyle.color);
309
textStyle.hasShadow = true;
310
textStyle.shadowColor = CalcFadedColor(textStyle.shadowColor);
311
return textStyle;
312
}
313
314
PPGeImageStyle PSPDialog::FadedImageStyle() {
315
PPGeImageStyle style;
316
style.color = CalcFadedColor(style.color);
317
return style;
318
}
319
320
void PSPDialog::DisplayButtons(int flags, std::string_view caption) {
321
bool useCaption = false;
322
char safeCaption[65] = {0};
323
if (!caption.empty()) {
324
useCaption = true;
325
truncate_cpy(safeCaption, sizeof(safeCaption), caption);
326
}
327
328
PPGeStyle textStyle = FadedStyle(PPGeAlign::BOX_LEFT, FONT_SCALE);
329
330
auto di = GetI18NCategory(I18NCat::DIALOG);
331
float x1 = 183.5f, x2 = 261.5f;
332
333
const pspUtilityDialogCommon *commonParams = GetCommonParam();
334
if (!commonParams) {
335
return;
336
}
337
338
if (commonParams->buttonSwap == 1) {
339
x1 = 261.5f;
340
x2 = 183.5f;
341
}
342
if (flags & DS_BUTTON_OK) {
343
std::string_view text = useCaption ? safeCaption : di->T("Enter");
344
PPGeDrawImage(okButtonImg, x2, 256, 11.5f, 11.5f, textStyle);
345
PPGeDrawText(text, x2 + 14.5f, 252, textStyle);
346
}
347
if (flags & DS_BUTTON_CANCEL) {
348
std::string_view text = useCaption ? safeCaption : di->T("Back");
349
PPGeDrawImage(cancelButtonImg, x1, 256, 11.5f, 11.5f, textStyle);
350
PPGeDrawText(text, x1 + 14.5f, 252, textStyle);
351
}
352
}
353
354
int PSPDialog::GetConfirmButton() {
355
if (PSP_CoreParameter().compat.flags().ForceCircleButtonConfirm) {
356
return CTRL_CIRCLE;
357
}
358
return g_Config.iButtonPreference == PSP_SYSTEMPARAM_BUTTON_CROSS ? CTRL_CROSS : CTRL_CIRCLE;
359
}
360
361
int PSPDialog::GetCancelButton() {
362
if (PSP_CoreParameter().compat.flags().ForceCircleButtonConfirm) {
363
return CTRL_CROSS;
364
}
365
return g_Config.iButtonPreference == PSP_SYSTEMPARAM_BUTTON_CROSS ? CTRL_CIRCLE : CTRL_CROSS;
366
}
367
368
void PSPDialog::DisplayMessage2(std::string_view text1, std::string_view text2a, std::string_view text2b, std::string_view text3a, std::string_view text3b, bool hasYesNo, bool hasOK) {
369
auto di = GetI18NCategory(I18NCat::DIALOG);
370
371
PPGeStyle buttonStyle = FadedStyle(PPGeAlign::BOX_CENTER, FONT_SCALE);
372
PPGeStyle messageStyle = FadedStyle(PPGeAlign::BOX_HCENTER, FONT_SCALE);
373
PPGeStyle messageStyleRight = FadedStyle(PPGeAlign::BOX_RIGHT, FONT_SCALE);
374
PPGeStyle messageStyleLeft = FadedStyle(PPGeAlign::BOX_LEFT, FONT_SCALE);
375
376
std::string text2 = std::string(text2a) + " " + std::string(text2b);
377
std::string text3 = std::string(text3a) + " " + std::string(text3b);
378
379
// Without the scrollbar, we have 350 total pixels.
380
float WRAP_WIDTH = 300.0f;
381
if (UTF8StringNonASCIICount(text1) >= (int)text1.size() / 4) {
382
WRAP_WIDTH = 336.0f;
383
if (text1.size() > 12) {
384
messageStyle.scale = 0.6f;
385
}
386
}
387
388
float totalHeight1 = 0.0f;
389
PPGeMeasureText(nullptr, &totalHeight1, text1, FONT_SCALE, PPGE_LINE_WRAP_WORD, WRAP_WIDTH);
390
float totalHeight2 = 0.0f;
391
if (text2 != " ")
392
PPGeMeasureText(nullptr, &totalHeight2, text2, FONT_SCALE, PPGE_LINE_USE_ELLIPSIS, WRAP_WIDTH);
393
float totalHeight3 = 0.0f;
394
if (text3 != " ")
395
PPGeMeasureText(nullptr, &totalHeight3, text3, FONT_SCALE, PPGE_LINE_USE_ELLIPSIS, WRAP_WIDTH);
396
float marginTop = 0.0f;
397
if (text2 != " " || text3 != " ")
398
marginTop = 11.0f;
399
float totalHeight = totalHeight1 + totalHeight2 + totalHeight3 + marginTop;
400
// The PSP normally only shows about 8 lines at a time.
401
// For improved UX, we intentionally show part of the next line.
402
float visibleHeight = std::min(totalHeight, 175.0f);
403
float h2 = visibleHeight / 2.0f;
404
405
float centerY = 135.0f;
406
float sy = centerY - h2 - 15.0f;
407
float ey = centerY + h2 + 20.0f;
408
float buttonY = centerY + h2 + 5.0f;
409
410
auto drawSelectionBoxAndAdjust = [&](float x) {
411
// Box has a fixed size.
412
float w = 15.0f;
413
float h = 8.0f;
414
PPGeDrawRect(x - w, buttonY - h, x + w, buttonY + h, CalcFadedColor(0x6DCFCFCF));
415
416
centerY -= h + 5.0f;
417
sy -= h + 5.0f;
418
ey = buttonY + h * 2.0f + 5.0f;
419
};
420
421
if (hasYesNo) {
422
if (yesnoChoice == 1) {
423
drawSelectionBoxAndAdjust(204.0f);
424
} else {
425
drawSelectionBoxAndAdjust(273.0f);
426
}
427
428
PPGeDrawText(di->T("Yes"), 203.0f, buttonY - 1.0f, buttonStyle);
429
PPGeDrawText(di->T("No"), 272.0f, buttonY - 1.0f, buttonStyle);
430
if (IsButtonPressed(CTRL_LEFT) && yesnoChoice == 0) {
431
yesnoChoice = 1;
432
} else if (IsButtonPressed(CTRL_RIGHT) && yesnoChoice == 1) {
433
yesnoChoice = 0;
434
}
435
buttonY += 8.0f + 5.0f;
436
}
437
438
if (hasOK) {
439
drawSelectionBoxAndAdjust(240.0f);
440
441
PPGeDrawText(di->T("OK"), 239.0f, buttonY - 1.0f, buttonStyle);
442
buttonY += 8.0f + 5.0f;
443
}
444
445
PPGeScissor(0, (int)(centerY - h2 - 2), 480, (int)(centerY + h2 + 2));
446
PPGeDrawTextWrapped(text1, 240.0f, centerY - h2 - scrollPos_, WRAP_WIDTH, 0, messageStyle);
447
if (!text2a.empty()) {
448
if (!text2b.empty())
449
PPGeDrawTextWrapped(text2a, 240.0f - 5.0f, centerY - h2 - scrollPos_ + totalHeight1 + marginTop, WRAP_WIDTH, 0, messageStyleRight);
450
else
451
PPGeDrawTextWrapped(text2a, 240.0f, centerY - h2 - scrollPos_ + totalHeight1 + marginTop, WRAP_WIDTH, 0, messageStyle);
452
}
453
if (!text2b.empty())
454
PPGeDrawTextWrapped(text2b, 240.0f + 5.0f, centerY - h2 - scrollPos_ + totalHeight1 + marginTop, WRAP_WIDTH, 0, messageStyleLeft);
455
if (!text3a.empty()) {
456
if (!text3b.empty())
457
PPGeDrawTextWrapped(text3a, 240.0f - 5.0f, centerY - h2 - scrollPos_ + totalHeight1 + totalHeight2 + marginTop, WRAP_WIDTH, 0, messageStyleRight);
458
else
459
PPGeDrawTextWrapped(text3a, 240.0f, centerY - h2 - scrollPos_ + totalHeight1 + totalHeight2 + marginTop, WRAP_WIDTH, 0, messageStyle);
460
}
461
if (!text3b.empty())
462
PPGeDrawTextWrapped(text3b, 240.0f + 5.0f, centerY - h2 - scrollPos_ + totalHeight1 + totalHeight2 + marginTop, WRAP_WIDTH, 0, messageStyleLeft);
463
PPGeScissorReset();
464
465
// Do we need a scrollbar?
466
if (visibleHeight < totalHeight) {
467
float scrollSpeed = 5.0f;
468
float scrollMax = totalHeight - visibleHeight;
469
470
float bobHeight = (visibleHeight / totalHeight) * visibleHeight;
471
float bobOffset = (scrollPos_ / scrollMax) * (visibleHeight - bobHeight);
472
float bobY1 = centerY - h2 + bobOffset;
473
PPGeDrawRect(415.0f, bobY1, 420.0f, bobY1 + bobHeight, CalcFadedColor(0xFFCCCCCC));
474
475
auto buttonDown = [this](int btn, int& held) {
476
if (IsButtonPressed(btn)) {
477
held = 0;
478
return true;
479
}
480
return IsButtonHeld(btn, held, 1, 1);
481
};
482
if (buttonDown(CTRL_DOWN, framesDownHeld_) && scrollPos_ < scrollMax) {
483
scrollPos_ = std::min(scrollMax, scrollPos_ + scrollSpeed);
484
}
485
if (buttonDown(CTRL_UP, framesUpHeld_) && scrollPos_ > 0.0f) {
486
scrollPos_ = std::max(0.0f, scrollPos_ - scrollSpeed);
487
}
488
}
489
490
PPGeDrawRect(60.0f, sy, 420.0f, sy + 1.0f, CalcFadedColor(0xFFFFFFFF));
491
PPGeDrawRect(60.0f, ey, 420.0f, ey + 1.0f, CalcFadedColor(0xFFFFFFFF));
492
}
493
494