123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
- //
- // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
- // All rights reserved.
- //
- // This file is part of SDLPAL.
- //
- // SDLPAL is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- // Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015
- //
- #include "main.h"
- static int g_iNumInventory = 0;
- static WORD g_wItemFlags = 0;
- static BOOL g_fNoDesc = FALSE;
- WORD
- PAL_ItemSelectMenuUpdate(
- VOID
- )
- /*++
- Purpose:
- Initialize the item selection menu.
- Parameters:
- None.
- Return value:
- The object ID of the selected item. 0 if cancelled, 0xFFFF if not confirmed.
- --*/
- {
- int i, j, k, line, item_delta;
- WORD wObject, wScript;
- BYTE bColor;
- static BYTE bufImage[2048];
- static WORD wPrevImageIndex = 0xFFFF;
- const int iItemsPerLine = 34 / gpGlobals->dwWordLength;
- const int iItemTextWidth = 8 * gpGlobals->dwWordLength + 20;
- const int iLinesPerPage = 7 - gpGlobals->dwExtraItemDescLines;
- const int iCursorXOffset = gpGlobals->dwWordLength * 5 / 2;
- const int iAmountXOffset = gpGlobals->dwWordLength * 8 + 1;
- const int iPageLineOffset = (iLinesPerPage + 1) / 2;
- const int iPictureYOffset = (gpGlobals->dwExtraItemDescLines > 1) ? (gpGlobals->dwExtraItemDescLines - 1) * 16 : 0;
- //
- // Process input
- //
- if (g_InputState.dwKeyPress & kKeyUp)
- {
- item_delta = -iItemsPerLine;
- }
- else if (g_InputState.dwKeyPress & kKeyDown)
- {
- item_delta = iItemsPerLine;
- }
- else if (g_InputState.dwKeyPress & kKeyLeft)
- {
- item_delta = -1;
- }
- else if (g_InputState.dwKeyPress & kKeyRight)
- {
- item_delta = 1;
- }
- else if (g_InputState.dwKeyPress & kKeyPgUp)
- {
- item_delta = -(iItemsPerLine * iLinesPerPage);
- }
- else if (g_InputState.dwKeyPress & kKeyPgDn)
- {
- item_delta = iItemsPerLine * iLinesPerPage;
- }
- else if (g_InputState.dwKeyPress & kKeyMenu)
- {
- return 0;
- }
- else
- {
- item_delta = 0;
- }
- //
- // Make sure the current menu item index is in bound
- //
- //if (gpGlobals->iCurInvMenuItem < 0)
- //{
- // gpGlobals->iCurInvMenuItem = 0;
- //}
- //else if (gpGlobals->iCurInvMenuItem >= g_iNumInventory)
- //{
- // gpGlobals->iCurInvMenuItem = g_iNumInventory - 1;
- //}
- if (gpGlobals->iCurInvMenuItem + item_delta >= 0 && gpGlobals->iCurInvMenuItem + item_delta < g_iNumInventory)
- gpGlobals->iCurInvMenuItem += item_delta;
- //
- // Redraw the box
- //
- PAL_CreateBox(PAL_XY(2, 0), iLinesPerPage - 1, 17, 1, FALSE);
- //
- // Draw the texts in the current page
- //
- i = gpGlobals->iCurInvMenuItem / iItemsPerLine * iItemsPerLine - iItemsPerLine * iPageLineOffset;
- if (i < 0)
- {
- i = 0;
- }
- for (j = 0; j < iLinesPerPage; j++)
- {
- for (k = 0; k < iItemsPerLine; k++)
- {
- wObject = gpGlobals->rgInventory[i].wItem;
- bColor = MENUITEM_COLOR;
- if (i >= MAX_INVENTORY || wObject == 0)
- {
- //
- // End of the list reached
- //
- j = iLinesPerPage;
- break;
- }
- if (i == gpGlobals->iCurInvMenuItem)
- {
- if (!(gpGlobals->g.rgObject[wObject].item.wFlags & g_wItemFlags) ||
- (SHORT)gpGlobals->rgInventory[i].nAmount <= (SHORT)gpGlobals->rgInventory[i].nAmountInUse)
- {
- //
- // This item is not selectable
- //
- bColor = MENUITEM_COLOR_SELECTED_INACTIVE;
- }
- else
- {
- //
- // This item is selectable
- //
- if (gpGlobals->rgInventory[i].nAmount == 0)
- {
- bColor = MENUITEM_COLOR_EQUIPPEDITEM;
- }
- else
- {
- bColor = MENUITEM_COLOR_SELECTED;
- }
- }
- }
- else if (!(gpGlobals->g.rgObject[wObject].item.wFlags & g_wItemFlags) ||
- (SHORT)gpGlobals->rgInventory[i].nAmount <= (SHORT)gpGlobals->rgInventory[i].nAmountInUse)
- {
- //
- // This item is not selectable
- //
- bColor = MENUITEM_COLOR_INACTIVE;
- }
- else if (gpGlobals->rgInventory[i].nAmount == 0)
- {
- bColor = MENUITEM_COLOR_EQUIPPEDITEM;
- }
- //
- // Draw the text
- //
- PAL_DrawText(PAL_GetWord(wObject), PAL_XY(15 + k * iItemTextWidth, 12 + j * 18),
- bColor, TRUE, FALSE);
- //
- // Draw the cursor on the current selected item
- //
- if (i == gpGlobals->iCurInvMenuItem)
- {
- PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_CURSOR),
- gpScreen, PAL_XY(15 + iCursorXOffset + k * iItemTextWidth, 22 + j * 18));
- }
- //
- // Draw the amount of this item
- //
- if ((SHORT)gpGlobals->rgInventory[i].nAmount - (SHORT)gpGlobals->rgInventory[i].nAmountInUse > 1)
- {
- PAL_DrawNumber(gpGlobals->rgInventory[i].nAmount - gpGlobals->rgInventory[i].nAmountInUse,
- 2, PAL_XY(15 + iAmountXOffset + k * iItemTextWidth, 17 + j * 18), kNumColorCyan, kNumAlignRight);
- }
- i++;
- }
- }
- //
- // Draw the picture of current selected item
- //
- PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_ITEMBOX), gpScreen,
- PAL_XY(5, 140 - iPictureYOffset));
- wObject = gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].wItem;
- if (gpGlobals->g.rgObject[wObject].item.wBitmap != wPrevImageIndex)
- {
- if (PAL_MKFReadChunk(bufImage, 2048,
- gpGlobals->g.rgObject[wObject].item.wBitmap, gpGlobals->f.fpBALL) > 0)
- {
- wPrevImageIndex = gpGlobals->g.rgObject[wObject].item.wBitmap;
- }
- else
- {
- wPrevImageIndex = 0xFFFF;
- }
- }
- if (wPrevImageIndex != 0xFFFF)
- {
- PAL_RLEBlitToSurface(bufImage, gpScreen, PAL_XY(12, 148 - iPictureYOffset));
- }
- //
- // Draw the description of the selected item
- //
- if (!gpGlobals->fIsWIN95)
- {
- if (!g_fNoDesc && gpGlobals->lpObjectDesc != NULL)
- {
- WCHAR szDesc[512], *next;
- const WCHAR *d = PAL_GetObjectDesc(gpGlobals->lpObjectDesc, wObject);
- if (d != NULL)
- {
- k = 150;
- wcscpy(szDesc, d);
- d = szDesc;
- while (TRUE)
- {
- next = wcschr(d, '*');
- if (next != NULL)
- {
- *next++ = '\0';
- }
- PAL_DrawText(d, PAL_XY(75, k), DESCTEXT_COLOR, TRUE, FALSE);
- k += 16;
- if (next == NULL)
- {
- break;
- }
- d = next;
- }
- }
- }
- }
- else
- {
- if (!g_fNoDesc)
- {
- wScript = gpGlobals->g.rgObject[wObject].item.wScriptDesc;
- line = 0;
- while (wScript && gpGlobals->g.lprgScriptEntry[wScript].wOperation != 0)
- {
- if (gpGlobals->g.lprgScriptEntry[wScript].wOperation == 0xFFFF)
- {
- int line_incr = (gpGlobals->g.lprgScriptEntry[wScript].rgwOperand[1] != 1) ? 1 : 0;
- wScript = PAL_RunAutoScript(wScript, PAL_ITEM_DESC_BOTTOM | line);
- line += line_incr;
- }
- else
- {
- wScript = PAL_RunAutoScript(wScript, 0);
- }
- }
- }
- }
- if (g_InputState.dwKeyPress & kKeySearch)
- {
- if ((gpGlobals->g.rgObject[wObject].item.wFlags & g_wItemFlags) &&
- (SHORT)gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].nAmount >
- (SHORT)gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].nAmountInUse)
- {
- if (gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].nAmount > 0)
- {
- j = (gpGlobals->iCurInvMenuItem < iItemsPerLine * iPageLineOffset) ? (gpGlobals->iCurInvMenuItem / iItemsPerLine) : iPageLineOffset;
- k = gpGlobals->iCurInvMenuItem % iItemsPerLine;
- PAL_DrawText(PAL_GetWord(wObject), PAL_XY(15 + k * iItemTextWidth, 12 + j * 18),
- MENUITEM_COLOR_CONFIRMED, FALSE, FALSE);
- }
- return wObject;
- }
- }
- return 0xFFFF;
- }
- VOID
- PAL_ItemSelectMenuInit(
- WORD wItemFlags
- )
- /*++
- Purpose:
- Initialize the item selection menu.
- Parameters:
- [IN] wItemFlags - flags for usable item.
- Return value:
- None.
- --*/
- {
- int i, j;
- WORD w;
- g_wItemFlags = wItemFlags;
- //
- // Compress the inventory
- //
- PAL_CompressInventory();
- //
- // Count the total number of items in inventory
- //
- g_iNumInventory = 0;
- while (g_iNumInventory < MAX_INVENTORY &&
- gpGlobals->rgInventory[g_iNumInventory].wItem != 0)
- {
- g_iNumInventory++;
- }
- //
- // Also add usable equipped items to the list
- //
- if ((wItemFlags & kItemFlagUsable) && !gpGlobals->fInBattle)
- {
- for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
- {
- w = gpGlobals->rgParty[i].wPlayerRole;
- for (j = 0; j < MAX_PLAYER_EQUIPMENTS; j++)
- {
- if (gpGlobals->g.rgObject[gpGlobals->g.PlayerRoles.rgwEquipment[j][w]].item.wFlags & kItemFlagUsable)
- {
- if (g_iNumInventory < MAX_INVENTORY)
- {
- gpGlobals->rgInventory[g_iNumInventory].wItem = gpGlobals->g.PlayerRoles.rgwEquipment[j][w];
- gpGlobals->rgInventory[g_iNumInventory].nAmount = 0;
- gpGlobals->rgInventory[g_iNumInventory].nAmountInUse = (WORD)-1;
- g_iNumInventory++;
- }
- }
- }
- }
- }
- }
- WORD
- PAL_ItemSelectMenu(
- LPITEMCHANGED_CALLBACK lpfnMenuItemChanged,
- WORD wItemFlags
- )
- /*++
- Purpose:
- Show the item selection menu.
- Parameters:
- [IN] lpfnMenuItemChanged - Callback function which is called when user
- changed the current menu item.
- [IN] wItemFlags - flags for usable item.
- Return value:
- The object ID of the selected item. 0 if cancelled.
- --*/
- {
- int iPrevIndex;
- WORD w;
- DWORD dwTime;
- PAL_ItemSelectMenuInit(wItemFlags);
- iPrevIndex = gpGlobals->iCurInvMenuItem;
- PAL_ClearKeyState();
- if (lpfnMenuItemChanged != NULL)
- {
- g_fNoDesc = TRUE;
- (*lpfnMenuItemChanged)(gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].wItem);
- }
- dwTime = SDL_GetTicks();
- while (TRUE)
- {
- if (lpfnMenuItemChanged == NULL)
- {
- PAL_MakeScene();
- }
- w = PAL_ItemSelectMenuUpdate();
- VIDEO_UpdateScreen(NULL);
- PAL_ClearKeyState();
- PAL_ProcessEvent();
- while (!SDL_TICKS_PASSED(SDL_GetTicks(), dwTime))
- {
- PAL_ProcessEvent();
- if (g_InputState.dwKeyPress != 0)
- {
- break;
- }
- SDL_Delay(5);
- }
- dwTime = SDL_GetTicks() + FRAME_TIME;
- if (w != 0xFFFF)
- {
- g_fNoDesc = FALSE;
- return w;
- }
- if (iPrevIndex != gpGlobals->iCurInvMenuItem)
- {
- if (gpGlobals->iCurInvMenuItem >= 0 && gpGlobals->iCurInvMenuItem < MAX_INVENTORY)
- {
- if (lpfnMenuItemChanged != NULL)
- {
- (*lpfnMenuItemChanged)(gpGlobals->rgInventory[gpGlobals->iCurInvMenuItem].wItem);
- }
- }
- iPrevIndex = gpGlobals->iCurInvMenuItem;
- }
- }
- assert(FALSE);
- return 0; // should not really reach here
- }
|