itemmenu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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->dwExtraDescLines;
  54. const int iBoxHeightOffset = gpGlobals->dwExtraDescLines * 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. #else
  59. const int iItemsPerLine = 3;
  60. const int iItemTextWidth = 100;
  61. const int iLinesPerPage = 7;
  62. const int iBoxHeightOffset = 0;
  63. const int iCursorXOffset = 25;
  64. const int iAmountXOffset = 81;
  65. const int iPageLineOffset = 4;
  66. #endif
  67. //
  68. // Process input
  69. //
  70. if (g_InputState.dwKeyPress & kKeyUp)
  71. {
  72. gpGlobals->iCurInvMenuItem -= iItemsPerLine;
  73. }
  74. else if (g_InputState.dwKeyPress & kKeyDown)
  75. {
  76. gpGlobals->iCurInvMenuItem += iItemsPerLine;
  77. }
  78. else if (g_InputState.dwKeyPress & kKeyLeft)
  79. {
  80. gpGlobals->iCurInvMenuItem--;
  81. }
  82. else if (g_InputState.dwKeyPress & kKeyRight)
  83. {
  84. gpGlobals->iCurInvMenuItem++;
  85. }
  86. else if (g_InputState.dwKeyPress & kKeyPgUp)
  87. {
  88. gpGlobals->iCurInvMenuItem -= iItemsPerLine * iLinesPerPage;
  89. }
  90. else if (g_InputState.dwKeyPress & kKeyPgDn)
  91. {
  92. gpGlobals->iCurInvMenuItem += iItemsPerLine * iLinesPerPage;
  93. }
  94. else if (g_InputState.dwKeyPress & kKeyMenu)
  95. {
  96. return 0;
  97. }
  98. //
  99. // Make sure the current menu item index is in bound
  100. //
  101. if (gpGlobals->iCurInvMenuItem < 0)
  102. {
  103. gpGlobals->iCurInvMenuItem = 0;
  104. }
  105. else if (gpGlobals->iCurInvMenuItem >= g_iNumInventory)
  106. {
  107. gpGlobals->iCurInvMenuItem = g_iNumInventory - 1;
  108. }
  109. //
  110. // Redraw the box
  111. //
  112. PAL_CreateBox(PAL_XY(2, 0), iLinesPerPage - 1, 17, 1, FALSE);
  113. //
  114. // Draw the texts in the current page
  115. //
  116. i = gpGlobals->iCurInvMenuItem / iItemsPerLine * iItemsPerLine - iItemsPerLine * iPageLineOffset;
  117. if (i < 0)
  118. {
  119. i = 0;
  120. }
  121. for (j = 0; j < iLinesPerPage; j++)
  122. {
  123. for (k = 0; k < iItemsPerLine; k++)
  124. {
  125. wObject = gpGlobals->rgInventory[i].wItem;
  126. bColor = MENUITEM_COLOR;
  127. if (i >= MAX_INVENTORY || wObject == 0)
  128. {
  129. //
  130. // End of the list reached
  131. //
  132. j = iLinesPerPage;
  133. break;
  134. }
  135. if (i == gpGlobals->iCurInvMenuItem)
  136. {
  137. if (!(gpGlobals->g.rgObject[wObject].item.wFlags & g_wItemFlags) ||
  138. (SHORT)gpGlobals->rgInventory[i].nAmount <= (SHORT)gpGlobals->rgInventory[i].nAmountInUse)
  139. {
  140. //
  141. // This item is not selectable
  142. //
  143. bColor = MENUITEM_COLOR_SELECTED_INACTIVE;
  144. }
  145. else
  146. {
  147. //
  148. // This item is selectable
  149. //
  150. if (gpGlobals->rgInventory[i].nAmount == 0)
  151. {
  152. bColor = MENUITEM_COLOR_EQUIPPEDITEM;
  153. }
  154. else
  155. {
  156. bColor = MENUITEM_COLOR_SELECTED;
  157. }
  158. }
  159. }
  160. else if (!(gpGlobals->g.rgObject[wObject].item.wFlags & g_wItemFlags) ||
  161. (SHORT)gpGlobals->rgInventory[i].nAmount <= (SHORT)gpGlobals->rgInventory[i].nAmountInUse)
  162. {
  163. //
  164. // This item is not selectable
  165. //
  166. bColor = MENUITEM_COLOR_INACTIVE;
  167. }
  168. else if (gpGlobals->rgInventory[i].nAmount == 0)
  169. {
  170. bColor = MENUITEM_COLOR_EQUIPPEDITEM;
  171. }
  172. //
  173. // Draw the text
  174. //
  175. PAL_DrawText(PAL_GetWord(wObject), PAL_XY(15 + k * iItemTextWidth, 12 + j * 18),
  176. bColor, TRUE, FALSE);
  177. //
  178. // Draw the cursor on the current selected item
  179. //
  180. if (i == gpGlobals->iCurInvMenuItem)
  181. {
  182. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_CURSOR),
  183. gpScreen, PAL_XY(15 + iCursorXOffset + k * iItemTextWidth, 22 + j * 18));
  184. }
  185. //
  186. // Draw the amount of this item
  187. //
  188. if ((SHORT)gpGlobals->rgInventory[i].nAmount - (SHORT)gpGlobals->rgInventory[i].nAmountInUse > 1)
  189. {
  190. PAL_DrawNumber(gpGlobals->rgInventory[i].nAmount - gpGlobals->rgInventory[i].nAmountInUse,
  191. 2, PAL_XY(15 + iAmountXOffset + k * iItemTextWidth, 17 + j * 18), kNumColorCyan, kNumAlignRight);
  192. }
  193. i++;
  194. }
  195. }
  196. //
  197. // Draw the picture of current selected item
  198. //
  199. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_ITEMBOX), gpScreen,
  200. PAL_XY(5, 140));
  201. wObject = gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].wItem;
  202. if (gpGlobals->g.rgObject[wObject].item.wBitmap != wPrevImageIndex)
  203. {
  204. if (PAL_MKFReadChunk(bufImage, 2048,
  205. gpGlobals->g.rgObject[wObject].item.wBitmap, gpGlobals->f.fpBALL) > 0)
  206. {
  207. wPrevImageIndex = gpGlobals->g.rgObject[wObject].item.wBitmap;
  208. }
  209. else
  210. {
  211. wPrevImageIndex = 0xFFFF;
  212. }
  213. }
  214. if (wPrevImageIndex != 0xFFFF)
  215. {
  216. PAL_RLEBlitToSurface(bufImage, gpScreen, PAL_XY(12, 148));
  217. }
  218. //
  219. // Draw the description of the selected item
  220. //
  221. #ifndef PAL_WIN95
  222. if (!g_fNoDesc && gpGlobals->lpObjectDesc != NULL)
  223. {
  224. # ifdef PAL_UNICODE
  225. WCHAR szDesc[512], *next;
  226. const WCHAR *d = PAL_GetObjectDesc(gpGlobals->lpObjectDesc, wObject);
  227. # else
  228. char szDesc[512], *next;
  229. const char *d = PAL_GetObjectDesc(gpGlobals->lpObjectDesc, wObject);
  230. # endif
  231. if (d != NULL)
  232. {
  233. k = 150;
  234. # ifdef PAL_UNICODE
  235. wcscpy(szDesc, d);
  236. # else
  237. strcpy(szDesc, d);
  238. # endif
  239. d = szDesc;
  240. while (TRUE)
  241. {
  242. # ifdef PAL_UNICODE
  243. next = wcschr(d, '*');
  244. # else
  245. next = strchr(d, '*');
  246. # endif
  247. if (next != NULL)
  248. {
  249. *next = '\0';
  250. next++;
  251. }
  252. PAL_DrawText(d, PAL_XY(75, k), DESCTEXT_COLOR, TRUE, FALSE);
  253. k += 16;
  254. if (next == NULL)
  255. {
  256. break;
  257. }
  258. d = next;
  259. }
  260. }
  261. }
  262. #else
  263. if (!g_fNoDesc)
  264. {
  265. wScript = gpGlobals->g.rgObject[wObject].item.wScriptDesc;
  266. line = 0;
  267. while (wScript && gpGlobals->g.lprgScriptEntry[wScript].wOperation != 0)
  268. {
  269. if (gpGlobals->g.lprgScriptEntry[wScript].wOperation == 0xFFFF)
  270. {
  271. #ifdef PAL_UNICODE
  272. int line_incr = (gpGlobals->g.lprgScriptEntry[wScript].rgwOperand[1] != 1) ? 1 : 0;
  273. #endif
  274. wScript = PAL_RunAutoScript(wScript, PAL_ITEM_DESC_BOTTOM | line);
  275. #ifdef PAL_UNICODE
  276. line += line_incr;
  277. #else
  278. line++;
  279. #endif
  280. }
  281. else
  282. {
  283. wScript = PAL_RunAutoScript(wScript, 0);
  284. }
  285. }
  286. }
  287. #endif
  288. if (g_InputState.dwKeyPress & kKeySearch)
  289. {
  290. if ((gpGlobals->g.rgObject[wObject].item.wFlags & g_wItemFlags) &&
  291. (SHORT)gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].nAmount >
  292. (SHORT)gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].nAmountInUse)
  293. {
  294. if (gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].nAmount > 0)
  295. {
  296. j = (gpGlobals->iCurInvMenuItem < iItemsPerLine * iPageLineOffset) ? (gpGlobals->iCurInvMenuItem / iItemsPerLine) : iPageLineOffset;
  297. k = gpGlobals->iCurInvMenuItem % iItemsPerLine;
  298. PAL_DrawText(PAL_GetWord(wObject), PAL_XY(15 + k * iItemTextWidth, 12 + j * 18),
  299. MENUITEM_COLOR_CONFIRMED, FALSE, FALSE);
  300. }
  301. return wObject;
  302. }
  303. }
  304. return 0xFFFF;
  305. }
  306. VOID
  307. PAL_ItemSelectMenuInit(
  308. WORD wItemFlags
  309. )
  310. /*++
  311. Purpose:
  312. Initialize the item selection menu.
  313. Parameters:
  314. [IN] wItemFlags - flags for usable item.
  315. Return value:
  316. None.
  317. --*/
  318. {
  319. int i, j;
  320. WORD w;
  321. g_wItemFlags = wItemFlags;
  322. //
  323. // Compress the inventory
  324. //
  325. PAL_CompressInventory();
  326. //
  327. // Count the total number of items in inventory
  328. //
  329. g_iNumInventory = 0;
  330. while (g_iNumInventory < MAX_INVENTORY &&
  331. gpGlobals->rgInventory[g_iNumInventory].wItem != 0)
  332. {
  333. g_iNumInventory++;
  334. }
  335. //
  336. // Also add usable equipped items to the list
  337. //
  338. if ((wItemFlags & kItemFlagUsable) && !gpGlobals->fInBattle)
  339. {
  340. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  341. {
  342. w = gpGlobals->rgParty[i].wPlayerRole;
  343. for (j = 0; j < MAX_PLAYER_EQUIPMENTS; j++)
  344. {
  345. if (gpGlobals->g.rgObject[gpGlobals->g.PlayerRoles.rgwEquipment[j][w]].item.wFlags & kItemFlagUsable)
  346. {
  347. if (g_iNumInventory < MAX_INVENTORY)
  348. {
  349. gpGlobals->rgInventory[g_iNumInventory].wItem = gpGlobals->g.PlayerRoles.rgwEquipment[j][w];
  350. gpGlobals->rgInventory[g_iNumInventory].nAmount = 0;
  351. gpGlobals->rgInventory[g_iNumInventory].nAmountInUse = (WORD)-1;
  352. g_iNumInventory++;
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. WORD
  360. PAL_ItemSelectMenu(
  361. LPITEMCHANGED_CALLBACK lpfnMenuItemChanged,
  362. WORD wItemFlags
  363. )
  364. /*++
  365. Purpose:
  366. Show the item selection menu.
  367. Parameters:
  368. [IN] lpfnMenuItemChanged - Callback function which is called when user
  369. changed the current menu item.
  370. [IN] wItemFlags - flags for usable item.
  371. Return value:
  372. The object ID of the selected item. 0 if cancelled.
  373. --*/
  374. {
  375. int iPrevIndex;
  376. WORD w;
  377. DWORD dwTime;
  378. PAL_ItemSelectMenuInit(wItemFlags);
  379. iPrevIndex = gpGlobals->iCurInvMenuItem;
  380. PAL_ClearKeyState();
  381. if (lpfnMenuItemChanged != NULL)
  382. {
  383. g_fNoDesc = TRUE;
  384. (*lpfnMenuItemChanged)(gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].wItem);
  385. }
  386. dwTime = SDL_GetTicks();
  387. while (TRUE)
  388. {
  389. if (lpfnMenuItemChanged == NULL)
  390. {
  391. PAL_MakeScene();
  392. }
  393. w = PAL_ItemSelectMenuUpdate();
  394. VIDEO_UpdateScreen(NULL);
  395. PAL_ClearKeyState();
  396. PAL_ProcessEvent();
  397. while (SDL_GetTicks() < dwTime)
  398. {
  399. PAL_ProcessEvent();
  400. if (g_InputState.dwKeyPress != 0)
  401. {
  402. break;
  403. }
  404. SDL_Delay(5);
  405. }
  406. dwTime = SDL_GetTicks() + FRAME_TIME;
  407. if (w != 0xFFFF)
  408. {
  409. g_fNoDesc = FALSE;
  410. return w;
  411. }
  412. if (iPrevIndex != gpGlobals->iCurInvMenuItem)
  413. {
  414. if (gpGlobals->iCurInvMenuItem >= 0 && gpGlobals->iCurInvMenuItem < MAX_INVENTORY)
  415. {
  416. if (lpfnMenuItemChanged != NULL)
  417. {
  418. (*lpfnMenuItemChanged)(gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].wItem);
  419. }
  420. }
  421. iPrevIndex = gpGlobals->iCurInvMenuItem;
  422. }
  423. }
  424. assert(FALSE);
  425. return 0; // should not really reach here
  426. }