| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (c) 2000, 2006 Michael J. Roberts. All Rights Reserved. |
| 5 |
* |
| 6 |
* TADS 3 Library: input |
| 7 |
* |
| 8 |
* This modules defines functions and objects related to reading input |
| 9 |
* from the player. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "adv3.h" |
| 13 |
|
| 14 |
|
| 15 |
/* ------------------------------------------------------------------------ */ |
| 16 |
/* |
| 17 |
* Keyboard input parameter definition. |
| 18 |
*/ |
| 19 |
class InputDef: object |
| 20 |
/* |
| 21 |
* The prompt function. This is a function pointer (which is |
| 22 |
* frequently given as an anonymous function) or nil; if it's nil, |
| 23 |
* we won't show any prompt at all, otherwise we'll call the |
| 24 |
* function pointer to display a prompt as needed. |
| 25 |
*/ |
| 26 |
promptFunc = nil |
| 27 |
|
| 28 |
/* |
| 29 |
* Allow real-time events. If this is true, we'll allow real-time |
| 30 |
* events to interrupt the input; if it's nil, we'll freeze the |
| 31 |
* real-time clock while reading input. |
| 32 |
*/ |
| 33 |
allowRealTime = nil |
| 34 |
|
| 35 |
/* |
| 36 |
* Begin the input style. This should do anything required to set |
| 37 |
* the font to the desired attributes for the input text. By |
| 38 |
* default, we'll simply display <.inputline> to set up the default |
| 39 |
* input style. |
| 40 |
*/ |
| 41 |
beginInputFont() { "<.inputline>"; } |
| 42 |
|
| 43 |
/* |
| 44 |
* End the input style. By default, we'll close the <.inputline> |
| 45 |
* that we opened in beginInputFont(). |
| 46 |
*/ |
| 47 |
endInputFont() { "<./inputline>"; } |
| 48 |
; |
| 49 |
|
| 50 |
/* |
| 51 |
* Basic keyboard input parameter definition. This class defines |
| 52 |
* keyboard input parameters with the real-time status and prompt |
| 53 |
* function specified via the constructor. |
| 54 |
*/ |
| 55 |
class BasicInputDef: InputDef |
| 56 |
construct(allowRealTime, promptFunc) |
| 57 |
{ |
| 58 |
self.allowRealTime = allowRealTime; |
| 59 |
self.promptFunc = promptFunc; |
| 60 |
} |
| 61 |
; |
| 62 |
|
| 63 |
|
| 64 |
/* ------------------------------------------------------------------------ */ |
| 65 |
/* |
| 66 |
* Keyboard input manager. |
| 67 |
*/ |
| 68 |
inputManager: PostRestoreObject |
| 69 |
/* |
| 70 |
* Read a line of input from the keyboard. |
| 71 |
* |
| 72 |
* If allowRealTime is true, we'll execute any real-time events that |
| 73 |
* are already due to run, and then we'll allow the input to be |
| 74 |
* interrupted by real-time events, if interrupted input is |
| 75 |
* supported on the local platform. Otherwise, we will not process |
| 76 |
* any real-time events. |
| 77 |
* |
| 78 |
* promptFunc is a callback function to invoke to display the |
| 79 |
* prompt. This is provided as a callback so that we can re-display |
| 80 |
* the prompt as necessary after real-time event interruptions. |
| 81 |
* Note that if real-time interruption is not to be allowed, the |
| 82 |
* caller can simply display the prompt before calling this routine |
| 83 |
* rather than passing in a prompt callback, if desired. |
| 84 |
* |
| 85 |
* If we're in HTML mode, this will switch into the 'tads-input' |
| 86 |
* font while reading the line, so this routine should be used |
| 87 |
* wherever possible rather than calling inputLine() or |
| 88 |
* inputLineTimeout() directly. |
| 89 |
*/ |
| 90 |
getInputLine(allowRealTime, promptFunc) |
| 91 |
{ |
| 92 |
/* read input using a basic InputDef for the given parameters */ |
| 93 |
return getInputLineExt(new BasicInputDef(allowRealTime, promptFunc)); |
| 94 |
} |
| 95 |
|
| 96 |
/* |
| 97 |
* Read a line of input from the keyboard - extended interface, |
| 98 |
* using the InputDef object to define the input parameters. |
| 99 |
* 'defObj' is an instance of class InputDef, defining how we're to |
| 100 |
* handle the input. |
| 101 |
*/ |
| 102 |
getInputLineExt(defObj) |
| 103 |
{ |
| 104 |
/* make sure the command transcript is flushed */ |
| 105 |
if (gTranscript != nil) |
| 106 |
gTranscript.flushForInput(); |
| 107 |
|
| 108 |
/* |
| 109 |
* If a previous input was in progress, cancel it - this must be |
| 110 |
* a recursive entry from a real-time event that's interrupting |
| 111 |
* the enclosing input attempt. Simply cancel out the enclosing |
| 112 |
* read attempt entirely in this case; if and when we return to |
| 113 |
* the enclosing reader, that reader will start over with a |
| 114 |
* fresh read attempt at that point. |
| 115 |
*/ |
| 116 |
cancelInputInProgress(true); |
| 117 |
|
| 118 |
/* |
| 119 |
* Keep going until we finish reading the command. We might |
| 120 |
* have to try several times, because our attempts might be |
| 121 |
* interrupted by real-time events. |
| 122 |
*/ |
| 123 |
for (;;) |
| 124 |
{ |
| 125 |
local result; |
| 126 |
local timeout; |
| 127 |
local t0; |
| 128 |
|
| 129 |
/* note the starting time, in case we want to freeze the clock */ |
| 130 |
t0 = realTimeManager.getElapsedTime(); |
| 131 |
|
| 132 |
/* process real-time events, if possible */ |
| 133 |
timeout = processRealTimeEvents(defObj.allowRealTime); |
| 134 |
|
| 135 |
/* show the prompt and any pre-input codes */ |
| 136 |
inputLineBegin(defObj); |
| 137 |
|
| 138 |
getInput: |
| 139 |
/* |
| 140 |
* Read the input. (Note that if our timeout is nil, this |
| 141 |
* will simply act like the ordinary untimed inputLine.) |
| 142 |
*/ |
| 143 |
result = aioInputLineTimeout(timeout); |
| 144 |
|
| 145 |
/* |
| 146 |
* If we're not allowing real-time event processing, freeze |
| 147 |
* the clock during the read - set the elapsed game |
| 148 |
* real-time clock back to the value it had on entry, so |
| 149 |
* that the input effectively consumes no real time. |
| 150 |
*/ |
| 151 |
if (!defObj.allowRealTime) |
| 152 |
realTimeManager.setElapsedTime(t0); |
| 153 |
|
| 154 |
/* check the event code from the result list */ |
| 155 |
switch(result[1]) |
| 156 |
{ |
| 157 |
case InEvtNoTimeout: |
| 158 |
/* |
| 159 |
* the platform doesn't support timeouts - note it for |
| 160 |
* future reference so that we don't ask for input with |
| 161 |
* timeout again, then go back to try the input again |
| 162 |
* without a timeout |
| 163 |
*/ |
| 164 |
noInputTimeout = true; |
| 165 |
timeout = nil; |
| 166 |
goto getInput; |
| 167 |
|
| 168 |
case InEvtLine: |
| 169 |
/* we've finished the current line - end input mode */ |
| 170 |
inputLineEnd(); |
| 171 |
|
| 172 |
/* return the line of text we got */ |
| 173 |
return result[2]; |
| 174 |
|
| 175 |
case InEvtTimeout: |
| 176 |
/* |
| 177 |
* We got a timeout without finishing the input line. |
| 178 |
* This means that we've reached the time when the next |
| 179 |
* real-time event is ready to execute. Simply continue |
| 180 |
* looping; we'll process all real-time events that are |
| 181 |
* ready to go, then we'll resume reading the command. |
| 182 |
* |
| 183 |
* Before we proceed, though, notify the command |
| 184 |
* sequencer (via the command-interrupt pseudo-tag) that |
| 185 |
* we're at the start of output text after an interrupted |
| 186 |
* command line input |
| 187 |
*/ |
| 188 |
"<.commandint>"; |
| 189 |
break; |
| 190 |
|
| 191 |
case InEvtEof: |
| 192 |
/* |
| 193 |
* End of file - this indicates that the user has closed |
| 194 |
* down the application, or that the keyboard has become |
| 195 |
* unreadable due to a hardware or OS error. |
| 196 |
* |
| 197 |
* Write a blank line to the display in an attempt to |
| 198 |
* flush any partially-entered command line text, then |
| 199 |
* throw an error to signal the EOF condition. |
| 200 |
*/ |
| 201 |
"\b"; |
| 202 |
throw new EndOfFileException(); |
| 203 |
|
| 204 |
case InEvtEndQuietScript: |
| 205 |
/* |
| 206 |
* End of "quiet" script - this indicates that we've |
| 207 |
* been reading input from a script file, but we've now |
| 208 |
* reached the end of that file and are about to return |
| 209 |
* to reading from the keyboard. |
| 210 |
* |
| 211 |
* "Quiet script" mode causes all output to be hidden |
| 212 |
* while the script is being processed. This means that |
| 213 |
* we won't have displayed a prompt for the current |
| 214 |
* line, or updated the status line. We'll |
| 215 |
* automatically display a new prompt when we loop back |
| 216 |
* for another line of input, but we have to mark the |
| 217 |
* current input line as actually ended now for that to |
| 218 |
* happen. |
| 219 |
*/ |
| 220 |
inputLineInProgress = nil; |
| 221 |
inProgressDefObj = nil; |
| 222 |
|
| 223 |
/* |
| 224 |
* update the status line, since the quiet script mode |
| 225 |
* will have suppressed all status line updates while we |
| 226 |
* were reading the script, and thus the last update |
| 227 |
* before this prompt won't have been shown |
| 228 |
*/ |
| 229 |
statusLine.showStatusLine(); |
| 230 |
|
| 231 |
/* back for more */ |
| 232 |
break; |
| 233 |
|
| 234 |
case 'newGuest': |
| 235 |
/* |
| 236 |
* Synthetic "new guest" event from the Web UI. This |
| 237 |
* indicates that a new user has joined the session. The |
| 238 |
* parameter is the new user's screen name. Announce the |
| 239 |
* new user's arrival as a real-time event, and go back |
| 240 |
* to reading input. |
| 241 |
*/ |
| 242 |
"<.commandint>"; |
| 243 |
libMessages.webNewUser(result[2]); |
| 244 |
break; |
| 245 |
|
| 246 |
case 'logError': |
| 247 |
/* |
| 248 |
* Synthetic "log error" event from the Web UI. The UI |
| 249 |
* posts this type of an event when an error occurs in an |
| 250 |
* asynchronous task, where it's not possible to display |
| 251 |
* an error message directly. |
| 252 |
*/ |
| 253 |
"<.commandint>\b<<result[2]>>\b"; |
| 254 |
break; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
/* |
| 260 |
* Pause for a MORE prompt. If freezeRealTime is true, we'll stop |
| 261 |
* the real-time clock; otherwise we'll let it keep running. Even if |
| 262 |
* we don't freeze the clock, we won't actually process any real-time |
| 263 |
* events while waiting: the point of the MORE prompt is to allow the |
| 264 |
* player to read and acknowledge the on-screen display before |
| 265 |
* showing anything new, so we don't want to allow any output to |
| 266 |
* result from real-time events that occur while waiting for user |
| 267 |
* input. If any real-time events come due while we're waiting, |
| 268 |
* we'll process them when we're done. |
| 269 |
* |
| 270 |
* In order to ensure that the display makes sense to the user, we |
| 271 |
* flush any captured input in the transcript before pausing. We |
| 272 |
* re-activate transcript capture after the pause if it was active |
| 273 |
* before. Note that in some cases, this could affect the overall |
| 274 |
* output for the command, since some commands wait until the very |
| 275 |
* end of the command to go back and process the entire transcript |
| 276 |
* for the command. Since we interrupt the transcript, flushing any |
| 277 |
* output that occurred before the pause, a command that goes back |
| 278 |
* over its entire output stream at the end of the turn won't be able |
| 279 |
* to see or modify any of the output that occurred prior to the |
| 280 |
* pause, since we will have flushed the output to that point. |
| 281 |
*/ |
| 282 |
pauseForMore(freezeRealTime) |
| 283 |
{ |
| 284 |
local t0; |
| 285 |
local wasTranscriptActive = nil; |
| 286 |
|
| 287 |
/* |
| 288 |
* flush any command transcript and turn off transcript capture, |
| 289 |
* so that we show any pent-up reports before pausing for the |
| 290 |
* MORE prompt |
| 291 |
*/ |
| 292 |
if (gTranscript != nil) |
| 293 |
wasTranscriptActive = gTranscript.flushForInput(); |
| 294 |
|
| 295 |
/* |
| 296 |
* cancel any pending input - we must be interrupting the |
| 297 |
* pending input with a real-time event |
| 298 |
*/ |
| 299 |
cancelInputInProgress(true); |
| 300 |
|
| 301 |
/* note the starting time, in case we want to freeze the clock */ |
| 302 |
t0 = realTimeManager.getElapsedTime(); |
| 303 |
|
| 304 |
/* run the MORE prompt */ |
| 305 |
aioMorePrompt(); |
| 306 |
|
| 307 |
/* if the transcript was previously active, re-activate it */ |
| 308 |
if (wasTranscriptActive) |
| 309 |
gTranscript.activate(); |
| 310 |
|
| 311 |
/* |
| 312 |
* if the caller wanted us to freeze the clock, restore the |
| 313 |
* elapsed game real time to what it was when we started, so |
| 314 |
* that the time the player took to acknowledge the MORE prompt |
| 315 |
* won't count against the elapsed game time; otherwise, process |
| 316 |
* any real-time events that came due while we were waiting |
| 317 |
*/ |
| 318 |
if (freezeRealTime) |
| 319 |
{ |
| 320 |
/* time was frozen - restore the original elapsed time */ |
| 321 |
realTimeManager.setElapsedTime(t0); |
| 322 |
} |
| 323 |
else |
| 324 |
{ |
| 325 |
/* |
| 326 |
* time wasn't frozen - check for any events that have come |
| 327 |
* due since we started waiting, and process them |
| 328 |
* immediately |
| 329 |
*/ |
| 330 |
processRealTimeEvents(true); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
/* |
| 335 |
* Ask for an input file. Freezes the real-time event clock for the |
| 336 |
* duration of reading the event. |
| 337 |
*/ |
| 338 |
getInputFile(prompt, dialogType, fileType, flags) |
| 339 |
{ |
| 340 |
/* |
| 341 |
* note the game elapsed time before we start - we want to |
| 342 |
* freeze the real-time clock while we're waiting for the user |
| 343 |
* to respond, since this system verb exists outside of the |
| 344 |
* usual time flow of the game |
| 345 |
*/ |
| 346 |
local origElapsedTime = realTimeManager.getElapsedTime(); |
| 347 |
|
| 348 |
/* ask for a file */ |
| 349 |
local result = aioInputFile(prompt, dialogType, fileType, flags); |
| 350 |
|
| 351 |
/* |
| 352 |
* restore the game real-time counter to what it was before we |
| 353 |
* started the interactive response |
| 354 |
*/ |
| 355 |
realTimeManager.setElapsedTime(origElapsedTime); |
| 356 |
|
| 357 |
/* return the result from inputFile */ |
| 358 |
return result; |
| 359 |
} |
| 360 |
|
| 361 |
/* |
| 362 |
* Ask for input through a dialog. Freezes the real-time clock for |
| 363 |
* the duration of the dialog display. The arguments are the same as |
| 364 |
* for the built-in inputDialog() function. |
| 365 |
*/ |
| 366 |
getInputDialog(icon, prompt, buttons, defaultButton, cancelButton) |
| 367 |
{ |
| 368 |
/* |
| 369 |
* note the current elapsed game real time, so we can restore it |
| 370 |
* after the dialog is done |
| 371 |
*/ |
| 372 |
local origElapsedTime = realTimeManager.getElapsedTime(); |
| 373 |
|
| 374 |
/* show the dialog */ |
| 375 |
local result = aioInputDialog(icon, prompt, buttons, |
| 376 |
defaultButton, cancelButton); |
| 377 |
|
| 378 |
/* |
| 379 |
* restore the real-time counter, so that the time spent in the |
| 380 |
* dialog doesn't count |
| 381 |
*/ |
| 382 |
realTimeManager.setElapsedTime(origElapsedTime); |
| 383 |
|
| 384 |
/* return the dialog result */ |
| 385 |
return result; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
/* |
| 390 |
* Read a keystroke, processing real-time events while waiting, if |
| 391 |
* desired. 'allowRealTime' and 'promptFunc' work the same way they |
| 392 |
* do with getInputLine(). |
| 393 |
*/ |
| 394 |
getKey(allowRealTime, promptFunc) |
| 395 |
{ |
| 396 |
local evt; |
| 397 |
|
| 398 |
/* get an event */ |
| 399 |
evt = getEventOrKey(allowRealTime, promptFunc, true); |
| 400 |
|
| 401 |
/* |
| 402 |
* the only event that getEventOrKey will return is a keystroke, |
| 403 |
* so return the keystroke from the event record |
| 404 |
*/ |
| 405 |
return evt[2]; |
| 406 |
} |
| 407 |
|
| 408 |
/* |
| 409 |
* Read an event, processing real-time events while waiting, if |
| 410 |
* desired. 'allowRealTime' and 'promptFunc' work the same way they |
| 411 |
* do with getInputLine(). |
| 412 |
*/ |
| 413 |
getEvent(allowRealTime, promptFunc) |
| 414 |
{ |
| 415 |
/* read and return an event */ |
| 416 |
return getEventOrKey(allowRealTime, promptFunc, nil); |
| 417 |
} |
| 418 |
|
| 419 |
/* |
| 420 |
* Read an event or keystroke. 'allowRealTime' and 'promptFunc' work |
| 421 |
* the same way they do in getInputLine(). If 'keyOnly' is true, |
| 422 |
* then we're only interested in keystroke events, and we'll ignore |
| 423 |
* any other events entered. |
| 424 |
* |
| 425 |
* Note that this routine is not generally called directly; callers |
| 426 |
* should usually call the convenience routines getKey() or |
| 427 |
* getEvent(), as needed. |
| 428 |
*/ |
| 429 |
getEventOrKey(allowRealTime, promptFunc, keyOnly) |
| 430 |
{ |
| 431 |
/* make sure the command transcript is flushed */ |
| 432 |
if (gTranscript != nil) |
| 433 |
gTranscript.flushForInput(); |
| 434 |
|
| 435 |
/* |
| 436 |
* Cancel any in-progress input. If there's an in-progress |
| 437 |
* input, a real-time event must be interrupting the input, |
| 438 |
* which is recursively invoking us to start a new input. |
| 439 |
*/ |
| 440 |
cancelInputInProgress(true); |
| 441 |
|
| 442 |
/* keep going until we get a keystroke or other event */ |
| 443 |
for (;;) |
| 444 |
{ |
| 445 |
local result; |
| 446 |
local timeout; |
| 447 |
local t0; |
| 448 |
|
| 449 |
/* note the starting time, in case we want to freeze the clock */ |
| 450 |
t0 = realTimeManager.getElapsedTime(); |
| 451 |
|
| 452 |
/* process real-time events, if possible */ |
| 453 |
timeout = processRealTimeEvents(allowRealTime); |
| 454 |
|
| 455 |
/* show the prompt and any pre-input codes */ |
| 456 |
inputEventBegin(promptFunc); |
| 457 |
|
| 458 |
getInput: |
| 459 |
/* |
| 460 |
* Read the input. (Note that if our timeout is nil, this |
| 461 |
* will simply act like the ordinary untimed inputLine.) |
| 462 |
*/ |
| 463 |
result = aioInputEvent(timeout); |
| 464 |
|
| 465 |
/* |
| 466 |
* If we're not allowing real-time event processing, freeze |
| 467 |
* the clock during the read - set the elapsed game |
| 468 |
* real-time clock back to the value it had on entry, so |
| 469 |
* that the input effectively consumes no real time. |
| 470 |
*/ |
| 471 |
if (!allowRealTime) |
| 472 |
realTimeManager.setElapsedTime(t0); |
| 473 |
|
| 474 |
/* check the event code from the result list */ |
| 475 |
switch(result[1]) |
| 476 |
{ |
| 477 |
case InEvtNoTimeout: |
| 478 |
/* |
| 479 |
* the platform doesn't support timeouts - note it for |
| 480 |
* future reference so that we don't ask for input with |
| 481 |
* timeout again, then go back to try the input again |
| 482 |
* without a timeout |
| 483 |
*/ |
| 484 |
noInputTimeout = true; |
| 485 |
timeout = nil; |
| 486 |
goto getInput; |
| 487 |
|
| 488 |
case InEvtTimeout: |
| 489 |
/* |
| 490 |
* We got a timeout without finishing the input line. |
| 491 |
* This means that we've reached the time when the next |
| 492 |
* real-time event is ready to execute. Simply continue |
| 493 |
* looping; we'll process all real-time events that are |
| 494 |
* ready to go, then we'll restart the event wait. |
| 495 |
*/ |
| 496 |
break; |
| 497 |
|
| 498 |
case InEvtEof: |
| 499 |
/* |
| 500 |
* End of file - this indicates that the user has closed |
| 501 |
* down the application, or that the keyboard has become |
| 502 |
* unreadable due to a hardware or OS error. |
| 503 |
* |
| 504 |
* Write a blank line to the display in an attempt to |
| 505 |
* flush any partially-entered command line text, then |
| 506 |
* throw an error to signal the EOF condition. |
| 507 |
*/ |
| 508 |
"\b"; |
| 509 |
throw new EndOfFileException(); |
| 510 |
|
| 511 |
case InEvtKey: |
| 512 |
/* keystroke - finish the input and return the event */ |
| 513 |
inputEventEnd(); |
| 514 |
return result; |
| 515 |
|
| 516 |
case InEvtHref: |
| 517 |
/* |
| 518 |
* Hyperlink activation - if we're allowed to return |
| 519 |
* events other than keystrokes, finish the input and |
| 520 |
* return the event; otherwise, ignore the event and keep |
| 521 |
* looping. |
| 522 |
*/ |
| 523 |
if (!keyOnly) |
| 524 |
{ |
| 525 |
inputEventEnd(); |
| 526 |
return result; |
| 527 |
} |
| 528 |
break; |
| 529 |
|
| 530 |
default: |
| 531 |
/* ignore other events */ |
| 532 |
break; |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
/* |
| 538 |
* Cancel input in progress. |
| 539 |
* |
| 540 |
* If 'reset' is true, we'll clear any input state saved from the |
| 541 |
* interrupted in-progress editing session; otherwise, we'll retain |
| 542 |
* the saved editing state for restoration on the next input. |
| 543 |
* |
| 544 |
* This MUST be called before calling tadsSay(). Games should |
| 545 |
* generally never call tadsSay() directly (call the library |
| 546 |
* function say() instead), so in most cases authors will not need |
| 547 |
* to worry about calling this on output. |
| 548 |
* |
| 549 |
* This MUST ALSO be called before performing any keyboard input. |
| 550 |
* Callers using inputManager methods for keyboard operations won't |
| 551 |
* have to worry about this, because the inputManager methods call |
| 552 |
* this routine when necessary. |
| 553 |
*/ |
| 554 |
cancelInputInProgress(reset) |
| 555 |
{ |
| 556 |
/* cancel the interpreter's internal input state */ |
| 557 |
aioInputLineCancel(reset); |
| 558 |
|
| 559 |
/* if we were editing a command line, terminate the editing session */ |
| 560 |
if (inputLineInProgress) |
| 561 |
{ |
| 562 |
/* do our normal after-input work */ |
| 563 |
inputLineEnd(); |
| 564 |
} |
| 565 |
|
| 566 |
/* if we were waiting for event input, note that we are no longer */ |
| 567 |
if (inputEventInProgress) |
| 568 |
{ |
| 569 |
/* do our normal after-input work */ |
| 570 |
inputEventEnd(); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
/* |
| 575 |
* Process any real-time events that are ready to run, and return the |
| 576 |
* timeout until the next real-time event. |
| 577 |
* |
| 578 |
* If allowRealTime is nil, we won't process real-time events at all; |
| 579 |
* we'll merely return nil for the timeout to indicate to the caller |
| 580 |
* that any user input interaction about to be attempted should wait |
| 581 |
* indefinitely. |
| 582 |
*/ |
| 583 |
processRealTimeEvents(allowRealTime) |
| 584 |
{ |
| 585 |
local timeout; |
| 586 |
|
| 587 |
/* presume we will not use a timeout */ |
| 588 |
timeout = nil; |
| 589 |
|
| 590 |
/* process real-time events, if allowed */ |
| 591 |
if (allowRealTime) |
| 592 |
{ |
| 593 |
local tNext; |
| 594 |
|
| 595 |
/* |
| 596 |
* Process any real-time events that are currently ready to |
| 597 |
* execute, and note the amount of time until the next |
| 598 |
* real-time event is ready. |
| 599 |
*/ |
| 600 |
tNext = realTimeManager.executeEvents(); |
| 601 |
|
| 602 |
/* |
| 603 |
* If there's an event pending, note the interval between the |
| 604 |
* current time and the event's scheduled time - this will |
| 605 |
* give us the maximum amount of time we want to wait for the |
| 606 |
* user to edit the command line before interrupting to |
| 607 |
* execute the pending event. Ignore this if the platform |
| 608 |
* doesn't support timeouts to begin with. |
| 609 |
*/ |
| 610 |
if (tNext != nil && !noInputTimeout) |
| 611 |
timeout = tNext - realTimeManager.getElapsedTime(); |
| 612 |
} |
| 613 |
|
| 614 |
/* return the timeout until the next real-time event */ |
| 615 |
return timeout; |
| 616 |
} |
| 617 |
|
| 618 |
/* |
| 619 |
* Begin reading key/event input. We'll cancel any report gatherer |
| 620 |
* so that prompt text shows immediately, and show the prompt if |
| 621 |
* desired. |
| 622 |
*/ |
| 623 |
inputEventBegin(promptFunc) |
| 624 |
{ |
| 625 |
/* if we're not continuing previous input, show the prompt */ |
| 626 |
if (!inputEventInProgress) |
| 627 |
{ |
| 628 |
inputBegin(promptFunc); |
| 629 |
|
| 630 |
/* note that we're in input mode */ |
| 631 |
inputEventInProgress = true; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
/* |
| 636 |
* End keystroke/event input. |
| 637 |
*/ |
| 638 |
inputEventEnd() |
| 639 |
{ |
| 640 |
/* if input is in progress, terminate it */ |
| 641 |
if (inputEventInProgress) |
| 642 |
{ |
| 643 |
/* note that we're no longer reading an event */ |
| 644 |
inputEventInProgress = nil; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
/* |
| 649 |
* Begin command line editing. If we're in HTML mode, we'll show |
| 650 |
* the appropriate codes to establish the input font. |
| 651 |
*/ |
| 652 |
inputLineBegin(defObj) |
| 653 |
{ |
| 654 |
/* notify the command sequencer that we're reading a command */ |
| 655 |
"<.commandbefore>"; |
| 656 |
|
| 657 |
/* if we're not resuming a session, set up a new session */ |
| 658 |
if (!inputLineInProgress) |
| 659 |
{ |
| 660 |
/* begin input */ |
| 661 |
inputBegin(defObj.promptFunc); |
| 662 |
|
| 663 |
/* switch to input font */ |
| 664 |
defObj.beginInputFont(); |
| 665 |
|
| 666 |
/* note that we're in input mode */ |
| 667 |
inputLineInProgress = true; |
| 668 |
|
| 669 |
/* remember the parameter object for this input */ |
| 670 |
inProgressDefObj = defObj; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
/* |
| 675 |
* End command line editing. If we're in HTML mode, we'll show the |
| 676 |
* appropriate codes to close the input font. |
| 677 |
*/ |
| 678 |
inputLineEnd() |
| 679 |
{ |
| 680 |
/* if input is in progress, terminate it */ |
| 681 |
if (inputLineInProgress) |
| 682 |
{ |
| 683 |
/* note that we're no longer reading a line of input */ |
| 684 |
inputLineInProgress = nil; |
| 685 |
|
| 686 |
/* end input font mode */ |
| 687 |
inProgressDefObj.endInputFont(); |
| 688 |
|
| 689 |
/* notify the command sequencer that we're done reading */ |
| 690 |
"<.commandafter>"; |
| 691 |
|
| 692 |
/* |
| 693 |
* tell the main text area's output stream that we just |
| 694 |
* ended an input line |
| 695 |
*/ |
| 696 |
mainOutputStream.inputLineEnd(); |
| 697 |
|
| 698 |
/* forget the parameter object for the input */ |
| 699 |
inProgressDefObj = nil; |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
/* |
| 704 |
* Begin generic input. Cancels command report list capture, and |
| 705 |
* shows the prompt if given. |
| 706 |
*/ |
| 707 |
inputBegin(promptFunc) |
| 708 |
{ |
| 709 |
/* |
| 710 |
* Turn off command transcript capture, if it's active. Once |
| 711 |
* we're soliciting input interactively, we can no longer |
| 712 |
* usefully capture the text output of commands, but this is fine |
| 713 |
* because we must be doing something for which capture isn't |
| 714 |
* important anyway. Reporting capture is used for things like |
| 715 |
* selecting the kind of result to show, which clearly isn't a |
| 716 |
* factor for actions involving interactive input. |
| 717 |
*/ |
| 718 |
if (gTranscript != nil) |
| 719 |
gTranscript.flushForInput(); |
| 720 |
|
| 721 |
/* if we have a prompt, display it */ |
| 722 |
if (promptFunc != nil) |
| 723 |
(promptFunc)(); |
| 724 |
} |
| 725 |
|
| 726 |
/* receive post-restore notification */ |
| 727 |
execute() |
| 728 |
{ |
| 729 |
/* |
| 730 |
* Reset the inputLine state. If we had any previously |
| 731 |
* interrupted input from the current interpreter session, forget |
| 732 |
* it by canceling and resetting the input line. If we had an |
| 733 |
* interrupted line in the session being restored, forget about |
| 734 |
* that, too. |
| 735 |
*/ |
| 736 |
aioInputLineCancel(true); |
| 737 |
inputLineInProgress = nil; |
| 738 |
inputEventInProgress = nil; |
| 739 |
|
| 740 |
/* |
| 741 |
* Clear the inputLineTimeout disabling flag - we might be |
| 742 |
* restoring the game on a different platform from the one where |
| 743 |
* the game started, so we might be able to use timed command |
| 744 |
* line input even if we didn't when we started the game. By |
| 745 |
* clearing this flag, we'll check again to see if we can |
| 746 |
* perform timed input; if we can't, we'll just set the flag |
| 747 |
* again, so there will be no harm done. |
| 748 |
*/ |
| 749 |
noInputTimeout = nil; |
| 750 |
} |
| 751 |
|
| 752 |
/* |
| 753 |
* Flag: command line input is in progress. If this is set, it means |
| 754 |
* that we interrupted command-line editing by a timeout, so we |
| 755 |
* should not show a prompt the next time we go back to the keyboard |
| 756 |
* for input. |
| 757 |
*/ |
| 758 |
inputLineInProgress = nil |
| 759 |
|
| 760 |
/* the InputDef object for the input in progress */ |
| 761 |
inProgressDefObj = nil |
| 762 |
|
| 763 |
/* flag: keystroke/event input is in progress */ |
| 764 |
inputEventInProgress = nil |
| 765 |
|
| 766 |
/* |
| 767 |
* Flag: inputLine does not support timeouts on the current platform. |
| 768 |
* We set this when we get an InEvtNoTimeout return code from |
| 769 |
* inputLineTimeout, so that we'll know not to try calling again with |
| 770 |
* a timeout. This applies to the current interpreter only, so we |
| 771 |
* must ignore any value restored from a previously saved game, since |
| 772 |
* the game might have been saved on a different platform. |
| 773 |
* |
| 774 |
* Note that if this value is nil, it means only that we've never |
| 775 |
* seen an InEvtNoTimeout return code from inputLineEvent - it does |
| 776 |
* NOT mean that timeouts are supported locally. |
| 777 |
* |
| 778 |
* We assume that the input functions are uniform in their treatment |
| 779 |
* of timeouts; that is, we assume that if inputLineTimeout supports |
| 780 |
* timeout, then so does inputEvent, and that if one doesn't support |
| 781 |
* timeout, the other won't either. |
| 782 |
*/ |
| 783 |
noInputTimeout = nil |
| 784 |
; |
| 785 |
|
| 786 |
|
| 787 |
/* ------------------------------------------------------------------------ */ |
| 788 |
/* |
| 789 |
* Read a command line from the player. Displays the main command |
| 790 |
* prompt and returns a line of input. |
| 791 |
* |
| 792 |
* We process any pending real-time events before reading the command. |
| 793 |
* If the local platform supports real-time command-line interruptions, |
| 794 |
* we'll continue processing real-time events as they occur in the |
| 795 |
* course of command editing. |
| 796 |
*/ |
| 797 |
readMainCommand(which) |
| 798 |
{ |
| 799 |
local str; |
| 800 |
|
| 801 |
/* execute any pre-command-prompt daemons */ |
| 802 |
eventManager.executePrompt(); |
| 803 |
|
| 804 |
/* |
| 805 |
* Read a line of input, allowing real-time event processing, and |
| 806 |
* return the line of text we read. Use the appropriate main |
| 807 |
* command prompt for the given prompt mode. |
| 808 |
*/ |
| 809 |
str = inputManager.getInputLine( |
| 810 |
true, {: gLibMessages.mainCommandPrompt(which) }); |
| 811 |
|
| 812 |
/* return the string we read */ |
| 813 |
return str; |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
/* ------------------------------------------------------------------------ */ |
| 818 |
/* |
| 819 |
* End-of-file exception - this is thrown when readMainCommand() |
| 820 |
* encounters end of file reading the console input. |
| 821 |
*/ |
| 822 |
class EndOfFileException: Exception |
| 823 |
; |
| 824 |
|
| 825 |
|
| 826 |
/* ------------------------------------------------------------------------ */ |
| 827 |
/* |
| 828 |
* 'Quitting' exception. This isn't an error - it merely indicates that |
| 829 |
* the user has explicitly asked to quit the game. |
| 830 |
*/ |
| 831 |
class QuittingException: Exception |
| 832 |
; |
| 833 |
|
| 834 |
/* ------------------------------------------------------------------------ */ |
| 835 |
/* |
| 836 |
* Base class for command input string preparsers. |
| 837 |
* |
| 838 |
* Preparsers must be registered in order to run. During |
| 839 |
* preinitialization, we will automatically register any existing |
| 840 |
* preparser objects; preparsers that are created dynamically during |
| 841 |
* execution must be registered explicitly, which can be accomplished by |
| 842 |
* inheriting the default constructor from this class. |
| 843 |
*/ |
| 844 |
class StringPreParser: PreinitObject |
| 845 |
/* |
| 846 |
* My execution order number. When multiple preparsers are |
| 847 |
* registered, we'll run the preparsers in ascending order of this |
| 848 |
* value (i.e., smallest runOrder goes first). |
| 849 |
*/ |
| 850 |
runOrder = 100 |
| 851 |
|
| 852 |
/* |
| 853 |
* Do our parsing. Each instance should override this method to |
| 854 |
* define the parsing that it does. |
| 855 |
* |
| 856 |
* 'str' is the string to parse, and 'which' is the rmcXxx enum |
| 857 |
* giving the type of command we're working with. |
| 858 |
* |
| 859 |
* This method returns a string or nil. If the method returns a |
| 860 |
* string, the caller will forget the original string and work from |
| 861 |
* here on out with the new version returned; this allows the method |
| 862 |
* to rewrite the original input as desired. If the method returns |
| 863 |
* nil, it means that the string has been fully handled and that |
| 864 |
* further parsing of the same string is not desired. |
| 865 |
*/ |
| 866 |
doParsing(str, which) |
| 867 |
{ |
| 868 |
/* return the original string unchanged */ |
| 869 |
return str; |
| 870 |
} |
| 871 |
|
| 872 |
/* |
| 873 |
* construction - when we dynamically create a preparser, register |
| 874 |
* it by default |
| 875 |
*/ |
| 876 |
construct() |
| 877 |
{ |
| 878 |
/* register the preparser */ |
| 879 |
StringPreParser.registerPreParser(self); |
| 880 |
} |
| 881 |
|
| 882 |
/* run pre-initialization */ |
| 883 |
execute() |
| 884 |
{ |
| 885 |
/* register the preparser if it's not already registered */ |
| 886 |
StringPreParser.registerPreParser(self); |
| 887 |
} |
| 888 |
|
| 889 |
/* register a preparser */ |
| 890 |
registerPreParser(pp) |
| 891 |
{ |
| 892 |
/* if the preparser isn't already in our list, add it */ |
| 893 |
if (regList.indexOf(pp) == nil) |
| 894 |
{ |
| 895 |
/* append this new item to the list */ |
| 896 |
regList.append(pp); |
| 897 |
|
| 898 |
/* the list is no longer sorted */ |
| 899 |
regListSorted = nil; |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
/* |
| 904 |
* Class method - Run all preparsers. Returns the result of |
| 905 |
* successively calling each preparser on the given string. |
| 906 |
*/ |
| 907 |
runAll(str, which) |
| 908 |
{ |
| 909 |
/* |
| 910 |
* if the list of preparsers isn't sorted, sort it in ascending |
| 911 |
* order of execution order number |
| 912 |
*/ |
| 913 |
if (!regListSorted) |
| 914 |
{ |
| 915 |
/* sort the list */ |
| 916 |
regList.sort(SortAsc, {x, y: x.runOrder - y.runOrder}); |
| 917 |
|
| 918 |
/* the list is now sorted */ |
| 919 |
regListSorted = true; |
| 920 |
} |
| 921 |
|
| 922 |
/* run each preparser */ |
| 923 |
foreach (local cur in regList) |
| 924 |
{ |
| 925 |
/* run this preparser */ |
| 926 |
str = cur.doParsing(str, which); |
| 927 |
|
| 928 |
/* |
| 929 |
* if the result is nil, it means that the string has been |
| 930 |
* fully handled, so we need not run any further preparsing |
| 931 |
*/ |
| 932 |
if (str == nil) |
| 933 |
return nil; |
| 934 |
} |
| 935 |
|
| 936 |
/* return the result of the series of preparsing steps */ |
| 937 |
return str; |
| 938 |
} |
| 939 |
|
| 940 |
/* class property containing the list of registered parsers */ |
| 941 |
regList = static new Vector(10) |
| 942 |
|
| 943 |
/* class property - the registration list has been sorted */ |
| 944 |
regListSorted = nil |
| 945 |
; |
| 946 |
|
| 947 |
/* ------------------------------------------------------------------------ */ |
| 948 |
/* |
| 949 |
* The "comment" pre-parser. If the command line starts with a special |
| 950 |
* prefix string (by default, "*", but this can be changed via our |
| 951 |
* commentPrefix property), this pre-parser intercepts the command, |
| 952 |
* treating it as a comment from the player and otherwise ignoring the |
| 953 |
* entire input line. The main purpose is to give players a way to put |
| 954 |
* comments into recorded transcripts, as notes to themselves when later |
| 955 |
* reviewing the transcripts or as notes to the author when submitting |
| 956 |
* play-testing feedback. |
| 957 |
*/ |
| 958 |
commentPreParser: StringPreParser |
| 959 |
doParsing(str, which) |
| 960 |
{ |
| 961 |
/* get the amount of leading whitespace, so we can ignore it */ |
| 962 |
local sp = rexMatch(leadPat, str); |
| 963 |
|
| 964 |
/* |
| 965 |
* if the command line starts with the comment prefix, treat it |
| 966 |
* as a comment |
| 967 |
*/ |
| 968 |
if (str.substr(sp + 1, commentPrefix.length()) == commentPrefix) |
| 969 |
{ |
| 970 |
/* |
| 971 |
* It's a comment. |
| 972 |
* |
| 973 |
* If a transcript is being recorded, simply acknowledge the |
| 974 |
* comment; if not, acknowledge it, but with a warning that |
| 975 |
* the comment isn't being saved anywhere |
| 976 |
*/ |
| 977 |
if (scriptStatus.scriptFile != nil) |
| 978 |
gLibMessages.noteWithScript; |
| 979 |
else if (warningCount++ == 0) |
| 980 |
gLibMessages.noteWithoutScriptWarning; |
| 981 |
else |
| 982 |
gLibMessages.noteWithoutScript; |
| 983 |
|
| 984 |
/* |
| 985 |
* Otherwise completely ignore the command line. To do this, |
| 986 |
* simply return nil: this tells the parser that the command |
| 987 |
* has been fully handled by the preparser. |
| 988 |
*/ |
| 989 |
return nil; |
| 990 |
} |
| 991 |
else |
| 992 |
{ |
| 993 |
/* it's not a command - return the string unchanged */ |
| 994 |
return str; |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
/* |
| 999 |
* The comment prefix. You can change this to any character, or to |
| 1000 |
* any sequence of characters (longer sequences, such as '//', will |
| 1001 |
* work fine). If a command line starts with this exact string (or |
| 1002 |
* starts with whitespace followed by this string), we'll consider |
| 1003 |
* the line to be a comment. |
| 1004 |
*/ |
| 1005 |
commentPrefix = '*' |
| 1006 |
|
| 1007 |
/* |
| 1008 |
* The leading-whitespace pattern. We skip any text that matches |
| 1009 |
* this pattern at the start of a command line before looking for the |
| 1010 |
* comment prefix. |
| 1011 |
* |
| 1012 |
* If you don't want to allow leading whitespace before the comment |
| 1013 |
* prefix, you can simply change this to '' - a pattern consisting of |
| 1014 |
* an empty string always matches zero characters, so it will prevent |
| 1015 |
* us from skipping any leading charactres in the player's input. |
| 1016 |
*/ |
| 1017 |
leadPat = static new RexPattern('<space>*') |
| 1018 |
|
| 1019 |
/* warning count for entering comments without SCRIPT in effect */ |
| 1020 |
warningCount = 0 |
| 1021 |
|
| 1022 |
/* |
| 1023 |
* Use a lower execution order than the default, so that we run |
| 1024 |
* before most other pre-parsers. Most other pre-parsers are written |
| 1025 |
* to handle actual commands, so it's usually just a waste of time to |
| 1026 |
* have them look at comments at all - and can occasionally be |
| 1027 |
* problematic, since the free-form text of a comment could confuse a |
| 1028 |
* pre-parser that's expecting a more conventional command format. |
| 1029 |
* When the comment pre-parser detects a comment, it halts any |
| 1030 |
* further processing of the command - so by running ahead of other |
| 1031 |
* pre-parsers, we'll effectively bypass other pre-parsers when we |
| 1032 |
* detect a comment. |
| 1033 |
*/ |
| 1034 |
runOrder = 50 |
| 1035 |
; |
| 1036 |
|
| 1037 |
|
| 1038 |
/* ------------------------------------------------------------------------ */ |
| 1039 |
/* |
| 1040 |
* Read a line of text and return the token list and the original text. |
| 1041 |
* We keep going until a non-empty line of text is read. |
| 1042 |
* |
| 1043 |
* 'which' is one of the rmcXxx enum values specifying what kind of |
| 1044 |
* command line we're reading. |
| 1045 |
* |
| 1046 |
* The return value is a list of two elements. The first element is the |
| 1047 |
* string entered, and the second element is the token list. |
| 1048 |
*/ |
| 1049 |
readMainCommandTokens(which) |
| 1050 |
{ |
| 1051 |
local str; |
| 1052 |
local toks; |
| 1053 |
|
| 1054 |
/* keep going until we get a non-empty command line */ |
| 1055 |
for (;;) |
| 1056 |
{ |
| 1057 |
/* read a command line */ |
| 1058 |
str = readMainCommand(which); |
| 1059 |
|
| 1060 |
/* run any preparsing desired on the string */ |
| 1061 |
str = StringPreParser.runAll(str, which); |
| 1062 |
|
| 1063 |
/* |
| 1064 |
* if preparsing returned nil, it means that the preparser fully |
| 1065 |
* handled the string - simply return nil to tell the caller |
| 1066 |
* that its work is done |
| 1067 |
*/ |
| 1068 |
if (str == nil) |
| 1069 |
return nil; |
| 1070 |
|
| 1071 |
try |
| 1072 |
{ |
| 1073 |
/* tokenize the command string */ |
| 1074 |
toks = cmdTokenizer.tokenize(str); |
| 1075 |
} |
| 1076 |
catch (TokErrorNoMatch tokExc) |
| 1077 |
{ |
| 1078 |
/* |
| 1079 |
* Invalid tokens in the response - complain about it. Flag |
| 1080 |
* the error as being in the first character of the |
| 1081 |
* remaining string, since that's the character for which we |
| 1082 |
* could find no match. |
| 1083 |
*/ |
| 1084 |
gLibMessages.invalidCommandToken(tokExc.curChar_.htmlify()); |
| 1085 |
|
| 1086 |
/* go back for another input line */ |
| 1087 |
continue; |
| 1088 |
} |
| 1089 |
|
| 1090 |
/* if we got a non-empty token list, return it */ |
| 1091 |
if (toks.length() != 0) |
| 1092 |
return [str, toks]; |
| 1093 |
|
| 1094 |
/* show the empty-command reply */ |
| 1095 |
gLibMessages.emptyCommandResponse(); |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
|