From 2be35dd96f1b577ea383ad82c954d89f30c36209 Mon Sep 17 00:00:00 2001 From: stephena Date: Sun, 27 Nov 2005 22:37:25 +0000 Subject: [PATCH] The CheatManager now supports adding new cheats, and editing existing cheats. There's still some error checking to do, to make sure that cheats don't contain characters that are reserved for the cheat file (comma, colon, quote, etc). git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@897 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba --- stella/src/cheat/Cheat.hxx | 4 +- stella/src/cheat/CheatCodeDialog.cxx | 168 ++++++++++++++------------ stella/src/cheat/CheatCodeDialog.hxx | 20 ++- stella/src/cheat/CheatManager.cxx | 43 +++++-- stella/src/cheat/CheatManager.hxx | 27 ++++- stella/src/debugger/gui/RamWidget.cxx | 14 ++- stella/src/debugger/gui/RomWidget.cxx | 7 +- stella/src/emucore/Console.cxx | 6 +- stella/src/emucore/OSystem.cxx | 5 +- stella/src/gui/CheckListWidget.cxx | 4 +- stella/src/gui/CheckListWidget.hxx | 3 +- stella/src/gui/InputTextDialog.cxx | 100 ++++++++++----- stella/src/gui/InputTextDialog.hxx | 25 ++-- 13 files changed, 264 insertions(+), 162 deletions(-) diff --git a/stella/src/cheat/Cheat.hxx b/stella/src/cheat/Cheat.hxx index 4358ebda2..78c501613 100644 --- a/stella/src/cheat/Cheat.hxx +++ b/stella/src/cheat/Cheat.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: Cheat.hxx,v 1.1 2005-11-11 21:44:18 stephena Exp $ +// $Id: Cheat.hxx,v 1.2 2005-11-27 22:37:24 stephena Exp $ //============================================================================ #ifndef CHEAT_HXX @@ -29,7 +29,7 @@ class Cheat : myOSystem(osystem), myName(name), myCode(code), - myEnabled(false) { } + myEnabled(false) { if(name == "") myName = code; } virtual ~Cheat() { } bool enabled() const { return myEnabled; } diff --git a/stella/src/cheat/CheatCodeDialog.cxx b/stella/src/cheat/CheatCodeDialog.cxx index 23c2eedf6..bd68f87f7 100644 --- a/stella/src/cheat/CheatCodeDialog.cxx +++ b/stella/src/cheat/CheatCodeDialog.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: CheatCodeDialog.cxx,v 1.4 2005-11-27 15:48:04 stephena Exp $ +// $Id: CheatCodeDialog.cxx,v 1.5 2005-11-27 22:37:24 stephena Exp $ // // Based on code from ScummVM - Scumm Interpreter // Copyright (C) 2002-2004 The ScummVM project @@ -25,18 +25,23 @@ #include "Props.hxx" #include "Widget.hxx" #include "Dialog.hxx" +#include "DialogContainer.hxx" #include "CheatCodeDialog.hxx" #include "GuiUtils.hxx" #include "CheckListWidget.hxx" #include "CheatManager.hxx" +#include "InputTextDialog.hxx" +#include "StringList.hxx" #include "bspf.hxx" enum { - kAddCheatCmd = 'CHTA', - kEditCheatCmd = 'CHTE', - kRemCheatCmd = 'CHTR', - kAddOneShotCmd = 'CHTO' + kAddCheatCmd = 'CHTa', + kEditCheatCmd = 'CHTe', + kCheatAdded = 'CHad', + kCheatEdited = 'CHed', + kRemCheatCmd = 'CHTr', + kAddOneShotCmd = 'CHTo' }; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -58,32 +63,25 @@ CheatCodeDialog::CheatCodeDialog(OSystem* osystem, DialogContainer* parent, xpos += myCheatList->getWidth() + 15; ypos = 15; addButton(xpos, ypos, "Add", kAddCheatCmd, 0); - addButton(xpos, ypos+=20, "Edit", kEditCheatCmd, 0); - addButton(xpos, ypos+=20, "Remove", kRemCheatCmd, 0); + myEditButton = addButton(xpos, ypos+=20, "Edit", kEditCheatCmd, 0); + myRemoveButton = addButton(xpos, ypos+=20, "Remove", kRemCheatCmd, 0); addButton(xpos, ypos+=30, "One shot", kAddOneShotCmd, 0); -/* -Move this to new dialog - xpos = 10; ypos = 10 + myCheatList->getHeight() + 10; - myTitle = new StaticTextWidget(this, xpos, ypos, lwidth, fontHeight, - "Cheat Code", kTextAlignLeft); - - xpos += myTitle->getWidth(); - myInput = new EditTextWidget(this, xpos, ypos-1, 48, fontHeight, ""); - - xpos = 10; ypos += fontHeight + 5; - myError = new StaticTextWidget(this, xpos, ypos, lwidth, kFontHeight, - "", kTextAlignLeft); - myError->setColor(kTextColorEm); -*/ - + // Inputbox which will pop up when adding/editing a cheat + StringList labels; + labels.push_back("Name: "); + labels.push_back("Code: "); + myCheatInput = new InputTextDialog(this, font, labels, _x+20, _y+20); + myCheatInput->setTarget(this); // Add OK and Cancel buttons #ifndef MAC_OSX addButton(_w - 2 * (kButtonWidth + 7), _h - 24, "OK", kOKCmd, 0); - addButton(_w - (kButtonWidth + 10), _h - 24, "Cancel", kCloseCmd, 0); + myCancelButton = addButton(_w - (kButtonWidth + 10), _h - 24, + "Cancel", kCloseCmd, 0); #else - addButton(_w - 2 * (kButtonWidth + 7), _h - 24, "Cancel", kCloseCmd, 0); + myCancelButton = addButton(_w - 2 * (kButtonWidth + 7), _h - 24, + "Cancel", kCloseCmd, 0); addButton(_w - (kButtonWidth + 10), _h - 24, "OK", kOKCmd, 0); #endif } @@ -102,44 +100,65 @@ void CheatCodeDialog::loadConfig() StringList l; BoolArray b; - const CheatList& list = instance()->cheat().myCheatList; + const CheatList& list = instance()->cheat().list(); for(unsigned int i = 0; i < list.size(); ++i) { l.push_back(list[i]->name()); b.push_back(bool(list[i]->enabled())); } myCheatList->setList(l, b); + + bool enabled = (list.size() > 0); + myEditButton->setEnabled(enabled); + myRemoveButton->setEnabled(enabled); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void CheatCodeDialog::saveConfig() { // Inspect checkboxes for enable/disable codes + const CheatList& list = instance()->cheat().list(); for(unsigned int i = 0; i < myCheatList->getList().size(); ++i) { if(myCheatList->getState(i)) - instance()->cheat().myCheatList[i]->enable(); + list[i]->enable(); else - instance()->cheat().myCheatList[i]->disable(); + list[i]->disable(); } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void CheatCodeDialog::addCheat() { -cerr << "CheatCodeDialog::addCheat()\n"; + myCheatInput->setEditString("", 0); + myCheatInput->setEditString("", 1); + myCheatInput->setTitle(""); + myCheatInput->setEmitSignal(kCheatAdded); + parent()->addDialog(myCheatInput); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void CheatCodeDialog::editCheat(int cheatNumber) +void CheatCodeDialog::editCheat() { -cerr << "CheatCodeDialog::editCheat() " << cheatNumber << endl; + int idx = myCheatList->getSelected(); + if(idx < 0) + return; + + const CheatList& list = instance()->cheat().list(); + const string& name = list[idx]->name(); + const string& code = list[idx]->code(); + + myCheatInput->setEditString(name, 0); + myCheatInput->setEditString(code, 1); + myCheatInput->setEmitSignal(kCheatEdited); + parent()->addDialog(myCheatInput); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void CheatCodeDialog::removeCheat(int cheatNumber) +void CheatCodeDialog::removeCheat() { -cerr << "CheatCodeDialog::removeCheat() " << cheatNumber << endl; + instance()->cheat().remove(myCheatList->getSelected()); + loadConfig(); // reload the cheat list } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -158,7 +177,7 @@ void CheatCodeDialog::handleCommand(CommandSender* sender, int cmd, break; case kListItemDoubleClickedCmd: - editCheat(myCheatList->getSelected()); + editCheat(); break; case kAddCheatCmd: @@ -166,62 +185,51 @@ void CheatCodeDialog::handleCommand(CommandSender* sender, int cmd, break; case kEditCheatCmd: - editCheat(myCheatList->getSelected()); + editCheat(); break; + case kCheatAdded: + { + const string& name = myCheatInput->getResult(0); + const string& code = myCheatInput->getResult(1); + if(instance()->cheat().isValidCode(code)) + { + instance()->cheat().add(name, code); + parent()->removeDialog(); + loadConfig(); // show changes onscreen + myCancelButton->setEnabled(false); // cannot cancel when a new cheat added + } + else + myCheatInput->setTitle("Invalid code"); + break; + } + + case kCheatEdited: + { + const string& name = myCheatInput->getResult(0); + const string& code = myCheatInput->getResult(1); + bool enable = myCheatList->getSelectedState(); + int idx = myCheatList->getSelected(); + if(instance()->cheat().isValidCode(code)) + { + instance()->cheat().add(name, code, enable, idx); + parent()->removeDialog(); + loadConfig(); // show changes onscreen + myCancelButton->setEnabled(false); // cannot cancel when a new cheat added + } + else + myCheatInput->setTitle("Invalid code"); + break; + } + case kRemCheatCmd: - removeCheat(myCheatList->getSelected()); + removeCheat(); break; case kAddOneShotCmd: cerr << "add one-shot cheat\n"; break; -/* - case kEditAcceptCmd: - { - // cerr << myInput->getEditString() << endl; - const Cheat* cheat = - instance()->cheat().add("DLG", myInput->getEditString(), true); - - if(cheat) - { - // make sure "invalid code" isn't showing any more: - myError->setLabel(""); - myErrorFlag = false; - - // get out of menu mode (back to emulation): - Dialog::handleCommand(sender, kCloseCmd, data, id); - instance()->eventHandler().leaveMenuMode(); - } - else // parse() returned 0 (null) - { - myInput->setEditString(""); - - // show error message "invalid code": - myError->setLabel("Invalid Code"); - myErrorFlag = true; - - // not sure this does anything useful: - Dialog::handleCommand(sender, cmd, data, 0); - } - break; - } - - case kEditCancelCmd: - Dialog::handleCommand(sender, kCloseCmd, data, id); - instance()->eventHandler().leaveMenuMode(); - break; - - case kEditChangedCmd: - // Erase the invalid message once editing is restarted - if(myErrorFlag) - { - myError->setLabel(""); - myErrorFlag = false; - } - break; -*/ default: Dialog::handleCommand(sender, cmd, data, 0); break; diff --git a/stella/src/cheat/CheatCodeDialog.hxx b/stella/src/cheat/CheatCodeDialog.hxx index 05b920d3a..29ee2acf6 100644 --- a/stella/src/cheat/CheatCodeDialog.hxx +++ b/stella/src/cheat/CheatCodeDialog.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: CheatCodeDialog.hxx,v 1.2 2005-11-26 21:23:35 stephena Exp $ +// $Id: CheatCodeDialog.hxx,v 1.3 2005-11-27 22:37:24 stephena Exp $ // // Based on code from ScummVM - Scumm Interpreter // Copyright (C) 2002-2004 The ScummVM project @@ -27,6 +27,7 @@ class CommandSender; class ButtonWidget; class StaticTextWidget; class CheckListWidget; +class InputTextDialog; #include "OSystem.hxx" #include "Dialog.hxx" @@ -50,19 +51,16 @@ class CheatCodeDialog : public Dialog private: void addCheat(); - void editCheat(int cheatNumber); - void removeCheat(int cheatNumber); + void editCheat(); + void removeCheat(); private: - CheckListWidget* myCheatList; -/* - ButtonWidget* myExitButton; - StaticTextWidget* myTitle; - StaticTextWidget* myError; - EditTextWidget* myInput; + CheckListWidget* myCheatList; + InputTextDialog* myCheatInput; - bool myErrorFlag; -*/ + ButtonWidget* myEditButton; + ButtonWidget* myRemoveButton; + ButtonWidget* myCancelButton; }; #endif diff --git a/stella/src/cheat/CheatManager.cxx b/stella/src/cheat/CheatManager.cxx index 38106c838..5c837a628 100644 --- a/stella/src/cheat/CheatManager.cxx +++ b/stella/src/cheat/CheatManager.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: CheatManager.cxx,v 1.3 2005-11-27 15:48:05 stephena Exp $ +// $Id: CheatManager.cxx,v 1.4 2005-11-27 22:37:24 stephena Exp $ //============================================================================ #include @@ -42,11 +42,10 @@ CheatManager::~CheatManager() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - const Cheat* CheatManager::add(const string& name, const string& code, - bool enable) + bool enable, int idx) { - for(unsigned int i = 0; i < code.size(); i++) - if(!isxdigit(code[i])) - return NULL; + if(!isValidCode(code)) + return NULL; Cheat* cheat = (Cheat*) NULL; @@ -79,7 +78,10 @@ const Cheat* CheatManager::add(const string& name, const string& code, // Add the cheat to the main cheat list if(cheat) { - myCheatList.push_back(cheat); + if(idx == -1) + myCheatList.push_back(cheat); + else + myCheatList.insert_at(idx, cheat); // And enable/disable it (the cheat knows how to enable or disable itself) if(enable) @@ -91,6 +93,22 @@ const Cheat* CheatManager::add(const string& name, const string& code, return cheat; } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void CheatManager::remove(int idx) +{ + if((unsigned int)idx >= myCheatList.size()) + return; + + Cheat* c = myCheatList[idx]; + + // First remove it from the per-frame list + addPerFrame(c, false); + + // Then remove it from the cheatlist entirely + myCheatList.remove_at(idx); + delete c; +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void CheatManager::addPerFrame(Cheat* cheat, bool enable) { @@ -124,8 +142,6 @@ void CheatManager::addPerFrame(Cheat* cheat, bool enable) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void CheatManager::parse(const string& cheats) { - cerr << "parsing cheats: " << cheats << endl; - StringList s; string::size_type lastPos = cheats.find_first_not_of(",", 0); string::size_type pos = cheats.find_first_of(",", lastPos); @@ -298,3 +314,14 @@ void CheatManager::clear() delete myCheatList[i]; myCheatList.clear(); } + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +bool CheatManager::isValidCode(const string& code) +{ + for(unsigned int i = 0; i < code.size(); i++) + if(!isxdigit(code[i])) + return false; + + int length = code.length(); + return (length == 4 || length == 6 || length == 8); +} diff --git a/stella/src/cheat/CheatManager.hxx b/stella/src/cheat/CheatManager.hxx index 92aebffe8..09ffd4624 100644 --- a/stella/src/cheat/CheatManager.hxx +++ b/stella/src/cheat/CheatManager.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: CheatManager.hxx,v 1.3 2005-11-27 15:48:05 stephena Exp $ +// $Id: CheatManager.hxx,v 1.4 2005-11-27 22:37:24 stephena Exp $ //============================================================================ #ifndef CHEAT_MANAGER_HXX @@ -36,12 +36,10 @@ typedef map CheatCodeMap; the list of all cheats currently in use. @author Stephen Anthony - @version $Id: CheatManager.hxx,v 1.3 2005-11-27 15:48:05 stephena Exp $ + @version $Id: CheatManager.hxx,v 1.4 2005-11-27 22:37:24 stephena Exp $ */ class CheatManager { - friend class CheatCodeDialog; - public: CheatManager(OSystem* osystem); virtual ~CheatManager(); @@ -52,10 +50,19 @@ class CheatManager @param name Name of the cheat (not absolutely required) @param code The actual cheatcode (in hex) @param enable Whether to enable this cheat right away + @param idx Index at which to insert the cheat @return The cheat (if was created), else NULL. */ - const Cheat* add(const string& name, const string& code, bool enable = true); + const Cheat* add(const string& name, const string& code, + bool enable = true, int idx = -1); + + /** + Remove the cheat at 'idx' from the cheat list(s). + + @param index Location in myCheatList of the cheat to remove + */ + void remove(int idx); /** Adds the specified cheat to the internal per-frame list. @@ -75,6 +82,11 @@ class CheatManager */ void enable(const string& code, bool enable); + /** + Returns the game cheatlist. + */ + const CheatList& list() { return myCheatList; } + /** Returns the per-frame cheatlist (needed to evaluate cheats each frame) */ @@ -100,6 +112,11 @@ class CheatManager */ void saveCheats(const string& md5sum); + /** + Checks if a code is valid. + */ + bool isValidCode(const string& code); + private: /** Parses a list of cheats and adds/enables each one. diff --git a/stella/src/debugger/gui/RamWidget.cxx b/stella/src/debugger/gui/RamWidget.cxx index 67620a16e..3836d935e 100644 --- a/stella/src/debugger/gui/RamWidget.cxx +++ b/stella/src/debugger/gui/RamWidget.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: RamWidget.cxx,v 1.1 2005-08-30 17:51:26 stephena Exp $ +// $Id: RamWidget.cxx,v 1.2 2005-11-27 22:37:24 stephena Exp $ // // Based on code from ScummVM - Scumm Interpreter // Copyright (C) 2002-2004 The ScummVM project @@ -140,18 +140,20 @@ RamWidget::RamWidget(GuiObject* boss, const GUI::Font& font, int x, int y) myBinValue->setFont(font); myBinValue->setEditable(false); + // Calculate real dimensions + _w = lwidth + myRamGrid->getWidth(); + _h = ypos + lineHeight - y; + // Inputbox which will pop up when searching RAM - myInputBox = new InputTextDialog(boss, font, + StringList label; + label.push_back("Search: "); + myInputBox = new InputTextDialog(boss, font, label, x + lwidth + 20, y + 2*lineHeight - 5); myInputBox->setTarget(this); // Start with these buttons disabled myCompareButton->clearFlags(WIDGET_ENABLED); myRestartButton->clearFlags(WIDGET_ENABLED); - - // Calculate real dimensions - _w = lwidth + myRamGrid->getWidth(); - _h = ypos + lineHeight - y; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/stella/src/debugger/gui/RomWidget.cxx b/stella/src/debugger/gui/RomWidget.cxx index a497840da..273fed1f7 100644 --- a/stella/src/debugger/gui/RomWidget.cxx +++ b/stella/src/debugger/gui/RomWidget.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: RomWidget.cxx,v 1.14 2005-10-22 15:43:17 stephena Exp $ +// $Id: RomWidget.cxx,v 1.15 2005-11-27 22:37:24 stephena Exp $ // // Based on code from ScummVM - Scumm Interpreter // Copyright (C) 2002-2004 The ScummVM project @@ -28,6 +28,7 @@ #include "PackedBitArray.hxx" #include "GuiObject.hxx" #include "InputTextDialog.hxx" +#include "EditTextWidget.hxx" #include "ContextMenu.hxx" #include "RomListWidget.hxx" #include "RomWidget.hxx" @@ -98,7 +99,9 @@ RomWidget::RomWidget(GuiObject* boss, const GUI::Font& font, int x, int y) _h = myRomList->getHeight(); // Create dialog box for save ROM (get name) - mySaveRom = new InputTextDialog(boss, font, _x + 50, _y + 80); + StringList label; + label.push_back("Filename: "); + mySaveRom = new InputTextDialog(boss, font, label, _x + 50, _y + 80); mySaveRom->setTarget(this); } diff --git a/stella/src/emucore/Console.cxx b/stella/src/emucore/Console.cxx index 3cab2b627..381057640 100644 --- a/stella/src/emucore/Console.cxx +++ b/stella/src/emucore/Console.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: Console.cxx,v 1.76 2005-11-27 15:48:06 stephena Exp $ +// $Id: Console.cxx,v 1.77 2005-11-27 22:37:24 stephena Exp $ //============================================================================ #include @@ -215,10 +215,6 @@ Console::Console(const uInt8* image, uInt32 size, const string& md5, myOSystem->debugger().setConsole(this); myOSystem->debugger().initialize(); #endif - -#ifdef CHEATCODE_SUPPORT - myOSystem->cheat().loadCheats(md5); -#endif } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/stella/src/emucore/OSystem.cxx b/stella/src/emucore/OSystem.cxx index f3d964b3b..413beb5d9 100644 --- a/stella/src/emucore/OSystem.cxx +++ b/stella/src/emucore/OSystem.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: OSystem.cxx,v 1.48 2005-11-27 15:48:06 stephena Exp $ +// $Id: OSystem.cxx,v 1.49 2005-11-27 22:37:24 stephena Exp $ //============================================================================ #include @@ -336,6 +336,9 @@ bool OSystem::createConsole(const string& romfile) // Create an instance of the 2600 game console // The Console c'tor takes care of updating the eventhandler state myConsole = new Console(image, size, md5, this); +#ifdef CHEATCODE_SUPPORT + myCheatManager->loadCheats(md5); +#endif if(showmessage) myFrameBuffer->showMessage("New console created"); diff --git a/stella/src/gui/CheckListWidget.cxx b/stella/src/gui/CheckListWidget.cxx index e5b2db150..81aa842f9 100644 --- a/stella/src/gui/CheckListWidget.cxx +++ b/stella/src/gui/CheckListWidget.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: CheckListWidget.cxx,v 1.8 2005-11-26 21:23:35 stephena Exp $ +// $Id: CheckListWidget.cxx,v 1.9 2005-11-27 22:37:25 stephena Exp $ // // Based on code from ScummVM - Scumm Interpreter // Copyright (C) 2002-2004 The ScummVM project @@ -185,7 +185,7 @@ GUI::Rect CheckListWidget::getEditRect() const // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool CheckListWidget::getState(int line) { - if(line < (int)_stateList.size()) + if(line >= 0 && line < (int)_stateList.size()) return _stateList[line]; else return false; diff --git a/stella/src/gui/CheckListWidget.hxx b/stella/src/gui/CheckListWidget.hxx index 1d89d440f..edc955721 100644 --- a/stella/src/gui/CheckListWidget.hxx +++ b/stella/src/gui/CheckListWidget.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: CheckListWidget.hxx,v 1.6 2005-08-26 16:44:17 stephena Exp $ +// $Id: CheckListWidget.hxx,v 1.7 2005-11-27 22:37:25 stephena Exp $ // // Based on code from ScummVM - Scumm Interpreter // Copyright (C) 2002-2004 The ScummVM project @@ -52,6 +52,7 @@ class CheckListWidget : public ListWidget void setLine(int line, const string& str, const bool& state); bool getState(int line); + bool getSelectedState() { return getState(_selectedItem); } void handleCommand(CommandSender* sender, int cmd, int data, int id); diff --git a/stella/src/gui/InputTextDialog.cxx b/stella/src/gui/InputTextDialog.cxx index 9bb96082c..330f48fe8 100644 --- a/stella/src/gui/InputTextDialog.cxx +++ b/stella/src/gui/InputTextDialog.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: InputTextDialog.cxx,v 1.7 2005-10-14 13:50:00 stephena Exp $ +// $Id: InputTextDialog.cxx,v 1.8 2005-11-27 22:37:25 stephena Exp $ // // Based on code from ScummVM - Scumm Interpreter // Copyright (C) 2002-2004 The ScummVM project @@ -35,38 +35,58 @@ enum { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - InputTextDialog::InputTextDialog(GuiObject* boss, const GUI::Font& font, - int x, int y) + const StringList& labels, int x, int y) : Dialog(boss->instance(), boss->parent(), x, y, 16, 16), CommandSender(boss), - _errorFlag(false) + myErrorFlag(false) { const int fontWidth = font.getMaxCharWidth(), fontHeight = font.getFontHeight(), lineHeight = font.getLineHeight(); - int xpos, ypos; + unsigned int xpos, ypos, i, lwidth = 0, maxIdx = 0; // Calculate real dimensions - _w = fontWidth * 30; - _h = lineHeight * 5; + _w = fontWidth * 25; + _h = lineHeight * 4 + labels.size() * (lineHeight + 5); - xpos = 10; ypos = lineHeight; - int lwidth = font.getStringWidth("Enter Data:"); - StaticTextWidget* t = - new StaticTextWidget(this, xpos, ypos, - lwidth, fontHeight, - "Enter Data:", kTextAlignLeft); - t->setFont(font); + // Determine longest label + for(i = 0; i < labels.size(); ++i) + { + if(labels[i].length() > lwidth) + { + lwidth = labels[i].length(); + maxIdx = i; + } + } + lwidth = font.getStringWidth(labels[maxIdx]); - xpos += lwidth + fontWidth; - _input = new EditTextWidget(this, xpos, ypos, - _w - xpos - 10, lineHeight, ""); - _input->setFont(font); - addFocusWidget(_input); + // Create editboxes for all labels + WidgetArray wid; + ypos = lineHeight; + for(i = 0; i < labels.size(); ++i) + { + xpos = 10; + StaticTextWidget* t = + new StaticTextWidget(this, xpos, ypos, + lwidth, fontHeight, + labels[i], kTextAlignLeft); + t->setFont(font); - xpos = 10; ypos = 2*lineHeight + 5; - _title = new StaticTextWidget(this, xpos, ypos, _w - 2*xpos, fontHeight, - "", kTextAlignCenter); - _title->setColor(kTextColorEm); + xpos += lwidth + fontWidth; + EditTextWidget* w = new EditTextWidget(this, xpos, ypos, + _w - xpos - 10, lineHeight, ""); + w->setFont(font); + wid.push_back(w); + + myInput.push_back(w); + ypos += lineHeight + 5; + } + addToFocusList(wid); + + xpos = 10; + myTitle = new StaticTextWidget(this, xpos, ypos, _w - 2*xpos, fontHeight, + "", kTextAlignCenter); + myTitle->setColor(kTextColorEm); #ifndef MAC_OSX addButton(_w - 2 * (kButtonWidth + 10), _h - 24, "OK", kAcceptCmd, 0); @@ -77,11 +97,33 @@ InputTextDialog::InputTextDialog(GuiObject* boss, const GUI::Font& font, #endif } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +InputTextDialog::~InputTextDialog() +{ + myInput.clear(); +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void InputTextDialog::setTitle(const string& title) { - _title->setLabel(title); - _errorFlag = true; + myTitle->setLabel(title); + myErrorFlag = true; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +const string& InputTextDialog::getResult(int idx) +{ + if((unsigned int)idx < myInput.size()) + return myInput[idx]->getEditString(); + else + return EmptyString; +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void InputTextDialog::setEditString(const string& str, int idx) +{ + if((unsigned int)idx < myInput.size()) + myInput[idx]->setEditString(str); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -95,8 +137,8 @@ void InputTextDialog::handleCommand(CommandSender* sender, int cmd, { // Send a signal to the calling class that a selection has been made // Since we aren't derived from a widget, we don't have a 'data' or 'id' - if(_cmd) - sendCommand(_cmd, 0, 0); + if(myCmd) + sendCommand(myCmd, 0, 0); // We don't close, but leave the parent to do it // If the data isn't valid, the parent may wait until it is @@ -105,10 +147,10 @@ void InputTextDialog::handleCommand(CommandSender* sender, int cmd, case kEditChangedCmd: // Erase the invalid message once editing is restarted - if(_errorFlag) + if(myErrorFlag) { - _title->setLabel(""); - _errorFlag = false; + myTitle->setLabel(""); + myErrorFlag = false; } break; diff --git a/stella/src/gui/InputTextDialog.hxx b/stella/src/gui/InputTextDialog.hxx index 60b8b27b2..71eabcd07 100644 --- a/stella/src/gui/InputTextDialog.hxx +++ b/stella/src/gui/InputTextDialog.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: InputTextDialog.hxx,v 1.3 2005-10-06 17:28:55 stephena Exp $ +// $Id: InputTextDialog.hxx,v 1.4 2005-11-27 22:37:25 stephena Exp $ // // Based on code from ScummVM - Scumm Interpreter // Copyright (C) 2002-2004 The ScummVM project @@ -24,31 +24,36 @@ class GuiObject; class StaticTextWidget; +class EditTextWidget; #include "Dialog.hxx" #include "Command.hxx" -#include "EditTextWidget.hxx" + +typedef GUI::Array InputWidget; class InputTextDialog : public Dialog, public CommandSender { public: - InputTextDialog(GuiObject* boss, const GUI::Font& font, int x, int y); + InputTextDialog(GuiObject* boss, const GUI::Font& font, + const StringList& labels, int x, int y); + virtual ~InputTextDialog(); - const string& getResult() { return _input->getEditString(); } + const string& getResult(int idx = 0); - void setEditString(const string& str) { _input->setEditString(str); } - void setEmitSignal(int cmd) { _cmd = cmd; } + void setEditString(const string& str, int idx = 0); + void setEmitSignal(int cmd) { myCmd = cmd; } void setTitle(const string& title); protected: virtual void handleCommand(CommandSender* sender, int cmd, int data, int id); private: - StaticTextWidget* _title; - EditTextWidget* _input; + private: + InputWidget myInput; + StaticTextWidget* myTitle; - int _cmd; - bool _errorFlag; + bool myErrorFlag; + int myCmd; }; #endif