OSDN Git Service

Merge pull request #982 from backwardsEric/nopch-target-setter
[hengbandforosx/hengbandosx.git] / src / autopick / autopick-command-menu.cpp
1 /*!
2  * @brief 自動拾いエディタのコマンドを受け付ける
3  * @date 2020/04/26
4  * @author Hourier
5  * @todo 1関数100行以上ある、後で関数を分割すること
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  */
26 static void redraw_edit_command_menu(bool *redraw, int level, int start, char *linestr, byte *menu_key, int max_len)
27 {
28         if (!*redraw) return;
29
30         int col0 = 5 + level * 7;
31         int row0 = 1 + level * 3;
32         int row1 = row0 + 1;
33         term_putstr(col0, row0, -1, TERM_WHITE, linestr);
34
35         *menu_key = 0;
36         for (int i = start; menu_data[i].level >= level; i++)
37         {
38                 char com_key_str[3];
39                 concptr str;
40                 if (menu_data[i].level > level) continue;
41
42                 if (menu_data[i].com_id == -1)
43                 {
44                         strcpy(com_key_str, _("▼", ">"));
45                 }
46                 else if (menu_data[i].key != -1)
47                 {
48                         com_key_str[0] = '^';
49                         com_key_str[1] = menu_data[i].key + '@';
50                         com_key_str[2] = '\0';
51                 }
52                 else
53                 {
54                         com_key_str[0] = '\0';
55                 }
56
57                 str = format("| %c) %-*s %2s | ", *menu_key + 'a', max_len, menu_data[i].name, com_key_str);
58
59                 term_putstr(col0, row1++, -1, TERM_WHITE, str);
60
61                 (*menu_key)++;
62         }
63
64         term_putstr(col0, row1, -1, TERM_WHITE, linestr);
65         *redraw = FALSE;
66 }
67
68
69 /*
70  * Display the menu, and get a command
71  */
72 int do_command_menu(int level, int start)
73 {
74         int max_len = 0;
75         int menu_id_list[26];
76         byte menu_key = 0;
77         for (int i = start; menu_data[i].level >= level; i++)
78         {
79                 /* Ignore lower level sub menus */
80                 if (menu_data[i].level > level) continue;
81
82                 int len = strlen(menu_data[i].name);
83                 if (len > max_len) max_len = len;
84
85                 menu_id_list[menu_key] = i;
86                 menu_key++;
87         }
88
89         while (menu_key < 26)
90         {
91                 menu_id_list[menu_key] = -1;
92                 menu_key++;
93         }
94
95         int max_menu_wid = max_len + 3 + 3;
96
97         char linestr[MAX_LINELEN];
98         linestr[0] = '\0';
99         strcat(linestr, "+");
100         for (int i = 0; i < max_menu_wid + 2; i++)
101         {
102                 strcat(linestr, "-");
103         }
104
105         strcat(linestr, "+");
106         bool redraw = TRUE;
107         while (TRUE)
108         {
109                 redraw_edit_command_menu(&redraw, level, start, linestr, &menu_key, max_len);
110                 prt(format(_("(a-%c) コマンド:", "(a-%c) Command:"), menu_key + 'a' - 1), 0, 0);
111                 char key = inkey();
112                 if (key == ESCAPE) return 0;
113
114                 int com_id;
115                 bool is_alphabet = key >= 'a' && key <= 'z';
116                 if (!is_alphabet)
117                 {
118                         com_id = get_com_id(key);
119                         if (com_id)
120                         {
121                                 return com_id;
122                         }
123
124                         continue;
125                 }
126
127                 int menu_id = menu_id_list[key - 'a'];
128                 if (menu_id < 0) continue;
129
130                 com_id = menu_data[menu_id].com_id;
131                 if (com_id == -1)
132                 {
133                         com_id = do_command_menu(level + 1, menu_id + 1);
134                         if (com_id) return com_id;
135                         else redraw = TRUE;
136                 }
137                 else if (com_id)
138                 {
139                         return com_id;
140                 }
141         }
142 }