OSDN Git Service

Merge branch 'master' of git.osdn.net:/gitroot/hengband/hengband
[hengband/hengband.git] / src / term / screen-processor.c
1 #include "term/screen-processor.h"
2 #include "io/input-key-acceptor.h"
3 #include "term/term-color-types.h"
4 #include "view/display-messages.h"
5 #include "world/world.h"
6
7 /*
8  * Hack -- prevent "accidents" in "screen_save()" or "screen_load()"
9  */
10 static int screen_depth = 0;
11
12 /*
13  * Move the cursor
14  */
15 void move_cursor(int row, int col) { term_gotoxy(col, row); }
16
17 /*
18  * Flush all input chars.  Actually, remember the flush,
19  * and do a "special flush" before the next "inkey()".
20  *
21  * This is not only more efficient, but also necessary to make sure
22  * that various "inkey()" codes are not "lost" along the way.
23  */
24 void flush(void) { inkey_xtra = TRUE; }
25
26 /*
27  * Save the screen, and increase the "icky" depth.
28  *
29  * This function must match exactly one call to "screen_load()".
30  */
31 void screen_save()
32 {
33     msg_print(NULL);
34     if (screen_depth++ == 0)
35         term_save();
36
37     current_world_ptr->character_icky++;
38 }
39
40 /*
41  * Load the screen, and decrease the "icky" depth.
42  *
43  * This function must match exactly one call to "screen_save()".
44  */
45 void screen_load()
46 {
47     msg_print(NULL);
48     if (--screen_depth == 0)
49         term_load();
50
51     current_world_ptr->character_icky--;
52 }
53
54 /*
55  * Display a string on the screen using an attribute.
56  *
57  * At the given location, using the given attribute, if allowed,
58  * add the given string.  Do not clear the line.
59  */
60 void c_put_str(TERM_COLOR attr, concptr str, TERM_LEN row, TERM_LEN col) { term_putstr(col, row, -1, attr, str); }
61
62 /*
63  * As above, but in "white"
64  */
65 void put_str(concptr str, TERM_LEN row, TERM_LEN col) { term_putstr(col, row, -1, TERM_WHITE, str); }
66
67 /*
68  * Display a string on the screen using an attribute, and clear
69  * to the end of the line.
70  */
71 void c_prt(TERM_COLOR attr, concptr str, TERM_LEN row, TERM_LEN col)
72 {
73     term_erase(col, row, 255);
74     term_addstr(-1, attr, str);
75 }
76
77 /*
78  * As above, but in "white"
79  */
80 void prt(concptr str, TERM_LEN row, TERM_LEN col)
81 {
82     /* Spawn */
83     c_prt(TERM_WHITE, str, row, col);
84 }
85
86 /*
87  * Print some (colored) text to the screen at the current cursor position,
88  * automatically "wrapping" existing text (at spaces) when necessary to
89  * avoid placing any text into the last column, and clearing every line
90  * before placing any text in that line.  Also, allow "newline" to force
91  * a "wrap" to the next line.  Advance the cursor as needed so sequential
92  * calls to this function will work correctly.
93  *
94  * Once this function has been called, the cursor should not be moved
95  * until all the related "c_roff()" calls to the window are complete.
96  *
97  * This function will correctly handle any width up to the maximum legal
98  * value of 256, though it works best for a standard 80 character width.
99  */
100 void c_roff(TERM_COLOR a, concptr str)
101 {
102     int w, h;
103     (void)term_get_size(&w, &h);
104
105     int x, y;
106     (void)term_locate(&x, &y);
107
108     if (y == h - 1 && x > w - 3)
109         return;
110
111     for (concptr s = str; *s; s++) {
112         char ch;
113 #ifdef JP
114         int k_flag = iskanji(*s);
115 #endif
116         if (*s == '\n') {
117             x = 0;
118             y++;
119             if (y == h)
120                 break;
121
122             term_erase(x, y, 255);
123             break;
124         }
125
126 #ifdef JP
127         ch = ((k_flag || isprint(*s)) ? *s : ' ');
128 #else
129         ch = (isprint(*s) ? *s : ' ');
130 #endif
131
132 #ifdef JP
133         if ((x >= ((k_flag) ? w - 2 : w - 1)) && (ch != ' '))
134 #else
135         if ((x >= w - 1) && (ch != ' '))
136 #endif
137         {
138             int i, n = 0;
139
140             TERM_COLOR av[256];
141             char cv[256];
142             if (x < w)
143 #ifdef JP
144             {
145                 /* 現在が半角文字の場合 */
146                 if (!k_flag)
147 #endif
148                 {
149                     for (i = w - 2; i >= 0; i--) {
150                         term_what(i, y, &av[i], &cv[i]);
151                         if (cv[i] == ' ')
152                             break;
153
154                         n = i;
155 #ifdef JP
156                         if (cv[i] == '(')
157                             break;
158 #endif
159                     }
160                 }
161 #ifdef JP
162                 else {
163                     /* 現在が全角文字のとき */
164                     /* 文頭が「。」「、」等になるときは、その1つ前の語で改行 */
165                     if (strncmp(s, "。", 2) == 0 || strncmp(s, "、", 2) == 0) {
166                         term_what(x, y, &av[x], &cv[x]);
167                         term_what(x - 1, y, &av[x - 1], &cv[x - 1]);
168                         term_what(x - 2, y, &av[x - 2], &cv[x - 2]);
169                         n = x - 2;
170                         cv[x] = '\0';
171                     }
172                 }
173             }
174 #endif
175             if (n == 0)
176                 n = w;
177
178             term_erase(n, y, 255);
179             x = 0;
180             y++;
181             if (y == h)
182                 break;
183
184             term_erase(x, y, 255);
185             for (i = n; i < w - 1; i++) {
186 #ifdef JP
187                 if (cv[i] == '\0')
188                     break;
189 #endif
190                 term_addch(av[i], cv[i]);
191                 if (++x > w)
192                     x = w;
193             }
194         }
195
196 #ifdef JP
197         term_addch((byte)(a | 0x10), ch);
198 #else
199         term_addch(a, ch);
200 #endif
201
202 #ifdef JP
203         if (k_flag) {
204             s++;
205             x++;
206             ch = *s;
207             term_addch((byte)(a | 0x20), ch);
208         }
209 #endif
210
211         if (++x > w)
212             x = w;
213     }
214 }
215
216 /*
217  * As above, but in "white"
218  */
219 void roff(concptr str)
220 {
221     /* Spawn */
222     c_roff(TERM_WHITE, str);
223 }
224
225 /*
226  * Clear part of the screen
227  */
228 void clear_from(int row)
229 {
230     for (int y = row; y < Term->hgt; y++) {
231         term_erase(0, y, 255);
232     }
233 }