forked from sdlpal/sdlpal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itemmenu.c
471 lines (403 loc) · 12.4 KB
/
itemmenu.c
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
//
// Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
// Copyright (c) 2011-2022, SDLPAL development team.
// All rights reserved.
//
// This file is part of SDLPAL.
//
// SDLPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 3
// as published by the Free Software Foundation.
//
// 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 for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "main.h"
static int g_iNumInventory = 0;
static WORD g_wItemFlags = 0;
static BOOL g_fNoDesc = FALSE;
WORD
PAL_ItemSelectMenuUpdate(
VOID
)
/*++
Purpose:
Initialize the item selection menu.
Parameters:
None.
Return value:
The object ID of the selected item. 0 if cancelled, 0xFFFF if not confirmed.
--*/
{
int i, j, k, line, item_delta;
WORD wObject, wScript;
BYTE bColor;
static BYTE bufImage[2048];
static WORD wPrevImageIndex = 0xFFFF;
const int iItemsPerLine = 32 / gConfig.dwWordLength;
const int iItemTextWidth = 8 * gConfig.dwWordLength + 20;
const int iLinesPerPage = 7 - gConfig.ScreenLayout.ExtraItemDescLines;
const int iCursorXOffset = gConfig.dwWordLength * 5 / 2;
const int iAmountXOffset = gConfig.dwWordLength * 8 + 1;
const int iPageLineOffset = (iLinesPerPage + 1) / 2;
const int iPictureYOffset = (gConfig.ScreenLayout.ExtraItemDescLines > 1) ? (gConfig.ScreenLayout.ExtraItemDescLines - 1) * 16 : 0;
//
// Process input
//
if (g_InputState.dwKeyPress & kKeyUp)
{
item_delta = -iItemsPerLine;
}
else if (g_InputState.dwKeyPress & kKeyDown)
{
item_delta = iItemsPerLine;
}
else if (g_InputState.dwKeyPress & kKeyLeft)
{
item_delta = -1;
}
else if (g_InputState.dwKeyPress & kKeyRight)
{
item_delta = 1;
}
else if (g_InputState.dwKeyPress & kKeyPgUp)
{
item_delta = -(iItemsPerLine * iLinesPerPage);
}
else if (g_InputState.dwKeyPress & kKeyPgDn)
{
item_delta = iItemsPerLine * iLinesPerPage;
}
else if (g_InputState.dwKeyPress & kKeyHome)
{
item_delta = -gpGlobals->iCurInvMenuItem;
}
else if (g_InputState.dwKeyPress & kKeyEnd)
{
item_delta = g_iNumInventory - gpGlobals->iCurInvMenuItem - 1;
}
else if (g_InputState.dwKeyPress & kKeyMenu)
{
return 0;
}
else
{
item_delta = 0;
}
//
// Make sure the current menu item index is in bound
//
if (gpGlobals->iCurInvMenuItem + item_delta < 0)
gpGlobals->iCurInvMenuItem = 0;
else if (gpGlobals->iCurInvMenuItem + item_delta >= g_iNumInventory)
gpGlobals->iCurInvMenuItem = g_iNumInventory-1;
else
gpGlobals->iCurInvMenuItem += item_delta;
//
// Redraw the box
//
PAL_CreateBoxWithShadow(PAL_XY(2, 0), iLinesPerPage - 1, 17, 1, FALSE, 0);
//
// Draw the texts in the current page
//
i = gpGlobals->iCurInvMenuItem / iItemsPerLine * iItemsPerLine - iItemsPerLine * iPageLineOffset;
if (i < 0)
{
i = 0;
}
for (j = 0; j < iLinesPerPage; j++)
{
for (k = 0; k < iItemsPerLine; k++)
{
wObject = gpGlobals->rgInventory[i].wItem;
bColor = MENUITEM_COLOR;
if (i >= MAX_INVENTORY || wObject == 0)
{
//
// End of the list reached
//
j = iLinesPerPage;
break;
}
if (i == gpGlobals->iCurInvMenuItem)
{
if (!(gpGlobals->g.rgObject[wObject].item.wFlags & g_wItemFlags) ||
(SHORT)gpGlobals->rgInventory[i].nAmount <= (SHORT)gpGlobals->rgInventory[i].nAmountInUse)
{
//
// This item is not selectable
//
bColor = MENUITEM_COLOR_SELECTED_INACTIVE;
}
else
{
//
// This item is selectable
//
if (gpGlobals->rgInventory[i].nAmount == 0)
{
bColor = MENUITEM_COLOR_EQUIPPEDITEM;
}
else
{
bColor = MENUITEM_COLOR_SELECTED;
}
}
}
else if (!(gpGlobals->g.rgObject[wObject].item.wFlags & g_wItemFlags) ||
(SHORT)gpGlobals->rgInventory[i].nAmount <= (SHORT)gpGlobals->rgInventory[i].nAmountInUse)
{
//
// This item is not selectable
//
bColor = MENUITEM_COLOR_INACTIVE;
}
else if (gpGlobals->rgInventory[i].nAmount == 0)
{
bColor = MENUITEM_COLOR_EQUIPPEDITEM;
}
//
// Draw the text
//
PAL_DrawText(PAL_GetWord(wObject), PAL_XY(15 + k * iItemTextWidth, 12 + j * 18), bColor, TRUE, FALSE, FALSE);
//
// Draw the cursor on the current selected item
//
if (i == gpGlobals->iCurInvMenuItem)
{
PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_CURSOR),
gpScreen, PAL_XY(15 + iCursorXOffset + k * iItemTextWidth, 22 + j * 18));
}
//
// Draw the amount of this item
//
if ((SHORT)gpGlobals->rgInventory[i].nAmount - (SHORT)gpGlobals->rgInventory[i].nAmountInUse > 1)
{
PAL_DrawNumber(gpGlobals->rgInventory[i].nAmount - gpGlobals->rgInventory[i].nAmountInUse,
2, PAL_XY(15 + iAmountXOffset + k * iItemTextWidth, 17 + j * 18), kNumColorCyan, kNumAlignRight);
}
i++;
}
}
int xBase = 0, yBase = 140;
//
// Draw the picture of current selected item
//
PAL_RLEBlitToSurfaceWithShadow(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_ITEMBOX), gpScreen,
PAL_XY(xBase+5, yBase+5 - iPictureYOffset),TRUE);
PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_ITEMBOX), gpScreen,
PAL_XY(xBase, yBase - iPictureYOffset));
wObject = gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].wItem;
if (gpGlobals->g.rgObject[wObject].item.wBitmap != wPrevImageIndex)
{
if (PAL_MKFReadChunk(bufImage, 2048,
gpGlobals->g.rgObject[wObject].item.wBitmap, gpGlobals->f.fpBALL) > 0)
{
wPrevImageIndex = gpGlobals->g.rgObject[wObject].item.wBitmap;
}
else
{
wPrevImageIndex = 0xFFFF;
}
}
if (wPrevImageIndex != 0xFFFF)
{
PAL_RLEBlitToSurface(bufImage, gpScreen, PAL_XY(xBase+8, yBase+7 - iPictureYOffset));
}
//
// Draw the description of the selected item
//
if (!gConfig.fIsWIN95)
{
if (!g_fNoDesc && gpGlobals->lpObjectDesc != NULL)
{
WCHAR szDesc[512], *next;
const WCHAR *d = PAL_GetObjectDesc(gpGlobals->lpObjectDesc, wObject);
if (d != NULL)
{
k = 150 - gConfig.ScreenLayout.ExtraItemDescLines * 16;
wcscpy(szDesc, d);
d = szDesc;
while (TRUE)
{
next = wcschr(d, '*');
if (next != NULL)
{
*next++ = '\0';
}
PAL_DrawText(d, PAL_XY(75, k), DESCTEXT_COLOR, TRUE, FALSE, FALSE);
k += 16;
if (next == NULL)
{
break;
}
d = next;
}
}
}
}
else
{
if (!g_fNoDesc)
{
wScript = gpGlobals->g.rgObject[wObject].item.wScriptDesc;
line = 0;
while (wScript && gpGlobals->g.lprgScriptEntry[wScript].wOperation != 0)
{
if (gpGlobals->g.lprgScriptEntry[wScript].wOperation == 0xFFFF)
{
int line_incr = (gpGlobals->g.lprgScriptEntry[wScript].rgwOperand[1] != 1) ? 1 : 0;
wScript = PAL_RunAutoScript(wScript, PAL_ITEM_DESC_BOTTOM | line);
line += line_incr;
}
else
{
wScript = PAL_RunAutoScript(wScript, 0);
}
}
}
}
if (g_InputState.dwKeyPress & kKeySearch)
{
if ((gpGlobals->g.rgObject[wObject].item.wFlags & g_wItemFlags) &&
(SHORT)gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].nAmount >
(SHORT)gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].nAmountInUse)
{
if (gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].nAmount > 0)
{
j = (gpGlobals->iCurInvMenuItem < iItemsPerLine * iPageLineOffset) ? (gpGlobals->iCurInvMenuItem / iItemsPerLine) : iPageLineOffset;
k = gpGlobals->iCurInvMenuItem % iItemsPerLine;
PAL_DrawText(PAL_GetWord(wObject), PAL_XY(15 + k * iItemTextWidth, 12 + j * 18), MENUITEM_COLOR_CONFIRMED, FALSE, FALSE, FALSE);
}
return wObject;
}
}
return 0xFFFF;
}
VOID
PAL_ItemSelectMenuInit(
WORD wItemFlags
)
/*++
Purpose:
Initialize the item selection menu.
Parameters:
[IN] wItemFlags - flags for usable item.
Return value:
None.
--*/
{
int i, j;
WORD w;
g_wItemFlags = wItemFlags;
//
// Compress the inventory
//
PAL_CompressInventory();
//
// Count the total number of items in inventory
//
g_iNumInventory = 0;
while (g_iNumInventory < MAX_INVENTORY &&
gpGlobals->rgInventory[g_iNumInventory].wItem != 0)
{
g_iNumInventory++;
}
//
// Also add usable equipped items to the list
//
if ((wItemFlags & kItemFlagUsable) && !gpGlobals->fInBattle)
{
for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
{
w = gpGlobals->rgParty[i].wPlayerRole;
for (j = 0; j < MAX_PLAYER_EQUIPMENTS; j++)
{
if (gpGlobals->g.rgObject[gpGlobals->g.PlayerRoles.rgwEquipment[j][w]].item.wFlags & kItemFlagUsable)
{
if (g_iNumInventory < MAX_INVENTORY)
{
gpGlobals->rgInventory[g_iNumInventory].wItem = gpGlobals->g.PlayerRoles.rgwEquipment[j][w];
gpGlobals->rgInventory[g_iNumInventory].nAmount = 0;
gpGlobals->rgInventory[g_iNumInventory].nAmountInUse = (WORD)-1;
g_iNumInventory++;
}
}
}
}
}
}
WORD
PAL_ItemSelectMenu(
LPITEMCHANGED_CALLBACK lpfnMenuItemChanged,
WORD wItemFlags
)
/*++
Purpose:
Show the item selection menu.
Parameters:
[IN] lpfnMenuItemChanged - Callback function which is called when user
changed the current menu item.
[IN] wItemFlags - flags for usable item.
Return value:
The object ID of the selected item. 0 if cancelled.
--*/
{
int iPrevIndex;
WORD w;
DWORD dwTime;
PAL_ItemSelectMenuInit(wItemFlags);
iPrevIndex = gpGlobals->iCurInvMenuItem;
PAL_ClearKeyState();
if (lpfnMenuItemChanged != NULL)
{
g_fNoDesc = TRUE;
(*lpfnMenuItemChanged)(gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].wItem);
}
dwTime = SDL_GetTicks();
while (TRUE)
{
if (lpfnMenuItemChanged == NULL)
{
PAL_MakeScene();
}
w = PAL_ItemSelectMenuUpdate();
VIDEO_UpdateScreen(NULL);
PAL_ClearKeyState();
PAL_ProcessEvent();
while (!SDL_TICKS_PASSED(SDL_GetTicks(), dwTime))
{
PAL_ProcessEvent();
if (g_InputState.dwKeyPress != 0)
{
break;
}
SDL_Delay(5);
}
dwTime = SDL_GetTicks() + FRAME_TIME;
if (w != 0xFFFF)
{
g_fNoDesc = FALSE;
return w;
}
if (iPrevIndex != gpGlobals->iCurInvMenuItem)
{
if (gpGlobals->iCurInvMenuItem >= 0 && gpGlobals->iCurInvMenuItem < MAX_INVENTORY)
{
if (lpfnMenuItemChanged != NULL)
{
(*lpfnMenuItemChanged)(gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].wItem);
}
}
iPrevIndex = gpGlobals->iCurInvMenuItem;
}
}
assert(FALSE);
return 0; // should not really reach here
}