Debian Codegraph @

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

raw
1 #charset "us-ascii"
2
3 /*
4 * Copyright (c) 2000, 2006 Michael J. Roberts. All Rights Reserved.
5 *
6 * TADS 3 Library: banner manager
7 *
8 * This module defines the banner manager, which provides high-level
9 * services to create and manipulate banner windows.
10 *
11 * A "banner" is an independent window shown within the interpreter's
12 * main application display frame (which might be the entire screen on a
13 * character-mode terminal, or could be a window in a GUI system). The
14 * game can control the creation and destruction of banner windows, and
15 * can control their placement and size.
16 *
17 * This implementation is based in part on Steve Breslin's banner
18 * manager, used by permission.
19 */
20
21 #include "adv3.h"
22
23 /* ------------------------------------------------------------------------ */
24 /*
25 * A BannerWindow corresponds to an on-screen banner. For each banner
26 * window a game wants to display, the game must create an object of this
27 * class.
28 *
29 * Note that merely creating a BannerWindow object doesn't actually
30 * display a banner window. Once a BannerWindow is created, the game
31 * must call the object's showBanner() method to create the on-screen
32 * window for the banner.
33 *
34 * BannerWindow instances are intended to be persistent (not transient).
35 * The banner manager keeps track of each banner window that's actually
36 * being displayed separately via an internal transient object; the game
37 * doesn't need to worry about these tracking objects, since the banner
38 * manager automatically handles them.
39 */
40 class BannerWindow: OutputStreamWindow
41 /*
42 * Construct the object.
43 *
44 * 'id' is a globally unique identifying string for the banner. When
45 * we dynamically create a banner object, we have to provide a unique
46 * identifying string, so that we can correlate transient on-screen
47 * banners with the banners in a saved state when restoring the saved
48 * state.
49 *
50 * Note that no ID string is needed for BannerWindow objects defined
51 * statically at compile-time, because the object itself ('self') is
52 * a suitably unique and stable identifier.
53 */
54 construct(id)
55 {
56 /* remember my unique identifier */
57 id_ = id;
58 }
59
60 /*
61 * Show the banner. The game should call this method when it first
62 * wants to display the banner.
63 *
64 * 'parent' is the parent banner; this is an existing BannerWindow
65 * object. If 'parent' is nil, then the parent is the main game
66 * text window. The new window's display space is obtained by
67 * carving space out of the parent's area, according to the
68 * alignment and size values specified.
69 *
70 * 'where' and 'other' give the position of the banner among the
71 * children of the given parent. 'where' is one of the constants
72 * BannerFirst, BannerLast, BannerBefore, or BannerAfter. If
73 * 'where' is BannerBefore or BannerAfter, 'other' gives the
74 * BannerWindow object to be used as the reference point in the
75 * parent's child list; 'other' is ignored in other cases. Note
76 * that 'other' must always be another child of the same parent; if
77 * it's not, then we act as though 'where' were given as BannerLast.
78 *
79 * 'windowType' is a BannerTypeXxx constant giving the new window's
80 * type.
81 *
82 * 'align' is a BannerAlignXxx constant giving the alignment of the
83 * new window. 'size' is an integer giving the size of the banner,
84 * in units specified by 'sizeUnits', which is a BannerSizeXxx
85 * constant. If 'size' is nil, it indicates that the caller doesn't
86 * care about the size, usually because the caller will be resizing
87 * the banner soon anyway; the banner will initially have zero size
88 * in this case if we create a new window, or will retain the
89 * existing size if there's already a system window.
90 *
91 * 'styleFlags' is a combination of BannerStyleXxx constants
92 * (combined with the bitwise OR operator, '|'), giving the requested
93 * display style of the new banner window.
94 *
95 * Note that if we already have a system banner window, and the
96 * existing banner window has the same characteristics as the new
97 * creation parameters, we'll simply re-use the existing window
98 * rather than closing and re-creating it; this reduces unnecessary
99 * redrawing in cases where the window isn't changing. If the caller
100 * explicitly wants to create a new window even if we already have a
101 * window, the caller should simply call removeBanner() before
102 * calling this routine.
103 */
104 showBanner(parent, where, other, windowType,
105 align, size, sizeUnits, styleFlags)
106 {
107 local parentID;
108 local otherID;
109
110 /* note the ID's of the parent window and the insertion point */
111 parentID = (parent != nil ? parent.getBannerID() : nil);
112 otherID = (other != nil ? other.getBannerID() : nil);
113
114 /*
115 * if we have an 'other' specified, its parent must match our
116 * proposed parent; otherwise, ignore 'other' and insert at the
117 * end of the parent list
118 */
119 if (other != nil && other.parentID_ != parentID)
120 {
121 other = nil;
122 where = BannerLast;
123 }
124
125 /* if we already have an existing banner window, check for a match */
126 if (handle_ != nil)
127 {
128 local t;
129 local match;
130
131 /* presume we won't find an exact match */
132 match = nil;
133
134 /* we already have a window - get the UI tracker object */
135 if ((t = bannerUITracker.getTracker(self)) != nil)
136 {
137 /* check the placement, window type, alignment, and style */
138 match = (t.windowType_ == windowType
139 && t.parentID_ == parentID
140 && t.align_ == align
141 && t.styleFlags_ == styleFlags
142 && bannerUITracker.orderMatches(t, where, otherID));
143 }
144
145 /*
146 * if it doesn't match the existing window, close it, so that
147 * we will open a brand new window with the new
148 * characteristics
149 */
150 if (!match)
151 removeBanner();
152 }
153
154 /* if the system-level banner doesn't already exist, create it */
155 if (handle_ == nil)
156 {
157 /* create my system-level banner window */
158 if (!createSystemBanner(parent, where, other, windowType, align,
159 size, sizeUnits, styleFlags))
160 {
161 /* we couldn't create the system banner - give up */
162 return nil;
163 }
164
165 /* create our output stream */
166 createOutputStream();
167
168 /* add myself to the UI tracker's active banner list */
169 bannerUITracker.addBanner(handle_, outputStream_, getBannerID(),
170 parentID, where, other, windowType,
171 align, styleFlags);
172 }
173 else
174 {
175 /*
176 * Our system-level window already exists, so we don't need
177 * to create a new one. However, our size could be
178 * different, so explicitly set the requested size if it
179 * doesn't match our recorded size. If the size is given as
180 * nil, leave the size as it is; a nil size indicates that
181 * the caller doesn't care about the size (probably because
182 * the caller is going to change the size shortly anyway),
183 * so we can avoid unnecessary redrawing by leaving the size
184 * as it is for now.
185 */
186 if (size != nil && (size != size_ || sizeUnits != sizeUnits_))
187 bannerSetSize(handle_, size, sizeUnits, nil);
188 }
189
190 /*
191 * remember the creation parameters, so that we can re-create the
192 * banner with the same characteristics in the future if we
193 * should need to restore the banner from a saved position
194 */
195 parentID_ = parentID;
196 windowType_ = windowType;
197 align_ = align;
198 size_ = size;
199 sizeUnits_ = sizeUnits;
200 styleFlags_ = styleFlags;
201
202 /*
203 * Add myself to the persistent banner tracker's active list. Do
204 * this even if we already had a system handle, since we might be
205 * initializing the window as part of a persistent restore
206 * operation, in which case the persistent tracking object might
207 * not yet exist. (This seems backwards: if we're restoring a
208 * persistent state, surely the persistent tracker would already
209 * exist. In fact, the case we're really handling is where the
210 * window is open in the transient UI, because it was already
211 * open in the ongoing session; but the persistent state we're
212 * restoring doesn't include the window. This is most likely to
213 * occur after a RESTART, since we could have a window that is
214 * always opened immediately at start-up and thus will be in the
215 * transient state up to and through the RESTART, but is only
216 * created as part of the initialization process.)
217 */
218 bannerTracker.addBanner(self, parent, where, other);
219
220 /* indicate success */
221 return true;
222 }
223
224 /*
225 * Remove the banner. This removes the banner's on-screen window.
226 * The BannerWindow object itself remains valid, but after this
227 * method returns, the BannerWindow no longer has an associated
228 * display window.
229 *
230 * Note that any child banners of ours will become undisplayable
231 * after we're gone. A child banner depends upon its parent to
232 * obtain display space, so once the parent is gone, its children no
233 * longer have any way to obtain any display space. Our children
234 * remain valid objects even after we're closed, but they won't be
235 * visible on the display.
236 */
237 removeBanner()
238 {
239 /* if I don't have a system-level handle, there's nothing to do */
240 if (handle_ == nil)
241 return;
242
243 /* remove my system-level banner window */
244 bannerDelete(handle_);
245
246 /* our system-level window is gone, so forget its handle */
247 handle_ = nil;
248
249 /* we only need an output stream when we're active */
250 outputStream_ = nil;
251
252 /* remove myself from the UI trackers's active list */
253 bannerUITracker.removeBanner(getBannerID());
254
255 /* remove myself from the persistent banner tracker's active list */
256 bannerTracker.removeBanner(self);
257 }
258
259 /* write the given text to the banner */
260 writeToBanner(txt)
261 {
262 /* write the text to our underlying output stream */
263 outputStream_.writeToStream(txt);
264 }
265
266 /* flush any pending output to the banner */
267 flushBanner() { bannerFlush(handle_); }
268
269 /*
270 * Set the banner window to a specific size. 'size' is the new
271 * size, in units given by 'sizeUnits', which is a BannerSizeXxx
272 * constant.
273 *
274 * 'isAdvisory' is true or nil; if true, it indicates that the size
275 * setting is purely advisory, and that a sizeToContents() call will
276 * eventually follow to set the actual size. When 'isAdvisory is
277 * true, the interpreter is free to ignore the request if
278 * sizeToContents()
279 */
280 setSize(size, sizeUnits, isAdvisory)
281 {
282 /* set the underlying system window size */
283 bannerSetSize(handle_, size, sizeUnits, isAdvisory);
284
285 /*
286 * remember my new size in case we have to re-create the banner
287 * from a saved state
288 */
289 size_ = size;
290 sizeUnits_ = sizeUnits;
291 }
292
293 /*
294 * Size the banner to its current contents. Note that some systems
295 * do not support this operation, so callers should always make an
296 * advisory call to setSize() first to set a size based on the
297 * expected content size.
298 */
299 sizeToContents()
300 {
301 /* size our system-level window to our contents */
302 bannerSizeToContents(handle_);
303 }
304
305 /*
306 * Clear my banner window. This clears out all of the contents of
307 * our on-screen display area.
308 */
309 clearWindow()
310 {
311 /* clear our system-level window */
312 bannerClear(handle_);
313 }
314
315 /* set the text color in the banner */
316 setTextColor(fg, bg) { bannerSetTextColor(handle_, fg, bg); }
317
318 /* set the screen color in the banner window */
319 setScreenColor(color) { bannerSetScreenColor(handle_, color); }
320
321 /*
322 * Move the cursor to the given row/column position. This can only
323 * be used with text-grid banners; for ordinary text banners, this
324 * has no effect.
325 */
326 cursorTo(row, col) { bannerGoTo(handle_, row, col); }
327
328 /*
329 * Get the banner identifier. If our 'id_' property is set to nil,
330 * we'll assume that we're a statically-defined object, in which case
331 * 'self' is a suitable identifier. Otherwise, we'll return the
332 * identifier string.
333 */
334 getBannerID() { return id_ != nil ? id_ : self; }
335
336 /*
337 * Restore this banner. This is called after a RESTORE or UNDO
338 * operation that finds that this banner was being displayed at the
339 * time the state was saved but is not currently displayed in the
340 * active UI. We'll show the banner using the characteristics saved
341 * persistently.
342 */
343 showForRestore(parent, where, other)
344 {
345 /* show myself, using my saved characteristics */
346 showBanner(parent, where, other, windowType_, align_,
347 size_, sizeUnits_, styleFlags_);
348
349 /* update my contents */
350 updateForRestore();
351 }
352
353 /*
354 * Create the system-level banner window. This can be customized as
355 * needed, although this default implementation should be suitable
356 * for most instances.
357 *
358 * Returns true if we are successful in creating the system window,
359 * nil if we fail.
360 */
361 createSystemBanner(parent, where, other, windowType, align,
362 size, sizeUnits, styleFlags)
363 {
364 /* create the system-level window */
365 handle_ = bannerCreate(parent != nil ? parent.handle_ : nil,
366 where, other != nil ? other.handle_ : nil,
367 windowType, align, size, sizeUnits,
368 styleFlags);
369
370 /* if we got a valid handle, we succeeded */
371 return (handle_ != nil);
372 }
373
374 /* create our banner output stream */
375 createOutputStreamObj()
376 {
377 return new transient BannerOutputStream(handle_);
378 }
379
380 /*
381 * Update my contents after being restored. By default, this does
382 * nothing; instances might want to override this to refresh the
383 * contents of the banner if the banner is normally updated only in
384 * response to specific events. Note that it's not necessary to do
385 * anything here if the banner will soon be updated automatically as
386 * part of normal processing; for example, the status line banner is
387 * updated at each new command line via a prompt-daemon, so there's
388 * no need for the status line banner to do anything here.
389 */
390 updateForRestore()
391 {
392 /* do nothing by default; subclasses can override as needed */
393 }
394
395 /*
396 * Initialize the banner window. This is called during
397 * initialization (when first starting the game, or when resetting
398 * with RESTART). If the banner is to be displayed from the start of
399 * the game, this can set up the on-screen display.
400 *
401 * Note that we might already have an on-screen handle when this is
402 * called. This indicates that we're restarting an ongoing session,
403 * and that this banner already existed in the session before the
404 * RESTART operation. If desired, we can attach ourselves to the
405 * existing on-screen banner, avoiding the redrawing that would occur
406 * if we created a new window.
407 *
408 * If this window depends upon another window for its layout order
409 * placement (i.e., we'll call showBanner() with another BannerWindow
410 * given as the 'other' parameter), then this routine should call the
411 * other window's initBannerWindow() method before creating its own
412 * window, to ensure that the other window has a system window and
413 * thus will be meaningful to establish the layout order.
414 *
415 * Overriding implementations should check the 'inited_' property.
416 * If this property is true, then it can be assumed that we've
417 * already been initialized and don't require further initialization.
418 * This routine can be called multiple times because dependent
419 * windows might call us directly, before we're called for our
420 * regular initialization.
421 */
422 initBannerWindow()
423 {
424 /* by default, simply note that we've been initialized */
425 inited_ = true;
426 }
427
428 /* flag: this banner has been initialized with initBannerWindow() */
429 inited_ = nil
430
431 /*
432 * The creator-assigned ID string to identify the banner
433 * persistently. This is only needed for banners created
434 * dynamically; for BannerWindow objects defined statically at
435 * compile time, simply leave this value as nil, and we'll use the
436 * object itself as the identifier.
437 */
438 id_ = nil
439
440 /* the handle to my system-level banner window */
441 handle_ = nil
442
443 /*
444 * Creation parameters. We store these when we create the banner,
445 * and update them as needed when the banner's display attributes
446 * are changed.
447 */
448 parentID_ = nil
449 windowType_ = nil
450 align_ = nil
451 size_ = nil
452 sizeUnits_ = nil
453 styleFlags_ = nil
454 ;
455
456 /* ------------------------------------------------------------------------ */
457 /*
458 * Banner Output Stream. This is a specialization of OutputStream that
459 * writes to a banner window.
460 */
461 class BannerOutputStream: OutputStream
462 /* construct */
463 construct(handle)
464 {
465 /* inherit base class constructor */
466 inherited();
467
468 /* remember my banner window handle */
469 handle_ = handle;
470 }
471
472 /* execute preinitialization */
473 execute()
474 {
475 /*
476 * We shouldn't need to do anything during pre-initialization,
477 * since we should always be constructed dynamically by a
478 * BannerWindow. Don't even inherit the base class
479 * initialization, since it could clear out state that we want to
480 * keep through a restart, restore, etc.
481 */
482 }
483
484 /* write text from the stream to the interpreter I/O system */
485 writeFromStream(txt)
486 {
487 /* write the text to the underlying system banner window */
488 bannerSay(handle_, txt);
489 }
490
491 /* our system-level banner window handle */
492 handle_ = nil
493 ;
494
495
496 /* ------------------------------------------------------------------------ */
497 /*
498 * The banner UI tracker. This object keeps track of the current user
499 * interface display state; this object is transient because the
500 * interpreter's user interface is not part of the persistence
501 * mechanism.
502 */
503 transient bannerUITracker: object
504 /* add a banner to the active display list */
505 addBanner(handle, ostr, id, parentID, where, other,
506 windowType, align, styleFlags)
507 {
508 local uiWin;
509 local parIdx;
510 local idx;
511
512 /* create a transient BannerUIWindow object to track the banner */
513 uiWin = new transient BannerUIWindow(handle, ostr, id, parentID,
514 windowType, align, styleFlags);
515
516 /*
517 * Find the parent in the list. If there's no parent, the
518 * parent is the main window; consider it to be at imaginary
519 * index zero in the list.
520 */
521 parIdx = (parentID == nil
522 ? 0 : activeBanners_.indexWhich({x: x.id_ == parentID}));
523
524 /* insert the banner at the proper point in our list */
525 switch(where)
526 {
527 case BannerFirst:
528 /*
529 * insert as the first child of the parent - put it
530 * immediately after the parent in the list
531 */
532 activeBanners_.insertAt(parIdx + 1, uiWin);
533 break;
534
535 case BannerLast:
536 ins_last:
537 /*
538 * Insert as the last child of the parent: insert
539 * immediately after the last window that descends from the
540 * parent.
541 */
542 activeBanners_.insertAt(skipDescendants(parIdx), uiWin);
543 break;
544
545 case BannerBefore:
546 case BannerAfter:
547 /* find the reference point ID in our list */
548 idx = activeBanners_.indexWhich(
549 {x: x.id_ == other.getBannerID()});
550
551 /*
552 * if we didn't find the reference point, or the reference
553 * point item doesn't have the same parent as the new item,
554 * then ignore the reference point and instead insert at the
555 * end of the parent's child list
556 */
557 if (idx == nil || activeBanners_[idx].parentID_ != parentID)
558 goto ins_last;
559
560 /*
561 * if inserting after, skip the reference item and all
562 * of its descendants
563 */
564 if (where == BannerAfter)
565 idx = skipDescendants(idx);
566
567 /* insert at the position we found */
568 activeBanners_.insertAt(idx, uiWin);
569 break;
570 }
571 }
572
573 /*
574 * Given an index in our list of active windows, skip the given item
575 * and all items whose windows are descended from this window.
576 * We'll leave the index positioned on the next entry in the list
577 * that isn't a descendant of the window at the given index. Note
578 * that this skips not only children but grandchildren (and so on)
579 * as well.
580 */
581 skipDescendants(idx)
582 {
583 local parentID;
584
585 /*
586 * if the index is zero, it's the main window; all windows are
587 * children of the root window, so return the next index after
588 * the last item
589 */
590 if (idx == 0)
591 return activeBanners_.length() + 1;
592
593 /* note ID of the parent item */
594 parentID = activeBanners_[idx].id_;
595
596 /* skip the parent item */
597 ++idx;
598
599 /* keep going as long as we see children of the parent */
600 while (idx <= activeBanners_.length()
601 && activeBanners_[idx].parentID_ == parentID)
602 {
603 /*
604 * This is a child of the given parent, so we must skip it;
605 * we must also skip its descendants, since they're all
606 * indirectly descendants of the original parent. So,
607 * simply skip this item and its descendants with a
608 * recursive call to this routine.
609 */
610 idx = skipDescendants(idx);
611 }
612
613 /* return the new index */
614 return idx;
615 }
616
617 /* remove a banner from the active display list */
618 removeBanner(id)
619 {
620 local idx;
621
622 /* find the entry with the given ID, and remove it */
623 if ((idx = activeBanners_.indexWhich({x: x.id_ == id})) != nil)
624 {
625 local lastIdx;
626
627 /*
628 * After removing an item, its children are no longer
629 * displayable, because a child obtains display space from
630 * its parent. So, we must remove any children of this item
631 * at the same time we remove the item itself. Find the
632 * index of the next item after all of our descendants, so
633 * that we can remove the item and its children all at once.
634 * An item and its descendants are always contiguous in our
635 * list, since we store children immediately after their
636 * parents, so we can simply remove the range of items from
637 * the specified item to its last descendant.
638 *
639 * Note that skipDescendants() returns the index of the
640 * first item that is NOT a descendant; so, decrement the
641 * result so that we end up with the index of the last
642 * descendant.
643 */
644 lastIdx = skipDescendants(idx) - 1;
645
646 /* remove the item and all of its children */
647 activeBanners_.removeRange(idx, lastIdx);
648 }
649 }
650
651 /* get the BannerUIWindow tracker object for a given BannerWindow */
652 getTracker(win)
653 {
654 local id;
655
656 /* get the window's ID */
657 id = win.getBannerID();
658
659 /* return the tracker with the same ID as the given BannerWindow */
660 return activeBanners_.valWhich({x: x.id_ == id});
661 }
662
663 /* check a BannerUIWindow to see if it matches the given layout order */
664 orderMatches(uiWin, where, otherID)
665 {
666 local idx;
667 local otherIdx;
668 local parentID;
669 local parIdx;
670
671 /* get the list index of the given window */
672 idx = activeBanners_.indexOf(uiWin);
673
674 /* get the list index of the reference point window */
675 otherIdx = (otherID != nil
676 ? activeBanners_.indexWhich({x: x.id_ == otherID}) : nil);
677
678 /*
679 * find the parent item (using imaginary index zero for the
680 * root, which we can think of as being just before the first
681 * item in the list)
682 */
683 parentID = uiWin.parentID_;
684 parIdx = (parentID == nil
685 ? 0 : activeBanners_.indexWhich({x: x.id_ == parentID}));
686
687 /*
688 * if 'other' is specified, it has to have our same parent; if
689 * it has a different parent, it's not a match
690 */
691 if (otherID != nil && parentID != activeBanners_[otherIdx].parentID_)
692 return nil;
693
694 /*
695 * if there's no such window in the list, it can't match the
696 * given placement no matter what the given placement is, as it
697 * has no placement
698 */
699 if (idx == nil)
700 return nil;
701
702 /* check the requested layout order */
703 switch (where)
704 {
705 case BannerFirst:
706 /* make sure it's immediately after the parent */
707 return idx == parIdx + 1;
708
709 case BannerLast:
710 /*
711 * Make sure it's the last child of the parent. To do this,
712 * make sure that the next item after this item's last
713 * descendant is the same as the next item after the
714 * parent's last descendant.
715 */
716 return skipDescendants(idx) == skipDescendants(parIdx);
717
718 case BannerBefore:
719 /*
720 * we want this item to come before 'other', so make sure
721 * the next item after all of this item's descendants is
722 * 'other'
723 */
724 return skipDescendants(idx) == otherIdx;
725
726 case BannerAfter:
727 /*
728 * we want this item to come just after 'other', so make
729 * sure that the next item after all of the descendants of
730 * 'other' is this item
731 */
732 return skipDescendants(otherIdx) == idx;
733
734 default:
735 /* other layout orders are invalid */
736 return nil;
737 }
738 }
739
740 /*
741 * The vector of banners currently on the screen. This is a list of
742 * transient BannerUIWindow objects, stored in the same order as the
743 * banner layout list.
744 */
745 activeBanners_ = static new transient Vector(32)
746 ;
747
748 /*
749 * A BannerUIWindow object. This keeps track of the transient UI state
750 * of a banner window while it appears on the screen. We create only
751 * transient instances of this class, since it tracks what's actually
752 * displayed at any given time.
753 */
754 class BannerUIWindow: object
755 /* construct */
756 construct(handle, ostr, id, parentID, windowType, align, styleFlags)
757 {
758 /* remember the banner's data */
759 handle_ = handle;
760 outputStream_ = ostr;
761 id_ = id;
762 parentID_ = parentID;
763 windowType_ = windowType;
764 align_ = align;
765 styleFlags_ = styleFlags;
766 }
767
768 /* the system-level banner handle */
769 handle_ = nil
770
771 /* the banner's ID */
772 id_ = nil
773
774 /* the parent banner's ID (nil if this is a top-level banner) */
775 parentID_ = nil
776
777 /*
778 * The banner's output stream. Output streams are always transient,
779 * so hang on to each active banner's stream so that we can plug it
780 * back in on restore.
781 */
782 outputStream_ = nil
783
784 /* creation parameters of the banner */
785 windowType_ = nil
786 align_ = nil
787 styleFlags_ = nil
788
789 /*
790 * Scratch-pad for our association to our BannerWindow object. We
791 * only use this during the RESTORE process, to tie the transient
792 * object back to the proper persistent object.
793 */
794 win_ = nil
795 ;
796
797 /*
798 * The persistent banner tracker. This keeps track of the active banner
799 * windows persistently. Whenever we save or restore the game's state,
800 * this object will be saved or restored along with the state. When we
801 * restore a previously saved state, we can look at this object to
802 * determine which banners were active at the time the state was saved,
803 * and use this information to restore the same active banners in the
804 * user interface.
805 *
806 * This is a post-restore and post-undo object, so we're notified via our
807 * execute() method whenever we restore a saved state using RESTORE or
808 * UNDO. When we restore a saved state, we'll restore the banner display
809 * conditions as they existed in the saved state.
810 */
811 bannerTracker: PostRestoreObject, PostUndoObject
812 /* add a banner to the active display list */
813 addBanner(win, parent, where, other)
814 {
815 local parIdx;
816 local otherIdx;
817
818 /*
819 * Don't add it if it's already in the list. If we're restoring
820 * the banner from persistent state, it'll already be in the
821 * active list, since the active list is the set of windows
822 * we're restoring in the first place.
823 */
824 if (activeBanners_.indexOf(win) != nil)
825 return;
826
827 /* find the parent among the existing windows */
828 parIdx = (parent == nil ? 0 : activeBanners_.indexOf(parent));
829
830 /* note the index of 'other' */
831 otherIdx = (other == nil ? nil : activeBanners_.indexOf(other));
832
833 /* insert the banner at the proper point in our list */
834 switch(where)
835 {
836 case BannerFirst:
837 /* insert immediately after the parent */
838 activeBanners_.insertAt(parIdx + 1, win);
839 break;
840
841 case BannerLast:
842 ins_last:
843 /* insert after the parent's last descendant */
844 activeBanners_.insertAt(skipDescendants(parIdx), win);
845 break;
846
847 case BannerBefore:
848 case BannerAfter:
849 /*
850 * if we didn't find the reference point, insert at the end
851 * of the parent's child list
852 */
853 if (otherIdx == nil)
854 goto ins_last;
855
856 /*
857 * if inserting after, skip the reference item and all of
858 * its descendants
859 */
860 if (where == BannerAfter)
861 otherIdx = skipDescendants(otherIdx);
862
863 /* insert at the position we found */
864 activeBanners_.insertAt(otherIdx, win);
865 break;
866 }
867 }
868
869 /*
870 * Skip all descendants of the window at the given index.
871 */
872 skipDescendants(idx)
873 {
874 local parentID;
875
876 /* index zero is the root item, so skip the entire list */
877 if (idx == 0)
878 return activeBanners_.length() + 1;
879
880 /* note the parent item */
881 parentID = activeBanners_[idx].getBannerID();
882
883 /* skip the parent item */
884 ++idx;
885
886 /* keep going as long as we see children of the parent */
887 while (idx < activeBanners_.length()
888 && activeBanners_[idx].parentID_ == parentID)
889 {
890 /* this is a child, so skip it and all of its descendants */
891 idx = skipDescendants(idx);
892 }
893
894 /* return the new index */
895 return idx;
896 }
897
898 /* remove a banner from the active list */
899 removeBanner(win)
900 {
901 local idx;
902 local lastIdx;
903
904 /* get the index of the item to remove */
905 idx = activeBanners_.indexOf(win);
906
907 /* if we didn't find it, ignore the request */
908 if (idx == nil)
909 return;
910
911 /* find the index of its last descendant */
912 lastIdx = skipDescendants(idx) - 1;
913
914 /*
915 * remove the item and all of its descendants - child items
916 * cannot be displayed once their parents are gone, so we can
917 * remove all of this item's children, all of their children,
918 * and so on, as they are becoming undisplayable
919 */
920 activeBanners_.removeRange(idx, lastIdx);
921 }
922
923 /*
924 * The list of active banners. This is a list of BannerWindow
925 * objects, stored in banner layout list order.
926 */
927 activeBanners_ = static new Vector(32)
928
929 /* receive RESTORE/UNDO notification */
930 execute()
931 {
932 /* restore the display state for a non-initial state */
933 restoreDisplayState(nil);
934 }
935
936 /*
937 * Restore the saved banner display state, so that the banner layout
938 * looks the same as it did when we saved the persistent state. This
939 * should be called after restoring a saved state, undoing to a
940 * savepoint, or initializing (when first starting the game or when
941 * restarting).
942 */
943 restoreDisplayState(initing)
944 {
945 local uiVec;
946 local uiIdx;
947 local origActive;
948
949 /* get the list of banners active in the UI */
950 uiVec = bannerUITracker.activeBanners_;
951
952 /*
953 * First, go through all of the persistent BannerWindow objects.
954 * For each one whose ID shows up in the active UI display list,
955 * tell the BannerWindow object its current UI handle.
956 */
957 forEachInstance(BannerWindow, function(cur)
958 {
959 local uiCur;
960
961 /* find this banner in the active UI list */
962 uiCur = uiVec.valWhich({x: x.id_ == cur.getBannerID()});
963
964 /*
965 * if the window exists in the active UI list, note the
966 * current system handle for the window; otherwise, we have
967 * no system window, so set the handle to nil
968 */
969 if (uiCur != nil)
970 {
971 /* note the current system banner handle */
972 cur.handle_ = uiCur.handle_;
973
974 /* re-establish the banner's active output stream */
975 cur.outputStream_ = uiCur.outputStream_;
976
977 /* tie the transient record to the current 'cur' */
978 uiCur.win_ = cur;
979 }
980 else
981 {
982 /* it's not shown, so it has no system banner handle */
983 cur.handle_ = nil;
984
985 /* it has no output stream */
986 cur.outputStream_ = nil;
987 }
988 });
989
990 /*
991 *
992 * 'initing' indicates whether we're initializing (startup or
993 * RESTART) or doing something else (RESTORE, UNDO). When
994 * initializing, if there are any banners on-screen, we'll give
995 * their associated BannerWindow objects (if any) a chance to set
996 * up their initial conditions; this allows us to avoid
997 * unnecessary redrawing if we have banners that we'd immediately
998 * set up to the same conditions anyway, since we can just keep
999 * the existing banners rather than removing and re-creating
1000 * them.
1001 *
1002 * So, if we're initializing, tell each banner that it's time to
1003 * set up its initial display.
1004 */
1005 if (initing)
1006 forEachInstance(BannerWindow, {cur: cur.initBannerWindow()});
1007
1008 /*
1009 * scan the active UI list, and close each window that isn't
1010 * still open in the saved state
1011 */
1012 foreach (local uiCur in uiVec)
1013 {
1014 /* if this window isn't in the active list, close it */
1015 if (activeBanners_.indexWhich(
1016 {x: x.getBannerID() == uiCur.id_}) == nil)
1017 {
1018 /*
1019 * There's no banner in the persistent list with this
1020 * ID, so this window is not part of the state we're
1021 * restoring. Close the window. If we have an
1022 * associated BannerWindow object, close through the
1023 * window object; otherwise, close the system handle
1024 * directly.
1025 */
1026 if (uiCur.win_ != nil)
1027 {
1028 /* we have a BannerWindow - close it */
1029 uiCur.win_.removeBanner();
1030 }
1031 else
1032 {
1033 /* there's no BannerWindow - close the system window */
1034 bannerDelete(uiCur.handle_);
1035
1036 /* remove the UI tracker object */
1037 uiVec.removeElement(uiCur);
1038 }
1039 }
1040 }
1041
1042 /* start at the first banner actually displayed right now */
1043 uiIdx = 1;
1044
1045 /*
1046 * make a copy of the original active list - we might modify the
1047 * actual active list in the course of restoring things, so make
1048 * a copy that we can refer to as we reconstruct the original
1049 * list
1050 */
1051 origActive = activeBanners_.toList();
1052
1053 /*
1054 * Scan the saved list of banners, and restore each one. Note
1055 * that by restoring windows in the order in which they appear
1056 * in the list, we ensure that we always restore a parent before
1057 * restoring any of its children, since a child always follows
1058 * its parent in the list.
1059 */
1060 for (local curIdx = 1, local aLen = origActive.length() ;
1061 curIdx <= aLen ; ++curIdx)
1062 {
1063 local redisp;
1064 local cur;
1065
1066 /* get the current item */
1067 cur = origActive[curIdx];
1068
1069 /* presume we will have to redisplay this banner */
1070 redisp = true;
1071
1072 /*
1073 * If this banner matches the current banner in the active
1074 * UI display list, and the characteristics match, we need
1075 * do nothing, as we're already displaying this banner
1076 * properly. If the current active UI banner doesn't match,
1077 * then we need to insert this saved banner at the current
1078 * active UI position.
1079 */
1080 if (uiVec.length() >= uiIdx)
1081 {
1082 local uiCur;
1083
1084 /* get this current UI display item (a BannerUIWindow) */
1085 uiCur = uiVec[uiIdx];
1086
1087 /* check for a match to 'cur' */
1088 if (uiCur.id_ == cur.getBannerID()
1089 && uiCur.parentID_ == cur.parentID_
1090 && uiCur.windowType_ == cur.windowType_
1091 && uiCur.align_ == cur.align_
1092 && uiCur.styleFlags_ == cur.styleFlags_)
1093 {
1094 /*
1095 * This saved banner ('cur') exactly matches the
1096 * active UI banner ('uiCur') at the same position
1097 * in the layout list. Therefore, we do not need to
1098 * redisplay 'cur'.
1099 */
1100 redisp = nil;
1101 }
1102 }
1103
1104 /* if we need to redisplay 'cur', do so */
1105 if (redisp)
1106 {
1107 local prvIdx;
1108 local where;
1109 local other;
1110 local parent;
1111
1112 /*
1113 * If 'cur' is already being displayed, we must remove
1114 * it before showing it anew. This is the only way to
1115 * ensure that we display it with the proper
1116 * characteristics, since the characteristics of the
1117 * current instance of its window don't match up to what
1118 * we want to restore.
1119 */
1120 if (cur.handle_ != nil)
1121 cur.removeBanner();
1122
1123 /*
1124 * Figure out how to specify this window's display list
1125 * position. A display list position is always
1126 * specified relative to the parent's child list, so
1127 * figure out where we go in our parent's list. Scan
1128 * backwards in the active list for the nearest previous
1129 * window with the same parent. If we find one, insert
1130 * the new window after that prior sibling; otherwise,
1131 * insert as the first child of our parent. Presume
1132 * that we'll fail to find a prior sibling, then search
1133 * for it and search for our parent.
1134 */
1135 where = BannerFirst;
1136 other = nil;
1137 for (prvIdx = curIdx - 1 ; prvIdx > 0 ; --prvIdx)
1138 {
1139 local prv;
1140
1141 /* note this item */
1142 prv = origActive[prvIdx];
1143
1144 /*
1145 * If this item has our same parent, and we haven't
1146 * already found a prior sibling, this is our most
1147 * recent prior sibling, so note it.
1148 */
1149 if (where == BannerFirst
1150 && prv.parentID_ == cur.parentID_)
1151 {
1152 /* insert after this prior sibling */
1153 where = BannerAfter;
1154 other = prv;
1155 }
1156
1157 /* if this is our parent, note it */
1158 if (prv.getBannerID() == cur.parentID_)
1159 {
1160 /*
1161 * note the parent BannerWindow object - we'll
1162 * need it to specify our window display
1163 * position
1164 */
1165 parent = prv;
1166
1167 /*
1168 * Children of a given parent always come after
1169 * the parent in the display list, so there's no
1170 * possibility of finding another sibling.
1171 * There's also obviously no possibility of
1172 * finding another parent. So, our work here is
1173 * done; we can stop scanning.
1174 */
1175 break;
1176 }
1177 }
1178
1179 /* show the window */
1180 cur.showForRestore(parent, where, other);
1181 }
1182 else
1183 {
1184 /*
1185 * the banner is already showing, so we don't need to
1186 * redisplay it; simply notify it that a Restore
1187 * operation has taken place so that it can do any
1188 * necessary updates
1189 */
1190 cur.updateForRestore();
1191 }
1192
1193 /*
1194 * 'cur' should now be displayed, but we might have failed
1195 * to re-create it. If we did show the window, we can
1196 * advance to the next slot in the UI list, since this
1197 * window will necessarily be at the current spot in the UI
1198 * list.
1199 */
1200 if (cur.handle_ != nil)
1201 {
1202 /*
1203 * We know that this window is now the entry in the
1204 * active UI list at the current index we're looking at.
1205 * Move on to the next position in the active list for
1206 * the next saved window.
1207 */
1208 ++uiIdx;
1209 }
1210 }
1211 }
1212 ;
1213
1214 /*
1215 * Initialization object - this will be called when we start the game the
1216 * first time or RESTART within a session. We'll restore the display
1217 * state to the initial conditions.
1218 */
1219 bannerInit: InitObject
1220 execute()
1221 {
1222 /* restore banner displays to their initial conditions */
1223 bannerTracker.restoreDisplayState(true);
1224 }
1225 ;