itemmenu.c 12 KB

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