OSDN Git Service

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