itemmenu.c 11 KB

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