OSDN Git Service

Merge pull request #3352 from whitehara/update-spec
[hengbandforosx/hengbandosx.git] / src / core / asking-player.cpp
1 #include "core/asking-player.h"
2 #include "cmd-io/macro-util.h"
3 #include "core/stuff-handler.h"
4 #include "core/window-redrawer.h"
5 #include "game-option/input-options.h"
6 #include "io/command-repeater.h"
7 #include "io/input-key-acceptor.h"
8 #include "io/input-key-requester.h" //!< @todo 相互依存している、後で何とかする.
9 #include "main/sound-of-music.h"
10 #include "system/player-type-definition.h"
11 #include "system/redrawing-flags-updater.h"
12 #include "term/gameterm.h"
13 #include "term/screen-processor.h"
14 #include "term/term-color-types.h"
15 #include "term/z-form.h"
16 #include "util/int-char-converter.h"
17 #include "util/string-processor.h"
18 #include "view/display-messages.h"
19 #include <algorithm>
20 #include <charconv>
21 #include <climits>
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25
26 /*
27  * Get some string input at the cursor location.
28  * Assume the buffer is initialized to a default string.
29  *
30  * The default buffer is in Overwrite mode and displayed in yellow at
31  * first.  Normal chars clear the yellow text and append the char in
32  * white text.
33  *
34  * LEFT (^B) and RIGHT (^F) movement keys move the cursor position.
35  * If the text is still displayed in yellow (Overwite mode), it will
36  * turns into white (Insert mode) when cursor moves.
37  *
38  * DELETE (^D) deletes a char at the cursor position.
39  * BACKSPACE (^H) deletes a char at the left of cursor position.
40  * ESCAPE clears the buffer and the window and returns FALSE.
41  * RETURN accepts the current buffer contents and returns TRUE.
42  */
43 bool askfor(char *buf, int len, bool numpad_cursor)
44 {
45     /*
46      * Text color
47      * TERM_YELLOW : Overwrite mode
48      * TERM_WHITE : Insert mode
49      */
50     auto color = TERM_YELLOW;
51
52     int y, x;
53     term_locate(&x, &y);
54     if (len < 1) {
55         len = 1;
56     }
57
58     if ((x < 0) || (x >= MAIN_TERM_MIN_COLS)) {
59         x = 0;
60     }
61
62     if (x + len > MAIN_TERM_MIN_COLS) {
63         len = MAIN_TERM_MIN_COLS - x;
64     }
65
66     buf[len] = '\0';
67     auto pos = 0;
68     while (true) {
69         term_erase(x, y, len);
70         term_putstr(x, y, -1, color, buf);
71         term_gotoxy(x + pos, y);
72         const auto skey = inkey_special(numpad_cursor);
73         switch (skey) {
74         case SKEY_LEFT:
75         case KTRL('b'): {
76             auto i = 0;
77             color = TERM_WHITE;
78             if (0 == pos) {
79                 break;
80             }
81
82             while (true) {
83                 auto next_pos = i + 1;
84 #ifdef JP
85                 if (iskanji(buf[i])) {
86                     next_pos++;
87                 }
88 #endif
89                 if (next_pos >= pos) {
90                     break;
91                 }
92
93                 i = next_pos;
94             }
95
96             pos = i;
97             break;
98         }
99         case SKEY_RIGHT:
100         case KTRL('f'):
101             color = TERM_WHITE;
102             if ('\0' == buf[pos]) {
103                 break;
104             }
105
106 #ifdef JP
107             if (iskanji(buf[pos])) {
108                 pos += 2;
109             } else {
110                 pos++;
111             }
112 #else
113             pos++;
114 #endif
115             break;
116         case ESCAPE:
117             buf[0] = '\0';
118             return false;
119         case '\n':
120         case '\r':
121             return true;
122         case '\010': {
123             auto i = 0;
124             color = TERM_WHITE;
125             if (pos == 0) {
126                 break;
127             }
128
129             while (true) {
130                 auto next_pos = i + 1;
131 #ifdef JP
132                 if (iskanji(buf[i])) {
133                     next_pos++;
134                 }
135 #endif
136                 if (next_pos >= pos) {
137                     break;
138                 }
139
140                 i = next_pos;
141             }
142
143             pos = i;
144         }
145             [[fallthrough]];
146         case 0x7F:
147         case KTRL('d'): {
148             color = TERM_WHITE;
149             if (buf[pos] == '\0') {
150                 break;
151             }
152
153             auto src = pos + 1;
154 #ifdef JP
155             if (iskanji(buf[pos])) {
156                 src++;
157             }
158 #endif
159             auto dst = pos;
160             while ('\0' != (buf[dst++] = buf[src++])) {
161                 ;
162             }
163
164             break;
165         }
166         default: {
167             char tmp[100];
168             if (skey & SKEY_MASK) {
169                 break;
170             }
171
172             const auto c = static_cast<char>(skey);
173             if (color == TERM_YELLOW) {
174                 buf[0] = '\0';
175                 color = TERM_WHITE;
176             }
177
178             strcpy(tmp, buf + pos);
179 #ifdef JP
180             if (iskanji(c)) {
181                 inkey_base = true;
182                 char next = inkey();
183                 if (pos + 1 < len) {
184                     buf[pos++] = c;
185                     buf[pos++] = next;
186                 } else {
187                     bell();
188                 }
189             } else
190 #endif
191             {
192                 const auto is_print = _(isprint(c) || iskana(c), isprint(c));
193                 if (pos < len && is_print) {
194                     buf[pos++] = c;
195                 } else {
196                     bell();
197                 }
198             }
199
200             buf[pos] = '\0';
201             angband_strcat(buf, tmp, len + 1);
202             break;
203         }
204         }
205     }
206 }
207
208 /*
209  * Get a string from the user
210  *
211  * The "prompt" should take the form "Prompt: "
212  *
213  * Note that the initial contents of the string is used as
214  * the default response, so be sure to "clear" it if needed.
215  *
216  * We clear the input, and return FALSE, on "ESCAPE".
217  */
218 bool get_string(std::string_view prompt, char *buf, int len)
219 {
220     bool res;
221     msg_print(nullptr);
222     prt(prompt, 0, 0);
223     res = askfor(buf, len);
224     prt("", 0, 0);
225     return res;
226 }
227
228 /*
229  * Verify something with the user
230  *
231  * The "prompt" should take the form "Query? "
232  *
233  * Note that "[y/n]" is appended to the prompt.
234  */
235 bool get_check(std::string_view prompt)
236 {
237     return get_check_strict(p_ptr, prompt, 0);
238 }
239
240 /*
241  * Verify something with the user strictly
242  *
243  * mode & CHECK_OKAY_CANCEL : force user to answer 'O'kay or 'C'ancel
244  * mode & CHECK_NO_ESCAPE   : don't allow ESCAPE key
245  * mode & CHECK_NO_HISTORY  : no message_add
246  * mode & CHECK_DEFAULT_Y   : accept any key as y, except n and Esc.
247  */
248 bool get_check_strict(PlayerType *player_ptr, std::string_view prompt, BIT_FLAGS mode)
249 {
250     if (!rogue_like_commands) {
251         mode &= ~CHECK_OKAY_CANCEL;
252     }
253
254     std::stringstream ss;
255     ss << prompt;
256     if (mode & CHECK_OKAY_CANCEL) {
257         ss << "[(O)k/(C)ancel]";
258     } else if (mode & CHECK_DEFAULT_Y) {
259         ss << "[Y/n]";
260     } else {
261         ss << "[y/n]";
262     }
263     const auto buf = ss.str();
264
265     auto &rfu = RedrawingFlagsUpdater::get_instance();
266     if (auto_more) {
267         rfu.set_flag(SubWindowRedrawingFlag::MESSAGE);
268         handle_stuff(player_ptr);
269         num_more = 0;
270     }
271
272     msg_print(nullptr);
273
274     prt(buf, 0, 0);
275     if (!(mode & CHECK_NO_HISTORY) && player_ptr->playing) {
276         message_add(buf);
277         rfu.set_flag(SubWindowRedrawingFlag::MESSAGE);
278         handle_stuff(player_ptr);
279     }
280
281     bool flag = false;
282     while (true) {
283         int i = inkey();
284
285         if (!(mode & CHECK_NO_ESCAPE)) {
286             if (i == ESCAPE) {
287                 flag = false;
288                 break;
289             }
290         }
291
292         if (mode & CHECK_OKAY_CANCEL) {
293             if (i == 'o' || i == 'O') {
294                 flag = true;
295                 break;
296             } else if (i == 'c' || i == 'C') {
297                 flag = false;
298                 break;
299             }
300         } else {
301             if (i == 'y' || i == 'Y') {
302                 flag = true;
303                 break;
304             } else if (i == 'n' || i == 'N') {
305                 flag = false;
306                 break;
307             }
308         }
309
310         if (mode & CHECK_DEFAULT_Y) {
311             flag = true;
312             break;
313         }
314
315         bell();
316     }
317
318     prt("", 0, 0);
319     return flag;
320 }
321
322 /*
323  * Prompts for a keypress
324  *
325  * The "prompt" should take the form "Command: "
326  *
327  * Returns TRUE unless the character is "Escape"
328  */
329 bool get_com(std::string_view prompt, char *command, bool z_escape)
330 {
331     msg_print(nullptr);
332     prt(prompt, 0, 0);
333     if (get_com_no_macros) {
334         *command = (char)inkey_special(false);
335     } else {
336         *command = inkey();
337     }
338
339     prt("", 0, 0);
340     if (*command == ESCAPE) {
341         return false;
342     }
343     if (z_escape && ((*command == 'z') || (*command == 'Z'))) {
344         return false;
345     }
346
347     return true;
348 }
349
350 /*
351  * Request a "quantity" from the user
352  *
353  * Hack -- allow "command_arg" to specify a quantity
354  */
355 QUANTITY get_quantity(std::optional<std::string_view> prompt_opt, QUANTITY max)
356 {
357     /*!
358      * @todo QUANTITY、COMMAND_CODE、その他の型サイズがまちまちな変数とのやり取りが多数ある.
359      * この処理での数の入力を0からSHRT_MAXに制限することで不整合の発生を回避する.
360      */
361     max = std::clamp<QUANTITY>(max, 0, SHRT_MAX);
362
363     bool res;
364     char tmp[80];
365     char buf[80];
366
367     QUANTITY amt;
368     if (command_arg) {
369         amt = command_arg;
370         command_arg = 0;
371         if (amt > max) {
372             amt = max;
373         }
374
375         return amt;
376     }
377
378     COMMAND_CODE code;
379     bool result = repeat_pull(&code);
380     amt = (QUANTITY)code;
381     if ((max != 1) && result) {
382         if (amt > max) {
383             amt = max;
384         }
385         if (amt < 0) {
386             amt = 0;
387         }
388
389         return amt;
390     }
391
392     std::string_view prompt;
393     if (prompt_opt.has_value()) {
394         prompt = prompt_opt.value();
395     } else {
396         strnfmt(tmp, sizeof(tmp), _("いくつですか (1-%d): ", "Quantity (1-%d): "), max);
397         prompt = tmp;
398     }
399
400     msg_print(nullptr);
401     prt(prompt, 0, 0);
402     amt = 1;
403     strnfmt(buf, sizeof(buf), "%d", amt);
404
405     /*
406      * Ask for a quantity
407      * Don't allow to use numpad as cursor key.
408      */
409     res = askfor(buf, 6, false);
410
411     prt("", 0, 0);
412     if (!res) {
413         return 0;
414     }
415
416     if (isalpha(buf[0])) {
417         amt = max;
418     } else {
419         amt = std::clamp<int>(atoi(buf), 0, max);
420     }
421
422     if (amt) {
423         repeat_push((COMMAND_CODE)amt);
424     }
425
426     return amt;
427 }
428
429 /*
430  * Pause for user response
431  */
432 void pause_line(int row)
433 {
434     prt("", row, 0);
435     put_str(_("[ 何かキーを押して下さい ]", "[Press any key to continue]"), row, _(26, 23));
436
437     (void)inkey();
438     prt("", row, 0);
439 }
440
441 bool get_value(std::string_view prompt, int min, int max, int *value)
442 {
443     std::stringstream st;
444     int val;
445     char tmp_val[12] = "";
446     std::to_chars(std::begin(tmp_val), std::end(tmp_val) - 1, *value);
447     st << prompt << "(" << min << "-" << max << "): ";
448     int digit = std::max(std::to_string(min).length(), std::to_string(max).length());
449     while (true) {
450         if (!get_string(st.str().data(), tmp_val, digit)) {
451             return false;
452         }
453
454         val = atoi(tmp_val);
455
456         if (min <= val && max >= val) {
457             break;
458         }
459         msg_format(_("%dから%dの間で指定して下さい。", "It must be between %d to %d."), min, max);
460     }
461     *value = val;
462     return true;
463 }