itemmenu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // This file is part of SDLPAL.
  7. //
  8. // SDLPAL is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. // Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015
  22. //
  23. #include "main.h"
  24. static int g_iNumInventory = 0;
  25. static WORD g_wItemFlags = 0;
  26. static BOOL g_fNoDesc = FALSE;
  27. WORD
  28. PAL_ItemSelectMenuUpdate(
  29. VOID
  30. )
  31. /*++
  32. Purpose:
  33. Initialize the item selection menu.
  34. Parameters:
  35. None.
  36. Return value:
  37. The object ID of the selected item. 0 if cancelled, 0xFFFF if not confirmed.
  38. --*/
  39. {
  40. #ifndef PAL_WIN95
  41. int i, j, k;
  42. WORD wObject;
  43. #else
  44. int i, j, k, line;
  45. WORD wObject, wScript;
  46. #endif
  47. BYTE bColor;
  48. static BYTE bufImage[2048];
  49. static WORD wPrevImageIndex = 0xFFFF;
  50. #ifdef PAL_UNICODE
  51. const int iItemsPerLine = 34 / gpGlobals->dwWordLength;
  52. const int iItemTextWidth = 8 * gpGlobals->dwWordLength + 20;
  53. const int iLinesPerPage = 7 - gpGlobals->dwExtraItemDescLines;
  54. const int iBoxHeightOffset = gpGlobals->dwExtraItemDescLines * 16;
  55. const int iCursorXOffset = gpGlobals->dwWordLength * 5 / 2;
  56. const int iAmountXOffset = gpGlobals->dwWordLength * 8 + 1;
  57. const int iPageLineOffset = (iLinesPerPage + 1) / 2;
  58. const int iPictureYOffset = (gpGlobals->dwExtraItemDescLines > 1) ? (gpGlobals->dwExtraItemDescLines - 1) * 16 : 0;
  59. #else
  60. const int iItemsPerLine = 3;
  61. const int iItemTextWidth = 100;
  62. const int iLinesPerPage = 7;
  63. const int iBoxHeightOffset = 0;
  64. const int iCursorXOffset = 25;
  65. const int iAmountXOffset = 81;
  66. const int iPageLineOffset = 4;
  67. const int iPictureYOffset = 0;
  68. #endif
  69. //
  70. // Process input
  71. //
  72. if (g_InputState.dwKeyPress & kKeyUp)
  73. {
  74. gpGlobals->iCurInvMenuItem -= iItemsPerLine;
  75. }
  76. else if (g_InputState.dwKeyPress & kKeyDown)
  77. {
  78. gpGlobals->iCurInvMenuItem += iItemsPerLine;
  79. }
  80. else if (g_InputState.dwKeyPress & kKeyLeft)
  81. {
  82. gpGlobals->iCurInvMenuItem--;
  83. }
  84. else if (g_InputState.dwKeyPress & kKeyRight)
  85. {
  86. gpGlobals->iCurInvMenuItem++;
  87. }
  88. else if (g_InputState.dwKeyPress & kKeyPgUp)
  89. {
  90. gpGlobals->iCurInvMenuItem -= iItemsPerLine * iLinesPerPage;
  91. }
  92. else if (g_InputState.dwKeyPress & kKeyPgDn)
  93. {
  94. gpGlobals->iCurInvMenuItem += iItemsPerLine * iLinesPerPage;
  95. }
  96. else if (g_InputState.dwKeyPress & kKeyMenu)
  97. {
  98. return 0;
  99. }
  100. //
  101. // Make sure the current menu item index is in bound
  102. //
  103. if (gpGlobals->iCurInvMenuItem < 0)
  104. {
  105. gpGlobals->iCurInvMenuItem = 0;
  106. }
  107. else if (gpGlobals->iCurInvMenuItem >= g_iNumInventory)
  108. {
  109. gpGlobals->iCurInvMenuItem = g_iNumInventory - 1;
  110. }
  111. //
  112. // Redraw the box
  113. //
  114. PAL_CreateBox(PAL_XY(2, 0), iLinesPerPage - 1, 17, 1, FALSE);
  115. //
  116. // Draw the texts in the current page
  117. //
  118. i = gpGlobals->iCurInvMenuItem / iItemsPerLine * iItemsPerLine - iItemsPerLine * iPageLineOffset;
  119. if (i < 0)
  120. {
  121. i = 0;
  122. }
  123. for (j = 0; j < iLinesPerPage; j++)
  124. {
  125. for (k = 0; k < iItemsPerLine; k++)
  126. {
  127. wObject = gpGlobals->rgInventory[i].wItem;
  128. bColor = MENUITEM_COLOR;
  129. if (i >= MAX_INVENTORY || wObject == 0)
  130. {
  131. //
  132. // End of the list reached
  133. //
  134. j = iLinesPerPage;
  135. break;
  136. }
  137. if (i == gpGlobals->iCurInvMenuItem)
  138. {
  139. if (!(gpGlobals->g.rgObject[wObject].item.wFlags & g_wItemFlags) ||
  140. (SHORT)gpGlobals->rgInventory[i].nAmount <= (SHORT)gpGlobals->rgInventory[i].nAmountInUse)
  141. {
  142. //
  143. // This item is not selectable
  144. //
  145. bColor = MENUITEM_COLOR_SELECTED_INACTIVE;
  146. }
  147. else
  148. {
  149. //
  150. // This item is selectable
  151. //
  152. if (gpGlobals->rgInventory[i].nAmount == 0)
  153. {
  154. bColor = MENUITEM_COLOR_EQUIPPEDITEM;
  155. }
  156. else
  157. {
  158. bColor = MENUITEM_COLOR_SELECTED;
  159. }
  160. }
  161. }
  162. else if (!(gpGlobals->g.rgObject[wObject].item.wFlags & g_wItemFlags) ||
  163. (SHORT)gpGlobals->rgInventory[i].nAmount <= (SHORT)gpGlobals->rgInventory[i].nAmountInUse)
  164. {
  165. //
  166. // This item is not selectable
  167. //
  168. bColor = MENUITEM_COLOR_INACTIVE;
  169. }
  170. else if (gpGlobals->rgInventory[i].nAmount == 0)
  171. {
  172. bColor = MENUITEM_COLOR_EQUIPPEDITEM;
  173. }
  174. //
  175. // Draw the text
  176. //
  177. PAL_DrawText(PAL_GetWord(wObject), PAL_XY(15 + k * iItemTextWidth, 12 + j * 18),
  178. bColor, TRUE, FALSE);
  179. //
  180. // Draw the cursor on the current selected item
  181. //
  182. if (i == gpGlobals->iCurInvMenuItem)
  183. {
  184. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_CURSOR),
  185. gpScreen, PAL_XY(15 + iCursorXOffset + k * iItemTextWidth, 22 + j * 18));
  186. }
  187. //
  188. // Draw the amount of this item
  189. //
  190. if ((SHORT)gpGlobals->rgInventory[i].nAmount - (SHORT)gpGlobals->rgInventory[i].nAmountInUse > 1)
  191. {
  192. PAL_DrawNumber(gpGlobals->rgInventory[i].nAmount - gpGlobals->rgInventory[i].nAmountInUse,
  193. 2, PAL_XY(15 + iAmountXOffset + k * iItemTextWidth, 17 + j * 18), kNumColorCyan, kNumAlignRight);
  194. }
  195. i++;
  196. }
  197. }
  198. //
  199. // Draw the picture of current selected item
  200. //
  201. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_ITEMBOX), gpScreen,
  202. PAL_XY(5, 140 - iPictureYOffset));
  203. wObject = gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].wItem;
  204. if (gpGlobals->g.rgObject[wObject].item.wBitmap != wPrevImageIndex)
  205. {
  206. if (PAL_MKFReadChunk(bufImage, 2048,
  207. gpGlobals->g.rgObject[wObject].item.wBitmap, gpGlobals->f.fpBALL) > 0)
  208. {
  209. wPrevImageIndex = gpGlobals->g.rgObject[wObject].item.wBitmap;
  210. }
  211. else
  212. {
  213. wPrevImageIndex = 0xFFFF;
  214. }
  215. }
  216. if (wPrevImageIndex != 0xFFFF)
  217. {
  218. PAL_RLEBlitToSurface(bufImage, gpScreen, PAL_XY(12, 148 - iPictureYOffset));
  219. }
  220. //
  221. // Draw the description of the selected item
  222. //
  223. #ifndef PAL_WIN95
  224. if (!g_fNoDesc && gpGlobals->lpObjectDesc != NULL)
  225. {
  226. # ifdef PAL_UNICODE
  227. WCHAR szDesc[512], *next;
  228. const WCHAR *d = PAL_GetObjectDesc(gpGlobals->lpObjectDesc, wObject);
  229. # else
  230. char szDesc[512], *next;
  231. const char *d = PAL_GetObjectDesc(gpGlobals->lpObjectDesc, wObject);
  232. # endif
  233. if (d != NULL)
  234. {
  235. k = 150;
  236. # ifdef PAL_UNICODE
  237. wcscpy(szDesc, d);
  238. # else
  239. strcpy(szDesc, d);
  240. # endif
  241. d = szDesc;
  242. while (TRUE)
  243. {
  244. # ifdef PAL_UNICODE
  245. next = wcschr(d, '*');
  246. # else
  247. next = strchr(d, '*');
  248. # endif
  249. if (next != NULL)
  250. {
  251. *next = '\0';
  252. next++;
  253. }
  254. PAL_DrawText(d, PAL_XY(75, k), DESCTEXT_COLOR, TRUE, FALSE);
  255. k += 16;
  256. if (next == NULL)
  257. {
  258. break;
  259. }
  260. d = next;
  261. }
  262. }
  263. }
  264. #else
  265. if (!g_fNoDesc)
  266. {
  267. wScript = gpGlobals->g.rgObject[wObject].item.wScriptDesc;
  268. line = 0;
  269. while (wScript && gpGlobals->g.lprgScriptEntry[wScript].wOperation != 0)
  270. {
  271. if (gpGlobals->g.lprgScriptEntry[wScript].wOperation == 0xFFFF)
  272. {
  273. #ifdef PAL_UNICODE
  274. int line_incr = (gpGlobals->g.lprgScriptEntry[wScript].rgwOperand[1] != 1) ? 1 : 0;
  275. #endif
  276. wScript = PAL_RunAutoScript(wScript, PAL_ITEM_DESC_BOTTOM | line);
  277. #ifdef PAL_UNICODE
  278. line += line_incr;
  279. #else
  280. line++;
  281. #endif
  282. }
  283. else
  284. {
  285. wScript = PAL_RunAutoScript(wScript, 0);
  286. }
  287. }
  288. }
  289. #endif
  290. if (g_InputState.dwKeyPress & kKeySearch)
  291. {
  292. if ((gpGlobals->g.rgObject[wObject].item.wFlags & g_wItemFlags) &&
  293. (SHORT)gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].nAmount >
  294. (SHORT)gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].nAmountInUse)
  295. {
  296. if (gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].nAmount > 0)
  297. {
  298. j = (gpGlobals->iCurInvMenuItem < iItemsPerLine * iPageLineOffset) ? (gpGlobals->iCurInvMenuItem / iItemsPerLine) : iPageLineOffset;
  299. k = gpGlobals->iCurInvMenuItem % iItemsPerLine;
  300. PAL_DrawText(PAL_GetWord(wObject), PAL_XY(15 + k * iItemTextWidth, 12 + j * 18),
  301. MENUITEM_COLOR_CONFIRMED, FALSE, FALSE);
  302. }
  303. return wObject;
  304. }
  305. }
  306. return 0xFFFF;
  307. }
  308. VOID
  309. PAL_ItemSelectMenuInit(
  310. WORD wItemFlags
  311. )
  312. /*++
  313. Purpose:
  314. Initialize the item selection menu.
  315. Parameters:
  316. [IN] wItemFlags - flags for usable item.
  317. Return value:
  318. None.
  319. --*/
  320. {
  321. int i, j;
  322. WORD w;
  323. g_wItemFlags = wItemFlags;
  324. //
  325. // Compress the inventory
  326. //
  327. PAL_CompressInventory();
  328. //
  329. // Count the total number of items in inventory
  330. //
  331. g_iNumInventory = 0;
  332. while (g_iNumInventory < MAX_INVENTORY &&
  333. gpGlobals->rgInventory[g_iNumInventory].wItem != 0)
  334. {
  335. g_iNumInventory++;
  336. }
  337. //
  338. // Also add usable equipped items to the list
  339. //
  340. if ((wItemFlags & kItemFlagUsable) && !gpGlobals->fInBattle)
  341. {
  342. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  343. {
  344. w = gpGlobals->rgParty[i].wPlayerRole;
  345. for (j = 0; j < MAX_PLAYER_EQUIPMENTS; j++)
  346. {
  347. if (gpGlobals->g.rgObject[gpGlobals->g.PlayerRoles.rgwEquipment[j][w]].item.wFlags & kItemFlagUsable)
  348. {
  349. if (g_iNumInventory < MAX_INVENTORY)
  350. {
  351. gpGlobals->rgInventory[g_iNumInventory].wItem = gpGlobals->g.PlayerRoles.rgwEquipment[j][w];
  352. gpGlobals->rgInventory[g_iNumInventory].nAmount = 0;
  353. gpGlobals->rgInventory[g_iNumInventory].nAmountInUse = (WORD)-1;
  354. g_iNumInventory++;
  355. }
  356. }
  357. }
  358. }
  359. }
  360. }
  361. WORD
  362. PAL_ItemSelectMenu(
  363. LPITEMCHANGED_CALLBACK lpfnMenuItemChanged,
  364. WORD wItemFlags
  365. )
  366. /*++
  367. Purpose:
  368. Show the item selection menu.
  369. Parameters:
  370. [IN] lpfnMenuItemChanged - Callback function which is called when user
  371. changed the current menu item.
  372. [IN] wItemFlags - flags for usable item.
  373. Return value:
  374. The object ID of the selected item. 0 if cancelled.
  375. --*/
  376. {
  377. int iPrevIndex;
  378. WORD w;
  379. DWORD dwTime;
  380. PAL_ItemSelectMenuInit(wItemFlags);
  381. iPrevIndex = gpGlobals->iCurInvMenuItem;
  382. PAL_ClearKeyState();
  383. if (lpfnMenuItemChanged != NULL)
  384. {
  385. g_fNoDesc = TRUE;
  386. (*lpfnMenuItemChanged)(gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].wItem);
  387. }
  388. dwTime = SDL_GetTicks();
  389. while (TRUE)
  390. {
  391. if (lpfnMenuItemChanged == NULL)
  392. {
  393. PAL_MakeScene();
  394. }
  395. w = PAL_ItemSelectMenuUpdate();
  396. VIDEO_UpdateScreen(NULL);
  397. PAL_ClearKeyState();
  398. PAL_ProcessEvent();
  399. while (SDL_GetTicks() < dwTime)
  400. {
  401. PAL_ProcessEvent();
  402. if (g_InputState.dwKeyPress != 0)
  403. {
  404. break;
  405. }
  406. SDL_Delay(5);
  407. }
  408. dwTime = SDL_GetTicks() + FRAME_TIME;
  409. if (w != 0xFFFF)
  410. {
  411. g_fNoDesc = FALSE;
  412. return w;
  413. }
  414. if (iPrevIndex != gpGlobals->iCurInvMenuItem)
  415. {
  416. if (gpGlobals->iCurInvMenuItem >= 0 && gpGlobals->iCurInvMenuItem < MAX_INVENTORY)
  417. {
  418. if (lpfnMenuItemChanged != NULL)
  419. {
  420. (*lpfnMenuItemChanged)(gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].wItem);
  421. }
  422. }
  423. iPrevIndex = gpGlobals->iCurInvMenuItem;
  424. }
  425. }
  426. assert(FALSE);
  427. return 0; // should not really reach here
  428. }