Debian Codegraph @

terps/tads/tads3/lib/adv3/status.t

raw
1 #charset "us-ascii"
2
3 /*
4 * Copyright (c) 2000, 2006 Michael J. Roberts. All Rights Reserved.
5 *
6 * TADS 3 Library - Status Line
7 *
8 * This module defines the framework for displaying the status line,
9 * which is the area conventionally displayed at the top of the screen
10 * showing information such as the current location, score (if scoring is
11 * used at all), and number of turns.
12 */
13
14 /* include the library header */
15 #include "adv3.h"
16
17
18 /* ------------------------------------------------------------------------ */
19 /*
20 * In case the 'score' module isn't included, make sure we can refer to
21 * totalScore as a property. Likewise for the banner API and Web UI
22 * frame API.
23 */
24 property totalScore;
25 property showBanner, setSize, sizeToContents;
26 property flushWin, resize;
27
28 /* ------------------------------------------------------------------------ */
29 /*
30 * A special OutputStream for the <BANNER> tag contents. This is really
31 * just part of the main output stream, but we use a separate output
32 * stream object so that we have our own separate stream state variables
33 * (for paragraph breaking and so forth).
34 */
35 transient statusTagOutputStream: OutputStream
36 /*
37 * We're really part of the main window's output stream as far as the
38 * underlying interpreter I/O system is concerned, so we have to
39 * coordinate with the main game window's input manager.
40 */
41 myInputManager = inputManager
42
43 /* we sit atop the system-level main console output stream */
44 writeFromStream(txt)
45 {
46 /* write the text directly to the main output stream */
47 tadsSay(txt);
48 }
49 ;
50
51 /*
52 * A special OutputStream for the left half of the status line (the
53 * short description area) in text mode. We use a separate stream for
54 * this because we must write the text using the output mode switching
55 * for the status line.
56 *
57 * We only use this stream when we use the old-style text-mode status
58 * line interface, which explicitly separates the status line into a
59 * left part and a right part. When we have the banner API available in
60 * the interpreter, we'll use banners instead, since banners give us
61 * much more flexibility.
62 */
63 transient statusLeftOutputStream: OutputStream
64 /* we sit atop the system-level main console output stream */
65 writeFromStream(txt)
66 {
67 /* write the text directly to the main output stream */
68 tadsSay(txt);
69 }
70 ;
71
72 /*
73 * A special OutputStream for the right half of the status line (the
74 * score/turn count area) in text mode. We use a separate stream for
75 * this because we have to write this text with the special
76 * statusRight() intrinsic in text mode.
77 *
78 * We only use this stream when we use the old-style text-mode status
79 * line interface, which explicitly separates the status line into a
80 * left part and a right part. When we have the banner API available in
81 * the interpreter, we'll use banners instead, since banners give us
82 * much more flexibility.
83 */
84 transient statusRightOutputStream: OutputStream
85 /*
86 * Write from the stream. We simply buffer up text until we're
87 * asked to display the final data.
88 */
89 writeFromStream(txt)
90 {
91 /* buffer the text */
92 buf_ += txt;
93 }
94
95 /*
96 * Flush the buffer. This writes whatever we've buffered up to the
97 * right half of the text-mode status line.
98 */
99 flushStream()
100 {
101 /* write the text to the system console */
102 statusRight(buf_);
103
104 /* we no longer have anything buffered */
105 buf_ = '';
106 }
107
108 /* our buffered text */
109 buf_ = ''
110 ;
111
112
113 /* ------------------------------------------------------------------------ */
114 /*
115 * Statusline modes. We have three different ways to display the
116 * statusline, depending on the level of support in the interpreter.
117 *
118 * StatusModeApi - use the banner API. This is preferred method, because
119 * it gives us uniform capabilities on text and graphical interpreters,
120 * and provides an output stream for the statusline that is fully
121 * independent on the main game window's output stream.
122 *
123 * StatusModeTag - use the <BANNER> tag. This is the method we must use
124 * for HTML-enabled interpreters that don't support the banner API. This
125 * gives us the full formatting capabilities of HTML, but isn't as good
126 * as StatusModeApi because we have to share our output stream with the
127 * main game window.
128 *
129 * StatusModeText - use the old-style dedicated statusline in a text-only
130 * interpreter. This is the least desirable method, because it gives us
131 * a rigid format for the statusline (exactly one line high, no control
132 * over colors, and with the strict left/right bifurcation). We'll only
133 * use this method if we're on a text-only interpreter that doesn't
134 * support the banner API.
135 *
136 * StatusModeBrowser - use the Web UI. This is similar to API mode, but
137 * instead of using banner windows we use Web UI windows, which are
138 * implemented as IFRAMEs in the browser.
139 */
140 enum StatusModeApi, StatusModeTag, StatusModeText, StatusModeBrowser;
141
142 /* ------------------------------------------------------------------------ */
143 /*
144 * Status line - this is an abstract object that controls the status line
145 * display.
146 *
147 * We provide two main methods: showStatusHtml, which shows the status
148 * line in HTML format, and showStatusText, which shows the status line
149 * in plain text mode. To display the status line, we invoke one or the
150 * other of these methods, according to the current mode, to display the
151 * statusline. The default implementations of these methods generate the
152 * appropriate formatting codes for a statusline with a left part and a
153 * right part, calling showStatusLeft and showStatusRight, respectively,
154 * to display the text for the parts.
155 *
156 * Games can customize the statusline at two levels. At the simpler
157 * level, a game can modify showStatusLeft and/or showStatusRight to
158 * change the text displayed on the left and/or right of the statusline.
159 * Since these two methods are used regardless of the statusline style of
160 * the underlying interpreter, games don't have to worry about the
161 * different modes when overriding these.
162 *
163 * At the more complex level, a game can modify showStatusHtml and/or
164 * showStatusText. Modifying these routines provides complete control
165 * over the formatting of the entir status line. If a game wants to use
166 * something other than the traditional left/right display, it must
167 * modify these methods.
168 *
169 * This object is transient, because the statusline style is a function
170 * of the interpreter we're currently running on, and thus isn't suitable
171 * for saving persistently.
172 */
173 transient statusLine: object
174 /*
175 * Show the status line, in HTML or text mode, as appropriate. By
176 * default, the library sets this up as a "prompt daemon," which
177 * means that this will be called automatically just before each
178 * command line is read.
179 */
180 showStatusLine()
181 {
182 local oldStr;
183
184 /* if the status line isn't active, or there's no PC, skip this */
185 if (statusDispMode == nil || gPlayerChar == nil)
186 return;
187
188 /*
189 * showing the status line doesn't normally change any game
190 * state, so we can turn on the sense cache while generating the
191 * display
192 */
193 libGlobal.enableSenseCache();
194
195 /*
196 * Enter status-line mode. This will do whatever is required for
197 * our current status-line display style to prepare the output
198 * manager so that any text we display to the default output
199 * stream is displayed on the status line.
200 */
201 oldStr = beginStatusLine();
202
203 /* make sure we restore statusline mode before we're done */
204 try
205 {
206 /*
207 * Generate a text or HTML status line, as appropriate. If
208 * we're in <BANNER> tag mode or banner API mode, use HTML to
209 * format the contents of the status line; if we're using the
210 * old-style text mode, use plain text, since the formatting
211 * is rigidly defined in this mode.
212 */
213 switch (statusDispMode)
214 {
215 case StatusModeTag:
216 case StatusModeApi:
217 case StatusModeBrowser:
218 /* show the HTML status line */
219 showStatusHtml();
220 break;
221
222 case StatusModeText:
223 /* show the status line in plain text mode */
224 showStatusText();
225 break;
226 }
227 }
228 finally
229 {
230 /* end status-line mode */
231 endStatusLine(oldStr);
232
233 /* turn off sense caching */
234 libGlobal.disableSenseCache();
235 }
236 }
237
238 /* prompt-daemon showing the status line */
239 showStatusLineDaemon()
240 {
241 /* show the status line as normal */
242 showStatusLine();
243
244 /*
245 * Explicitly flush the status line if it's in a banner window.
246 * This will ensure that we'll redraw the status line on each
247 * turn if we're reading an input script, which is nice because
248 * it provides a visual indication that something's happening.
249 */
250 if (statusDispMode is in (StatusModeApi, StatusModeBrowser))
251 statuslineBanner.flushBanner();
252 }
253
254 /*
255 * Show the status line in HTML format. Our default implementation
256 * shows the traditional two-part (left/right) status line, using
257 * showStatusLeft() and showStatusRight() to display the parts.
258 */
259 showStatusHtml()
260 {
261 /*
262 * start the left half, and write the <A HREF> to hyperlink the
263 * location name to a "look around" command
264 */
265 "<<statusHTML(0)>><<
266 aHref(gLibMessages.commandLookAround, nil, nil, AHREF_Plain)>>";
267
268 /* show the left part of the status line */
269 showStatusLeft();
270
271 /*
272 * end the left portion and start the right portion, then
273 * generate the <A HREF> to link the score to a FULL SCORE
274 * command
275 */
276 "<./a></a><<statusHTML(1)>><<
277 aHref(gLibMessages.commandFullScore, nil, nil, AHREF_Plain)>>";
278
279 /* show the right part of the status line */
280 showStatusRight();
281
282 /* end the score link, and end the right half wrapper */
283 "<./a></a><<statusHTML(2)>>";
284
285 /* add the status-line exit list, if desired */
286 if (gPlayerChar.location != nil)
287 gPlayerChar.location.showStatuslineExits();
288 }
289
290 /*
291 * Get the estimated HTML-style banner height, in lines of text.
292 * This is used to set the status line banner size for platforms
293 * where sizing to the exact height of the rendered contents isn't
294 * supported.
295 *
296 * If showStatusHtml() is overridden to display more or fewer lines
297 * of text than the basic implementation here, then this routine must
298 * be overridden as well to reflect the new height.
299 */
300 getEstimatedHeightHtml()
301 {
302 local ht;
303
304 /*
305 * we need one line for the basic display (the location name and
306 * score/turn count)
307 */
308 ht = 1;
309
310 /* add in the estimated height of the exits display, if appropriate */
311 if (gPlayerChar.location != nil)
312 ht += gPlayerChar.location.getStatuslineExitsHeight();
313
314 /* return the result */
315 return ht;
316 }
317
318 /*
319 * Show the statusline in text mode. Our default implementation
320 * shows the traditional two-part (left/right) status line, using
321 * showStatusLeft() and showStatusRight() to display the parts.
322 */
323 showStatusText()
324 {
325 /* show the left part of the display */
326 showStatusLeft();
327
328 /* switch to the right-side status stream */
329 outputManager.setOutputStream(statusRightOutputStream);
330
331 /* show the right-half text */
332 showStatusRight();
333
334 /* flush the right-side stream */
335 statusRightOutputStream.flushStream();
336 }
337
338 /*
339 * Show the left part of a standard left/right statusline. By
340 * default, we'll show the player character's location, by calling
341 * statusName() on the PC's immediate container.
342 */
343 showStatusLeft()
344 {
345 local actor;
346
347 /* get the player character actor */
348 actor = gPlayerChar;
349
350 "<.statusroom>";
351
352 /* show the actor's location's status name */
353 if (actor != nil && actor.location != nil)
354 actor.location.statusName(actor);
355
356 "<./statusroom>";
357 }
358
359 /*
360 * Show the right part of a standard left/right statusline. By
361 * default, we'll show the current score, a slash, and the number of
362 * turns.
363 */
364 showStatusRight()
365 {
366 local s;
367
368 /* if there's a score object, show the score */
369 if ((s = libGlobal.scoreObj) != nil)
370 {
371 /* show the score and the number of turns so far */
372 "<.statusscore><<s.totalScore>>/<<
373 libGlobal.totalTurns>><./statusscore>";
374 }
375 }
376
377 /*
378 * Set up the status line's color scheme. This is called each time
379 * we redraw the status line to set the background and text colors.
380 * We call the statusline banner window to do the work, since the
381 * mechanism is different between the traditional and Web UIs.
382 */
383 setColorScheme()
384 {
385 /* call the banner window to do the work */
386 statuslineBanner.setColorScheme();
387 }
388
389 /*
390 * Begin status-line mode. This sets up the output manager so that
391 * text written to the default output stream is displayed on the
392 * status line. Returns the original output stream.
393 */
394 beginStatusLine()
395 {
396 local oldStr;
397
398 /* check what kind of statusline display we're using */
399 switch(statusDispMode)
400 {
401 case StatusModeApi:
402 /*
403 * We have a banner API window. Start by clearing the
404 * window, so we can completely replace everything in it.
405 */
406 statuslineBanner.clearWindow();
407
408 /*
409 * If the platform doesn't support size-to-contents, then set
410 * the height to our best estimate for the size.
411 *
412 * If we do support size-to-contents, we'll set the height to
413 * the exact rendered size when we're done, so we don't need
414 * to worry about setting an estimate; indicate this to the
415 * interpreter by setting the is-advisory flag to true.
416 */
417 statuslineBanner.setSize(getEstimatedHeightHtml(),
418 BannerSizeAbsolute, true);
419
420 /* switch to the banner's output stream */
421 oldStr = statuslineBanner.setOutputStream();
422
423 /* set up the statusline color in the window */
424 setColorScheme();
425
426 /* done */
427 break;
428
429 case StatusModeBrowser:
430 /* browser UI - clear the window */
431 statuslineBanner.clearWindow();
432
433 /* switch to its output stream */
434 oldStr = statuslineBanner.setOutputStream();
435 break;
436
437 case StatusModeTag:
438 /*
439 * We're using <BANNER> tags. Switch to our statusline
440 * output stream.
441 */
442 oldStr = outputManager.setOutputStream(statusTagOutputStream);
443
444 /* set up the <BANNER> tag */
445 "<banner id=StatusLine height=previous border>";
446
447 /* set up the color scheme */
448 setColorScheme();
449
450 /* done */
451 break;
452
453 case StatusModeText:
454 /* flush the main window */
455 flushOutput();
456
457 /* plain text mode - enter text status mode */
458 statusMode(StatModeStatus);
459
460 /* switch to the status-left output stream */
461 oldStr = outputManager.setOutputStream(statusLeftOutputStream);
462
463 /* done */
464 break;
465 }
466
467 /* return the original output stream */
468 return oldStr;
469 }
470
471 /* end statusline display */
472 endStatusLine(oldStr)
473 {
474 /* restore the old default output stream */
475 outputManager.setOutputStream(oldStr);
476
477 /* check the type of statusline we're generating */
478 switch (statusDispMode)
479 {
480 case StatusModeApi:
481 /* banner API mode - end the last line */
482 statuslineBanner.writeToBanner('\n');
483
484 /*
485 * Size the window to its current contents. This doesn't
486 * work everywhere - on a few platforms, it does nothing -
487 * but this will give us the optimal size where it's
488 * supported. On platforms that don't support this, it'll do
489 * nothing, which means we'll simply be left with the
490 * "advisory" size we established earlier.
491 */
492 statuslineBanner.sizeToContents();
493 break;
494
495 case StatusModeBrowser:
496 /* browser mode - flush the window and update the content size */
497 statuslineBanner.flushWin();
498 break;
499
500 case StatusModeTag:
501 /* HTML <BANNER> mode - end the <BANNER> tag */
502 statusTagOutputStream.writeToStream('</banner>');
503 break;
504
505 case StatusModeText:
506 /* plain text statusline - end status mode */
507 statusMode(StatModeNormal);
508 }
509 }
510
511 /*
512 * Initialize the banner window, given the BannerWindow object
513 * representing the status line banner API window.
514 */
515 initBannerWindow(win)
516 {
517 /*
518 * Try showing the banner API window. If that succeeds, use the
519 * banner API window, since it's the most portable and flexible
520 * way to show the status line. If we can't create the banner
521 * API window, it means we're on an interpreter that doesn't
522 * support the banner API, so fall back on one of the older, less
523 * flexible mechanisms; which older mechanism we choose depends
524 * on what kind of interpreter we're on.
525 *
526 * Since we create the status line banner during initialization
527 * and normally leave it as the first item in the display list at
528 * all times, we can attach to an existing status line banner
529 * window if there is one. This will avoid unnecessary redrawing
530 * on RESTART.
531 */
532 if (win.showBanner(nil, BannerFirst, nil, BannerTypeText,
533 BannerAlignTop, nil, nil,
534 BannerStyleBorder | BannerStyleTabAlign))
535 {
536 /*
537 * we successfully created the banner window - use the banner
538 * API to show the status line
539 */
540 statusDispMode = StatusModeApi;
541 }
542 else if (outputManager.htmlMode)
543 {
544 /*
545 * We failed to create a banner API window, and we're running
546 * on a full HTML interpreter, so use <BANNER> tags to
547 * produce the status line.
548 */
549 statusDispMode = StatusModeTag;
550 }
551 else
552 {
553 /*
554 * We failed to create a banner API window, and we're running
555 * on a text-only interpreter - use the old-style
556 * fixed-format status line mechanism.
557 */
558 statusDispMode = StatusModeText;
559 }
560 }
561
562 /*
563 * The status mode we're using. If this is nil, it means we haven't
564 * chosen a mode yet.
565 */
566 statusDispMode = nil
567 ;
568