itemmenu.c 11 KB

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