-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhexpad.c
More file actions
444 lines (405 loc) · 10.6 KB
/
hexpad.c
File metadata and controls
444 lines (405 loc) · 10.6 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "hexpad.h"
static ColorConfig* digit_color(HexPad* prPad, const int x, const int y);
static int render_cursor(HexPad* prPad);
static int render_digit(HexPad* prPad, const int x, const int y);
static int render_value(HexPad* prPad);
static ColorConfig* digit_color(HexPad* prPad, const int x, const int y) {
CursorPos* prCursor = NULL;
if (prPad != NULL) {
prCursor = hexpad_get_cursorpos(prPad);
if ((prCursor->x == x) && (prCursor->y == y)) {
return hexpad_get_cursorcolor(prPad);
} else {
return hexpad_get_panelcolor(prPad);
}
}
return NULL;
}
int hexpad_button_circle(HexPad* prPad) {
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prPad->cancelled = 1;
prPad->visible = 0;
return HEXPAD_SUCCESS;
}
int hexpad_button_cross(HexPad* prPad) {
CursorPos* prCursor = NULL;
u8 value = 0;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prCursor = hexpad_get_cursorpos(prPad);
value = (u8)((prCursor->y * 4) + prCursor->x);
prPad->byteval[prPad->digit] = value;
if (hexpad_next_digit(prPad) < 0) {
return HEXPAD_FAILURE;
}
return HEXPAD_SUCCESS;
}
int hexpad_button_square(HexPad* prPad) {
return HEXPAD_SUCCESS;
}
int hexpad_button_triangle(HexPad* prPad) {
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prPad->cancelled = 0;
prPad->visible = 0;
return HEXPAD_SUCCESS;
}
int hexpad_prev_digit(HexPad* prPad) {
int digit = 0;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
digit = prPad->digit;
digit--;
if (digit < 0) {
digit = 7;
}
prPad->digit = digit;
if (hexpad_set_cursordigit(prPad, prPad->byteval[prPad->digit]) < 0) {
return HEXPAD_FAILURE;
}
return HEXPAD_SUCCESS;
}
int hexpad_next_digit(HexPad* prPad) {
int digit = 0;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
digit = prPad->digit;
digit++;
if (digit > 7) {
digit = 0;
prPad->visible = 0;
prPad->cancelled = 0;
return HEXPAD_SUCCESS;
}
prPad->digit = digit;
if (hexpad_set_cursordigit(prPad, prPad->byteval[prPad->digit]) < 0) {
return HEXPAD_FAILURE;
}
return HEXPAD_SUCCESS;
}
int hexpad_cursor_down(HexPad* prPad) {
CursorPos* prCursor = NULL;
int y = 0;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prCursor = hexpad_get_cursorpos(prPad);
y = prCursor->y;
y++;
if (y > 3) {
y = 0;
}
prCursor->y = y;
return HEXPAD_SUCCESS;
}
int hexpad_cursor_left(HexPad* prPad) {
CursorPos* prCursor = NULL;
int x = 0;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prCursor = hexpad_get_cursorpos(prPad);
x = prCursor->x;
x--;
if (x < 0) {
x = 3;
}
prCursor->x = x;
return HEXPAD_SUCCESS;
}
int hexpad_cursor_right(HexPad* prPad) {
CursorPos* prCursor = NULL;
int x = 0;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prCursor = hexpad_get_cursorpos(prPad);
x = prCursor->x;
x++;
if (x > 3) {
x = 0;
}
prCursor->x = x;
return HEXPAD_SUCCESS;
}
int hexpad_cursor_up(HexPad* prPad) {
CursorPos* prCursor = NULL;
int y = 0;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prCursor = hexpad_get_cursorpos(prPad);
y = prCursor->y;
y--;
if (y < 0) {
y = 3;
}
prCursor->y = y;
return HEXPAD_SUCCESS;
}
PanelConfig* hexpad_get_config(HexPad* prPad) {
if (prPad != NULL) {
return &prPad->panelConfig;
}
return NULL;
}
ColorConfig* hexpad_get_cursorcolor(HexPad* prPad) {
PanelConfig* prConfig = NULL;
if (prPad != NULL) {
prConfig = hexpad_get_config(prPad);
return panelconfig_get_cursorcolor(prConfig);
}
return NULL;
}
CursorPos* hexpad_get_cursorpos(HexPad* prPad) {
if (prPad != NULL) {
return &prPad->cursorPos;
}
return NULL;
}
ColorConfig* hexpad_get_panelcolor(HexPad* prPad) {
PanelConfig* prConfig = NULL;
if (prPad != NULL) {
prConfig = hexpad_get_config(prPad);
return panelconfig_get_panelcolor(prConfig);
}
return NULL;
}
CursorPos* hexpad_get_position(HexPad* prPad) {
PanelConfig* prConfig = NULL;
if (prPad != NULL) {
prConfig = hexpad_get_config(prPad);
return panelconfig_get_position(prConfig);
}
return NULL;
}
Dimension* hexpad_get_size(HexPad* prPad) {
PanelConfig* prConfig = NULL;
if (prPad != NULL) {
prConfig = hexpad_get_config(prPad);
return panelconfig_get_size(prConfig);
}
return NULL;
}
SceUInt32 hexpad_get_value(HexPad* prPad) {
SceUInt32 value = 0;
int i = 0;
if (prPad != NULL) {
for (i = 0; i < 8; i++) {
value |= prPad->byteval[i];
if (i < 7) {
value <<= 4;
}
}
}
return value;
}
int hexpad_init(HexPad* prPad) {
PanelConfig* prConfig = NULL;
CursorPos* prCursor = NULL;
Dimension* prSize = NULL;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prConfig = hexpad_get_config(prPad);
if (panelconfig_init(prConfig) < 0) {
return HEXPAD_FAILURE;
}
prCursor = hexpad_get_cursorpos(prPad);
if (cursorpos_init(prCursor) < 0) {
return HEXPAD_FAILURE;
}
prSize = hexpad_get_size(prPad);
if (dimension_set(prSize, 12, 14) < 0) {
return HEXPAD_FAILURE;
}
return HEXPAD_SUCCESS;
}
int hexpad_redraw(HexPad* prPad) {
int x = 0;
int y = 0;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
if (prPad->visible != 1) {
return HEXPAD_SUCCESS;
}
for (y = 0; y < 4; y++) {
for (x = 0; x < 4; x++) {
if (render_digit(prPad, x, y) < 0) {
return HEXPAD_FAILURE;
}
}
}
if (render_value(prPad) < 0) {
return HEXPAD_FAILURE;
}
if (render_cursor(prPad) < 0) {
return HEXPAD_FAILURE;
}
return HEXPAD_SUCCESS;
}
int hexpad_reset(HexPad* prPad) {
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prPad->cancelled = 0;
prPad->digit = 0;
if (hexpad_set_value(prPad, 0) < 0) {
return HEXPAD_FAILURE;
}
return HEXPAD_SUCCESS;
}
int hexpad_set_cursorcolor(HexPad* prPad, const u32 background, const u32 text)
{
ColorConfig* prColor = NULL;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prColor = hexpad_get_cursorcolor(prPad);
if (colorconfig_setcolor(prColor, background, text) < 0) {
return HEXPAD_FAILURE;
}
return HEXPAD_SUCCESS;
}
int hexpad_set_cursordigit(HexPad* prPad, const int digit) {
int x = 0;
int y = 0;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
if ((digit < 0) || (digit > 0xF)) {
return HEXPAD_INVIDX;
}
y = (int)(digit / 4);
x = digit % 4;
if (hexpad_set_cursorpos(prPad, x, y) < 0) {
return HEXPAD_FAILURE;
}
return HEXPAD_SUCCESS;
}
int hexpad_set_cursorpos(HexPad* prPad, const int x, const int y) {
CursorPos* prCursor = NULL;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
if ((x < 0) || (x > 3)) {
return HEXPAD_INVIDX;
}
if ((y < 0) || (y > 3)) {
return HEXPAD_INVIDX;
}
prCursor = hexpad_get_cursorpos(prPad);
prCursor->x = x;
prCursor->y = y;
return HEXPAD_SUCCESS;
}
int hexpad_set_panelcolor(HexPad* prPad, const u32 background, const u32 text)
{
ColorConfig* prColor = NULL;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prColor = hexpad_get_panelcolor(prPad);
if (colorconfig_setcolor(prColor, background, text) < 0) {
return HEXPAD_FAILURE;
}
return HEXPAD_SUCCESS;
}
int hexpad_set_position(HexPad* prPad, const int x, const int y) {
CursorPos* prPos = NULL;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prPos = hexpad_get_position(prPad);
if (cursorpos_set(prPos, x, y) < 0) {
return HEXPAD_FAILURE;
}
return HEXPAD_SUCCESS;
}
int hexpad_set_value(HexPad* prPad, const SceUInt32 value) {
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prPad->byteval[0] = (u8)((value & 0xF0000000) >> 28);
prPad->byteval[1] = (u8)((value & 0x0F000000) >> 24);
prPad->byteval[2] = (u8)((value & 0x00F00000) >> 20);
prPad->byteval[3] = (u8)((value & 0x000F0000) >> 16);
prPad->byteval[4] = (u8)((value & 0x0000F000) >> 12);
prPad->byteval[5] = (u8)((value & 0x00000F00) >> 8);
prPad->byteval[6] = (u8)((value & 0x000000F0) >> 4);
prPad->byteval[7] = (u8)(value & 0x0000000F);
return HEXPAD_SUCCESS;
}
static int render_cursor(HexPad* prPad) {
CursorPos* prPos = NULL;
ColorConfig* prColor = NULL;
int bp = 0;
int ap = 0;
int x = 0;
int y = 0;
char sFmt[20];
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
bp = 3 + prPad->digit;
ap = 12 - bp - 1;
sprintf(sFmt, "%%%ds^%%-%ds", bp, ap);
prColor = hexpad_get_panelcolor(prPad);
pspDebugScreenSetBackColor(prColor->background);
pspDebugScreenSetTextColor(prColor->text);
prPos = hexpad_get_position(prPad);
x = prPos->x;
y = prPos->y + 13;
pspDebugScreenSetXY(x, y);
pspDebugScreenKprintf(sFmt, "", "");
return HEXPAD_SUCCESS;
}
static int render_digit(HexPad* prPad, const int x, const int y) {
CursorPos* prPos = NULL;
ColorConfig* prColor = NULL;
int vx = 0;
int vy = 0;
int digit = 0;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
digit = (4 * y) + x;
prPos = hexpad_get_position(prPad);
vx = prPos->x + (x * 3);
vy = prPos->y + (y * 3);
prColor = digit_color(prPad, x, y);
if (prColor != NULL) {
pspDebugScreenSetBackColor(prColor->background);
pspDebugScreenSetTextColor(prColor->text);
}
pspDebugScreenSetXY(vx, vy);
pspDebugScreenPuts(" ");
pspDebugScreenSetXY(vx, vy + 1);
pspDebugScreenKprintf(" %X ", digit);
pspDebugScreenSetXY(vx, vy + 2);
pspDebugScreenPuts(" ");
return HEXPAD_SUCCESS;
}
static int render_value(HexPad* prPad) {
CursorPos* prPos = NULL;
ColorConfig* prColor = NULL;
SceUInt32 value = 0;
if (prPad == NULL) {
return HEXPAD_NULLPTR;
}
prPos = hexpad_get_position(prPad);
prColor = hexpad_get_panelcolor(prPad);
value = hexpad_get_value(prPad);
pspDebugScreenSetXY(prPos->x, prPos->y + 12);
pspDebugScreenSetBackColor(prColor->background);
pspDebugScreenSetTextColor(prColor->text);
pspDebugScreenKprintf(" 0x%08X ", value);
return HEXPAD_SUCCESS;
}