OSDN Git Service

macOS: fix mistakes in merge
[hengbandforosx/hengbandosx.git] / src / core / asking-player.cpp
index 2296438..ef86af8 100644 (file)
@@ -8,18 +8,20 @@
 #include "io/input-key-requester.h" //!< @todo 相互依存している、後で何とかする.
 #include "main/sound-of-music.h"
 #include "system/player-type-definition.h"
+#include "system/redrawing-flags-updater.h"
+#include "term/gameterm.h"
 #include "term/screen-processor.h"
 #include "term/term-color-types.h"
 #include "term/z-form.h"
 #include "util/int-char-converter.h"
 #include "util/string-processor.h"
 #include "view/display-messages.h"
-
 #include <algorithm>
 #include <charconv>
 #include <climits>
 #include <iostream>
 #include <sstream>
+#include <stdexcept>
 #include <string>
 
 /*
@@ -54,12 +56,12 @@ bool askfor(char *buf, int len, bool numpad_cursor)
         len = 1;
     }
 
-    if ((x < 0) || (x >= 80)) {
+    if ((x < 0) || (x >= MAIN_TERM_MIN_COLS)) {
         x = 0;
     }
 
-    if (x + len > 80) {
-        len = 80 - x;
+    if (x + len > MAIN_TERM_MIN_COLS) {
+        len = MAIN_TERM_MIN_COLS - x;
     }
 
     buf[len] = '\0';
@@ -261,8 +263,9 @@ bool get_check_strict(PlayerType *player_ptr, std::string_view prompt, BIT_FLAGS
     }
     const auto buf = ss.str();
 
+    auto &rfu = RedrawingFlagsUpdater::get_instance();
     if (auto_more) {
-        player_ptr->window_flags |= PW_MESSAGE;
+        rfu.set_flag(SubWindowRedrawingFlag::MESSAGE);
         handle_stuff(player_ptr);
         num_more = 0;
     }
@@ -272,7 +275,7 @@ bool get_check_strict(PlayerType *player_ptr, std::string_view prompt, BIT_FLAGS
     prt(buf, 0, 0);
     if (!(mode & CHECK_NO_HISTORY) && player_ptr->playing) {
         message_add(buf);
-        player_ptr->window_flags |= (PW_MESSAGE);
+        rfu.set_flag(SubWindowRedrawingFlag::MESSAGE);
         handle_stuff(player_ptr);
     }
 
@@ -352,7 +355,10 @@ bool get_com(std::string_view prompt, char *command, bool z_escape)
  */
 QUANTITY get_quantity(std::optional<std::string_view> prompt_opt, QUANTITY max)
 {
-    // FIXME : QUANTITY、COMMAND_CODE、その他の型サイズがまちまちな変数とのやり取りが多数ある。この処理での数の入力を0からSHRT_MAXに制限することで不整合の発生を回避する。
+    /*!
+     * @todo QUANTITY、COMMAND_CODE、その他の型サイズがまちまちな変数とのやり取りが多数ある.
+     * この処理での数の入力を0からSHRT_MAXに制限することで不整合の発生を回避する.
+     */
     max = std::clamp<QUANTITY>(max, 0, SHRT_MAX);
 
     bool res;
@@ -433,26 +439,32 @@ void pause_line(int row)
     prt("", row, 0);
 }
 
-bool get_value(std::string_view prompt, int min, int max, int *value)
+std::optional<int> input_value_int(std::string_view prompt, int min, int max, int initial_value)
 {
-    std::stringstream st;
-    int val;
+    std::stringstream ss;
     char tmp_val[12] = "";
-    std::to_chars(std::begin(tmp_val), std::end(tmp_val) - 1, *value);
-    st << prompt << "(" << min << "-" << max << "): ";
-    int digit = std::max(std::to_string(min).length(), std::to_string(max).length());
+    std::to_chars(std::begin(tmp_val), std::end(tmp_val) - 1, initial_value);
+    ss << prompt << "(" << min << "-" << max << "): ";
+    auto digit = std::max(std::to_string(min).length(), std::to_string(max).length());
     while (true) {
-        if (!get_string(st.str().data(), tmp_val, digit)) {
-            return false;
+        if (!get_string(ss.str().data(), tmp_val, digit)) {
+            return std::nullopt;
         }
 
-        val = atoi(tmp_val);
+        try {
+            auto val = std::stoi(tmp_val);
+            if ((val < min) || (val > max)) {
+                msg_format(_("%dから%dの間で指定して下さい。", "It must be between %d to %d."), min, max);
+                continue;
+            }
 
-        if (min <= val && max >= val) {
-            break;
+            return val;
+        } catch (std::invalid_argument const &) {
+            msg_print(_("数値を入力して下さい。", "Please input numeric value."));
+            continue;
+        } catch (std::out_of_range const &) {
+            msg_print(_("入力可能な数値の範囲を超えています。", "Input value overflows the maximum number."));
+            continue;
         }
-        msg_format(_("%dから%dの間で指定して下さい。", "It must be between %d to %d."), min, max);
     }
-    *value = val;
-    return true;
 }