Skip to content

Commit

Permalink
Merge pull request #4571 from sikabane-works/merge/heng#3498
Browse files Browse the repository at this point in the history
[Refactor] #3499 input_command() のシグネチャを変更した
  • Loading branch information
sikabane-works authored May 12, 2024
2 parents 3a5214f + 69ceb5b commit da39522
Show file tree
Hide file tree
Showing 28 changed files with 229 additions and 188 deletions.
41 changes: 25 additions & 16 deletions src/blue-magic/learnt-power-getter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ static std::optional<BlueMagicType> select_blue_magic_type_by_menu()
*/
static std::optional<BlueMagicType> select_blue_magic_kind_by_symbol()
{
auto candidate_desc = _("[A]ボルト, [B]ボール, [C]ブレス, [D]召喚, [E]その他:", "[A] bolt, [B] ball, [C] breath, [D] summoning, [E] others:");

constexpr auto candidate_desc = _("[A]ボルト, [B]ボール, [C]ブレス, [D]召喚, [E]その他:",
"[A] bolt, [B] ball, [C] breath, [D] summoning, [E] others:");
while (true) {
char ch;
if (!input_command(candidate_desc, &ch, true)) {
const auto command = input_command(candidate_desc, true);
if (!command.has_value()) {
return std::nullopt;
}

switch (ch) {
switch (command.value()) {
case 'A':
case 'a':
return BlueMagicType::BOLT;
Expand Down Expand Up @@ -185,7 +185,7 @@ static std::optional<std::vector<MonsterAbilityType>> sweep_learnt_spells(const
* @param blue_magics 青魔法のリスト(覚えていないものも含まれているが、カーソル移動時に選択をスキップする)
* @return 選択確定キーが入力された場合は true、そうでなければ false
*/
static bool switch_blue_magic_choice(char key, int &menu_line, const bluemage_data_type &bluemage_data, const std::vector<MonsterAbilityType> blue_magics)
static bool switch_blue_magic_choice(const char key, int &menu_line, const bluemage_data_type &bluemage_data, const std::vector<MonsterAbilityType> blue_magics)
{
const auto &learnt_blue_magics = bluemage_data.learnt_blue_magics;
const int blue_magics_count = blue_magics.size();
Expand Down Expand Up @@ -361,18 +361,22 @@ static bool confirm_cast_blue_magic(MonsterAbilityType spell)
*/
static std::optional<MonsterAbilityType> select_learnt_spells_by_symbol(PlayerType *player_ptr, const bluemage_data_type &bluemage_data, std::vector<MonsterAbilityType> spells)
{
char out_val[80];
(void)strnfmt(out_val, sizeof(out_val), _("(%c-%c, '*'で一覧, ESC) どの%sを唱えますか?", "(%c-%c, *=List, ESC=exit) Use which %s? "),
I2A(0), I2A(spells.size() - 1), _("魔法", "magic"));
constexpr auto fmt = _("(%c-%c, '*'で一覧, ESC) どの%sを唱えますか?", "(%c-%c, *=List, ESC=exit) Use which %s? ");
const auto prompt = format(fmt, I2A(0), I2A(spells.size() - 1), _("魔法", "magic"));

bool first_show_list = always_show_list;
auto show_list = false;
std::optional<MonsterAbilityType> selected_spell;

while (!selected_spell.has_value()) {
char choice = 0;
if (!first_show_list && !input_command(out_val, &choice, true)) {
break;
auto choice = '\0';
if (!first_show_list) {
const auto choice_opt = input_command(prompt, true);
if (!choice_opt.has_value()) {
break;
}

choice = choice_opt.value();
}

if (first_show_list || (choice == ' ') || (choice == '*') || (choice == '?')) {
Expand All @@ -385,6 +389,7 @@ static std::optional<MonsterAbilityType> select_learnt_spells_by_symbol(PlayerTy
} else {
screen_load();
}

continue;
}

Expand Down Expand Up @@ -418,8 +423,7 @@ static std::optional<MonsterAbilityType> select_learnt_spells_by_symbol(PlayerTy
*/
static std::optional<MonsterAbilityType> select_learnt_spells_by_menu(PlayerType *player_ptr, const bluemage_data_type &bluemage_data, std::vector<MonsterAbilityType> spells)
{
char out_val[80];
angband_strcpy(out_val, _("(ESC=中断) どの魔法を唱えますか?", "(ESC=exit) Use which magic? "), sizeof(out_val));
constexpr auto prompt = _("(ESC=中断) どの魔法を唱えますか?", "(ESC=exit) Use which magic? ");

auto it = std::find_if(
spells.begin(), spells.end(), [&bluemage_data](const auto &spell) { return bluemage_data.learnt_blue_magics.has(spell); });
Expand All @@ -431,8 +435,13 @@ static std::optional<MonsterAbilityType> select_learnt_spells_by_menu(PlayerType
while (!selected_spell.has_value()) {
describe_blue_magic_name(player_ptr, menu_line, bluemage_data, spells);

char choice;
if (!input_command(out_val, &choice, true) || choice == '0') {
const auto choice_opt = input_command(prompt, true);
if (!choice_opt.has_value()) {
break;
}

const auto choice = choice_opt.value();
if (choice == '0') {
break;
}

Expand Down
12 changes: 8 additions & 4 deletions src/cmd-action/cmd-hissatsu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ static int get_hissatsu_power(PlayerType *player_ptr, SPELL_IDX *sn)
POSITION x = 15;
PLAYER_LEVEL plev = player_ptr->lev;
char choice;
char out_val[160];
concptr p = _("必殺剣", "special attack");
COMMAND_CODE code;
magic_type spell;
Expand Down Expand Up @@ -100,7 +99,7 @@ static int get_hissatsu_power(PlayerType *player_ptr, SPELL_IDX *sn)
}

constexpr auto fmt = _("(%s^ %c-%c, '*'で一覧, ESC) どの%sを使いますか?", "(%s^s %c-%c, *=List, ESC=exit) Use which %s? ");
(void)strnfmt(out_val, 78, fmt, p, I2A(0), "abcdefghijklmnopqrstuvwxyz012345"[num - 1], p);
const auto prompt = format(fmt, p, I2A(0), "abcdefghijklmnopqrstuvwxyz012345"[num - 1], p);

if (use_menu) {
screen_save();
Expand All @@ -110,8 +109,13 @@ static int get_hissatsu_power(PlayerType *player_ptr, SPELL_IDX *sn)
while (!flag) {
if (choice == ESCAPE) {
choice = ' ';
} else if (!input_command(out_val, &choice, false)) {
break;
} else {
const auto new_choice = input_command(prompt);
if (!new_choice.has_value()) {
break;
}

choice = new_choice.value();
}

auto should_redraw_cursor = true;
Expand Down
14 changes: 9 additions & 5 deletions src/cmd-action/cmd-mane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ static int get_mane_power(PlayerType *player_ptr, int *sn, bool baigaesi)
PLAYER_LEVEL plev = player_ptr->lev;
PERCENTAGE chance = 0;
char choice;
char out_val[MAX_MONSTER_NAME];
concptr p = _("能力", "power");

monster_power spell;
Expand All @@ -158,15 +157,20 @@ static int get_mane_power(PlayerType *player_ptr, int *sn, bool baigaesi)
num = mane_data->mane_list.size();

/* Build a prompt (accept all spells) */
constexpr auto mes = _("(%c-%c, '*'で一覧, ESC) どの%sをまねますか?", "(%c-%c, *=List, ESC=exit) Use which %s? ");
(void)strnfmt(out_val, 78, mes, I2A(0), I2A(num - 1), p);
constexpr auto fmt = _("(%c-%c, '*'で一覧, ESC) どの%sをまねますか?", "(%c-%c, *=List, ESC=exit) Use which %s? ");
const auto prompt = format(fmt, I2A(0), I2A(num - 1), p);

choice = always_show_list ? ESCAPE : 1;
while (!flag) {
if (choice == ESCAPE) {
choice = ' ';
} else if (!input_command(out_val, &choice, true)) {
break;
} else {
const auto new_choice = input_command(prompt, true);
if (!new_choice.has_value()) {
break;
}

choice = new_choice.value();
}

/* Request redraw */
Expand Down
14 changes: 10 additions & 4 deletions src/cmd-action/cmd-pet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,9 @@ void do_cmd_pet(PlayerType *player_ptr)
screen_save();
prompt = _("(コマンド、ESC=終了) コマンドを選んでください:", "(Command, ESC=exit) Choose command from menu.");
} else {
prompt = format(_("(コマンド %c-%c、'*'=一覧、ESC=終了) コマンドを選んでください:", "(Command %c-%c, *=List, ESC=exit) Select a command: "),
I2A(0), I2A(num - 1));
constexpr auto fmt = _("(コマンド %c-%c、'*'=一覧、ESC=終了) コマンドを選んでください:",
"(Command %c-%c, *=List, ESC=exit) Select a command: ");
prompt = format(fmt, I2A(0), I2A(num - 1));
}

choice = (always_show_list || use_menu) ? ESCAPE : 1;
Expand All @@ -565,8 +566,13 @@ void do_cmd_pet(PlayerType *player_ptr)
while (!flag) {
if (choice == ESCAPE) {
choice = ' ';
} else if (!input_command(prompt.data(), &choice, true)) {
break;
} else {
const auto new_choice = input_command(prompt, true);
if (!new_choice.has_value()) {
break;
}

choice = new_choice.value();
}

auto should_redraw_cursor = true;
Expand Down
9 changes: 7 additions & 2 deletions src/cmd-action/cmd-racial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,13 @@ static bool racial_power_process_input(PlayerType *player_ptr, rc_type *rc_ptr)
while (true) {
if (rc_ptr->choice == ESCAPE) {
rc_ptr->choice = ' ';
} else if (!input_command(rc_ptr->out_val, &rc_ptr->choice, false)) {
return RC_CANCEL;
} else {
const auto choice = input_command(rc_ptr->out_val);
if (!choice.has_value()) {
return true;
}

rc_ptr->choice = choice.value();
}

if (racial_power_select_by_menu(player_ptr, rc_ptr) == RC_CANCEL) {
Expand Down
5 changes: 4 additions & 1 deletion src/cmd-action/cmd-spell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,12 @@ static int get_spell(PlayerType *player_ptr, SPELL_IDX *sn, std::string_view pro
if (choice == ESCAPE) {
choice = ' ';
} else {
if (!input_command(prompt, &choice, true)) {
const auto new_choice = input_command(prompt, true);
if (!new_choice.has_value()) {
break;
}

choice = new_choice.value();
}

auto should_redraw_cursor = true;
Expand Down
21 changes: 9 additions & 12 deletions src/cmd-io/cmd-floor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,29 @@ void do_cmd_locate(PlayerType *player_ptr)
{ { _("西", " west of"), _("真上", ""), _("", " east of") } },
{ { _("南西", " southwest of"), _("", " south of"), _("南東", " southeast of") } },
} };

DIRECTION dir;
POSITION y1, x1;
TERM_LEN wid, hgt;
get_screen_size(&wid, &hgt);
POSITION y2 = y1 = panel_row_min;
POSITION x2 = x1 = panel_col_min;
constexpr auto fmt = _("マップ位置 [%d(%02d),%d(%02d)] (プレイヤーの%s) 方向?", "Map sector [%d(%02d),%d(%02d)], which is%s your sector. Direction?");
while (true) {
std::string_view dirstring = dirstrings[(y2 < y1) ? 0 : ((y2 > y1) ? 2 : 1)][(x2 < x1) ? 0 : ((x2 > x1) ? 2 : 1)];
std::string out_val = format(fmt, y2 / (hgt / 2), y2 % (hgt / 2), x2 / (wid / 2), x2 % (wid / 2), dirstring.data());

dir = 0;
while (!dir) {
char command;
if (!input_command(out_val, &command, true)) {
const auto &dirstring = dirstrings[(y2 < y1) ? 0 : ((y2 > y1) ? 2 : 1)][(x2 < x1) ? 0 : ((x2 > x1) ? 2 : 1)];
const auto prompt = format(fmt, y2 / (hgt / 2), y2 % (hgt / 2), x2 / (wid / 2), x2 % (wid / 2), dirstring.data());
auto dir = 0;
while (dir == 0) {
const auto command = input_command(prompt, true);
if (!command.has_value()) {
break;
}

dir = get_keymap_dir(command);
if (!dir) {
dir = get_keymap_dir(command.value());
if (dir == 0) {
bell();
}
}

if (!dir) {
if (dir == 0) {
break;
}

Expand Down
12 changes: 6 additions & 6 deletions src/cmd-io/cmd-lore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
*/
void do_cmd_query_symbol(PlayerType *player_ptr)
{
char sym, query;

bool all = false;
bool uniq = false;
bool norm = false;
Expand All @@ -46,12 +44,14 @@ void do_cmd_query_symbol(PlayerType *player_ptr)

uint16_t why = 0;

if (!input_command(_("知りたい文字を入力して下さい(記号 or ^A全,^Uユ,^N非ユ,^R乗馬,^M名前): ",
"Enter character to be identified(^A:All,^U:Uniqs,^N:Non uniqs,^M:Name): "),
&sym, false)) {
constexpr auto prompt = _("知りたい文字を入力して下さい(記号 or ^A全,^Uユ,^N非ユ,^R乗馬,^M名前): ",
"Enter character to be identified(^A:All,^U:Uniqs,^N:Non uniqs,^M:Name): ");
const auto sym_opt = input_command(prompt);
if (!sym_opt.has_value()) {
return;
}

const auto sym = sym_opt.value();
int ident_i;
for (ident_i = 0; ident_info[ident_i]; ++ident_i) {
if (sym == ident_info[ident_i][0]) {
Expand Down Expand Up @@ -149,7 +149,7 @@ void do_cmd_query_symbol(PlayerType *player_ptr)
}

put_str(_("思い出を見ますか? (k:殺害順/y/n): ", "Recall details? (k/y/n): "), 0, _(36, 40));
query = inkey();
auto query = inkey();
prt(buf, 0, 0);
why = 2;
ang_sort(player_ptr, monraces.data(), &why, monraces.size(), ang_sort_comp_hook, ang_sort_swap_hook);
Expand Down
13 changes: 7 additions & 6 deletions src/cmd-item/cmd-magiceat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,9 @@ static std::optional<BaseitemKey> check_magic_eater_spell_repeat(magic_eater_dat
*/
static std::optional<BaseitemKey> select_magic_eater(PlayerType *player_ptr, bool only_browse)
{
char choice;
bool flag, request_list;
auto tval = ItemKindType::NONE;
int menu_line = (use_menu ? 1 : 0);
char out_val[160];

auto magic_eater_data = PlayerClass(player_ptr).get_specific_data<magic_eater_data_type>();

if (auto result = check_magic_eater_spell_repeat(magic_eater_data.get());
Expand Down Expand Up @@ -168,7 +165,7 @@ static std::optional<BaseitemKey> select_magic_eater(PlayerType *player_ptr, boo
prt(_("どの種類の魔法を使いますか?", "Which type of magic do you use?"), 0, 0);
}

choice = inkey();
const auto choice = inkey();
switch (choice) {
case ESCAPE:
case 'z':
Expand Down Expand Up @@ -204,10 +201,12 @@ static std::optional<BaseitemKey> select_magic_eater(PlayerType *player_ptr, boo
screen_load();
} else {
while (true) {
if (!input_command(_("[A] 杖, [B] 魔法棒, [C] ロッド:", "[A] staff, [B] wand, [C] rod:"), &choice, true)) {
const auto new_choice = input_command(_("[A] 杖, [B] 魔法棒, [C] ロッド:", "[A] staff, [B] wand, [C] rod:"), true);
if (!new_choice.has_value()) {
return std::nullopt;
}

const auto choice = new_choice.value();
if (choice == 'A' || choice == 'a') {
tval = ItemKindType::STAFF;
break;
Expand Down Expand Up @@ -355,10 +354,12 @@ static std::optional<BaseitemKey> select_magic_eater(PlayerType *player_ptr, boo
}
}

if (!input_command(out_val, &choice, false)) {
const auto choice_opt = input_command(prompt);
if (!choice_opt.has_value()) {
break;
}

const auto choice = choice_opt.value();
auto should_redraw_cursor = true;
if (use_menu && choice != ' ') {
switch (choice) {
Expand Down
17 changes: 8 additions & 9 deletions src/core/asking-player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,25 +342,24 @@ bool get_check_strict(PlayerType *player_ptr, std::string_view prompt, BIT_FLAGS
*
* Returns TRUE unless the character is "Escape"
*/
bool input_command(std::string_view prompt, char *command, bool z_escape)
std::optional<char> input_command(std::string_view prompt, bool z_escape)
{
msg_print(nullptr);
prt(prompt, 0, 0);
char command;
if (get_com_no_macros) {
*command = (char)inkey_special(false);
command = static_cast<char>(inkey_special(false));
} else {
*command = inkey();
command = inkey();
}

prt("", 0, 0);
if (*command == ESCAPE) {
return false;
}
if (z_escape && ((*command == 'z') || (*command == 'Z'))) {
return false;
const auto is_z = (command == 'z') || (command == 'Z');
if ((command == ESCAPE) || (z_escape && is_z)) {
return std::nullopt;
}

return true;
return command;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/core/asking-player.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bool askfor(char *buf, int len, bool numpad_cursor = true);
std::optional<std::string> input_string(std::string_view prompt, int len, std::string_view initial_value = "");
bool get_check(std::string_view prompt);
bool get_check_strict(PlayerType *player_ptr, std::string_view prompt, BIT_FLAGS mode);
bool input_command(std::string_view prompt, char *command, bool z_escape);
std::optional<char> input_command(std::string_view prompt, bool z_escape = false);
QUANTITY get_quantity(std::optional<std::string_view> prompt_opt, QUANTITY max);
void pause_line(int row);
std::optional<int> input_integer(std::string_view prompt, int min, int max, int initial_value = 0);
Expand Down
Loading

0 comments on commit da39522

Please sign in to comment.