OSDN Git Service

[Refactor] #39964 Removed the dependency from read-pref-file to autopick-pref-processor
[hengband/hengband.git] / src / cmd / cmd-macro.c
1 #include "angband.h"
2 #include "cmd-macro.h"
3 #include "cmd-gameoption.h"
4 #include "files.h"
5 #include "io/read-pref-file.h"
6 #include "gameterm.h"
7
8 /*!
9  * @brief マクロ情報をprefファイルに保存する /
10  * @param fname ファイル名
11  * @return なし
12  */
13 static void macro_dump(FILE **fpp, concptr fname)
14 {
15         static concptr mark = "Macro Dump";
16         char buf[1024];
17         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, fname);
18         FILE_TYPE(FILE_TYPE_TEXT);
19         if (!open_auto_dump(fpp, buf, mark)) return;
20
21         auto_dump_printf(*fpp, _("\n# 自動マクロセーブ\n\n", "\n# Automatic macro dump\n\n"));
22
23         for (int i = 0; i < macro__num; i++)
24         {
25                 ascii_to_text(buf, macro__act[i]);
26                 auto_dump_printf(*fpp, "A:%s\n", buf);
27                 ascii_to_text(buf, macro__pat[i]);
28                 auto_dump_printf(*fpp, "P:%s\n", buf);
29                 auto_dump_printf(*fpp, "\n");
30         }
31
32         close_auto_dump(fpp, mark);
33 }
34
35
36 /*!
37  * @brief マクロのトリガーキーを取得する /
38  * Hack -- ask for a "trigger" (see below)
39  * @param buf キー表記を保管するバッファ
40  * @return なし
41  * @details
42  * <pre>
43  * Note the complex use of the "inkey()" function from "util.c".
44  *
45  * Note that both "flush()" calls are extremely important.
46  * </pre>
47  */
48 static void do_cmd_macro_aux(char *buf)
49 {
50         flush();
51         inkey_base = TRUE;
52         char i = inkey();
53         int n = 0;
54         while (i)
55         {
56                 buf[n++] = i;
57                 inkey_base = TRUE;
58                 inkey_scan = TRUE;
59                 i = inkey();
60         }
61
62         buf[n] = '\0';
63         flush();
64         char tmp[1024];
65         ascii_to_text(tmp, buf);
66         Term_addstr(-1, TERM_WHITE, tmp);
67 }
68
69
70 /*!
71  * @brief マクロのキー表記からアスキーコードを得てターミナルに表示する /
72  * Hack -- ask for a keymap "trigger" (see below)
73  * @param buf キー表記を取得するバッファ
74  * @return なし
75  * @details
76  * <pre>
77  * Note that both "flush()" calls are extremely important.  This may
78  * no longer be true, since "util.c" is much simpler now.
79  * </pre>
80  */
81 static void do_cmd_macro_aux_keymap(char *buf)
82 {
83         char tmp[1024];
84         flush();
85         buf[0] = inkey();
86         buf[1] = '\0';
87         ascii_to_text(tmp, buf);
88         Term_addstr(-1, TERM_WHITE, tmp);
89         flush();
90 }
91
92
93 /*!
94  * @brief キーマップをprefファイルにダンプする /
95  * Hack -- append all keymaps to the given file
96  * @param fname ファイルネーム
97  * @return エラーコード
98  * @details
99  */
100 static errr keymap_dump(concptr fname)
101 {
102         FILE *auto_dump_stream;
103         static concptr mark = "Keymap Dump";
104         char key[1024];
105         char buf[1024];
106         BIT_FLAGS mode;
107         if (rogue_like_commands)
108         {
109                 mode = KEYMAP_MODE_ROGUE;
110         }
111         else
112         {
113                 mode = KEYMAP_MODE_ORIG;
114         }
115
116         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, fname);
117         FILE_TYPE(FILE_TYPE_TEXT);
118         if (!open_auto_dump(&auto_dump_stream, buf, mark)) return -1;
119
120         auto_dump_printf(auto_dump_stream, _("\n# 自動キー配置セーブ\n\n", "\n# Automatic keymap dump\n\n"));
121         for (int i = 0; i < 256; i++)
122         {
123                 concptr act;
124                 act = keymap_act[mode][i];
125                 if (!act) continue;
126
127                 buf[0] = (char)i;
128                 buf[1] = '\0';
129                 ascii_to_text(key, buf);
130                 ascii_to_text(buf, act);
131                 auto_dump_printf(auto_dump_stream, "A:%s\n", buf);
132                 auto_dump_printf(auto_dump_stream, "C:%d:%s\n", mode, key);
133         }
134
135         close_auto_dump(&auto_dump_stream, mark);
136         return 0;
137 }
138
139
140 /*!
141  * @brief マクロを設定するコマンドのメインルーチン /
142  * Interact with "macros"
143  * @return なし
144  * @details
145  * <pre>
146  * Note that the macro "action" must be defined before the trigger.
147  *
148  * Could use some helpful instructions on this page.
149  * </pre>
150  */
151 void do_cmd_macros(player_type *creature_ptr, void(*process_autopick_file_command)(char*))
152 {
153         char tmp[1024];
154         char buf[1024];
155         FILE *auto_dump_stream;
156         BIT_FLAGS mode = rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG;
157         FILE_TYPE(FILE_TYPE_TEXT);
158         screen_save();
159         while (TRUE)
160         {
161                 Term_clear();
162                 prt(_("[ マクロの設定 ]", "Interact with Macros"), 2, 0);
163                 prt(_("マクロ行動が(もしあれば)下に表示されます:", "Current action (if any) shown below:"), 20, 0);
164                 ascii_to_text(buf, macro__buf);
165                 prt(buf, 22, 0);
166
167                 prt(_("(1) ユーザー設定ファイルのロード", "(1) Load a user pref file"), 4, 5);
168                 prt(_("(2) ファイルにマクロを追加", "(2) Append macros to a file"), 5, 5);
169                 prt(_("(3) マクロの確認", "(3) Query a macro"), 6, 5);
170                 prt(_("(4) マクロの作成", "(4) Create a macro"), 7, 5);
171                 prt(_("(5) マクロの削除", "(5) Remove a macro"), 8, 5);
172                 prt(_("(6) ファイルにキー配置を追加", "(6) Append keymaps to a file"), 9, 5);
173                 prt(_("(7) キー配置の確認", "(7) Query a keymap"), 10, 5);
174                 prt(_("(8) キー配置の作成", "(8) Create a keymap"), 11, 5);
175                 prt(_("(9) キー配置の削除", "(9) Remove a keymap"), 12, 5);
176                 prt(_("(0) マクロ行動の入力", "(0) Enter a new action"), 13, 5);
177
178                 prt(_("コマンド: ", "Command: "), 16, 0);
179                 int i = inkey();
180                 if (i == ESCAPE) break;
181
182                 else if (i == '1')
183                 {
184                         prt(_("コマンド: ユーザー設定ファイルのロード", "Command: Load a user pref file"), 16, 0);
185                         prt(_("ファイル: ", "File: "), 18, 0);
186                         sprintf(tmp, "%s.prf", creature_ptr->base_name);
187                         if (!askfor(tmp, 80)) continue;
188
189                         errr err = process_pref_file(creature_ptr, tmp, process_autopick_file_command);
190                         if (-2 == err)
191                                 msg_format(_("標準の設定ファイル'%s'を読み込みました。", "Loaded default '%s'."), tmp);
192                         else if (err)
193                                 msg_format(_("'%s'の読み込みに失敗しました!", "Failed to load '%s'!"), tmp);
194                         else
195                                 msg_format(_("'%s'を読み込みました。", "Loaded '%s'."), tmp);
196                 }
197                 else if (i == '2')
198                 {
199                         prt(_("コマンド: マクロをファイルに追加する", "Command: Append macros to a file"), 16, 0);
200                         prt(_("ファイル: ", "File: "), 18, 0);
201                         sprintf(tmp, "%s.prf", creature_ptr->base_name);
202                         if (!askfor(tmp, 80)) continue;
203
204                         macro_dump(&auto_dump_stream, tmp);
205                         msg_print(_("マクロを追加しました。", "Appended macros."));
206                 }
207                 else if (i == '3')
208                 {
209                         prt(_("コマンド: マクロの確認", "Command: Query a macro"), 16, 0);
210                         prt(_("トリガーキー: ", "Trigger: "), 18, 0);
211                         do_cmd_macro_aux(buf);
212                         int k = macro_find_exact(buf);
213                         if (k < 0)
214                         {
215                                 msg_print(_("そのキーにはマクロは定義されていません。", "Found no macro."));
216                         }
217                         else
218                         {
219                                 strcpy(macro__buf, macro__act[k]);
220                                 ascii_to_text(buf, macro__buf);
221                                 prt(buf, 22, 0);
222                                 msg_print(_("マクロを確認しました。", "Found a macro."));
223                         }
224                 }
225                 else if (i == '4')
226                 {
227                         prt(_("コマンド: マクロの作成", "Command: Create a macro"), 16, 0);
228                         prt(_("トリガーキー: ", "Trigger: "), 18, 0);
229                         do_cmd_macro_aux(buf);
230                         clear_from(20);
231                         c_prt(TERM_L_RED, _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
232                                 "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."), 22, 0);
233                         prt(_("マクロ行動: ", "Action: "), 20, 0);
234                         ascii_to_text(tmp, macro__buf);
235                         if (askfor(tmp, 80))
236                         {
237                                 text_to_ascii(macro__buf, tmp);
238                                 macro_add(buf, macro__buf);
239                                 msg_print(_("マクロを追加しました。", "Added a macro."));
240                         }
241                 }
242                 else if (i == '5')
243                 {
244                         prt(_("コマンド: マクロの削除", "Command: Remove a macro"), 16, 0);
245                         prt(_("トリガーキー: ", "Trigger: "), 18, 0);
246                         do_cmd_macro_aux(buf);
247                         macro_add(buf, buf);
248                         msg_print(_("マクロを削除しました。", "Removed a macro."));
249                 }
250                 else if (i == '6')
251                 {
252                         prt(_("コマンド: キー配置をファイルに追加する", "Command: Append keymaps to a file"), 16, 0);
253                         prt(_("ファイル: ", "File: "), 18, 0);
254                         sprintf(tmp, "%s.prf", creature_ptr->base_name);
255                         if (!askfor(tmp, 80)) continue;
256
257                         (void)keymap_dump(tmp);
258                         msg_print(_("キー配置を追加しました。", "Appended keymaps."));
259                 }
260                 else if (i == '7')
261                 {
262                         prt(_("コマンド: キー配置の確認", "Command: Query a keymap"), 16, 0);
263                         prt(_("押すキー: ", "Keypress: "), 18, 0);
264                         do_cmd_macro_aux_keymap(buf);
265                         concptr act = keymap_act[mode][(byte)(buf[0])];
266                         if (!act)
267                         {
268                                 msg_print(_("キー配置は定義されていません。", "Found no keymap."));
269                         }
270                         else
271                         {
272                                 strcpy(macro__buf, act);
273                                 ascii_to_text(buf, macro__buf);
274                                 prt(buf, 22, 0);
275                                 msg_print(_("キー配置を確認しました。", "Found a keymap."));
276                         }
277                 }
278                 else if (i == '8')
279                 {
280                         prt(_("コマンド: キー配置の作成", "Command: Create a keymap"), 16, 0);
281                         prt(_("押すキー: ", "Keypress: "), 18, 0);
282                         do_cmd_macro_aux_keymap(buf);
283                         clear_from(20);
284                         c_prt(TERM_L_RED, _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
285                                 "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."), 22, 0);
286                         prt(_("行動: ", "Action: "), 20, 0);
287                         ascii_to_text(tmp, macro__buf);
288                         if (askfor(tmp, 80))
289                         {
290                                 text_to_ascii(macro__buf, tmp);
291                                 string_free(keymap_act[mode][(byte)(buf[0])]);
292                                 keymap_act[mode][(byte)(buf[0])] = string_make(macro__buf);
293                                 msg_print(_("キー配置を追加しました。", "Added a keymap."));
294                         }
295                 }
296                 else if (i == '9')
297                 {
298                         prt(_("コマンド: キー配置の削除", "Command: Remove a keymap"), 16, 0);
299                         prt(_("押すキー: ", "Keypress: "), 18, 0);
300                         do_cmd_macro_aux_keymap(buf);
301                         string_free(keymap_act[mode][(byte)(buf[0])]);
302                         keymap_act[mode][(byte)(buf[0])] = NULL;
303                         msg_print(_("キー配置を削除しました。", "Removed a keymap."));
304                 }
305                 else if (i == '0')
306                 {
307                         prt(_("コマンド: マクロ行動の入力", "Command: Enter a new action"), 16, 0);
308                         clear_from(20);
309                         c_prt(TERM_L_RED, _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
310                                 "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."), 22, 0);
311                         prt(_("マクロ行動: ", "Action: "), 20, 0);
312                         tmp[80] = '\0';
313                         if (!askfor(buf, 80)) continue;
314
315                         text_to_ascii(macro__buf, buf);
316                 }
317                 else
318                 {
319                         bell();
320                 }
321
322                 msg_erase();
323         }
324
325         screen_load();
326 }