itemmenu.c 10 KB

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