123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- /* -*- 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/>.
- //
- #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;
- WORD wObject;
- BYTE bColor;
- static BYTE bufImage[2048];
- static WORD wPrevImageIndex = 0xFFFF;
- //
- // Process input
- //
- if (g_InputState.dwKeyPress & kKeyUp)
- {
- gpGlobals->iCurInvMenuItem -= 3;
- }
- else if (g_InputState.dwKeyPress & kKeyDown)
- {
- gpGlobals->iCurInvMenuItem += 3;
- }
- else if (g_InputState.dwKeyPress & kKeyLeft)
- {
- gpGlobals->iCurInvMenuItem--;
- }
- else if (g_InputState.dwKeyPress & kKeyRight)
- {
- gpGlobals->iCurInvMenuItem++;
- }
- else if (g_InputState.dwKeyPress & kKeyPgUp)
- {
- gpGlobals->iCurInvMenuItem -= 3 * 7;
- }
- else if (g_InputState.dwKeyPress & kKeyPgDn)
- {
- gpGlobals->iCurInvMenuItem += 3 * 7;
- }
- else if (g_InputState.dwKeyPress & kKeyMenu)
- {
- return 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;
- }
- //
- // Redraw the box
- //
- PAL_CreateBox(PAL_XY(2, 0), 6, 17, 1, FALSE);
- //
- // Draw the texts in the current page
- //
- i = gpGlobals->iCurInvMenuItem / 3 * 3 - 3 * 4;
- if (i < 0)
- {
- i = 0;
- }
- for (j = 0; j < 7; j++)
- {
- for (k = 0; k < 3; k++)
- {
- wObject = gpGlobals->rgInventory[i].wItem;
- bColor = MENUITEM_COLOR;
- if (i >= MAX_INVENTORY || wObject == 0)
- {
- //
- // End of the list reached
- //
- j = 7;
- 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 * 100, 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(40 + k * 100, 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(96 + k * 100, 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));
- 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));
- }
- //
- // Draw the description of the selected item
- //
- if (!g_fNoDesc && gpGlobals->lpObjectDesc != NULL)
- {
- char szDesc[512], *next;
- const char *d = PAL_GetObjectDesc(gpGlobals->lpObjectDesc, wObject);
- if (d != NULL)
- {
- k = 150;
- strcpy(szDesc, d);
- d = szDesc;
- while (TRUE)
- {
- next = strchr(d, '*');
- if (next != NULL)
- {
- *next = '\0';
- next++;
- }
- PAL_DrawText(d, PAL_XY(75, k), DESCTEXT_COLOR, TRUE, FALSE);
- k += 16;
- if (next == NULL)
- {
- break;
- }
- d = next;
- }
- }
- }
- 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 < 3 * 4) ? (gpGlobals->iCurInvMenuItem / 3) : 4;
- k = gpGlobals->iCurInvMenuItem % 3;
- PAL_DrawText(PAL_GetWord(wObject), PAL_XY(15 + k * 100, 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_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
- }
|