Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Dialog/PSPMsgDialog.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
20
#include "Common/Serialize/Serializer.h"
21
#include "Common/Serialize/SerializeFuncs.h"
22
#include "Common/StringUtils.h"
23
#include "Core/Dialog/PSPMsgDialog.h"
24
#include "Core/Dialog/PSPSaveDialog.h"
25
#include "Core/Util/PPGeDraw.h"
26
#include "Core/HLE/ErrorCodes.h"
27
#include "Core/HLE/sceCtrl.h"
28
#include "Core/MemMapHelpers.h"
29
#include "Core/Reporting.h"
30
#include "Common/Data/Text/I18n.h"
31
#include "Common/Data/Encoding/Utf8.h"
32
33
static const float FONT_SCALE = 0.65f;
34
35
// These are rough, it seems to take a long time to init, and probably depends on threads.
36
// TODO: This takes like 700ms on a PSP but that's annoyingly long.
37
const static int MSG_INIT_DELAY_US = 300000;
38
const static int MSG_SHUTDOWN_DELAY_US = 26000;
39
40
PSPMsgDialog::PSPMsgDialog(UtilityDialogType type) : PSPDialog(type) {
41
}
42
43
PSPMsgDialog::~PSPMsgDialog() {
44
}
45
46
int PSPMsgDialog::Init(unsigned int paramAddr) {
47
// Ignore if already running
48
if (GetStatus() != SCE_UTILITY_STATUS_NONE) {
49
ERROR_LOG_REPORT(Log::sceUtility, "sceUtilityMsgDialogInitStart: invalid status");
50
return SCE_ERROR_UTILITY_INVALID_STATUS;
51
}
52
53
messageDialogAddr = paramAddr;
54
if (!Memory::IsValidAddress(messageDialogAddr))
55
{
56
return 0;
57
}
58
int size = Memory::Read_U32(paramAddr);
59
memset(&messageDialog,0,sizeof(messageDialog));
60
// Only copy the right size to support different request format
61
Memory::Memcpy(&messageDialog,paramAddr,size);
62
63
// debug info
64
int optionsNotCoded = messageDialog.options & ~SCE_UTILITY_MSGDIALOG_OPTION_SUPPORTED;
65
if(optionsNotCoded)
66
{
67
ERROR_LOG_REPORT(Log::sceUtility, "PSPMsgDialog options not coded : 0x%08x", optionsNotCoded);
68
}
69
70
flag = 0;
71
scrollPos_ = 0.0f;
72
framesUpHeld_ = 0;
73
framesDownHeld_ = 0;
74
75
// Check request invalidity
76
if(messageDialog.type == 0 && !(messageDialog.errorNum & 0x80000000))
77
{
78
flag |= DS_ERROR;
79
messageDialog.result = SCE_ERROR_UTILITY_MSGDIALOG_ERRORCODEINVALID;
80
}
81
else if(size == SCE_UTILITY_MSGDIALOG_SIZE_V2 && messageDialog.type == 1)
82
{
83
unsigned int validOp = SCE_UTILITY_MSGDIALOG_OPTION_TEXTSOUND |
84
SCE_UTILITY_MSGDIALOG_OPTION_YESNO |
85
SCE_UTILITY_MSGDIALOG_OPTION_DEFAULT_NO;
86
if (((messageDialog.options | validOp) ^ validOp) != 0)
87
{
88
flag |= DS_ERROR;
89
messageDialog.result = SCE_ERROR_UTILITY_MSGDIALOG_BADOPTION;
90
}
91
}
92
else if(size == SCE_UTILITY_MSGDIALOG_SIZE_V3)
93
{
94
if((messageDialog.options & SCE_UTILITY_MSGDIALOG_OPTION_DEFAULT_NO) &&
95
!(messageDialog.options & SCE_UTILITY_MSGDIALOG_OPTION_YESNO))
96
{
97
flag |= DS_ERROR;
98
messageDialog.result = SCE_ERROR_UTILITY_MSGDIALOG_BADOPTION;
99
}
100
if (messageDialog.options & ~SCE_UTILITY_MSGDIALOG_OPTION_SUPPORTED)
101
{
102
flag |= DS_ERROR;
103
messageDialog.result = SCE_ERROR_UTILITY_MSGDIALOG_BADOPTION;
104
}
105
}
106
107
if(flag == 0)
108
{
109
yesnoChoice = 1;
110
if(messageDialog.type == 1)
111
flag |= DS_MSG;
112
if(messageDialog.type == 0)
113
flag |= DS_ERRORMSG;
114
if((messageDialog.options & SCE_UTILITY_MSGDIALOG_OPTION_YESNO) &&
115
((size == SCE_UTILITY_MSGDIALOG_SIZE_V3) ||
116
(size == SCE_UTILITY_MSGDIALOG_SIZE_V2 && messageDialog.type == 1)))
117
flag |= DS_YESNO;
118
if(messageDialog.options & SCE_UTILITY_MSGDIALOG_OPTION_DEFAULT_NO)
119
{
120
yesnoChoice = 0;
121
flag |= DS_DEFNO;
122
}
123
if((messageDialog.options & SCE_UTILITY_MSGDIALOG_OPTION_OK) && (size == SCE_UTILITY_MSGDIALOG_SIZE_V3))
124
{
125
yesnoChoice = 1;
126
flag |= DS_OK;
127
}
128
if((flag & DS_YESNO) || (flag & DS_OK))
129
flag |= DS_VALIDBUTTON;
130
if(!((messageDialog.options & SCE_UTILITY_MSGDIALOG_OPTION_NOCANCEL) && (size == SCE_UTILITY_MSGDIALOG_SIZE_V3)))
131
flag |= DS_CANCELBUTTON;
132
if(messageDialog.options & SCE_UTILITY_MSGDIALOG_OPTION_NOSOUND)
133
flag |= DS_NOSOUND;
134
}
135
136
if (flag & DS_ERRORMSG) {
137
FormatErrorCode(messageDialog.errorNum);
138
} else {
139
truncate_cpy(msgText, messageDialog.string);
140
}
141
142
ChangeStatusInit(MSG_INIT_DELAY_US);
143
144
UpdateButtons();
145
InitCommon();
146
StartFade(true);
147
return 0;
148
}
149
150
151
void PSPMsgDialog::FormatErrorCode(uint32_t code) {
152
auto err = GetI18NCategory(I18NCat::DIALOG);
153
154
switch (code) {
155
case SCE_UTILITY_SAVEDATA_ERROR_LOAD_DATA_BROKEN:
156
snprintf(msgText, 512, "%s (%08x)", err->T_cstr("MsgErrorSavedataDataBroken", "Save data was corrupt."), code);
157
break;
158
159
case SCE_UTILITY_SAVEDATA_ERROR_LOAD_NO_MS:
160
case SCE_UTILITY_SAVEDATA_ERROR_RW_NO_MEMSTICK:
161
case SCE_UTILITY_SAVEDATA_ERROR_SAVE_NO_MS:
162
case SCE_UTILITY_SAVEDATA_ERROR_DELETE_NO_MS:
163
case SCE_UTILITY_SAVEDATA_ERROR_SIZES_NO_MS:
164
snprintf(msgText, 512, "%s (%08x)", err->T_cstr("MsgErrorSavedataNoMS", "Memory stick not inserted."), code);
165
break;
166
167
case SCE_UTILITY_SAVEDATA_ERROR_LOAD_NO_DATA:
168
case SCE_UTILITY_SAVEDATA_ERROR_RW_NO_DATA:
169
case SCE_UTILITY_SAVEDATA_ERROR_DELETE_NO_DATA:
170
case SCE_UTILITY_SAVEDATA_ERROR_SIZES_NO_DATA:
171
snprintf(msgText, 512, "%s (%08x)", err->T_cstr("MsgErrorSavedataNoData", "Warning: no save data was found."), code);
172
break;
173
174
case SCE_UTILITY_SAVEDATA_ERROR_RW_MEMSTICK_FULL:
175
case SCE_UTILITY_SAVEDATA_ERROR_SAVE_MS_NOSPACE:
176
snprintf(msgText, 512, "%s (%08x)", err->T_cstr("MsgErrorSavedataMSFull", "Memory stick full. Check your storage space."), code);
177
break;
178
179
default:
180
snprintf(msgText, 512, "%s %08x", err->T_cstr("MsgErrorCode", "Error code:"), code);
181
}
182
}
183
184
void PSPMsgDialog::DisplayMessage(const std::string &text, bool hasYesNo, bool hasOK) {
185
auto di = GetI18NCategory(I18NCat::DIALOG);
186
187
PPGeStyle buttonStyle = FadedStyle(PPGeAlign::BOX_CENTER, FONT_SCALE);
188
PPGeStyle messageStyle = FadedStyle(PPGeAlign::BOX_HCENTER, FONT_SCALE);
189
190
// Without the scrollbar, we have 390 total pixels.
191
float WRAP_WIDTH = 340.0f;
192
if ((size_t)UTF8StringNonASCIICount(text) >= text.size() / 4) {
193
WRAP_WIDTH = 376.0f;
194
if (text.size() > 12) {
195
messageStyle.scale = 0.6f;
196
}
197
}
198
199
float totalHeight = 0.0f;
200
PPGeMeasureText(nullptr, &totalHeight, text, FONT_SCALE, PPGE_LINE_WRAP_WORD, WRAP_WIDTH);
201
// The PSP normally only shows about 8 lines at a time.
202
// For improved UX, we intentionally show part of the next line.
203
float visibleHeight = std::min(totalHeight, 175.0f);
204
float h2 = visibleHeight / 2.0f;
205
206
float centerY = 135.0f;
207
float sy = centerY - h2 - 15.0f;
208
float ey = centerY + h2 + 20.0f;
209
float buttonY = centerY + h2 + 5.0f;
210
211
auto drawSelectionBoxAndAdjust = [&](float x) {
212
// Box has a fixed size.
213
float w = 15.0f;
214
float h = 8.0f;
215
PPGeDrawRect(x - w, buttonY - h, x + w, buttonY + h, CalcFadedColor(0x6DCFCFCF));
216
217
centerY -= h + 5.0f;
218
sy -= h + 5.0f;
219
ey = buttonY + h * 2.0f + 5.0f;
220
};
221
222
if (hasYesNo) {
223
if (yesnoChoice == 1) {
224
drawSelectionBoxAndAdjust(204.0f);
225
} else {
226
drawSelectionBoxAndAdjust(273.0f);
227
}
228
229
PPGeDrawText(di->T("Yes"), 203.0f, buttonY - 1.0f, buttonStyle);
230
PPGeDrawText(di->T("No"), 272.0f, buttonY - 1.0f, buttonStyle);
231
if (IsButtonPressed(CTRL_LEFT) && yesnoChoice == 0) {
232
yesnoChoice = 1;
233
} else if (IsButtonPressed(CTRL_RIGHT) && yesnoChoice == 1) {
234
yesnoChoice = 0;
235
}
236
buttonY += 8.0f + 5.0f;
237
}
238
239
if (hasOK) {
240
drawSelectionBoxAndAdjust(240.0f);
241
242
PPGeDrawText(di->T("OK"), 239.0f, buttonY - 1.0f, buttonStyle);
243
buttonY += 8.0f + 5.0f;
244
}
245
246
PPGeScissor(0, (int)(centerY - h2 - 2), 480, (int)(centerY + h2 + 2));
247
PPGeDrawTextWrapped(text.c_str(), 240.0f, centerY - h2 - scrollPos_, WRAP_WIDTH, 0, messageStyle);
248
PPGeScissorReset();
249
250
// Do we need a scrollbar?
251
if (visibleHeight < totalHeight) {
252
float scrollSpeed = 5.0f;
253
float scrollMax = totalHeight - visibleHeight;
254
255
float bobHeight = (visibleHeight / totalHeight) * visibleHeight;
256
float bobOffset = (scrollPos_ / scrollMax) * (visibleHeight - bobHeight);
257
float bobY1 = centerY - h2 + bobOffset;
258
PPGeDrawRect(435.0f, bobY1, 440.0f, bobY1 + bobHeight, CalcFadedColor(0xFFCCCCCC));
259
260
auto buttonDown = [this](int btn, int &held) {
261
if (IsButtonPressed(btn)) {
262
held = 0;
263
return true;
264
}
265
return IsButtonHeld(btn, held, 1, 1);
266
};
267
if (buttonDown(CTRL_DOWN, framesDownHeld_) && scrollPos_ < scrollMax) {
268
scrollPos_ = std::min(scrollMax, scrollPos_ + scrollSpeed);
269
}
270
if (buttonDown(CTRL_UP, framesUpHeld_) && scrollPos_ > 0.0f) {
271
scrollPos_ = std::max(0.0f, scrollPos_ - scrollSpeed);
272
}
273
}
274
275
PPGeDrawRect(40.0f, sy, 440.0f, sy + 1.0f, CalcFadedColor(0xFFFFFFFF));
276
PPGeDrawRect(40.0f, ey, 440.0f, ey + 1.0f, CalcFadedColor(0xFFFFFFFF));
277
}
278
279
int PSPMsgDialog::Update(int animSpeed) {
280
if (GetStatus() != SCE_UTILITY_STATUS_RUNNING) {
281
return SCE_ERROR_UTILITY_INVALID_STATUS;
282
}
283
284
if (flag & (DS_ERROR | DS_ABORT)) {
285
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0);
286
} else {
287
UpdateButtons();
288
UpdateCommon();
289
UpdateFade(animSpeed);
290
291
StartDraw();
292
// white -> RGB(168,173,189), black -> RGB(129,134,150)
293
// (255 - a) + (x * a / 255) = 173, x * a / 255 = 134
294
// a = 255 - w + b = 158, x = b * 255 / a = ?
295
// but is not drawn using x * a + y * (255 - a) here?
296
//PPGeDrawRect(0, 0, 480, 272, CalcFadedColor(0x9EF2D8D0));
297
PPGeDrawRect(0, 0, 480, 272, CalcFadedColor(0xC0C8B2AC));
298
299
if ((flag & DS_MSG) || (flag & DS_ERRORMSG))
300
DisplayMessage(msgText, (flag & DS_YESNO) != 0, (flag & DS_OK) != 0);
301
302
if (flag & (DS_OK | DS_VALIDBUTTON))
303
DisplayButtons(DS_BUTTON_OK, messageDialog.common.size == SCE_UTILITY_MSGDIALOG_SIZE_V3 ? messageDialog.okayButton : "");
304
305
if (flag & DS_CANCELBUTTON)
306
DisplayButtons(DS_BUTTON_CANCEL, messageDialog.common.size == SCE_UTILITY_MSGDIALOG_SIZE_V3 ? messageDialog.cancelButton : "");
307
308
if (IsButtonPressed(cancelButtonFlag) && (flag & DS_CANCELBUTTON))
309
{
310
if(messageDialog.common.size == SCE_UTILITY_MSGDIALOG_SIZE_V3 ||
311
((messageDialog.common.size == SCE_UTILITY_MSGDIALOG_SIZE_V2) && (flag & DS_YESNO)))
312
messageDialog.buttonPressed = 3;
313
else
314
messageDialog.buttonPressed = 0;
315
StartFade(false);
316
}
317
else if (IsButtonPressed(okButtonFlag) && (flag & DS_VALIDBUTTON))
318
{
319
if (yesnoChoice == 0)
320
{
321
messageDialog.buttonPressed = 2;
322
}
323
else
324
{
325
messageDialog.buttonPressed = 1;
326
}
327
StartFade(false);
328
}
329
330
331
EndDraw();
332
333
messageDialog.result = 0;
334
}
335
336
Memory::Memcpy(messageDialogAddr, &messageDialog, messageDialog.common.size, "MsgDialogParam");
337
return 0;
338
}
339
340
int PSPMsgDialog::Abort() {
341
// Katekyoushi Hitman Reborn! Battle Arena expects this to fail when not running.
342
if (GetStatus() != SCE_UTILITY_STATUS_RUNNING) {
343
return SCE_ERROR_UTILITY_INVALID_STATUS;
344
} else {
345
// Status is not actually changed until Update().
346
flag |= DS_ABORT;
347
return 0;
348
}
349
}
350
351
int PSPMsgDialog::Shutdown(bool force) {
352
if (GetStatus() != SCE_UTILITY_STATUS_FINISHED && !force)
353
return SCE_ERROR_UTILITY_INVALID_STATUS;
354
355
PSPDialog::Shutdown(force);
356
if (!force) {
357
ChangeStatusShutdown(MSG_SHUTDOWN_DELAY_US);
358
}
359
360
return 0;
361
}
362
363
void PSPMsgDialog::DoState(PointerWrap &p)
364
{
365
PSPDialog::DoState(p);
366
367
auto s = p.Section("PSPMsgDialog", 1);
368
if (!s)
369
return;
370
371
Do(p, flag);
372
Do(p, messageDialog);
373
Do(p, messageDialogAddr);
374
DoArray(p, msgText, sizeof(msgText));
375
Do(p, yesnoChoice);
376
377
// We don't save state this, you'll just have to scroll down again.
378
if (p.mode == p.MODE_READ) {
379
scrollPos_ = 0.0f;
380
framesUpHeld_ = 0;
381
framesDownHeld_ = 0;
382
}
383
}
384
385
pspUtilityDialogCommon *PSPMsgDialog::GetCommonParam()
386
{
387
return &messageDialog.common;
388
}
389
390