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"
27 * Get some string input at the cursor location.
28 * Assume the buffer is initialized to a default string.
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
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.
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.
43 bool askfor(char *buf, int len, bool numpad_cursor)
47 * TERM_YELLOW : Overwrite mode
48 * TERM_WHITE : Insert mode
50 auto color = TERM_YELLOW;
58 if ((x < 0) || (x >= MAIN_TERM_MIN_COLS)) {
62 if (x + len > MAIN_TERM_MIN_COLS) {
63 len = MAIN_TERM_MIN_COLS - x;
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);
83 auto next_pos = i + 1;
85 if (iskanji(buf[i])) {
89 if (next_pos >= pos) {
102 if ('\0' == buf[pos]) {
107 if (iskanji(buf[pos])) {
130 auto next_pos = i + 1;
132 if (iskanji(buf[i])) {
136 if (next_pos >= pos) {
149 if (buf[pos] == '\0') {
155 if (iskanji(buf[pos])) {
160 while ('\0' != (buf[dst++] = buf[src++])) {
168 if (skey & SKEY_MASK) {
172 const auto c = static_cast<char>(skey);
173 if (color == TERM_YELLOW) {
178 strcpy(tmp, buf + pos);
192 const auto is_print = _(isprint(c) || iskana(c), isprint(c));
193 if (pos < len && is_print) {
201 angband_strcat(buf, tmp, len + 1);
209 * Get a string from the user
211 * The "prompt" should take the form "Prompt: "
213 * Note that the initial contents of the string is used as
214 * the default response, so be sure to "clear" it if needed.
216 * We clear the input, and return FALSE, on "ESCAPE".
218 bool get_string(std::string_view prompt, char *buf, int len)
223 res = askfor(buf, len);
229 * Verify something with the user
231 * The "prompt" should take the form "Query? "
233 * Note that "[y/n]" is appended to the prompt.
235 bool get_check(std::string_view prompt)
237 return get_check_strict(p_ptr, prompt, 0);
241 * Verify something with the user strictly
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.
248 bool get_check_strict(PlayerType *player_ptr, std::string_view prompt, BIT_FLAGS mode)
250 if (!rogue_like_commands) {
251 mode &= ~CHECK_OKAY_CANCEL;
254 std::stringstream ss;
256 if (mode & CHECK_OKAY_CANCEL) {
257 ss << "[(O)k/(C)ancel]";
258 } else if (mode & CHECK_DEFAULT_Y) {
263 const auto buf = ss.str();
265 auto &rfu = RedrawingFlagsUpdater::get_instance();
267 rfu.set_flag(SubWindowRedrawingFlag::MESSAGE);
268 handle_stuff(player_ptr);
275 if (!(mode & CHECK_NO_HISTORY) && player_ptr->playing) {
277 rfu.set_flag(SubWindowRedrawingFlag::MESSAGE);
278 handle_stuff(player_ptr);
285 if (!(mode & CHECK_NO_ESCAPE)) {
292 if (mode & CHECK_OKAY_CANCEL) {
293 if (i == 'o' || i == 'O') {
296 } else if (i == 'c' || i == 'C') {
301 if (i == 'y' || i == 'Y') {
304 } else if (i == 'n' || i == 'N') {
310 if (mode & CHECK_DEFAULT_Y) {
323 * Prompts for a keypress
325 * The "prompt" should take the form "Command: "
327 * Returns TRUE unless the character is "Escape"
329 bool get_com(std::string_view prompt, char *command, bool z_escape)
333 if (get_com_no_macros) {
334 *command = (char)inkey_special(false);
340 if (*command == ESCAPE) {
343 if (z_escape && ((*command == 'z') || (*command == 'Z'))) {
351 * Request a "quantity" from the user
353 * Hack -- allow "command_arg" to specify a quantity
355 QUANTITY get_quantity(std::optional<std::string_view> prompt_opt, QUANTITY max)
358 * @todo QUANTITY、COMMAND_CODE、その他の型サイズがまちまちな変数とのやり取りが多数ある.
359 * この処理での数の入力を0からSHRT_MAXに制限することで不整合の発生を回避する.
361 max = std::clamp<QUANTITY>(max, 0, SHRT_MAX);
379 bool result = repeat_pull(&code);
380 amt = (QUANTITY)code;
381 if ((max != 1) && result) {
392 std::string_view prompt;
393 if (prompt_opt.has_value()) {
394 prompt = prompt_opt.value();
396 strnfmt(tmp, sizeof(tmp), _("いくつですか (1-%d): ", "Quantity (1-%d): "), max);
403 strnfmt(buf, sizeof(buf), "%d", amt);
407 * Don't allow to use numpad as cursor key.
409 res = askfor(buf, 6, false);
416 if (isalpha(buf[0])) {
419 amt = std::clamp<int>(atoi(buf), 0, max);
423 repeat_push((COMMAND_CODE)amt);
430 * Pause for user response
432 void pause_line(int row)
435 put_str(_("[ 何かキーを押して下さい ]", "[Press any key to continue]"), row, _(26, 23));
441 bool get_value(std::string_view prompt, int min, int max, int *value)
443 std::stringstream st;
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());
450 if (!get_string(st.str().data(), tmp_val, digit)) {
456 if (min <= val && max >= val) {
459 msg_format(_("%dから%dの間で指定して下さい。", "It must be between %d to %d."), min, max);