Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added timeout functionality to askForString #1263

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 57 additions & 7 deletions libs/game/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ namespace game {
* Ask the player for a string value.
* @param message The message to display on the text-entry screen
* @param answerLength The maximum number of characters the user can enter (1 - 24)
* @param timeOut The time till the keyboard will time out. You can enter in seconds (0 - 99).
*/
//% group="Gameplay"
//% weight=10 help=game/ask-for-string
//% blockId=gameaskforstring block="ask for string %message || and max length %answerLength"
//% blockId=gameaskforstring block="ask for string %message || and max length %answerLength and timeout after %timeOut"
//% message.defl=""
//% answerLength.defl="12"
//% answerLength.min=1
//% answerLength.max=24
//% timeOut.defl=0
//% timeOut.min=0
//% timeOut.max=99
//% group="Prompt"
export function askForString(message: string, answerLength = 12) {
//% expandableArgumentMode="enabled"
export function askForString(message: string, answerLength = 12, timeOut: number = 0) {
let p = new game.Prompt();
const result = p.show(message, answerLength);
const result = p.show(message, answerLength, timeOut * 1000);
return result;
}

Expand Down Expand Up @@ -135,6 +140,8 @@ namespace game {
private inputIndex: number;
private blink: boolean;
private frameCount: number;
private timerEnd: number;
private timerSprite: Sprite;

constructor(theme?: PromptTheme) {
if (theme) {
Expand All @@ -159,24 +166,33 @@ namespace game {
this.inputIndex = 0;
}

show(message: string, answerLength: number) {
show(message: string, answerLength: number, timeOut: number) {
this.message = message;
this.answerLength = answerLength;
this.inputIndex = 0;

if (timeOut != 0) {
this.timerEnd = game.currentScene().millis() + timeOut;
}

controller._setUserEventsEnabled(false);
game.pushScene()

this.draw();
this.registerHandlers();
this.confirmPressed = false;

pauseUntil(() => this.confirmPressed);
pauseUntil(() => this.confirmPressed, timeOut);

game.popScene();
controller._setUserEventsEnabled(true);

return this.result;
if(this.confirmPressed) {
return this.result;
}
else {
return null;
}
}

private draw() {
Expand All @@ -186,6 +202,38 @@ namespace game {
this.drawBottomBar();
}

private drawTimerCheck() {
if(this.timerEnd !== undefined) {
this.drawTimer(this.timerEnd - game.currentScene().millis());
}
}

private drawTimer(millis: number) {
if (millis < 0) millis = 0;
millis |= 0;

const seconds = Math.idiv(millis, 1000) + 1;
const secondsString = seconds.toString()

if(this.timerSprite === undefined) {
const letter = image.create(CELL_WIDTH, CELL_HEIGHT);

this.timerSprite = sprites.create(letter, -1);

// Set to the top right corner of the screen
this.timerSprite.setPosition(155, 5);
}

this.timerSprite.image.fill(3);

if(secondsString.length != 1) {
this.timerSprite.image.print(secondsString, 0, LETTER_OFFSET_Y);
}
else {
this.timerSprite.image.print(secondsString, LETTER_OFFSET_X, LETTER_OFFSET_Y);
}
}

private drawPromptText() {
const prompt = sprites.create(layoutText(this.message, CONTENT_WIDTH, PROMPT_HEIGHT, this.theme.colorPrompt), -1);
prompt.x = screen.width / 2
Expand Down Expand Up @@ -359,6 +407,8 @@ namespace game {

this.updateSelectedInput();
}

this.drawTimerCheck()
})
}

Expand Down Expand Up @@ -539,4 +589,4 @@ namespace game {
}
}
}
}
}