Text Window Input-Output Control

version 2 by Erik Temple

  • Home page
  • Beginning
  • Previous
  • Next



  • Example: ** On the Edge - Another dual-window I/O example, but this time with a number of refinements. The input window clears after each command, to avoid scrolling, and is set off from the main window by a horizontal line (actually, a very narrow graphics window). To make up for the lack of command history in the input window, we echo each command in the output window before printing the result. The example also serves to illustrate a number of tweaks related to line spacing and transcript output.

    We begin by defining the input window, as well as the graphics window that serves as the border between the input window and the main window. After opening these windows and declaring the input window to be the "current text input window", we start the game with some yes-no input, as a way of demonstrating that functionality.

        "On the Edge"

        Include Text Window Input-Output Control by Erik Temple.
        
        The input-window is a text-buffer g-window spawned by the main-window. The position is g-placebelow. The measurement is 10.
        
        The border-window is a graphics g-window spawned by the main-window. The position is g-placebelow. The scale method is g-fixed-size. The measurement is 2. The back-colour is g-dark-grey.
        
        When play begins:
            open up the input-window as the main text input window;
            open up the border-window;
            say "Are you sure you want to 'play' this 'game'?";
            unless the player consents:
                say "Well, that's probably for the best. Enjoy your life.";
                follow the immediately quit rule;
            say "OK, here we go...".

    We want the input window to clear after each command, so we slot that behavior into the "printing the command prompt" activity. We also insert some line breaks to improve the paragraphing behavior.

        Before printing the command prompt:
            clear the current text input window.
            
        After printing a parser error:
            say line break.
        
        Before reading a command when the current action is restarting the game or the current action is quitting the game:
            say line break.

    We are clearing the input window after each command. This leaves the player with no visual record of the commands she has entered. So, we append the text of the command to the main window after reading it, printing it in italics to set it off from the other text in the window. However, because the transcript is receiving text streams from both the input and output windows, it will already have the input text. To avoid printing the player's input to the transcript twice, we temporarily suspend the main window's "echo stream"--the stream of data that is sent to the transcript--before printing the input in the main window.

    This example also includes hyperlink input (see the next code block). Commands pasted from the hyperlink look good onscreen, but they need a line break when printing to the transcript. The definition of the "command-pasting terminator" global variable adds a line break to the transcript ONLY, bypassing the main window.

        After reading a command:
            shut down the echo stream of the main-window;
            say "[italic type][player's command][roman type]: [run paragraph on]";
            echo the stream of the main-window to the transcript.
        
         The command-pasting terminator is "[run paragraph on][if we are writing a transcript][echo stream of current text input window][line break][stream of current text input window][end if]".

    Finally, we have the scenario. We include two alternate forms of input, a hyperlink interface and single keystroke, merely to illustrate how such alternate input methods might work with the dual-window I/O, you are correct. Standard input works as well.

        Ledge is a room. "You are standing on a narrow ledge that encircles the upper floor of a very high building. There are no windows, no signs of life, no apparent exit except [link 1]jumping[end link]."
        
        Table of Glulx Hyperlink Replacement Commands (continued)
      link ID  replacement  
      1  "JUMP"  
      2  "EXAMINE THE PEN"  
      3  "TAKE THE PEN"  
        
        Instead of jumping:
            say "You throw yourself forward...";
            end the game in death.
                        
        The pen is in the Ledge. "Someone left a [link 2]pen[end link] on the ledge..." The description is "Odd, the [link 3]pen[end link] looks familiar."
        
        Instead of taking the pen:
            say "Press the space bar.";
            wait for the SPACE key in the current text output window;
            clear the current text output window;
            say "The world fades as if it were mist, and you are now in an office. Your office, in fact. You find that you are drooling on a yellow legal pad. ";
            end the game in victory.