itemmenu.c 13 KB

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