OSDN Git Service

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