diff options
Diffstat (limited to 'engines/kyra/gui_v2.cpp')
-rw-r--r-- | engines/kyra/gui_v2.cpp | 473 |
1 files changed, 473 insertions, 0 deletions
diff --git a/engines/kyra/gui_v2.cpp b/engines/kyra/gui_v2.cpp index e449c6b22c..dd6a774ca0 100644 --- a/engines/kyra/gui_v2.cpp +++ b/engines/kyra/gui_v2.cpp @@ -26,12 +26,22 @@ #include "kyra/gui_v2.h" #include "kyra/kyra_v2.h" #include "kyra/screen_v2.h" +#include "kyra/text.h" + +#include "common/savefile.h" namespace Kyra { GUI_v2::GUI_v2(KyraEngine_v2 *vm) : GUI(vm), _vm(vm), _screen(vm->screen_v2()) { _backUpButtonList = _unknownButtonList = 0; _buttonListChanged = false; + + _currentMenu = 0; + _isDeathMenu = false; + _isSaveMenu = false; + _isLoadMenu = false; + _scrollUpFunctor = BUTTON_FUNCTOR(GUI_v2, this, &GUI_v2::scrollUpButton); + _scrollDownFunctor = BUTTON_FUNCTOR(GUI_v2, this, &GUI_v2::scrollDownButton); } Button *GUI_v2::addButtonToList(Button *list, Button *newButton) { @@ -383,5 +393,468 @@ void GUI_v2::updateButton(Button *button) { processButton(button); } +void GUI_v2::getInput() { + if (!_displayMenu) + return; + + _vm->checkInput(_menuButtonList); + _vm->removeInputTop(); + if (_vm->quit()) { + _displayMenu = false; + _isLoadMenu = false; + _isSaveMenu = false; + _isOptionsMenu = false; + _isDeleteMenu = false; + } +} + +void GUI_v2::renewHighlight(Menu &menu) { + if (!_displayMenu) + return; + + MenuItem &item = menu.item[menu.highlightedItem]; + int x = item.x + menu.x; int y = item.y + menu.y; + int x2 = x + item.width - 1; int y2 = y + item.height - 1; + redrawText(menu); + _screen->fillRect(x+2, y+2, x2-2, y2-2, item.bkgdColor); + redrawHighlight(menu); + _screen->updateScreen(); +} + +void GUI_v2::backUpPage1(uint8 *buffer) { + _screen->copyRegionToBuffer(1, 0, 0, 320, 200, buffer); +} + +void GUI_v2::restorePage1(const uint8 *buffer) { + _screen->copyBlockToPage(1, 0, 0, 320, 200, buffer); +} + +void GUI_v2::setupSavegameNames(Menu &menu, int num) { + for (int i = 0; i < num; ++i) { + strcpy(getTableString(menu.item[i].itemId), ""); + menu.item[i].saveSlot = -1; + menu.item[i].enabled = false; + } + + int startSlot = 0; + if (_isSaveMenu && _savegameOffset == 0) + startSlot = 1; + + KyraEngine::SaveHeader header; + Common::InSaveFile *in; + for (int i = startSlot; i < num && uint(_savegameOffset + i) < _saveSlots.size(); ++i) { + if ((in = _vm->openSaveForReading(_vm->getSavegameFilename(_saveSlots[i + _savegameOffset]), header)) != 0) { + strncpy(getTableString(menu.item[i].itemId), header.description.c_str(), 80); + menu.item[i].saveSlot = _saveSlots[i + _savegameOffset]; + menu.item[i].enabled = true; + delete in; + } + } + + if (_savegameOffset == 0) { + if (_isSaveMenu) { + char *dst = getTableString(menu.item[0].itemId); + const char *src = getTableString(_vm->gameFlags().isTalkie ? 10 : 18); + strcpy(dst, src); + menu.item[0].saveSlot = -2; + menu.item[0].enabled = true; + } else { + char *dst = getTableString(menu.item[0].itemId); + const char *src = getTableString(_vm->gameFlags().isTalkie ? 34 : 42); + strcpy(dst, src); + } + } +} + +int GUI_v2::scrollUpButton(Button *button) { + updateMenuButton(button); + + if (_savegameOffset == (_isDeleteMenu ? 1 : 0)) + return 0; + + --_savegameOffset; + if (_isLoadMenu) { + setupSavegameNames(_loadMenu, 5); + // original calls something different here... + initMenu(_loadMenu); + } else if (_isSaveMenu || _isDeleteMenu) { + setupSavegameNames(_saveMenu, 5); + // original calls something different here... + initMenu(_saveMenu); + } + + return 0; +} + +int GUI_v2::scrollDownButton(Button *button) { + updateMenuButton(button); + ++_savegameOffset; + + if (uint(_savegameOffset + 5) >= _saveSlots.size()) + _savegameOffset = MAX<int>(_saveSlots.size() - 5, _isDeleteMenu ? 1 : 0); + + if (_isLoadMenu) { + setupSavegameNames(_loadMenu, 5); + // original calls something different here... + initMenu(_loadMenu); + } else if (_isSaveMenu || _isDeleteMenu) { + setupSavegameNames(_saveMenu, 5); + // original calls something different here... + initMenu(_saveMenu); + } + + return 0; +} + +int GUI_v2::resumeGame(Button *caller) { + updateMenuButton(caller); + _displayMenu = false; + return 0; +} + +int GUI_v2::quitOptionsMenu(Button *caller) { + updateMenuButton(caller); + _isOptionsMenu = false; + return 0; +} + +int GUI_v2::clickLoadSlot(Button *caller) { + updateMenuButton(caller); + + int index = caller->index - _menuButtons[0].index; + assert(index >= 0 && index <= 6); + MenuItem &item = _loadMenu.item[index]; + + if (item.saveSlot >= 0) { + _vm->_gameToLoad = item.saveSlot; + _isLoadMenu = false; + } + + return 0; +} + +int GUI_v2::cancelLoadMenu(Button *caller) { + updateMenuButton(caller); + _isLoadMenu = false; + _noLoadProcess = true; + return 0; +} + +int GUI_v2::saveMenu(Button *caller) { + updateSaveList(); + + updateMenuButton(caller); + + restorePage1(_vm->_screenBuffer); + backUpPage1(_vm->_screenBuffer); + + _isSaveMenu = true; + _noSaveProcess = false; + _saveSlot = -1; + _savegameOffset = 0; + setupSavegameNames(_saveMenu, 5); + initMenu(_saveMenu); + + updateAllMenuButtons(); + + while (_isSaveMenu) { + processHighlights(_saveMenu, _vm->_mouseX, _vm->_mouseY); + getInput(); + } + + if (_noSaveProcess) { + restorePage1(_vm->_screenBuffer); + backUpPage1(_vm->_screenBuffer); + initMenu(*_currentMenu); + updateAllMenuButtons(); + return 0; + } else if(_saveSlot <= -1) { + return 0; + } + + restorePage1(_vm->_screenBuffer); + restorePalette(); + _vm->saveGame(_vm->getSavegameFilename(_saveSlot), _saveDescription); + _displayMenu = false; + _madeSave = true; + + return 0; +} + +int GUI_v2::clickSaveSlot(Button *caller) { + updateMenuButton(caller); + + int index = caller->index - _menuButtons[0].index; + assert(index >= 0 && index <= 6); + MenuItem &item = _saveMenu.item[index]; + + if (item.saveSlot >= 0) { + if (_isDeleteMenu) { + _slotToDelete = item.saveSlot; + _isDeleteMenu = false; + return 0; + } else { + _saveSlot = item.saveSlot; + strcpy(_saveDescription, getTableString(item.itemId)); + } + } else if (item.saveSlot == -2) { + _saveSlot = getNextSavegameSlot(); + memset(_saveDescription, 0, sizeof(_saveDescription)); + } + + restorePage1(_vm->_screenBuffer); + backUpPage1(_vm->_screenBuffer); + + initMenu(_savenameMenu); + _screen->fillRect(0x26, 0x5B, 0x11F, 0x66, textFieldColor2()); + const char *desc = nameInputProcess(_saveDescription, 0x27, 0x5C, textFieldColor1(), textFieldColor2(), textFieldColor3(), 0x50); + restorePage1(_vm->_screenBuffer); + backUpPage1(_vm->_screenBuffer); + if (desc) { + _isSaveMenu = false; + _isDeleteMenu = false; + } else { + initMenu(_saveMenu); + } + + return 0; +} + +int GUI_v2::cancelSaveMenu(Button *caller) { + updateMenuButton(caller); + _isSaveMenu = false; + _isDeleteMenu = false; + _noSaveProcess = true; + return 0; +} + +int GUI_v2::deleteMenu(Button *caller) { + updateSaveList(); + + updateMenuButton(caller); + if (_saveSlots.size() < 2) { + _vm->snd_playSoundEffect(0x0D); + return 0; + } + + do { + restorePage1(_vm->_screenBuffer); + backUpPage1(_vm->_screenBuffer); + _savegameOffset = 1; + _saveMenu.menuNameId = _vm->gameFlags().isTalkie ? 35 : 1; + setupSavegameNames(_saveMenu, 5); + initMenu(_saveMenu); + _isDeleteMenu = true; + _slotToDelete = -1; + updateAllMenuButtons(); + + while (_isDeleteMenu) { + processHighlights(_saveMenu, _vm->_mouseX, _vm->_mouseY); + getInput(); + } + + if (_slotToDelete < 1) { + restorePage1(_vm->_screenBuffer); + backUpPage1(_vm->_screenBuffer); + initMenu(*_currentMenu); + updateAllMenuButtons(); + _saveMenu.menuNameId = _vm->gameFlags().isTalkie ? 9 : 17; + return 0; + } + } while (choiceDialog(_vm->gameFlags().isTalkie ? 0x24 : 2, 1) == 0); + + restorePage1(_vm->_screenBuffer); + backUpPage1(_vm->_screenBuffer); + initMenu(*_currentMenu); + updateAllMenuButtons(); + _vm->_saveFileMan->removeSavefile(_vm->getSavegameFilename(_slotToDelete)); + Common::Array<int>::iterator i = Common::find(_saveSlots.begin(), _saveSlots.end(), _slotToDelete); + while (i != _saveSlots.end()) { + ++i; + if (i == _saveSlots.end()) + break; + // We are only renaming all savefiles until we get some slots missing + // Also not rename quicksave slot filenames + if (*(i-1) != *i || *i >= 990) + break; + Common::String oldName = _vm->getSavegameFilename(*i); + Common::String newName = _vm->getSavegameFilename(*i-1); + _vm->_saveFileMan->renameSavefile(oldName.c_str(), newName.c_str()); + } + _saveMenu.menuNameId = _vm->gameFlags().isTalkie ? 9 : 17; + return 0; +} + +const char *GUI_v2::nameInputProcess(char *buffer, int x, int y, uint8 c1, uint8 c2, uint8 c3, int bufferSize) { + bool running = true; + int curPos = strlen(buffer); + + int x2 = x, y2 = y; + _text->printText(buffer, x, y, c1, c2, c2); + + for (int i = 0; i < curPos; ++i) + x2 += getCharWidth(buffer[i]); + + drawTextfieldBlock(x2, y2, c3); + + _keyPressed.reset(); + _cancelNameInput = _finishNameInput = false; + while (running && !_vm->quit()) { + processHighlights(_savenameMenu, _vm->_mouseX, _vm->_mouseY); + checkTextfieldInput(); + if (_keyPressed.keycode == Common::KEYCODE_RETURN || _keyPressed.keycode == Common::KEYCODE_KP_ENTER || _finishNameInput) { + if (checkSavegameDescription(buffer, curPos)) { + buffer[curPos] = 0; + running = false; + } else { + _finishNameInput = false; + } + } else if (_keyPressed.keycode == Common::KEYCODE_ESCAPE || _cancelNameInput) { + running = false; + return 0; + } else if ((_keyPressed.keycode == Common::KEYCODE_BACKSPACE || _keyPressed.keycode == Common::KEYCODE_DELETE) && curPos > 0) { + drawTextfieldBlock(x2, y2, c2); + --curPos; + x2 -= getCharWidth(buffer[curPos]); + drawTextfieldBlock(x2, y2, c3); + _screen->updateScreen(); + } else if (_keyPressed.ascii > 31 && _keyPressed.ascii < 127 && curPos < bufferSize) { + if (x2 + getCharWidth(_keyPressed.ascii) + 7 < 0x11F) { + buffer[curPos] = _keyPressed.ascii; + const char text[2] = { buffer[curPos], 0 }; + _text->printText(text, x2, y2, c1, c2, c2); + x2 += getCharWidth(_keyPressed.ascii); + drawTextfieldBlock(x2, y2, c3); + ++curPos; + _screen->updateScreen(); + } + } + + _keyPressed.reset(); + } + + return buffer; +} + +int GUI_v2::finishSavename(Button *caller) { + updateMenuButton(caller); + _finishNameInput = true; + return 0; +} + +int GUI_v2::cancelSavename(Button *caller) { + updateMenuButton(caller); + _cancelNameInput = true; + return 0; +} + +bool GUI_v2::checkSavegameDescription(const char *buffer, int size) { + if (!buffer || !size) + return false; + if (buffer[0] == 0) + return false; + for (int i = 0; i < size; ++i) { + if (buffer[i] != 0x20) + return true; + else if (buffer[i] == 0x00) + return false; + } + return false; +} + +int GUI_v2::getCharWidth(uint8 c) { + Screen::FontId old = _screen->setFont(Screen::FID_8_FNT); + _screen->_charWidth = -2; + int width = _screen->getCharWidth(c); + _screen->_charWidth = 0; + _screen->setFont(old); + return width; +} + +void GUI_v2::checkTextfieldInput() { + Common::Event event; + + bool running = true; + int keys = 0; + while (_vm->_eventMan->pollEvent(event) && running) { + switch (event.type) { + case Common::EVENT_QUIT: + _vm->_quitFlag = true; + break; + + case Common::EVENT_KEYDOWN: + if (event.kbd.keycode == 'q' && event.kbd.flags == Common::KBD_CTRL) + _vm->_quitFlag = true; + else + _keyPressed = event.kbd; + running = false; + break; + + case Common::EVENT_LBUTTONDOWN: + case Common::EVENT_LBUTTONUP: { + Common::Point pos = _vm->getMousePos(); + _vm->_mouseX = pos.x; + _vm->_mouseY = pos.y; + keys = event.type == Common::EVENT_LBUTTONDOWN ? 199 : (200 | 0x800); + running = false; + } break; + + case Common::EVENT_MOUSEMOVE: { + Common::Point pos = _vm->getMousePos(); + _vm->_mouseX = pos.x; + _vm->_mouseY = pos.y; + _screen->updateScreen(); + } break; + + default: + break; + } + } + + processButtonList(_menuButtonList, keys | 0x8000); +} + +void GUI_v2::drawTextfieldBlock(int x, int y, uint8 c) { + _screen->fillRect(x+1, y+1, x+7, y+8, c); +} + +bool GUI_v2::choiceDialog(int name, bool type) { + _choiceMenu.highlightedItem = 0; + restorePage1(_vm->_screenBuffer); + backUpPage1(_vm->_screenBuffer); + if (type) + _choiceMenu.numberOfItems = 2; + else + _choiceMenu.numberOfItems = 1; + _choiceMenu.menuNameId = name; + + initMenu(_choiceMenu); + _isChoiceMenu = true; + _choice = false; + + while (_isChoiceMenu) { + processHighlights(_choiceMenu, _vm->_mouseX, _vm->_mouseY); + getInput(); + } + + restorePage1(_vm->_screenBuffer); + backUpPage1(_vm->_screenBuffer); + return _choice; +} + +int GUI_v2::choiceYes(Button *caller) { + updateMenuButton(caller); + _choice = true; + _isChoiceMenu = false; + return 0; +} + +int GUI_v2::choiceNo(Button *caller) { + updateMenuButton(caller); + _choice = false; + _isChoiceMenu = false; + return 0; +} + } // end of namespace Kyra |