itemmenu.c 12 KB

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