itemmenu.c 10 KB

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