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