itemmenu.c 12 KB

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