/
PSPDialog.cpp
344 lines (295 loc) · 9.02 KB
/
PSPDialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright (c) 2012- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "Common/Data/Text/I18n.h"
#include "Common/Serialize/Serializer.h"
#include "Common/Serialize/SerializeFuncs.h"
#include "Common/StringUtils.h"
#include "Core/Config.h"
#include "Core/System.h"
#include "Core/CoreTiming.h"
#include "Core/Dialog/PSPDialog.h"
#include "Core/HLE/sceCtrl.h"
#include "Core/HLE/scePower.h"
#include "Core/HLE/sceUtility.h"
#include "Core/MemMapHelpers.h"
#include "Core/Util/PPGeDraw.h"
#define FADE_TIME 1.0
const float FONT_SCALE = 0.55f;
PSPDialog::PSPDialog(UtilityDialogType type) : dialogType_(type) {
}
PSPDialog::~PSPDialog() {
}
void PSPDialog::InitCommon() {
UpdateCommon();
if (GetCommonParam() && GetCommonParam()->language != g_Config.iLanguage) {
WARN_LOG(SCEUTILITY, "Game requested language %d, ignoring and using user language", GetCommonParam()->language);
}
}
void PSPDialog::UpdateCommon() {
okButtonImg = ImageID("I_CIRCLE");
cancelButtonImg = ImageID("I_CROSS");
okButtonFlag = CTRL_CIRCLE;
cancelButtonFlag = CTRL_CROSS;
if (GetCommonParam() && GetCommonParam()->buttonSwap == 1) {
okButtonImg = ImageID("I_CROSS");
cancelButtonImg = ImageID("I_CIRCLE");
okButtonFlag = CTRL_CROSS;
cancelButtonFlag = CTRL_CIRCLE;
}
}
PSPDialog::DialogStatus PSPDialog::GetStatus() {
if (pendingStatusTicks != 0 && CoreTiming::GetTicks() >= pendingStatusTicks) {
bool changeAllowed = true;
if (pendingStatus == SCE_UTILITY_STATUS_NONE && status == SCE_UTILITY_STATUS_SHUTDOWN) {
FinishVolatile();
} else if (pendingStatus == SCE_UTILITY_STATUS_RUNNING && status == SCE_UTILITY_STATUS_INITIALIZE) {
if (!volatileLocked_) {
volatileLocked_ = KernelVolatileMemLock(0, 0, 0) == 0;
changeAllowed = volatileLocked_;
}
}
if (changeAllowed) {
status = pendingStatus;
pendingStatusTicks = 0;
}
}
PSPDialog::DialogStatus retval = status;
if (UseAutoStatus()) {
if (status == SCE_UTILITY_STATUS_SHUTDOWN)
status = SCE_UTILITY_STATUS_NONE;
if (status == SCE_UTILITY_STATUS_INITIALIZE)
status = SCE_UTILITY_STATUS_RUNNING;
}
return retval;
}
void PSPDialog::ChangeStatus(DialogStatus newStatus, int delayUs) {
if (delayUs <= 0) {
if (newStatus == SCE_UTILITY_STATUS_NONE && status == SCE_UTILITY_STATUS_SHUTDOWN) {
FinishVolatile();
} else if (newStatus == SCE_UTILITY_STATUS_RUNNING && status == SCE_UTILITY_STATUS_INITIALIZE) {
if (!volatileLocked_) {
// TODO: Should probably make the status pending instead?
volatileLocked_ = KernelVolatileMemLock(0, 0, 0) == 0;
}
}
status = newStatus;
pendingStatus = newStatus;
pendingStatusTicks = 0;
} else {
pendingStatus = newStatus;
pendingStatusTicks = CoreTiming::GetTicks() + usToCycles(delayUs);
}
}
void PSPDialog::FinishVolatile() {
if (!volatileLocked_)
return;
if (KernelVolatileMemUnlock(0) == 0) {
volatileLocked_ = false;
// Simulate modifications to volatile memory.
Memory::Memset(PSP_GetVolatileMemoryStart(), 0, PSP_GetVolatileMemoryEnd() - PSP_GetVolatileMemoryStart());
}
}
int PSPDialog::FinishInit() {
if (ReadStatus() != SCE_UTILITY_STATUS_INITIALIZE)
return -1;
// The thread already locked.
volatileLocked_ = true;
ChangeStatus(SCE_UTILITY_STATUS_RUNNING, 0);
return 0;
}
int PSPDialog::FinishShutdown() {
if (ReadStatus() != SCE_UTILITY_STATUS_SHUTDOWN)
return -1;
ChangeStatus(SCE_UTILITY_STATUS_NONE, 0);
return 0;
}
void PSPDialog::ChangeStatusInit(int delayUs) {
ChangeStatus(SCE_UTILITY_STATUS_INITIALIZE, 0);
auto params = GetCommonParam();
if (params)
UtilityDialogInitialize(DialogType(), delayUs, params->accessThread);
else
ChangeStatus(SCE_UTILITY_STATUS_RUNNING, delayUs);
}
void PSPDialog::ChangeStatusShutdown(int delayUs) {
// If we're doing shutdown right away and skipped start, we don't run the dialog thread.
bool skipDialogShutdown = status == SCE_UTILITY_STATUS_NONE && pendingStatus == SCE_UTILITY_STATUS_NONE;
ChangeStatus(SCE_UTILITY_STATUS_SHUTDOWN, 0);
auto params = GetCommonParam();
if (params && !skipDialogShutdown)
UtilityDialogShutdown(DialogType(), delayUs, params->accessThread);
else
ChangeStatus(SCE_UTILITY_STATUS_NONE, delayUs);
}
void PSPDialog::StartDraw()
{
PPGeBegin();
PPGeDrawRect(0, 0, 480, 272, CalcFadedColor(0x20000000));
}
void PSPDialog::EndDraw()
{
PPGeEnd();
}
int PSPDialog::Shutdown(bool force)
{
if (force) {
ChangeStatus(SCE_UTILITY_STATUS_NONE, 0);
} else {
ChangeStatus(SCE_UTILITY_STATUS_SHUTDOWN, 0);
}
return 0;
}
void PSPDialog::StartFade(bool fadeIn_)
{
isFading = true;
fadeTimer = 0;
fadeIn = fadeIn_;
}
void PSPDialog::UpdateFade(int animSpeed) {
if (isFading) {
fadeTimer += 1.0f/30.0f * animSpeed; // Probably need a more real value of delta time
if (fadeTimer < FADE_TIME) {
if (fadeIn)
fadeValue = (u32) (fadeTimer / FADE_TIME * 255);
else
fadeValue = 255 - (u32) (fadeTimer / FADE_TIME * 255);
} else {
fadeValue = (fadeIn ? 255 : 0);
isFading = false;
if (!fadeIn) {
FinishFadeOut();
}
}
}
}
void PSPDialog::FinishFadeOut() {
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0);
}
u32 PSPDialog::CalcFadedColor(u32 inColor)
{
u32 alpha = inColor >> 24;
alpha = alpha * fadeValue / 255;
return (inColor & 0x00FFFFFF) | (alpha << 24);
}
void PSPDialog::DoState(PointerWrap &p) {
auto s = p.Section("PSPDialog", 1, 3);
if (!s)
return;
Do(p, status);
Do(p, lastButtons);
Do(p, buttons);
Do(p, fadeTimer);
Do(p, isFading);
Do(p, fadeIn);
Do(p, fadeValue);
// I don't think we should save these two... Let's just ignore them for now for compat.
int okButtonImg = 0;
Do(p, okButtonImg);
int cancelButtonImg = 0;
Do(p, cancelButtonImg);
Do(p, okButtonFlag);
Do(p, cancelButtonFlag);
if (s >= 2) {
Do(p, pendingStatus);
Do(p, pendingStatusTicks);
} else {
pendingStatusTicks = 0;
}
if (s >= 3) {
Do(p, volatileLocked_);
} else {
volatileLocked_ = false;
}
}
pspUtilityDialogCommon *PSPDialog::GetCommonParam()
{
// FIXME
return 0;
}
void PSPDialog::UpdateButtons()
{
lastButtons = __CtrlPeekButtons();
buttons = __CtrlReadLatch();
}
bool PSPDialog::IsButtonPressed(int checkButton)
{
return !isFading && (buttons & checkButton);
}
bool PSPDialog::IsButtonHeld(int checkButton, int &framesHeld, int framesHeldThreshold, int framesHeldRepeatRate)
{
bool btnWasHeldLastFrame = (lastButtons & checkButton) && (__CtrlPeekButtons() & checkButton);
if (!isFading && btnWasHeldLastFrame) {
framesHeld++;
}
else {
framesHeld = 0;
return false;
}
// It's considered held for dialog purposes after 30 frames (~0.5 seconds),
// and set to repeat every 10 frames, by default.
if (framesHeld >= framesHeldThreshold && ((framesHeld % framesHeldRepeatRate) == 0))
return true;
return false;
}
PPGeStyle PSPDialog::FadedStyle(PPGeAlign align, float scale) {
PPGeStyle textStyle;
textStyle.align = align;
textStyle.scale = scale;
textStyle.color = CalcFadedColor(textStyle.color);
textStyle.hasShadow = true;
textStyle.shadowColor = CalcFadedColor(textStyle.shadowColor);
return textStyle;
}
PPGeImageStyle PSPDialog::FadedImageStyle() {
PPGeImageStyle style;
style.color = CalcFadedColor(style.color);
return style;
}
void PSPDialog::DisplayButtons(int flags, const char *caption)
{
bool useCaption = false;
char safeCaption[65] = {0};
if (caption != NULL && *caption != '\0') {
useCaption = true;
truncate_cpy(safeCaption, caption);
}
PPGeStyle textStyle = FadedStyle(PPGeAlign::BOX_LEFT, FONT_SCALE);
auto di = GetI18NCategory("Dialog");
float x1 = 183.5f, x2 = 261.5f;
if (GetCommonParam()->buttonSwap == 1) {
x1 = 261.5f;
x2 = 183.5f;
}
if (flags & DS_BUTTON_OK) {
const char *text = useCaption ? safeCaption : di->T("Enter");
PPGeDrawImage(okButtonImg, x2, 256, 11.5f, 11.5f, textStyle);
PPGeDrawText(text, x2 + 14.5f, 252, textStyle);
}
if (flags & DS_BUTTON_CANCEL) {
const char *text = useCaption ? safeCaption : di->T("Back");
PPGeDrawImage(cancelButtonImg, x1, 256, 11.5f, 11.5f, textStyle);
PPGeDrawText(text, x1 + 14.5f, 252, textStyle);
}
}
int PSPDialog::GetConfirmButton() {
if (PSP_CoreParameter().compat.flags().ForceCircleButtonConfirm) {
return CTRL_CIRCLE;
}
return g_Config.iButtonPreference == PSP_SYSTEMPARAM_BUTTON_CROSS ? CTRL_CROSS : CTRL_CIRCLE;
}
int PSPDialog::GetCancelButton() {
if (PSP_CoreParameter().compat.flags().ForceCircleButtonConfirm) {
return CTRL_CROSS;
}
return g_Config.iButtonPreference == PSP_SYSTEMPARAM_BUTTON_CROSS ? CTRL_CIRCLE : CTRL_CROSS;
}