itemmenu.c 12 KB

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