OSDN Git Service

Merge pull request #424 from backwardsEric/english-help-defend
[hengbandforosx/hengbandosx.git] / src / autopick / autopick-command-menu.cpp
1 /*!
2  * todo 1関数100行以上ある、後で関数を分割すること
3  * @brief 自動拾いエディタのコマンドを受け付ける
4  * @date 2020/04/26
5  * @author Hourier
6  */
7
8 #include "autopick/autopick-command-menu.h"
9 #include "autopick/autopick-menu-data-table.h"
10 #include "autopick/autopick-util.h"
11 #include "io/input-key-acceptor.h"
12 #include "system/angband.h"
13 #include "term/screen-processor.h"
14 #include "term/term-color-types.h"
15 #include "util/int-char-converter.h"
16
17 /*!
18  * @brief 自動拾いエディタの画面を再描画する
19  * @param redraw 再描画が必要ならTRUE
20  * @param level command_menu_type 構造体におけるメニュー (詳細不明)
21  * @param start
22  * @param linestr
23  * @param menu_key 自動拾いエディタのメニューで入力したキー
24  * @param max_len
25  * @return なし
26  */
27 static void redraw_edit_command_menu(bool *redraw, int level, int start, char *linestr, byte *menu_key, int max_len)
28 {
29         if (!*redraw) return;
30
31         int col0 = 5 + level * 7;
32         int row0 = 1 + level * 3;
33         int row1 = row0 + 1;
34         term_putstr(col0, row0, -1, TERM_WHITE, linestr);
35
36         *menu_key = 0;
37         for (int i = start; menu_data[i].level >= level; i++)
38         {
39                 char com_key_str[3];
40                 concptr str;
41                 if (menu_data[i].level > level) continue;
42
43                 if (menu_data[i].com_id == -1)
44                 {
45                         strcpy(com_key_str, _("▼", ">"));
46                 }
47                 else if (menu_data[i].key != -1)
48                 {
49                         com_key_str[0] = '^';
50                         com_key_str[1] = menu_data[i].key + '@';
51                         com_key_str[2] = '\0';
52                 }
53                 else
54                 {
55                         com_key_str[0] = '\0';
56                 }
57
58                 str = format("| %c) %-*s %2s | ", *menu_key + 'a', max_len, menu_data[i].name, com_key_str);
59
60                 term_putstr(col0, row1++, -1, TERM_WHITE, str);
61
62                 (*menu_key)++;
63         }
64
65         term_putstr(col0, row1, -1, TERM_WHITE, linestr);
66         *redraw = FALSE;
67 }
68
69
70 /*
71  * Display the menu, and get a command
72  */
73 int do_command_menu(int level, int start)
74 {
75         int max_len = 0;
76         int menu_id_list[26];
77         byte menu_key = 0;
78         for (int i = start; menu_data[i].level >= level; i++)
79         {
80                 /* Ignore lower level sub menus */
81                 if (menu_data[i].level > level) continue;
82
83                 int len = strlen(menu_data[i].name);
84                 if (len > max_len) max_len = len;
85
86                 menu_id_list[menu_key] = i;
87                 menu_key++;
88         }
89
90         while (menu_key < 26)
91         {
92                 menu_id_list[menu_key] = -1;
93                 menu_key++;
94         }
95
96         int max_menu_wid = max_len + 3 + 3;
97
98         char linestr[MAX_LINELEN];
99         linestr[0] = '\0';
100         strcat(linestr, "+");
101         for (int i = 0; i < max_menu_wid + 2; i++)
102         {
103                 strcat(linestr, "-");
104         }
105
106         strcat(linestr, "+");
107         bool redraw = TRUE;
108         while (TRUE)
109         {
110                 redraw_edit_command_menu(&redraw, level, start, linestr, &menu_key, max_len);
111                 prt(format(_("(a-%c) コマンド:", "(a-%c) Command:"), menu_key + 'a' - 1), 0, 0);
112                 char key = inkey();
113                 if (key == ESCAPE) return 0;
114
115                 int com_id;
116                 bool is_alphabet = key >= 'a' && key <= 'z';
117                 if (!is_alphabet)
118                 {
119                         com_id = get_com_id(key);
120                         if (com_id)
121                         {
122                                 return com_id;
123                         }
124
125                         continue;
126                 }
127
128                 int menu_id = menu_id_list[key - 'a'];
129                 if (menu_id < 0) continue;
130
131                 com_id = menu_data[menu_id].com_id;
132                 if (com_id == -1)
133                 {
134                         com_id = do_command_menu(level + 1, menu_id + 1);
135                         if (com_id) return com_id;
136                         else redraw = TRUE;
137                 }
138                 else if (com_id)
139                 {
140                         return com_id;
141                 }
142         }
143 }