Debian Codegraph @

msg.cpp

raw symbols SCIP
1 /*
2 * PROPRIETARY INFORMATION. This software is proprietary to POWDER
3 * Development, and is not to be reproduced, transmitted, or disclosed
4 * in any way without written permission.
5 *
6 * Produced by: Jeff Lait
7 *
8 * POWDER Development
9 *
10 * NAME: msg.cpp ( POWDER Library, C++ )
11 *
12 * COMMENTS:
13 * This is the message glue library. It handles the writing
14 * of status messages or prompts to the display. It deals with
15 * -more-, scrollback, wordwrap, etc.
16 *
17 * For displaying a block of scrollable text, see the gfx_
18 * functions in gfx_engine.
19 */
20
21 #include <string.h>
22 #include <ctype.h>
23 #include <stdio.h>
24 #include "control.h"
25 #include "gfxengine.h"
26 #include "msg.h"
27 #include "assert.h"
28 #include "grammar.h"
29 #include "map.h" // Needed for refresh.
30 #include "stylus.h"
31 #include "victory.h"
32
33 // Width of screen in characters
34 #define SCREEN_WIDTH 30
35 // Number of lines per page.
36 #define PAGE_SIZE 3
37
38 // Just used as our default system sucks
39 #define HYPHEN_SYMBOL '-'
40
41 // Buttons..
42 #define CONTINUE_BUTTON BUTTON_R
43 #define YES_BUTTON BUTTON_A
44 #define NO_BUTTON BUTTON_B
45
46 #define NUM_STASHES 20
47
48 // The current message.
49 char *glbMsg[PAGE_SIZE];
50 char *glbStashedMsg[NUM_STASHES][PAGE_SIZE];
51 int glbCurLine;
52 const char *glbBlank = " ";
53 int glbStashHead, glbStashView;
54 bool glbViewingOld;
55
56 // Defined in Main. Really ugly to do this.
57 // I feel great shame.
58 extern void writeStatusLine();
59 extern bool glbAutoRunEnabled;
60
61 void
62 msg_init()
63 {
64 int i, j;
65 for (i = 0; i < PAGE_SIZE; i++)
66 {
67 glbMsg[i] = new char[SCREEN_WIDTH+1];
68 for (j = 0; j < NUM_STASHES; j++)
69 {
70 glbStashedMsg[j][i] = new char[SCREEN_WIDTH+1];
71 glbStashedMsg[j][i][0] = '\0';
72 }
73 glbMsg[i][0] = '\0';
74 }
75 glbCurLine = 0;
76 glbViewingOld = false;
77 }
78
79 void
80 msg_awaitaccept()
81 {
82 // We take this chance to rebuild our display so we can react
83 // to changes.
84 // TODO: Since we may be inside fireball code, we don't want to update
85 // overlay tiles. (Or do we?)
86 if (glbCurLevel)
87 glbCurLevel->refresh(true);
88 writeStatusLine();
89
90 if (glbStressTest)
91 return;
92 if (hamfake_forceQuit())
93 return;
94
95 // After some play testings, I decided that hitting anything should
96 // bring the next message. It has to be a net new press though.
97
98 #ifndef HAS_KEYBOARD
99 // Wait for it to go off
100 while (ctrl_anyrawpressed() && !hamfake_forceQuit());
101 // And on
102 while (!ctrl_anyrawpressed() && !hamfake_forceQuit());
103 // And off...
104 while (ctrl_anyrawpressed() && !hamfake_forceQuit());
105 #else
106 hamfake_clearKeyboardBuffer();
107
108 // Await a new non-trivial keypress & eat it.
109 while (!hamfake_getKeyPress(false) && !hamfake_forceQuit())
110 {
111 hamfake_awaitEvent();
112 }
113 // We don't want the new press to count as repeatable key.
114 hamfake_clearKeyboardBuffer();
115 #endif
116
117 #if 0
118 // Wait for L to go off..
119
120 while (ctrl_rawpressed(CONTINUE_BUTTON));
121
122 // Wait for it to go on...
123 while (!ctrl_rawpressed(CONTINUE_BUTTON));
124
125 // And off again...
126 while (ctrl_rawpressed(CONTINUE_BUTTON));
127 #endif
128 }
129
130 // Updates message display - doesn't erase old!
131 static void
132 msg_refresh()
133 {
134 int i;
135
136 for (i = 0; i < PAGE_SIZE; i++)
137 {
138 gfx_printtext(0, i, glbMsg[i]);
139 }
140 }
141
142 // Clears the graphics text...
143 static void
144 msg_cleartext()
145 {
146 int i;
147
148 for (i = 0; i < PAGE_SIZE; i++)
149 {
150 gfx_printtext(0, i, glbBlank);
151 }
152 }
153
154 // Private methods...
155 static void
156 msg_write(const char *str, bool wait=true)
157 {
158 const char *src;
159 char *dst;
160 char *origdst;
161 int dstlen, dstmaxlen;
162
163 glbViewingOld = false;
164
165 src = str;
166 // Find first clear line...
167 origdst = dst = glbMsg[glbCurLine];
168 // Last line needs extra char for next symbol.
169 dstmaxlen = SCREEN_WIDTH - (glbCurLine == (PAGE_SIZE-1));
170 dstlen = strlen(dst);
171 dst += dstlen;
172
173 while (*src && dstlen < dstmaxlen)
174 {
175 *dst++ = *src++;
176 dstlen++;
177 }
178 // If we quit because we ran out of source material, great!
179 // Null terminate and quit...
180 if (!*src)
181 {
182 *dst++ = '\0';
183 msg_refresh();
184 return;
185 }
186
187 // We have to wrap... If src is a space, wrapping is trivial.
188 // Other wise, we backup src until it is a space...
189 while (!isspace(*src) && src > str)
190 {
191 src--;
192 dst--;
193 dstlen--;
194 }
195
196 // Now, move forward until we ate all the spaces (this seems odd
197 // except if *src was a space going into the last loop)
198 while (*src && isspace(*src))
199 {
200 src++;
201 }
202
203 // It could be trailing spaces sent us over the bounds. We will
204 // thus eat these and ignore them.
205 if (!*src)
206 {
207 *dst++ = '\0';
208 msg_refresh();
209 return;
210 }
211
212 char prefix[SCREEN_WIDTH+1];
213 prefix[0] = '\0';
214
215 // Special case: If src == str now, yet our dstlen == 0, we have a
216 // single word over SCREEN_WIDTH. We resolve this by dumping
217 // dstmaxlen - 1 chars, a hyphen, and then continuing as if we
218 // found a space.
219 if (src == str && !dstlen)
220 {
221 while (*src && dstlen < dstmaxlen-1)
222 {
223 *dst++ = *src++;
224 dstlen++;
225 }
226 // Append a hyphen...
227 *dst++ = HYPHEN_SYMBOL;
228 }
229 // Special case: If src == str, dstlen != 0, we are tacking onto
230 // the end of an existing word. If that word ends with a space,
231 // great, we wrap as expected. However, we often build up
232 // a string with a series of writes, such as "foo" followed by ", bar".
233 // We want to wrap the last word on dst. If dst doesn't have a sub
234 // word, obviously we avoid this.
235 // Of course, this isn't the full story. It could be that the
236 // previous line got to exactly 30 columns. Any spaces would be
237 // silently eaten so it would look like there are no spaces.
238 // To solve this, we look for special non-breaking characters that
239 // we feel should be enjoined. Specifically, a punctuation on the
240 // LHS means we should break, and a punctuation on the RHS means
241 // we should not.
242 else if (src == str)
243 {
244 char rhs, lhs;
245 rhs = src[0];
246 lhs = origdst[dstlen-1];
247 // Check if LHS is breaking, in which case we always break
248 // so ignore this code path.
249 if (!isspace(lhs) && lhs != '.' && lhs != ',' && lhs != '!' && lhs != '?')
250 {
251 // Find the start of the word in dst...
252 int wordstart = dstlen - 1;
253 while (!isspace(origdst[wordstart]) && wordstart > 0)
254 {
255 wordstart--;
256 }
257
258 // If wordstart is now zero, our previuos entry was the
259 // full length. We might hyphenate here, but I think it
260 // is simpler just to let the break lie where we broke
261 // it before.
262 if (wordstart)
263 {
264 // Since wordstart points to a space, we can safely
265 // mark wordstart as null & strcpy out wordstart+1
266 strcpy(prefix, &origdst[wordstart+1]);
267 origdst[wordstart] = '\0';
268 }
269 }
270 }
271
272 // Wrap to the next line. We now check if this was the last line,
273 // in which case we dump the next page symbol and wait, and then
274 // reupdate all. Otherwise, we just move forward a bit...
275 if (glbCurLine == PAGE_SIZE-1)
276 {
277 // Append a next line char...
278 *dst++ = SYMBOL_NEXT;
279 *dst++ = '\0';
280 msg_refresh();
281 if (wait)
282 msg_awaitaccept();
283 // Now, reset...
284 msg_clear();
285 }
286 else
287 {
288 // Null append, go to next line...
289 *dst++ = '\0';
290 glbCurLine++;
291 }
292 // Write out src to the next line via recursion.
293 msg_write(prefix);
294 msg_write(src);
295 }
296
297 void
298 msg_report(const char *msg, bool wait)
299 {
300 msg_write(msg, wait);
301 msg_refresh();
302 }
303
304 void
305 msg_report(BUF buf, bool wait)
306 {
307 msg_report(buf.buffer(), wait);
308 }
309
310 void
311 msg_announce(const char *msg)
312 {
313 // Write across the center of the screen:
314 int y, x;
315 const char *cur, *lastword;
316
317 y = 7;
318 x = 0;
319
320 // We always break out of auto-run for these important
321 // messages!
322 glbAutoRunEnabled = false;
323
324 cur = msg;
325 while (*cur)
326 {
327 gfx_printchar(x, y, *cur);
328 cur++;
329 x++;
330
331 // Check for line wraps.
332 if (x >= SCREEN_WIDTH)
333 {
334 lastword = cur;
335 if (*lastword && !isspace(*lastword))
336 {
337 // We did not end on a new word.
338 // We try to rewind to the last space.
339 while (x > 0 && lastword > msg)
340 {
341 if (isspace(*lastword))
342 break;
343 lastword--;
344 x--;
345 }
346 if (x)
347 {
348 // Erase previous line and reset our cur to
349 // last word.
350 // This fails if the single word was more than 30
351 // char, in which case we just wrap (TODO: Hyphen!)
352 cur = lastword;
353 gfx_cleartextline(y, x);
354 }
355 }
356
357 // Eat all spaces from cur as they are oldschool.
358 while (*cur && isspace(*cur))
359 cur++;
360
361 // Move to next line:
362 x = 0;
363 y++;
364 }
365 }
366
367 // Await confirmation of having read this announcment.
368 msg_awaitaccept();
369
370 // Clear written to lines.
371 while (y >= 7)
372 {
373 gfx_cleartextline(y);
374 y--;
375 }
376
377 // Do old school report. This way it ends up in our buffer.
378 // We disable the wait for next as the user has already confirmed
379 // reading this message.
380 msg_report(msg, false);
381 }
382
383 void
384 msg_announce(BUF buf)
385 {
386 msg_announce(buf.buffer());
387 }
388
389 void
390 msg_append(const char *msg)
391 {
392 msg_write(msg);
393 msg_refresh();
394 }
395
396 void
397 msg_alert(const char *msg)
398 {
399 BUF buf;
400
401 if (!msg_isempty())
402 {
403 msg_awaitaccept();
404 msg_clear();
405 }
406 buf.sprintf("%s%c", msg, SYMBOL_NEXT);
407
408 msg_write(buf.buffer());
409 msg_refresh();
410 msg_awaitaccept();
411 msg_clear();
412 msg_refresh();
413 }
414
415
416 bool
417 msg_askdir(const char *question, int &dx, int &dy, bool allowdiag)
418 {
419 bool found = false;
420 STYLUSLOCK styluslock;
421
422 hamfake_flushdir();
423 if (!allowdiag)
424 {
425 styluslock.setRegion(REGION_FOURDIR);
426 hamfake_buttonreq(5, 3);
427 }
428 else
429 {
430 styluslock.setRegion(REGION_TENDIR); // Should be 8-dir!
431 hamfake_buttonreq(5, 2);
432 }
433
434 msg_write(question);
435 msg_refresh();
436
437 #ifdef HAS_KEYBOARD
438 int keypress = 0;
439 bool lock;
440 int dz = 0;
441
442 while (1)
443 {
444 keypress = hamfake_getKeyPress(false);
445 if (hamfake_forceQuit())
446 break;
447
448 if (keypress && keypress != GFX_KEYLMB)
449 {
450 ctrl_getdirfromkey(keypress, dx, dy, allowdiag);
451 found = (dx || dy);
452 break;
453 }
454
455 if (!allowdiag)
456 lock = styluslock.getfourdir(dx, dy, found);
457 else
458 lock = styluslock.gettendir(dx, dy, dz, found);
459 if (lock)
460 {
461 found = !found;
462 if (found && !dx && !dy)
463 found = false;
464 break;
465 }
466 }
467 #else
468 while (1)
469 {
470 if (!gfx_isnewframe())
471 continue;
472
473 if (hamfake_forceQuit())
474 break;
475
476 ctrl_getdir(dx, dy);
477 hamfake_clearKeyboardBuffer();
478 if (dx || dy)
479 {
480 found = true;
481 break;
482 }
483
484 if (hamfake_externaldir(dx, dy))
485 {
486 found = true;
487 if (!dx && !dy)
488 found = false;
489 break;
490 }
491
492 if (styluslock.getfourdir(dx, dy, found))
493 {
494 found = !found;
495 if (found && !dx && !dy)
496 found = false;
497 break;
498 }
499 // Check for B, which is abort.
500 if (ctrl_hit(BUTTON_B))
501 break;
502 }
503 #endif
504
505 msg_clear();
506 hamfake_buttonreq(5, 0);
507
508 return found;
509 }
510
511 void
512 msg_clear()
513 {
514 int i, j;
515
516 if (msg_isempty())
517 {
518 glbViewingOld = false;
519 glbStashView = glbStashHead;
520 return;
521 }
522
523 if (!glbViewingOld)
524 {
525 // Stash the message...
526 glbStashHead++;
527 if (glbStashHead >= NUM_STASHES)
528 glbStashHead = 0;
529 for (i = 0; i < PAGE_SIZE; i++)
530 {
531 strcpy(glbStashedMsg[glbStashHead][i], glbMsg[i]);
532
533 // Strip continuation characters from the old message
534 // as now irrelvant. This also makes the DS look neater.
535 for (j = 0; glbStashedMsg[glbStashHead][i][j]; j++)
536 {
537 switch (glbStashedMsg[glbStashHead][i][j])
538 {
539 case SYMBOL_NEXT:
540 glbStashedMsg[glbStashHead][i][j] = ' ';
541 break;
542 }
543 }
544
545 #ifdef USING_DS
546 if (glbStashedMsg[glbStashHead][i][0] != '\0')
547 {
548 // Print out to console.
549 iprintf(glbStashedMsg[glbStashHead][i]);
550 iprintf("\n");
551 }
552 #endif
553 }
554 }
555 glbViewingOld = false;
556 glbStashView = glbStashHead;
557
558 msg_cleartext();
559 glbCurLine = 0;
560 for (i = 0; i < PAGE_SIZE; i++)
561 {
562 glbMsg[i][0] = '\0';
563 }
564 }
565
566 bool
567 msg_isempty()
568 {
569 if (glbCurLine != 0)
570 return false;
571
572 if (glbMsg[0][0] != '\0')
573 return false;
574
575 return true;
576 }
577
578 // Returns false if did nothing.
579 bool
580 msg_restore(bool restorelast)
581 {
582 int i;
583 int newview;
584 bool startview = false;
585
586 if (!glbViewingOld)
587 {
588 // If someone wants the next message while viewing the
589 // most current, nothing to do...
590 if (!restorelast)
591 return false;
592
593 if (!msg_isempty())
594 {
595 // We first need to stash our current message...
596 msg_clear();
597 }
598 else
599 {
600 // We want to start off viewing the most recent message.
601 startview = true;
602 }
603 }
604
605 // Determine the new message to view...
606 if (!restorelast && (glbStashView == glbStashHead))
607 {
608 // We are viewing the most recent and asked for next,
609 // so we abandon.
610 return false;
611 }
612 if (restorelast)
613 {
614 if (startview)
615 newview = glbStashView;
616 else
617 {
618 newview = glbStashView - 1;
619 if (newview < 0)
620 newview = NUM_STASHES-1;
621 if (newview == glbStashHead)
622 {
623 // The user is viewing the oldest possible message
624 // and asked for last - we thus fail.
625 return false;
626 }
627 }
628 }
629 else
630 {
631 newview = glbStashView + 1;
632 if (newview >= NUM_STASHES)
633 newview = 0;
634 }
635
636 // We have a valid stashedview...
637 glbStashView = newview;
638 glbViewingOld = true;
639
640 glbCurLine = 0;
641 for (i = 0; i < PAGE_SIZE; i++)
642 {
643 strcpy(glbMsg[i], glbStashedMsg[glbStashView][i]);
644 if (glbMsg[i][0] != '\0')
645 glbCurLine = i;
646 }
647 msg_cleartext();
648 msg_refresh();
649
650 return true;
651 }
652
653 void
654 msg_displayhistory()
655 {
656 // First, clear out menu.
657 // Also reset if we are viewing an old message.
658 if (!msg_isempty() || glbViewingOld)
659 msg_clear();
660
661 // Now read out our entire list.
662 int view, line, numline;
663
664 // Find first message.
665 view = glbStashHead;
666
667 numline = 0;
668 do
669 {
670 // Move to newer view
671 view++;
672 if (view >= NUM_STASHES)
673 view -= NUM_STASHES;
674
675 // Add this message to the pager.
676 for (line = 0; line < PAGE_SIZE; line++)
677 {
678 // Only add the line if it is non empty.
679 if (glbStashedMsg[view][line][0] == '\0')
680 continue;
681
682 gfx_pager_addsingleline(glbStashedMsg[view][line]);
683 numline++;
684 }
685 } while (view != glbStashHead);
686
687 gfx_pager_display(numline);
688 }