Now allows you to store a state of the current puzzle, and to restore it later on.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22843 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2007-11-06 13:45:32 +00:00
parent 6405549873
commit b35a3514f1
2 changed files with 56 additions and 1 deletions

View File

@ -37,6 +37,8 @@ const uint32 kMsgAbortSudokuGenerator = 'asgn';
const uint32 kMsgSudokuGenerated = 'sugn';
const uint32 kMsgMarkInvalid = 'minv';
const uint32 kMsgMarkValidHints = 'mvht';
const uint32 kMsgStoreState = 'stst';
const uint32 kMsgRestoreState = 'rest';
const uint32 kMsgNew = 'new ';
const uint32 kMsgStartAgain = 'stag';
const uint32 kMsgExportAsText = 'extx';
@ -133,7 +135,8 @@ GenerateSudoku::_GenerateThread(void* _self)
SudokuWindow::SudokuWindow()
: BWindow(BRect(100, 100, 500, 520), "Sudoku", B_TITLED_WINDOW,
B_ASYNCHRONOUS_CONTROLS),
fGenerator(NULL)
fGenerator(NULL),
fStoredState(NULL)
{
BMessage settings;
_LoadSettings(settings);
@ -146,6 +149,14 @@ SudokuWindow::SudokuWindow()
} else
frame = Bounds();
if (settings.HasMessage("stored state")) {
fStoredState = new BMessage;
if (settings.FindMessage("stored state", fStoredState) != B_OK) {
delete fStoredState;
fStoredState = NULL;
}
}
// create GUI
BMenuBar* menuBar = new BMenuBar(Bounds(), "menu");
@ -167,6 +178,7 @@ SudokuWindow::SudokuWindow()
// add menu
// "File" menu
BMenu* menu = new BMenu("File");
menu->AddItem(new BMenuItem("New", new BMessage(kMsgNew)));
menu->AddItem(new BMenuItem("Start Again", new BMessage(kMsgStartAgain)));
@ -204,6 +216,7 @@ SudokuWindow::SudokuWindow()
item->SetTarget(be_app);
menuBar->AddItem(menu);
// "View" menu
menu = new BMenu("View");
menu->AddItem(item = new BMenuItem("Mark Invalid Values",
new BMessage(kMsgMarkInvalid)));
@ -216,7 +229,14 @@ SudokuWindow::SudokuWindow()
menu->SetTargetForItems(this);
menuBar->AddItem(menu);
// "Help" menu
menu = new BMenu("Help");
menu->AddItem(new BMenuItem("Store Current", new BMessage(kMsgStoreState)));
menu->AddItem(fRestoreStateItem = new BMenuItem("Restore Saved",
new BMessage(kMsgRestoreState)));
fRestoreStateItem->SetEnabled(fStoredState != NULL);
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem("Solve", new BMessage(kMsgSolveSudoku)));
menu->AddItem(new BMenuItem("Solve Single Field",
new BMessage(kMsgSolveSingle)));
@ -282,6 +302,8 @@ SudokuWindow::_SaveSettings()
status = settings.AddRect("window frame", Frame());
if (status == B_OK)
status = fSudokuView->SaveState(settings);
if (status == B_OK && fStoredState != NULL)
status = settings.AddMessage("stored state", fStoredState);
if (status == B_OK)
status = settings.Flatten(&file);
@ -289,6 +311,15 @@ SudokuWindow::_SaveSettings()
}
void
SudokuWindow::_ResetStoredState()
{
delete fStoredState;
fStoredState = NULL;
fRestoreStateItem->SetEnabled(false);
}
void
SudokuWindow::_MessageDropped(BMessage* message)
{
@ -344,6 +375,8 @@ SudokuWindow::_Generate(int32 level)
fSudokuView->SetEditable(false);
fProgressWindow->Start(this);
_ResetStoredState();
fGenerator = new GenerateSudoku(*fSudokuView->Field(), level,
fProgressWindow, this);
}
@ -417,6 +450,7 @@ SudokuWindow::MessageReceived(BMessage* message)
}
case kMsgNew:
_ResetStoredState();
fSudokuView->ClearAll();
break;
@ -442,6 +476,23 @@ SudokuWindow::MessageReceived(BMessage* message)
break;
}
case kMsgStoreState:
delete fStoredState;
fStoredState = new BMessage;
fSudokuView->Field()->Archive(fStoredState, true);
fRestoreStateItem->SetEnabled(true);
break;
case kMsgRestoreState:
{
if (fStoredState == NULL)
break;
SudokuField* field = new SudokuField(fStoredState);
fSudokuView->SetTo(field);
break;
}
case kMsgSudokuSolved:
(new BAlert("Sudoku request",
"Sudoku solved - congratulations!", "Ok", NULL, NULL,

View File

@ -10,6 +10,7 @@
class BFile;
class BFilePanel;
class BMenuItem;
class GenerateSudoku;
class ProgressWindow;
class SudokuView;
@ -28,6 +29,7 @@ private:
status_t _LoadSettings(BMessage& settings);
status_t _SaveSettings();
void _ResetStoredState();
void _MessageDropped(BMessage *message);
void _Generate(int32 level);
@ -36,6 +38,8 @@ private:
ProgressWindow* fProgressWindow;
SudokuView* fSudokuView;
GenerateSudoku* fGenerator;
BMenuItem* fRestoreStateItem;
BMessage* fStoredState;
};
#endif // SUDOKU_WINDOW_H