Skip to content

Commit

Permalink
Use a dialog window when saving a game or recording (tmewett#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
zenzombie authored Feb 17, 2024
1 parent bcfa335 commit 933045b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
15 changes: 11 additions & 4 deletions src/brogue/IO.c
Original file line number Diff line number Diff line change
Expand Up @@ -2726,16 +2726,23 @@ void executeKeystroke(signed long keystroke, boolean controlKey, boolean shiftKe
boolean getInputTextString(char *inputText,
const char *prompt,
short maxLength,
const char *defaultEntry,
char *defaultEntry,
const char *promptSuffix,
short textEntryType,
boolean useDialogBox) {
short charNum, i, x, y;
short charNum, i, x, y, promptSuffixLen, defaultEntrylengthOverflow;
char keystroke, suffix[100];
const short textEntryBounds[TEXT_INPUT_TYPES][2] = {{' ', '~'}, {' ', '~'}, {'0', '9'}};
screenDisplayBuffer dbuf;
SavedDisplayBuffer rbuf;

// handle defaultEntry values exceeding maxLength
promptSuffixLen = strlen(promptSuffix);
defaultEntrylengthOverflow = strlen(defaultEntry) + promptSuffixLen - maxLength;
if(defaultEntrylengthOverflow > 0) {
defaultEntry[strlen(defaultEntry) - defaultEntrylengthOverflow] = '\0';
}

// x and y mark the origin for text entry.
if (useDialogBox) {
x = (COLS - max(maxLength, strLenWithoutEscapes(prompt))) / 2;
Expand Down Expand Up @@ -2795,8 +2802,8 @@ boolean getInputTextString(char *inputText,

inputText[charNum] = keystroke;
plotCharWithColor(keystroke, (windowpos){ x + charNum, y }, &white, &black);
printString(suffix, charNum + x + 1, y, &gray, &black, 0);
if (charNum < maxLength) {
if (charNum < maxLength - promptSuffixLen) {
printString(suffix, charNum + x + 1, y, &gray, &black, 0);
charNum++;
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/brogue/Recordings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,7 @@ void saveGameNoPrompt() {
rogue.gameExitStatusCode = EXIT_STATUS_SUCCESS;
rogue.recording = false;
}
#define MAX_TEXT_INPUT_FILENAME_LENGTH (COLS - 12) // max length including the suffix

void saveGame() {
char filePathWithoutSuffix[BROGUE_FILENAME_MAX - sizeof(GAME_SUFFIX)], filePath[BROGUE_FILENAME_MAX], defaultPath[BROGUE_FILENAME_MAX];
Expand All @@ -1212,11 +1213,10 @@ void saveGame() {
getAvailableFilePath(filePathWithoutSuffix, defaultPath, GAME_SUFFIX);
filePath[0] = '\0';

deleteMessages();
do {
askAgain = false;
if (getInputTextString(filePathWithoutSuffix, "Save game as (<esc> to cancel): ",
BROGUE_FILENAME_MAX - strlen(GAME_SUFFIX), filePathWithoutSuffix, GAME_SUFFIX, TEXT_INPUT_FILENAME, false)) {
MAX_TEXT_INPUT_FILENAME_LENGTH, filePathWithoutSuffix, GAME_SUFFIX, TEXT_INPUT_FILENAME, true)) {
snprintf(filePath, BROGUE_FILENAME_MAX, "%s%s", filePathWithoutSuffix, GAME_SUFFIX);
if (!fileExists(filePath) || confirm("File of that name already exists. Overwrite?", true)) {
remove(filePath);
Expand All @@ -1232,7 +1232,6 @@ void saveGame() {
}
}
} while (askAgain);
displayRecentMessages();
}

void saveRecordingNoPrompt(char *filePath) {
Expand Down Expand Up @@ -1260,11 +1259,10 @@ void saveRecording(char *filePathWithoutSuffix) {
getAvailableFilePath(filePathWithoutSuffix, defaultPath, RECORDING_SUFFIX);
filePath[0] = '\0';

deleteMessages();
do {
askAgain = false;
if (getInputTextString(filePathWithoutSuffix, "Save recording as (<esc> to cancel): ",
BROGUE_FILENAME_MAX - strlen(RECORDING_SUFFIX), filePathWithoutSuffix, RECORDING_SUFFIX, TEXT_INPUT_FILENAME, false)) {
MAX_TEXT_INPUT_FILENAME_LENGTH, filePathWithoutSuffix, RECORDING_SUFFIX, TEXT_INPUT_FILENAME, true)) {

snprintf(filePath, BROGUE_FILENAME_MAX, "%s%s", filePathWithoutSuffix, RECORDING_SUFFIX);
if (!fileExists(filePath) || confirm("File of that name already exists. Overwrite?", true)) {
Expand All @@ -1283,7 +1281,6 @@ void saveRecording(char *filePathWithoutSuffix) {
rogue.recording = false;
}
} while (askAgain);
deleteMessages();
}

static void copyFile(char *fromFilePath, char *toFilePath, unsigned long fromFileLength) {
Expand Down
2 changes: 1 addition & 1 deletion src/brogue/Rogue.h
Original file line number Diff line number Diff line change
Expand Up @@ -2985,7 +2985,7 @@ extern "C" {
boolean getInputTextString(char *inputText,
const char *prompt,
short maxLength,
const char *defaultEntry,
char *defaultEntry,
const char *promptSuffix,
short textEntryType,
boolean useDialogBox);
Expand Down
5 changes: 3 additions & 2 deletions src/brogue/RogueMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -1324,11 +1324,12 @@ void victory(boolean superVictory) {
rogue.playbackMode = false;
rogue.playbackMode = isPlayback;

displayMoreSign();

if (serverMode) {
// There's no save recording prompt, so let the player see achievements.
displayMoreSign();
saveRecordingNoPrompt(recordingFilename);
} else {
blackOutScreen();
saveRecording(recordingFilename);
printHighScores(qualified);
}
Expand Down

0 comments on commit 933045b

Please sign in to comment.