itemmenu.c 12 KB

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