ui.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. LPSPRITE gpSpriteUI = NULL;
  25. INT
  26. PAL_InitUI(
  27. VOID
  28. )
  29. /*++
  30. Purpose:
  31. Initialze the UI subsystem.
  32. Parameters:
  33. None.
  34. Return value:
  35. 0 = success, -1 = fail.
  36. --*/
  37. {
  38. int iSize;
  39. //
  40. // Load the UI sprite.
  41. //
  42. iSize = PAL_MKFGetChunkSize(CHUNKNUM_SPRITEUI, gpGlobals->f.fpDATA);
  43. if (iSize < 0)
  44. {
  45. return -1;
  46. }
  47. gpSpriteUI = (LPSPRITE)calloc(1, iSize);
  48. if (gpSpriteUI == NULL)
  49. {
  50. return -1;
  51. }
  52. PAL_MKFReadChunk(gpSpriteUI, iSize, CHUNKNUM_SPRITEUI, gpGlobals->f.fpDATA);
  53. return 0;
  54. }
  55. VOID
  56. PAL_FreeUI(
  57. VOID
  58. )
  59. /*++
  60. Purpose:
  61. Shutdown the UI subsystem.
  62. Parameters:
  63. None.
  64. Return value:
  65. None.
  66. --*/
  67. {
  68. if (gpSpriteUI != NULL)
  69. {
  70. free(gpSpriteUI);
  71. gpSpriteUI = NULL;
  72. }
  73. }
  74. LPBOX
  75. PAL_CreateBox(
  76. PAL_POS pos,
  77. INT nRows,
  78. INT nColumns,
  79. INT iStyle,
  80. BOOL fSaveScreen
  81. )
  82. /*++
  83. Purpose:
  84. Create a box on the screen.
  85. Parameters:
  86. [IN] pos - position of the box.
  87. [IN] nRows - number of rows of the box.
  88. [IN] nColumns - number of columns of the box.
  89. [IN] iStyle - style of the box (0 or 1).
  90. [IN] fSaveScreen - whether save the used screen area or not.
  91. Return value:
  92. Pointer to a BOX structure. NULL if failed.
  93. If fSaveScreen is false, then always returns NULL.
  94. --*/
  95. {
  96. int i, j, x, m, n;
  97. LPCBITMAPRLE rglpBorderBitmap[3][3];
  98. LPBOX lpBox = NULL;
  99. SDL_Surface *save;
  100. SDL_Rect rect;
  101. //
  102. // Get the bitmaps
  103. //
  104. for (i = 0; i < 3; i++)
  105. {
  106. for (j = 0; j < 3; j++)
  107. {
  108. rglpBorderBitmap[i][j] = PAL_SpriteGetFrame(gpSpriteUI, i * 3 + j + iStyle * 9);
  109. }
  110. }
  111. rect.x = PAL_X(pos);
  112. rect.y = PAL_Y(pos);
  113. rect.w = 0;
  114. rect.h = 0;
  115. //
  116. // Get the total width and total height of the box
  117. //
  118. for (i = 0; i < 3; i++)
  119. {
  120. if (i == 1)
  121. {
  122. rect.w += PAL_RLEGetWidth(rglpBorderBitmap[0][i]) * nColumns;
  123. rect.h += PAL_RLEGetHeight(rglpBorderBitmap[i][0]) * nRows;
  124. }
  125. else
  126. {
  127. rect.w += PAL_RLEGetWidth(rglpBorderBitmap[0][i]);
  128. rect.h += PAL_RLEGetHeight(rglpBorderBitmap[i][0]);
  129. }
  130. }
  131. if (fSaveScreen)
  132. {
  133. //
  134. // Save the used part of the screen
  135. //
  136. save = SDL_CreateRGBSurface(gpScreen->flags, rect.w, rect.h, 8,
  137. gpScreen->format->Rmask, gpScreen->format->Gmask,
  138. gpScreen->format->Bmask, gpScreen->format->Amask);
  139. if (save == NULL)
  140. {
  141. return NULL;
  142. }
  143. lpBox = (LPBOX)calloc(1, sizeof(BOX));
  144. if (lpBox == NULL)
  145. {
  146. SDL_FreeSurface(save);
  147. return NULL;
  148. }
  149. #if SDL_VERSION_ATLEAST(2,0,0)
  150. SDL_SetSurfacePalette(save, gpScreen->format->palette);
  151. #else
  152. SDL_SetPalette(save, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
  153. #endif
  154. SDL_BlitSurface(gpScreen, &rect, save, NULL);
  155. lpBox->lpSavedArea = save;
  156. lpBox->pos = pos;
  157. lpBox->wWidth = rect.w;
  158. lpBox->wHeight = rect.h;
  159. }
  160. //
  161. // Border takes 2 additional rows and columns...
  162. //
  163. nRows += 2;
  164. nColumns += 2;
  165. //
  166. // Draw the box
  167. //
  168. for (i = 0; i < nRows; i++)
  169. {
  170. x = rect.x;
  171. m = (i == 0) ? 0 : ((i == nRows - 1) ? 2 : 1);
  172. for (j = 0; j < nColumns; j++)
  173. {
  174. n = (j == 0) ? 0 : ((j == nColumns - 1) ? 2 : 1);
  175. PAL_RLEBlitToSurface(rglpBorderBitmap[m][n], gpScreen, PAL_XY(x, rect.y));
  176. x += PAL_RLEGetWidth(rglpBorderBitmap[m][n]);
  177. }
  178. rect.y += PAL_RLEGetHeight(rglpBorderBitmap[m][0]);
  179. }
  180. return lpBox;
  181. }
  182. LPBOX
  183. PAL_CreateSingleLineBox(
  184. PAL_POS pos,
  185. INT nLen,
  186. BOOL fSaveScreen
  187. )
  188. /*++
  189. Purpose:
  190. Create a single-line box on the screen.
  191. Parameters:
  192. [IN] pos - position of the box.
  193. [IN] nLen - length of the box.
  194. [IN] fSaveScreen - whether save the used screen area or not.
  195. Return value:
  196. Pointer to a BOX structure. NULL if failed.
  197. If fSaveScreen is false, then always returns NULL.
  198. --*/
  199. {
  200. static const int iNumLeftSprite = 44;
  201. static const int iNumMidSprite = 45;
  202. static const int iNumRightSprite = 46;
  203. LPCBITMAPRLE lpBitmapLeft;
  204. LPCBITMAPRLE lpBitmapMid;
  205. LPCBITMAPRLE lpBitmapRight;
  206. SDL_Surface *save;
  207. SDL_Rect rect;
  208. LPBOX lpBox = NULL;
  209. int i;
  210. //
  211. // Get the bitmaps
  212. //
  213. lpBitmapLeft = PAL_SpriteGetFrame(gpSpriteUI, iNumLeftSprite);
  214. lpBitmapMid = PAL_SpriteGetFrame(gpSpriteUI, iNumMidSprite);
  215. lpBitmapRight = PAL_SpriteGetFrame(gpSpriteUI, iNumRightSprite);
  216. rect.x = PAL_X(pos);
  217. rect.y = PAL_Y(pos);
  218. //
  219. // Get the total width and total height of the box
  220. //
  221. rect.w = PAL_RLEGetWidth(lpBitmapLeft) + PAL_RLEGetWidth(lpBitmapRight);
  222. rect.w += PAL_RLEGetWidth(lpBitmapMid) * nLen;
  223. rect.h = PAL_RLEGetHeight(lpBitmapLeft);
  224. if (fSaveScreen)
  225. {
  226. //
  227. // Save the used part of the screen
  228. //
  229. save = SDL_CreateRGBSurface(gpScreen->flags, rect.w, rect.h, 8,
  230. gpScreen->format->Rmask, gpScreen->format->Gmask,
  231. gpScreen->format->Bmask, gpScreen->format->Amask);
  232. if (save == NULL)
  233. {
  234. return NULL;
  235. }
  236. lpBox = (LPBOX)calloc(1, sizeof(BOX));
  237. if (lpBox == NULL)
  238. {
  239. SDL_FreeSurface(gpScreen);
  240. return NULL;
  241. }
  242. #if SDL_VERSION_ATLEAST(2,0,0)
  243. SDL_SetSurfacePalette(save, gpScreen->format->palette);
  244. #else
  245. SDL_SetPalette(save, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
  246. #endif
  247. SDL_BlitSurface(gpScreen, &rect, save, NULL);
  248. lpBox->pos = pos;
  249. lpBox->lpSavedArea = save;
  250. lpBox->wHeight = (WORD)rect.w;
  251. lpBox->wWidth = (WORD)rect.h;
  252. }
  253. //
  254. // Draw the box
  255. //
  256. PAL_RLEBlitToSurface(lpBitmapLeft, gpScreen, pos);
  257. rect.x += PAL_RLEGetWidth(lpBitmapLeft);
  258. for (i = 0; i < nLen; i++)
  259. {
  260. PAL_RLEBlitToSurface(lpBitmapMid, gpScreen, PAL_XY(rect.x, rect.y));
  261. rect.x += PAL_RLEGetWidth(lpBitmapMid);
  262. }
  263. PAL_RLEBlitToSurface(lpBitmapRight, gpScreen, PAL_XY(rect.x, rect.y));
  264. return lpBox;
  265. }
  266. VOID
  267. PAL_DeleteBox(
  268. LPBOX lpBox
  269. )
  270. /*++
  271. Purpose:
  272. Delete a box and restore the saved part of the screen.
  273. Parameters:
  274. [IN] lpBox - pointer to the BOX struct.
  275. Return value:
  276. None.
  277. --*/
  278. {
  279. SDL_Rect rect;
  280. //
  281. // Check for NULL pointer.
  282. //
  283. if (lpBox == NULL)
  284. {
  285. return;
  286. }
  287. //
  288. // Restore the saved screen part
  289. //
  290. rect.x = PAL_X(lpBox->pos);
  291. rect.y = PAL_Y(lpBox->pos);
  292. rect.w = lpBox->wWidth;
  293. rect.h = lpBox->wHeight;
  294. SDL_BlitSurface(lpBox->lpSavedArea, NULL, gpScreen, &rect);
  295. //
  296. // Free the memory used by the box
  297. //
  298. SDL_FreeSurface(lpBox->lpSavedArea);
  299. free(lpBox);
  300. }
  301. WORD
  302. PAL_ReadMenu(
  303. LPITEMCHANGED_CALLBACK lpfnMenuItemChanged,
  304. LPCMENUITEM rgMenuItem,
  305. INT nMenuItem,
  306. WORD wDefaultItem,
  307. BYTE bLabelColor
  308. )
  309. /*++
  310. Purpose:
  311. Execute a menu.
  312. Parameters:
  313. [IN] lpfnMenuItemChanged - Callback function which is called when user
  314. changed the current menu item.
  315. [IN] rgMenuItem - Array of the menu items.
  316. [IN] nMenuItem - Number of menu items.
  317. [IN] wDefaultItem - default item index.
  318. [IN] bLabelColor - color of the labels.
  319. Return value:
  320. Return value of the selected menu item. MENUITEM_VALUE_CANCELLED if cancelled.
  321. --*/
  322. {
  323. int i;
  324. WORD wCurrentItem = (wDefaultItem < nMenuItem) ? wDefaultItem : 0;
  325. //
  326. // Draw all the menu texts.
  327. //
  328. for (i = 0; i < nMenuItem; i++)
  329. {
  330. BYTE bColor = bLabelColor;
  331. if (!rgMenuItem[i].fEnabled)
  332. {
  333. if (i == wCurrentItem)
  334. {
  335. bColor = MENUITEM_COLOR_SELECTED_INACTIVE;
  336. }
  337. else
  338. {
  339. bColor = MENUITEM_COLOR_INACTIVE;
  340. }
  341. }
  342. PAL_DrawText(PAL_GetWord(rgMenuItem[i].wNumWord), rgMenuItem[i].pos, bColor, TRUE, TRUE, FALSE);
  343. }
  344. if (lpfnMenuItemChanged != NULL)
  345. {
  346. (*lpfnMenuItemChanged)(rgMenuItem[wDefaultItem].wValue);
  347. }
  348. while (TRUE)
  349. {
  350. PAL_ClearKeyState();
  351. //
  352. // Redraw the selected item if needed.
  353. //
  354. if (rgMenuItem[wCurrentItem].fEnabled)
  355. {
  356. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  357. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED, FALSE, TRUE, FALSE);
  358. }
  359. PAL_ProcessEvent();
  360. if (g_InputState.dwKeyPress & (kKeyDown | kKeyRight))
  361. {
  362. //
  363. // User pressed the down or right arrow key
  364. //
  365. if (rgMenuItem[wCurrentItem].fEnabled)
  366. {
  367. //
  368. // Dehighlight the unselected item.
  369. //
  370. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  371. rgMenuItem[wCurrentItem].pos, bLabelColor, FALSE, TRUE, FALSE);
  372. }
  373. else
  374. {
  375. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  376. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_INACTIVE, FALSE, TRUE, FALSE);
  377. }
  378. wCurrentItem++;
  379. if (wCurrentItem >= nMenuItem)
  380. {
  381. wCurrentItem = 0;
  382. }
  383. //
  384. // Highlight the selected item.
  385. //
  386. if (rgMenuItem[wCurrentItem].fEnabled)
  387. {
  388. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  389. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED, FALSE, TRUE, FALSE);
  390. }
  391. else
  392. {
  393. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  394. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED_INACTIVE, FALSE, TRUE, FALSE);
  395. }
  396. if (lpfnMenuItemChanged != NULL)
  397. {
  398. (*lpfnMenuItemChanged)(rgMenuItem[wCurrentItem].wValue);
  399. }
  400. }
  401. else if (g_InputState.dwKeyPress & (kKeyUp | kKeyLeft))
  402. {
  403. //
  404. // User pressed the up or left arrow key
  405. //
  406. if (rgMenuItem[wCurrentItem].fEnabled)
  407. {
  408. //
  409. // Dehighlight the unselected item.
  410. //
  411. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  412. rgMenuItem[wCurrentItem].pos, bLabelColor, FALSE, TRUE, FALSE);
  413. }
  414. else
  415. {
  416. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  417. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_INACTIVE, FALSE, TRUE, FALSE);
  418. }
  419. if (wCurrentItem > 0)
  420. {
  421. wCurrentItem--;
  422. }
  423. else
  424. {
  425. wCurrentItem = nMenuItem - 1;
  426. }
  427. //
  428. // Highlight the selected item.
  429. //
  430. if (rgMenuItem[wCurrentItem].fEnabled)
  431. {
  432. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  433. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED, FALSE, TRUE, FALSE);
  434. }
  435. else
  436. {
  437. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  438. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_SELECTED_INACTIVE, FALSE, TRUE, FALSE);
  439. }
  440. if (lpfnMenuItemChanged != NULL)
  441. {
  442. (*lpfnMenuItemChanged)(rgMenuItem[wCurrentItem].wValue);
  443. }
  444. }
  445. else if (g_InputState.dwKeyPress & kKeyMenu)
  446. {
  447. //
  448. // User cancelled
  449. //
  450. if (rgMenuItem[wCurrentItem].fEnabled)
  451. {
  452. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  453. rgMenuItem[wCurrentItem].pos, bLabelColor, FALSE, TRUE, FALSE);
  454. }
  455. else
  456. {
  457. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  458. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_INACTIVE, FALSE, TRUE, FALSE);
  459. }
  460. break;
  461. }
  462. else if (g_InputState.dwKeyPress & kKeySearch)
  463. {
  464. //
  465. // User pressed Enter
  466. //
  467. if (rgMenuItem[wCurrentItem].fEnabled)
  468. {
  469. PAL_DrawText(PAL_GetWord(rgMenuItem[wCurrentItem].wNumWord),
  470. rgMenuItem[wCurrentItem].pos, MENUITEM_COLOR_CONFIRMED, FALSE, TRUE, FALSE);
  471. return rgMenuItem[wCurrentItem].wValue;
  472. }
  473. }
  474. //
  475. // Use delay function to avoid high CPU usage.
  476. //
  477. SDL_Delay(50);
  478. }
  479. return MENUITEM_VALUE_CANCELLED;
  480. }
  481. VOID
  482. PAL_DrawNumber(
  483. UINT iNum,
  484. UINT nLength,
  485. PAL_POS pos,
  486. NUMCOLOR color,
  487. NUMALIGN align
  488. )
  489. /*++
  490. Purpose:
  491. Draw the specified number with the bitmaps in the UI sprite.
  492. Parameters:
  493. [IN] iNum - the number to be drawn.
  494. [IN] nLength - max. length of the number.
  495. [IN] pos - position on the screen.
  496. [IN] color - color of the number (yellow or blue).
  497. [IN] align - align mode of the number.
  498. Return value:
  499. None.
  500. --*/
  501. {
  502. UINT nActualLength, i;
  503. int x, y;
  504. LPCBITMAPRLE rglpBitmap[10];
  505. //
  506. // Get the bitmaps. Blue starts from 29, Cyan from 56, Yellow from 19.
  507. //
  508. x = (color == kNumColorBlue) ? 29 : ((color == kNumColorCyan) ? 56 : 19);
  509. for (i = 0; i < 10; i++)
  510. {
  511. rglpBitmap[i] = PAL_SpriteGetFrame(gpSpriteUI, (UINT)x + i);
  512. }
  513. i = iNum;
  514. nActualLength = 0;
  515. //
  516. // Calculate the actual length of the number.
  517. //
  518. while (i > 0)
  519. {
  520. i /= 10;
  521. nActualLength++;
  522. }
  523. if (nActualLength > nLength)
  524. {
  525. nActualLength = nLength;
  526. }
  527. else if (nActualLength == 0)
  528. {
  529. nActualLength = 1;
  530. }
  531. x = PAL_X(pos) - 6;
  532. y = PAL_Y(pos);
  533. switch (align)
  534. {
  535. case kNumAlignLeft:
  536. x += 6 * nActualLength;
  537. break;
  538. case kNumAlignMid:
  539. x += 3 * (nLength + nActualLength);
  540. break;
  541. case kNumAlignRight:
  542. x += 6 * nLength;
  543. break;
  544. }
  545. //
  546. // Draw the number.
  547. //
  548. while (nActualLength-- > 0)
  549. {
  550. PAL_RLEBlitToSurface(rglpBitmap[iNum % 10], gpScreen, PAL_XY(x, y));
  551. x -= 6;
  552. iNum /= 10;
  553. }
  554. }
  555. INT
  556. PAL_MenuTextMaxWidth(
  557. LPCMENUITEM rgMenuItem,
  558. INT nMenuItem
  559. )
  560. /*++
  561. Purpose:
  562. Calculate the maximal text width of all the menu items in number of full width characters.
  563. Parameters:
  564. [IN] rgMenuItem - Pointer to the menu item array.
  565. [IN] nMenuItem - Number of menu items.
  566. Return value:
  567. Maximal text width.
  568. --*/
  569. {
  570. int i, r = 0;
  571. for (i = 0; i < nMenuItem; i++)
  572. {
  573. LPCWSTR itemText = PAL_GetWord(rgMenuItem[i].wNumWord);
  574. int j = 0, l = wcslen(itemText), w = 0;
  575. for (j = 0; j < l; j++)
  576. {
  577. w += PAL_CharWidth(itemText[j]);
  578. }
  579. w = (w + 8) >> 4;
  580. if (r < w)
  581. {
  582. r = w;
  583. }
  584. }
  585. return r;
  586. }