OSDN Git Service

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