Debian Codegraph @

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

raw
1 #charset "us-ascii"
2
3 /*
4 * TADS 3 Library - Menu System
5 *
6 * Copyright 2003 by Stephen Granade
7 *. Modifications copyright 2003, 2010 Michael J. Roberts
8 *
9 * This module is designed to make it easy to add on-screen menu trees to
10 * a game. Note that we're not using the term "menu" in its modern GUI
11 * sense of a compact, mouse-driven pop-up list. The style of menu we
12 * implement is more like the kind you'd find in old character-mode
13 * terminal programs, where a list of text items takes over the main
14 * window contents.
15 *
16 * Note that in plain-text mode (for interpreters without banner
17 * capabilities), a menu won't be fully usable if it exceeds nine
18 * subitems: each item in a menu is numbered, and the user selects an
19 * item by entering its number; but we only accept a single digit as
20 * input, so only items 1 through 9 can be selected on any given menu.
21 * In practice you probably wouldn't want to create larger menus anyway,
22 * for usability reasons, but this is something to keep in mind. If you
23 * need more items, you can group some of them into a submenu.
24 *
25 * The user interface for the menu system is implemented in menucon.t for
26 * traditional console interpreter, and in menuweb.t for the Web UI.
27 *
28 * Stephen Granade adapted this module from his TADS 2 menu system, and
29 * Mike Roberts made some minor cosmetic changes to integrate it with the
30 * main TADS 3 library.
31 */
32
33 #include "adv3.h"
34
35
36 /*
37 * General instructions:
38 *
39 * Menus consist of MenuItems, MenuTopicItems, and MenuLongTopicItems.
40 *
41 * - MenuItems are the menu (and sub-menu) items that the player will
42 * select. Their "title" attribute is what will be shown in the menu,
43 * and the "heading" attribute is shown as the heading while the menu
44 * itself is active; by default, the heading simply uses the title.
45 *
46 * - MenuTopicItems are for lists of topic strings that the player will
47 * be shown, like hints. "title" is what will be shown in the menu;
48 * "menuContents" is a list of either strings to be displayed, one at a
49 * time, or objects which each must return a string via a "menuContents"
50 * method.
51 *
52 * - MenuLongTopicItems are for longer discourses. "title" is what will
53 * be shown in the menu; "menuContents" is either a string to be printed
54 * or a routine to be called.
55 *
56 * adv3.h contains templates for MenuItems, for your convenience.
57 *
58 * A simple example menu:
59 *
60 * FirstMenu: MenuItem 'Test menu';
61 *. + MenuItem 'Pets';
62 *. ++ MenuItem 'Chinchillas';
63 *. +++ MenuTopicItem 'About them'
64 *. menuContents = ['Furry', 'South American', 'Curious',
65 * 'Note: Not a coat'];
66 *. +++ MenuTopicItem 'Benefits'
67 *. menuContents = ['Non-allergenic', 'Cute', 'Require little space'];
68 *. +++ MenuTopicItem 'Downsides'
69 *. menuContents = ['Require dust baths', 'Startle easily'];
70 *. ++ MenuItem 'Cats';
71 *. +++ MenuLongTopicItem 'Pure evil'
72 *. menuContents = 'Cats are, quite simply, pure evil. I would provide
73 *. ample evidence were there room for it in this
74 *. simple example.';
75 *. +++ MenuTopicItem 'Benefits'
76 *. menuContents = ['They, uh, well...', 'Okay, I can\'t think of any.'];
77 */
78
79 /* ------------------------------------------------------------------------ */
80 /*
81 * Menu output stream. We run topic contents through this output stream
82 * to allow topics to use the special paragraph and style tag markups.
83 */
84 transient menuOutputStream: OutputStream
85 /*
86 * Process a function call through the stream. If the function
87 * generates any output, we capture it. If the function simply
88 * returns text, we run it through the filters.
89 */
90 captureOutput(val)
91 {
92 /* reset our buffer */
93 buf_.deleteChars(1);
94
95 /* call the function while capturing its output */
96 outputManager.withOutputStream(menuOutputStream, function()
97 {
98 /* if it's a function, invoke it */
99 if (dataType(val) != TypeSString)
100 val = val();
101
102 /* if we have a string, run it through my filters */
103 if (dataType(val) == TypeSString)
104 writeToStream(val);
105 });
106
107 /* return my captured output */
108 return toString(buf_);
109 }
110
111 /* we capture our output to a string buffer */
112 writeFromStream(txt) { buf_.append(txt); }
113
114 /* initialize */
115 execute()
116 {
117 inherited();
118 buf_ = new StringBuffer();
119 addOutputFilter(typographicalOutputFilter);
120 addOutputFilter(menuParagraphManager);
121 addOutputFilter(styleTagFilter);
122 }
123
124 /* our capture buffer (a StringBuffer object) */
125 buf_ = nil
126 ;
127
128 /*
129 * Paragraph manager for the menu output stream.
130 */
131 transient menuParagraphManager: ParagraphManager
132 ;
133
134 /* ------------------------------------------------------------------------ */
135 /*
136 * A basic menu object. This is an abstract base class that
137 * encapsulates some behavior common to different menu classes, and
138 * allows the use of the + syntax (like "+ MenuItem") to define
139 * containment.
140 */
141 class MenuObject: object
142 /* our contents list */
143 contents = []
144
145 /*
146 * Since we're inheriting from object, but need to use the "+"
147 * syntax, we need to set up the contents appropriately
148 */
149 initializeLocation()
150 {
151 if (location != nil)
152 location.addToContents(self);
153 }
154
155 /* add a menu item */
156 addToContents(obj)
157 {
158 /*
159 * If the menu has a nil menuOrder, and it inherits menuOrder
160 * from us, then it must be a dynamically-created object that
161 * doesn't provide a custom menuOrder. Provide a suitable
162 * default of a value one higher than the highest menuOrder
163 * currently in our list, to ensure that the item always sorts
164 * after any items currently in the list.
165 */
166 if (obj.menuOrder == nil && !overrides(obj, MenuObject, &menuOrder))
167 {
168 local maxVal;
169
170 /* find the maximum current menuOrder value */
171 maxVal = nil;
172 foreach (local cur in contents)
173 {
174 /*
175 * if this one has a value, and it's the highest so far
176 * (or the only one with a value we've found so far),
177 * take it as the maximum so far
178 */
179 if (cur.menuOrder != nil
180 && (maxVal == nil || cur.menuOrder > maxVal))
181 maxVal = cur.menuOrder;
182 }
183
184 /* if we didn't find any values, use 0 as the arbitrary default */
185 if (maxVal == nil)
186 maxVal = 0;
187
188 /* go one higher than the maximum of the existing items */
189 obj.menuOrder = maxVal;
190 }
191
192 /* add the item to our contents list */
193 contents += obj;
194 }
195
196 /*
197 * The menu order. When we're about to show a list of menu items,
198 * we'll sort the list in ascending order of this property, then in
199 * ascending order of title. By default, we set this order value to
200 * be equal to the menu item's sourceTextOrder. This makes the menu
201 * order default to the order of objects as defined in the source. If
202 * some other basis is desired, override topicOrder.
203 */
204 menuOrder = (sourceTextOrder)
205
206 /*
207 * Compare this menu object to another, for the purposes of sorting a
208 * list of menu items. Returns a positive number if this menu item
209 * sorts after the other one, a negative number if this menu item
210 * sorts before the other one, 0 if the relative order is arbitrary.
211 *
212 * By default, we'll sort by menuOrder if the menuOrder values are
213 * different, otherwise arbitrarily.
214 */
215 compareForMenuSort(other)
216 {
217 /*
218 * if one menuOrder value is nil, sort it earlier than the other;
219 * if they're both nil, they sort as equivalent
220 */
221 if (menuOrder == nil && other.menuOrder == nil)
222 return 0;
223 else if (menuOrder == nil)
224 return -1;
225 else if (other.menuOrder == nil)
226 return 1;
227
228 /* return the difference of the sort order values */
229 return menuOrder - other.menuOrder;
230 }
231
232 /*
233 * Finish initializing our contents list. This will be called on
234 * each MenuObject *after* we've called initializeLocation() on every
235 * object. In other words, every menu will already have been added
236 * to its parent's contents; this can do anything else that's needed
237 * to initialize the contents list. For example, some subclasses
238 * might want to sort their contents here, so that they list their
239 * menus in a defined order. By default, we sort the menu items by
240 * menuOrder; subclasses can override this as needed.
241 */
242 initializeContents()
243 {
244 /* sort our contents list in the object-defined sorting order */
245 contents = contents.sort(
246 SortAsc, {a, b: a.compareForMenuSort(b)});
247 }
248 ;
249
250 /*
251 * This preinit object makes sure the MenuObjects all have their
252 * contents initialized properly.
253 */
254 PreinitObject
255 execute()
256 {
257 /* initialize each menu's location */
258 forEachInstance(MenuObject, { menu: menu.initializeLocation() });
259
260 /* do any extra work to initialize each menu's contents list */
261 forEachInstance(MenuObject, { menu: menu.initializeContents() });
262 }
263 ;
264
265 /* ------------------------------------------------------------------------ */
266 /*
267 * A MenuItem is a given item in the menu tree. In general all you need
268 * to do to use menus is create a tree of MenuItems with titles.
269 *
270 * To display a menu tree, call displayMenu() on the top menu in the
271 * tree. That routine displays the menu and processes user input until
272 * the user dismisses the menu, automatically displaying submenus as
273 * necessary.
274 */
275 class MenuItem: MenuObject
276 /* the name of the menu; this is listed in the parent menu */
277 title = ''
278
279 /*
280 * the heading - this is shown when this menu is active; by default,
281 * we simply use the title
282 */
283 heading = (title)
284
285 /*
286 * Display properties. These properties control the way the menu
287 * appears on the screen. By default, a menu looks to its parent
288 * menu for display properties; this makes it easy to customize an
289 * entire menu tree, since changes in the top-level menu will cascade
290 * to all children that don't override these settings. However, each
291 * menu can customize its own appearance by overriding these
292 * properties itself.
293 *
294 * 'fgcolor' and 'bgcolor' are the foreground (text) and background
295 * colors, expressed as HTML color names (so '#nnnnnn' values can be
296 * used to specify RGB colors).
297 *
298 * 'indent' is the number of pixels to indent the menu's contents
299 * from the left margin. This is used only in HTML mode.
300 *
301 * 'fullScreenMode' indicates whether the menu should take over the
302 * entire screen, or limit itself to the space it actually requires.
303 * Full screen mode makes the menu block out any game window text.
304 * Limited mode leaves the game window partially uncovered, but can
305 * be a bit jumpy, since the window changes size as the user
306 * navigates through different menus.
307 */
308
309 /* foreground (text) and background colors, as HTML color names */
310 fgcolor = (location != nil ? location.fgcolor : 'text')
311 bgcolor = (location != nil ? location.bgcolor : 'bgcolor')
312
313 /*
314 * Foreground and background colors for the top instructions bar.
315 * By default, we use the color scheme of the parent menu, or the
316 * inverse of our main menu color scheme if we're the top menu.
317 */
318 topbarfg = (location != nil ? location.topbarfg : 'statustext')
319 topbarbg = (location != nil ? location.topbarbg : 'statusbg')
320
321 /* number of spaces to indent the menu's contents */
322 indent = (location != nil ? location.indent : '10')
323
324 /*
325 * full-screen mode: make our menu take up the whole screen (apart
326 * from the instructions bar, of course)
327 */
328 fullScreenMode = (location != nil ? location.fullScreenMode : true)
329
330 /*
331 * The keys used to navigate the menus, in order:
332 *
333 * [quit, previous, up, down, select]
334 *
335 * Since multiple keys can be used for the same navigation, the list
336 * is implemented as a List of Lists. Keys must be given as
337 * lower-case in order to match input, since we convert all input
338 * keys to lower-case before matching them.
339 *
340 * In the sublist for each key, we use the first element as the key
341 * name we show in the instruction bar at the top of the screen.
342 *
343 * By default, we use our parent menu's key list, if we have a
344 * parent; if we have no parent, we use the standard keys from the
345 * library messages.
346 */
347 keyList = (location != nil ? location.keyList : gLibMessages.menuKeyList)
348
349 /*
350 * the current key list - we'll set this on entry to the start of
351 * each showMenuXxx method, so that we keep track of the actual key
352 * list in use, as inherited from the top-level menu
353 */
354 curKeyList = nil
355
356 /*
357 * Title for the link to the previous menu, if any. If the menu has
358 * a parent menu, we'll display this link next to the menu title in
359 * the top instructions/title bar. If this is nil, we won't display
360 * a link at all. Note that this can contain an HTML fragment; for
361 * example, you could use an <IMG> tag to display an icon here.
362 */
363 prevMenuLink = (location != nil ? gLibMessages.prevMenuLink : nil)
364
365 /*
366 * Update our contents. By default, we'll do nothing; subclasses
367 * can override this to manage dynamic menus if desired. This is
368 * called just before the menu is displayed, each time it's
369 * displayed.
370 */
371 updateContents() { }
372
373
374 /*
375 * Get the next menu in our list following the given menu. Returns
376 * nil if we don't find the given menu, or the given menu is the last
377 * menu.
378 */
379 getNextMenu(menu)
380 {
381 /* find the menu in our contents list */
382 local idx = contents.indexOf(menu);
383
384 /*
385 * if we found it, and it's not the last, return the menu at the
386 * next index; otherwise return nil
387 */
388 return (idx != nil && idx < contents.length()
389 ? contents[idx + 1] : nil);
390 }
391
392 /*
393 * Get the menu previous tot he given menu. Returns nil if we don't
394 * find the given menu or the given menu is the first one.
395 */
396 getPrevMenu(menu)
397 {
398 /* find the menu in our contents list */
399 local idx = contents.indexOf(menu);
400
401 /*
402 * if we found it, and it's not the first, return the menu at the
403 * prior index; otherwise return nil
404 */
405 return (idx != nil && idx > 1 ? contents[idx - 1] : nil);
406 }
407
408 /* get the index in the parent of the given child menu */
409 getChildIndex(child)
410 {
411 return contents.indexOf(child);
412 }
413 ;
414
415 /* ------------------------------------------------------------------------ */
416 /*
417 * MenuTopicItem displays a series of entries successively. This is
418 * intended to be used for displaying something like a list of hints for
419 * a topic. Set menuContents to be a list of strings to be displayed.
420 */
421 class MenuTopicItem: MenuItem
422 /* the name of this topic, as it appears in our parent menu */
423 title = ''
424
425 /* heading, displayed while we're showing this topic list */
426 heading = (title)
427
428 /* hyperlink text for showing the next menu */
429 nextMenuTopicLink = (gLibMessages.nextMenuTopicLink)
430
431 /*
432 * A list of strings and/or MenuTopicSubItem items. Each one of
433 * these is meant to be something like a single hint on our topic.
434 * We display these items one at a time when our menu item is
435 * selected.
436 */
437 menuContents = []
438
439 /* the index of the last item we displayed from our menuContents list */
440 lastDisplayed = 1
441
442 /*
443 * The maximum number of our sub-items that we'll display at once.
444 * This is only used on interpreters with banner capabilities, and is
445 * ignored in full-screen mode.
446 */
447 chunkSize = 6
448
449 /* we'll display this after we've shown all of our items */
450 menuTopicListEnd = (gLibMessages.menuTopicListEnd)
451 ;
452
453 /* ------------------------------------------------------------------------ */
454 /*
455 * A menu topic sub-item can be used to represent an item in a
456 * MenuTopicItem's list of display items. This can be useful when
457 * displaying a topic must trigger a side-effect.
458 */
459 class MenuTopicSubItem: object
460 /*
461 * Get the item's text. By default, we just return an empty string.
462 * This should be overridden to return the appropriate text, and can
463 * also trigger any desired side-effects.
464 */
465 getItemText() { return ''; }
466 ;
467
468 /* ------------------------------------------------------------------------ */
469 /*
470 * Long Topic Items are used to print out big long gobs of text on a
471 * subject. Use it for printing long treatises on your design
472 * philosophy and the like.
473 */
474 class MenuLongTopicItem: MenuItem
475 /* the title of the menu, shown in parent menus */
476 title = ''
477
478 /* the heading, shown while we're displaying our contents */
479 heading = (title)
480
481 /* either a string to be displayed, or a method returning a string */
482 menuContents = ''
483
484 /*
485 * Flag - this is a "chapter" in a list of chapters. If this is set
486 * to true, then we'll offer the options to proceed directly to the
487 * next and previous chapters. If this is nil, we'll simply wait for
488 * acknowledgment and return to the parent menu.
489 */
490 isChapterMenu = nil
491
492 /* the message we display at the end of our text */
493 menuLongTopicEnd = (gLibMessages.menuLongTopicEnd)
494 ;
495