OSDN Git Service

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