| | 1 | #charset "us-ascii" |
| | 2 | |
| | 3 | /* Copyright 2002 Michael J. Roberts */ |
| | 4 | /* |
| | 5 | * This is an interactive test for the new persistent banner manager |
| | 6 | * module, banner.t. You can add this into any adv3-based game; we'll |
| | 7 | * simply add a few new verbs to the game that let the player exercise |
| | 8 | * banners interactively: |
| | 9 | * |
| | 10 | * addbanner left|right|top|bottom id - adds a new banner, aligned as |
| | 11 | * requested and with the given ID. |
| | 12 | * |
| | 13 | * writebanner id <any text> - writes the given text to the banner. The |
| | 14 | * new text is appended to any text currently in the banner, followed by |
| | 15 | * a newline. |
| | 16 | * |
| | 17 | * delbanner id - removes the banner with the given ID. |
| | 18 | */ |
| | 19 | |
| | 20 | #include <adv3.h> |
| | 21 | #include <en_us.h> |
| | 22 | |
| | 23 | /* |
| | 24 | * An object to track our interactively-created banners. |
| | 25 | */ |
| | 26 | addedBannerTracker: object |
| | 27 | /* a table of our added banners, indexed by ID */ |
| | 28 | bannerTab = static new LookupTable(16, 32) |
| | 29 | ; |
| | 30 | |
| | 31 | DefineIAction(AddBanner) |
| | 32 | execAction() |
| | 33 | { |
| | 34 | /* add the banner at the top level (i.e., no banner parent) */ |
| | 35 | doAddBanner(nil, id_, align_.bannerAlign, 10); |
| | 36 | } |
| | 37 | |
| | 38 | doAddBanner(parentID, id, align, pct) |
| | 39 | { |
| | 40 | local parent; |
| | 41 | |
| | 42 | /* make sure the name isn't already in use */ |
| | 43 | if (addedBannerTracker.bannerTab[id] != nil) |
| | 44 | { |
| | 45 | "That banner ID is already in use. "; |
| | 46 | return; |
| | 47 | } |
| | 48 | |
| | 49 | /* create a new banner window object */ |
| | 50 | local win = new BannerWindow('user:' + id); |
| | 51 | |
| | 52 | /* if a parent was specified, find it by ID */ |
| | 53 | if (parentID != nil) |
| | 54 | { |
| | 55 | /* look up the parent by ID */ |
| | 56 | parent = addedBannerTracker.bannerTab[parentID]; |
| | 57 | |
| | 58 | /* make sure we found it */ |
| | 59 | if (parent == nil) |
| | 60 | { |
| | 61 | "The specified parent doesn't exist. "; |
| | 62 | return; |
| | 63 | } |
| | 64 | } |
| | 65 | else |
| | 66 | { |
| | 67 | /* there's no parent */ |
| | 68 | parent = nil; |
| | 69 | } |
| | 70 | |
| | 71 | /* show the banner */ |
| | 72 | if (!win.showBanner(parent, BannerLast, nil, BannerTypeText, |
| | 73 | align, pct, BannerSizePercent, |
| | 74 | BannerStyleBorder | BannerStyleVScroll |
| | 75 | | BannerStyleAutoVScroll)) |
| | 76 | { |
| | 77 | "Unable to create new banner window. "; |
| | 78 | return; |
| | 79 | } |
| | 80 | |
| | 81 | /* write some initial text */ |
| | 82 | win.writeToBanner('This is banner <q>' + id + '</q>\n'); |
| | 83 | win.flushBanner(); |
| | 84 | |
| | 85 | /* add it to the table of active banners */ |
| | 86 | addedBannerTracker.bannerTab[id] = win; |
| | 87 | |
| | 88 | /* indicate success */ |
| | 89 | "Banner created. "; |
| | 90 | } |
| | 91 | ; |
| | 92 | |
| | 93 | DefineIAction(AddSubBanner) |
| | 94 | execAction() |
| | 95 | { |
| | 96 | /* add the banner with the given parent */ |
| | 97 | AddBannerAction.doAddBanner(parid_, id_, align_.bannerAlign, 40); |
| | 98 | } |
| | 99 | ; |
| | 100 | |
| | 101 | VerbRule(AddBanner) |
| | 102 | 'addbanner' bannerAlignType->align_ tokWord->id_ |
| | 103 | : AddBannerAction |
| | 104 | verbPhrase = 'addbanner/adding banner' |
| | 105 | ; |
| | 106 | |
| | 107 | VerbRule(AddBannerEmpty) 'addbanner' : IAction |
| | 108 | execAction() { "usage: addbanner left|right|top|bottom <i>id</i> "; } |
| | 109 | ; |
| | 110 | |
| | 111 | VerbRule(AddSubBanner) |
| | 112 | 'addsub' tokWord->parid_ bannerAlignType->align_ tokWord->id_ |
| | 113 | : AddSubBannerAction |
| | 114 | verbPhrase = 'addsub/adding sub-banner' |
| | 115 | ; |
| | 116 | |
| | 117 | VerbRule(AddSubBannerEmpty) 'addsub' : IAction |
| | 118 | execAction() { "usage: addsub <i>parent_id</i> left|right|top|bottom |
| | 119 | <i>id</i> "; } |
| | 120 | ; |
| | 121 | |
| | 122 | grammar bannerAlignType(left): 'left' : BasicProd |
| | 123 | bannerAlign = BannerAlignLeft |
| | 124 | ; |
| | 125 | grammar bannerAlignType(right): 'right' : BasicProd |
| | 126 | bannerAlign = BannerAlignRight |
| | 127 | ; |
| | 128 | grammar bannerAlignType(top): 'top' : BasicProd |
| | 129 | bannerAlign = BannerAlignTop |
| | 130 | ; |
| | 131 | grammar bannerAlignType(bottom): 'bottom' : BasicProd |
| | 132 | bannerAlign = BannerAlignBottom |
| | 133 | ; |
| | 134 | |
| | 135 | DefineLiteralAction(WriteBanner) |
| | 136 | execAction() |
| | 137 | { |
| | 138 | local win; |
| | 139 | |
| | 140 | /* make sure the banner actually exists */ |
| | 141 | win = addedBannerTracker.bannerTab[id_]; |
| | 142 | if (win == nil) |
| | 143 | { |
| | 144 | "There is no such banner. "; |
| | 145 | return; |
| | 146 | } |
| | 147 | |
| | 148 | /* write the literal text to the banner */ |
| | 149 | win.writeToBanner(getLiteral() + '\n'); |
| | 150 | win.flushBanner(); |
| | 151 | |
| | 152 | /* indicate success */ |
| | 153 | "Done. "; |
| | 154 | } |
| | 155 | ; |
| | 156 | |
| | 157 | VerbRule(WriteBanner) 'writebanner' tokWord->id_ singleLiteral |
| | 158 | : WriteBannerAction |
| | 159 | verbPhrase = 'write/writing to banner (what)' |
| | 160 | ; |
| | 161 | |
| | 162 | DefineIAction(DelBanner) |
| | 163 | execAction() |
| | 164 | { |
| | 165 | local win; |
| | 166 | |
| | 167 | /* make sure the banner actually exists */ |
| | 168 | win = addedBannerTracker.bannerTab[id_]; |
| | 169 | if (win == nil) |
| | 170 | { |
| | 171 | "There is no such banner. "; |
| | 172 | return; |
| | 173 | } |
| | 174 | |
| | 175 | /* remove the banner window */ |
| | 176 | win.removeBanner(); |
| | 177 | |
| | 178 | /* stop tracking the banner window object */ |
| | 179 | addedBannerTracker.bannerTab.removeElement(id_); |
| | 180 | |
| | 181 | /* indicate success */ |
| | 182 | "Done. "; |
| | 183 | } |
| | 184 | ; |
| | 185 | |
| | 186 | VerbRule(DelBanner) 'delbanner' tokWord->id_ : DelBannerAction |
| | 187 | verbPhrase = 'delbanner/deleting banner' |
| | 188 | ; |