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 "term/screen-processor.h"
12 #include "term/term-color-types.h"
13 #include "term/z-form.h"
14 #include "util/int-char-converter.h"
15 #include "util/string-processor.h"
16 #include "view/display-messages.h"
26 * Get some string input at the cursor location.
27 * Assume the buffer is initialized to a default string.
29 * The default buffer is in Overwrite mode and displayed in yellow at
30 * first. Normal chars clear the yellow text and append the char in
33 * LEFT (^B) and RIGHT (^F) movement keys move the cursor position.
34 * If the text is still displayed in yellow (Overwite mode), it will
35 * turns into white (Insert mode) when cursor moves.
37 * DELETE (^D) deletes a char at the cursor position.
38 * BACKSPACE (^H) deletes a char at the left of cursor position.
39 * ESCAPE clears the buffer and the window and returns FALSE.
40 * RETURN accepts the current buffer contents and returns TRUE.
42 bool askfor(char *buf, int len, bool numpad_cursor)
46 * TERM_YELLOW : Overwrite mode
47 * TERM_WHITE : Insert mode
49 auto color = TERM_YELLOW;
57 if ((x < 0) || (x >= 80)) {
68 term_erase(x, y, len);
69 term_putstr(x, y, -1, color, buf);
70 term_gotoxy(x + pos, y);
71 const auto skey = inkey_special(numpad_cursor);
82 auto next_pos = i + 1;
84 if (iskanji(buf[i])) {
88 if (next_pos >= pos) {
101 if ('\0' == buf[pos]) {
106 if (iskanji(buf[pos])) {
129 auto next_pos = i + 1;
131 if (iskanji(buf[i])) {
135 if (next_pos >= pos) {
148 if (buf[pos] == '\0') {
154 if (iskanji(buf[pos])) {
159 while ('\0' != (buf[dst++] = buf[src++])) {
167 if (skey & SKEY_MASK) {
171 const auto c = static_cast<char>(skey);
172 if (color == TERM_YELLOW) {
177 strcpy(tmp, buf + pos);
191 const auto is_print = _(isprint(c) || iskana(c), isprint(c));
192 if (pos < len && is_print) {
200 angband_strcat(buf, tmp, len + 1);
208 * Get a string from the user
210 * The "prompt" should take the form "Prompt: "
212 * Note that the initial contents of the string is used as
213 * the default response, so be sure to "clear" it if needed.
215 * We clear the input, and return FALSE, on "ESCAPE".
217 bool get_string(std::string_view prompt, char *buf, int len)
222 res = askfor(buf, len);
228 * Verify something with the user
230 * The "prompt" should take the form "Query? "
232 * Note that "[y/n]" is appended to the prompt.
234 bool get_check(std::string_view prompt)
236 return get_check_strict(p_ptr, prompt, 0);
240 * Verify something with the user strictly
242 * mode & CHECK_OKAY_CANCEL : force user to answer 'O'kay or 'C'ancel
243 * mode & CHECK_NO_ESCAPE : don't allow ESCAPE key
244 * mode & CHECK_NO_HISTORY : no message_add
245 * mode & CHECK_DEFAULT_Y : accept any key as y, except n and Esc.
247 bool get_check_strict(PlayerType *player_ptr, std::string_view prompt, BIT_FLAGS mode)
249 if (!rogue_like_commands) {
250 mode &= ~CHECK_OKAY_CANCEL;
253 std::stringstream ss;
255 if (mode & CHECK_OKAY_CANCEL) {
256 ss << "[(O)k/(C)ancel]";
257 } else if (mode & CHECK_DEFAULT_Y) {
262 const auto buf = ss.str();
265 player_ptr->window_flags |= PW_MESSAGE;
266 handle_stuff(player_ptr);
273 if (!(mode & CHECK_NO_HISTORY) && player_ptr->playing) {
275 player_ptr->window_flags |= (PW_MESSAGE);
276 handle_stuff(player_ptr);
283 if (!(mode & CHECK_NO_ESCAPE)) {
290 if (mode & CHECK_OKAY_CANCEL) {
291 if (i == 'o' || i == 'O') {
294 } else if (i == 'c' || i == 'C') {
299 if (i == 'y' || i == 'Y') {
302 } else if (i == 'n' || i == 'N') {
308 if (mode & CHECK_DEFAULT_Y) {
321 * Prompts for a keypress
323 * The "prompt" should take the form "Command: "
325 * Returns TRUE unless the character is "Escape"
327 bool get_com(std::string_view prompt, char *command, bool z_escape)
331 if (get_com_no_macros) {
332 *command = (char)inkey_special(false);
338 if (*command == ESCAPE) {
341 if (z_escape && ((*command == 'z') || (*command == 'Z'))) {
349 * Request a "quantity" from the user
351 * Hack -- allow "command_arg" to specify a quantity
353 QUANTITY get_quantity(std::optional<std::string_view> prompt_opt, QUANTITY max)
355 // FIXME : QUANTITY、COMMAND_CODE、その他の型サイズがまちまちな変数とのやり取りが多数ある。この処理での数の入力を0からSHRT_MAXに制限することで不整合の発生を回避する。
356 max = std::clamp<QUANTITY>(max, 0, SHRT_MAX);
374 bool result = repeat_pull(&code);
375 amt = (QUANTITY)code;
376 if ((max != 1) && result) {
387 std::string_view prompt;
388 if (prompt_opt.has_value()) {
389 prompt = prompt_opt.value();
391 strnfmt(tmp, sizeof(tmp), _("いくつですか (1-%d): ", "Quantity (1-%d): "), max);
398 strnfmt(buf, sizeof(buf), "%d", amt);
402 * Don't allow to use numpad as cursor key.
404 res = askfor(buf, 6, false);
411 if (isalpha(buf[0])) {
414 amt = std::clamp<int>(atoi(buf), 0, max);
418 repeat_push((COMMAND_CODE)amt);
425 * Pause for user response
427 void pause_line(int row)
430 put_str(_("[ 何かキーを押して下さい ]", "[Press any key to continue]"), row, _(26, 23));
436 bool get_value(std::string_view prompt, int min, int max, int *value)
438 std::stringstream st;
440 char tmp_val[12] = "";
441 /* std::to_chars() requires Mac OS X 10.15 or later. */
442 #if !defined(MACH_O_COCOA) || (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15)
443 std::to_chars(std::begin(tmp_val), std::end(tmp_val) - 1, *value);
445 snprintf(tmp_val, sizeof(tmp_val), "%d", *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);