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"
28 * Get some string input at the cursor location.
29 * Assume the buffer is initialized to a default string.
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
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.
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.
44 bool askfor(char *buf, int len, bool numpad_cursor)
48 * TERM_YELLOW : Overwrite mode
49 * TERM_WHITE : Insert mode
51 auto color = TERM_YELLOW;
59 if ((x < 0) || (x >= MAIN_TERM_MIN_COLS)) {
63 if (x + len > MAIN_TERM_MIN_COLS) {
64 len = MAIN_TERM_MIN_COLS - x;
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);
84 auto next_pos = i + 1;
86 if (iskanji(buf[i])) {
90 if (next_pos >= pos) {
103 if ('\0' == buf[pos]) {
108 if (iskanji(buf[pos])) {
131 auto next_pos = i + 1;
133 if (iskanji(buf[i])) {
137 if (next_pos >= pos) {
150 if (buf[pos] == '\0') {
156 if (iskanji(buf[pos])) {
161 while ('\0' != (buf[dst++] = buf[src++])) {
169 if (skey & SKEY_MASK) {
173 const auto c = static_cast<char>(skey);
174 if (color == TERM_YELLOW) {
179 strcpy(tmp, buf + pos);
193 const auto is_print = _(isprint(c) || iskana(c), isprint(c));
194 if (pos < len && is_print) {
202 angband_strcat(buf, tmp, len + 1);
210 * Get a string from the user
212 * The "prompt" should take the form "Prompt: "
214 * Note that the initial contents of the string is used as
215 * the default response, so be sure to "clear" it if needed.
217 * We clear the input, and return FALSE, on "ESCAPE".
219 bool get_string(std::string_view prompt, char *buf, int len)
224 res = askfor(buf, len);
230 * Verify something with the user
232 * The "prompt" should take the form "Query? "
234 * Note that "[y/n]" is appended to the prompt.
236 bool get_check(std::string_view prompt)
238 return get_check_strict(p_ptr, prompt, 0);
242 * Verify something with the user strictly
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.
249 bool get_check_strict(PlayerType *player_ptr, std::string_view prompt, BIT_FLAGS mode)
251 if (!rogue_like_commands) {
252 mode &= ~CHECK_OKAY_CANCEL;
255 std::stringstream ss;
257 if (mode & CHECK_OKAY_CANCEL) {
258 ss << "[(O)k/(C)ancel]";
259 } else if (mode & CHECK_DEFAULT_Y) {
264 const auto buf = ss.str();
266 auto &rfu = RedrawingFlagsUpdater::get_instance();
268 rfu.set_flag(SubWindowRedrawingFlag::MESSAGE);
269 handle_stuff(player_ptr);
276 if (!(mode & CHECK_NO_HISTORY) && player_ptr->playing) {
278 rfu.set_flag(SubWindowRedrawingFlag::MESSAGE);
279 handle_stuff(player_ptr);
286 if (!(mode & CHECK_NO_ESCAPE)) {
293 if (mode & CHECK_OKAY_CANCEL) {
294 if (i == 'o' || i == 'O') {
297 } else if (i == 'c' || i == 'C') {
302 if (i == 'y' || i == 'Y') {
305 } else if (i == 'n' || i == 'N') {
311 if (mode & CHECK_DEFAULT_Y) {
324 * Prompts for a keypress
326 * The "prompt" should take the form "Command: "
328 * Returns TRUE unless the character is "Escape"
330 bool get_com(std::string_view prompt, char *command, bool z_escape)
334 if (get_com_no_macros) {
335 *command = (char)inkey_special(false);
341 if (*command == ESCAPE) {
344 if (z_escape && ((*command == 'z') || (*command == 'Z'))) {
352 * Request a "quantity" from the user
354 * Hack -- allow "command_arg" to specify a quantity
356 QUANTITY get_quantity(std::optional<std::string_view> prompt_opt, QUANTITY max)
359 * @todo QUANTITY、COMMAND_CODE、その他の型サイズがまちまちな変数とのやり取りが多数ある.
360 * この処理での数の入力を0からSHRT_MAXに制限することで不整合の発生を回避する.
362 max = std::clamp<QUANTITY>(max, 0, SHRT_MAX);
380 bool result = repeat_pull(&code);
381 amt = (QUANTITY)code;
382 if ((max != 1) && result) {
393 std::string_view prompt;
394 if (prompt_opt.has_value()) {
395 prompt = prompt_opt.value();
397 strnfmt(tmp, sizeof(tmp), _("いくつですか (1-%d): ", "Quantity (1-%d): "), max);
404 strnfmt(buf, sizeof(buf), "%d", amt);
408 * Don't allow to use numpad as cursor key.
410 res = askfor(buf, 6, false);
417 if (isalpha(buf[0])) {
420 amt = std::clamp<int>(atoi(buf), 0, max);
424 repeat_push((COMMAND_CODE)amt);
431 * Pause for user response
433 void pause_line(int row)
436 put_str(_("[ 何かキーを押して下さい ]", "[Press any key to continue]"), row, _(26, 23));
442 std::optional<int> input_value_int(std::string_view prompt, int min, int max, int initial_value)
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());
450 if (!get_string(ss.str().data(), tmp_val, digit)) {
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);
462 } catch (std::invalid_argument const &) {
463 msg_print(_("数値を入力して下さい。", "Please input numeric value."));
465 } catch (std::out_of_range const &) {
466 msg_print(_("入力可能な数値の範囲を超えています。", "Input value overflows the maximum number."));