OSDN Git Service

[Refactor] clang-format適用
[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))
29         return;
30
31     auto_dump_printf(*fpp, _("\n# 自動マクロセーブ\n\n", "\n# Automatic macro dump\n\n"));
32
33     for (int i = 0; i < macro__num; i++) {
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  * @brief マクロのトリガーキーを取得する /
46  * Hack -- ask for a "trigger" (see below)
47  * @param buf キー表記を保管するバッファ
48  * @details
49  * <pre>
50  * Note the complex use of the "inkey()" function from "util.c".
51  *
52  * Note that both "flush()" calls are extremely important.
53  * </pre>
54  */
55 static void do_cmd_macro_aux(char *buf)
56 {
57     flush();
58     inkey_base = TRUE;
59     char i = inkey();
60     int n = 0;
61     while (i) {
62         buf[n++] = i;
63         inkey_base = TRUE;
64         inkey_scan = TRUE;
65         i = inkey();
66     }
67
68     buf[n] = '\0';
69     flush();
70     char tmp[1024];
71     ascii_to_text(tmp, buf);
72     term_addstr(-1, TERM_WHITE, tmp);
73 }
74
75 /*!
76  * @brief マクロのキー表記からアスキーコードを得てターミナルに表示する /
77  * Hack -- ask for a keymap "trigger" (see below)
78  * @param buf キー表記を取得するバッファ
79  * @details
80  * <pre>
81  * Note that both "flush()" calls are extremely important.  This may
82  * no longer be true, since "util.c" is much simpler now.
83  * </pre>
84  */
85 static void do_cmd_macro_aux_keymap(char *buf)
86 {
87     char tmp[1024];
88     flush();
89     buf[0] = inkey();
90     buf[1] = '\0';
91     ascii_to_text(tmp, buf);
92     term_addstr(-1, TERM_WHITE, tmp);
93     flush();
94 }
95
96 /*!
97  * @brief キーマップをprefファイルにダンプする /
98  * Hack -- append all keymaps to the given file
99  * @param fname ファイルネーム
100  * @return エラーコード
101  * @details
102  */
103 static errr keymap_dump(concptr fname)
104 {
105     FILE *auto_dump_stream;
106     static concptr mark = "Keymap Dump";
107     char key[1024];
108     char buf[1024];
109     BIT_FLAGS mode;
110     if (rogue_like_commands) {
111         mode = KEYMAP_MODE_ROGUE;
112     } else {
113         mode = KEYMAP_MODE_ORIG;
114     }
115
116     path_build(buf, sizeof(buf), ANGBAND_DIR_USER, fname);
117     if (!open_auto_dump(&auto_dump_stream, buf, mark))
118         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         concptr act;
123         act = keymap_act[mode][i];
124         if (!act)
125             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  * @brief マクロを設定するコマンドのメインルーチン /
141  * Interact with "macros"
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     FILE *auto_dump_stream;
154     BIT_FLAGS mode = rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG;
155     screen_save();
156     while (TRUE) {
157         term_clear();
158         prt(_("[ マクロの設定 ]", "Interact with Macros"), 2, 0);
159         prt(_("マクロ行動が(もしあれば)下に表示されます:", "Current action (if any) shown below:"), 20, 0);
160         ascii_to_text(buf, macro__buf);
161         prt(buf, 22, 0);
162
163         prt(_("(1) ユーザー設定ファイルのロード", "(1) Load a user pref file"), 4, 5);
164         prt(_("(2) ファイルにマクロを追加", "(2) Append macros to a file"), 5, 5);
165         prt(_("(3) マクロの確認", "(3) Query a macro"), 6, 5);
166         prt(_("(4) マクロの作成", "(4) Create a macro"), 7, 5);
167         prt(_("(5) マクロの削除", "(5) Remove a macro"), 8, 5);
168         prt(_("(6) ファイルにキー配置を追加", "(6) Append keymaps to a file"), 9, 5);
169         prt(_("(7) キー配置の確認", "(7) Query a keymap"), 10, 5);
170         prt(_("(8) キー配置の作成", "(8) Create a keymap"), 11, 5);
171         prt(_("(9) キー配置の削除", "(9) Remove a keymap"), 12, 5);
172         prt(_("(0) マクロ行動の入力", "(0) Enter a new action"), 13, 5);
173
174         prt(_("コマンド: ", "Command: "), 16, 0);
175         int i = inkey();
176         if (i == ESCAPE)
177             break;
178
179         else if (i == '1') {
180             prt(_("コマンド: ユーザー設定ファイルのロード", "Command: Load a user pref file"), 16, 0);
181             prt(_("ファイル: ", "File: "), 18, 0);
182             sprintf(tmp, "%s.prf", creature_ptr->base_name);
183             if (!askfor(tmp, 80))
184                 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         } else if (i == '2') {
194             prt(_("コマンド: マクロをファイルに追加する", "Command: Append macros to a file"), 16, 0);
195             prt(_("ファイル: ", "File: "), 18, 0);
196             sprintf(tmp, "%s.prf", creature_ptr->base_name);
197             if (!askfor(tmp, 80))
198                 continue;
199
200             macro_dump(&auto_dump_stream, tmp);
201             msg_print(_("マクロを追加しました。", "Appended macros."));
202         } else if (i == '3') {
203             prt(_("コマンド: マクロの確認", "Command: Query a macro"), 16, 0);
204             prt(_("トリガーキー: ", "Trigger: "), 18, 0);
205             do_cmd_macro_aux(buf);
206             int k = macro_find_exact(buf);
207             if (k < 0) {
208                 msg_print(_("そのキーにはマクロは定義されていません。", "Found no macro."));
209             } else {
210                 strcpy(macro__buf, macro__act[k]);
211                 ascii_to_text(buf, macro__buf);
212                 prt(buf, 22, 0);
213                 msg_print(_("マクロを確認しました。", "Found a macro."));
214             }
215         } else if (i == '4') {
216             prt(_("コマンド: マクロの作成", "Command: Create a macro"), 16, 0);
217             prt(_("トリガーキー: ", "Trigger: "), 18, 0);
218             do_cmd_macro_aux(buf);
219             clear_from(20);
220             c_prt(TERM_L_RED,
221                 _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
222                     "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."),
223                 22, 0);
224             prt(_("マクロ行動: ", "Action: "), 20, 0);
225             ascii_to_text(tmp, macro__buf);
226             if (askfor(tmp, 80)) {
227                 text_to_ascii(macro__buf, tmp);
228                 macro_add(buf, macro__buf);
229                 msg_print(_("マクロを追加しました。", "Added a macro."));
230             }
231         } else if (i == '5') {
232             prt(_("コマンド: マクロの削除", "Command: Remove a macro"), 16, 0);
233             prt(_("トリガーキー: ", "Trigger: "), 18, 0);
234             do_cmd_macro_aux(buf);
235             macro_add(buf, buf);
236             msg_print(_("マクロを削除しました。", "Removed a macro."));
237         } else if (i == '6') {
238             prt(_("コマンド: キー配置をファイルに追加する", "Command: Append keymaps to a file"), 16, 0);
239             prt(_("ファイル: ", "File: "), 18, 0);
240             sprintf(tmp, "%s.prf", creature_ptr->base_name);
241             if (!askfor(tmp, 80))
242                 continue;
243
244             (void)keymap_dump(tmp);
245             msg_print(_("キー配置を追加しました。", "Appended keymaps."));
246         } else if (i == '7') {
247             prt(_("コマンド: キー配置の確認", "Command: Query a keymap"), 16, 0);
248             prt(_("押すキー: ", "Keypress: "), 18, 0);
249             do_cmd_macro_aux_keymap(buf);
250             concptr act = keymap_act[mode][(byte)(buf[0])];
251             if (!act) {
252                 msg_print(_("キー配置は定義されていません。", "Found no keymap."));
253             } else {
254                 strcpy(macro__buf, act);
255                 ascii_to_text(buf, macro__buf);
256                 prt(buf, 22, 0);
257                 msg_print(_("キー配置を確認しました。", "Found a keymap."));
258             }
259         } else if (i == '8') {
260             prt(_("コマンド: キー配置の作成", "Command: Create a keymap"), 16, 0);
261             prt(_("押すキー: ", "Keypress: "), 18, 0);
262             do_cmd_macro_aux_keymap(buf);
263             clear_from(20);
264             c_prt(TERM_L_RED,
265                 _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
266                     "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."),
267                 22, 0);
268             prt(_("行動: ", "Action: "), 20, 0);
269             ascii_to_text(tmp, macro__buf);
270             if (askfor(tmp, 80)) {
271                 text_to_ascii(macro__buf, tmp);
272                 string_free(keymap_act[mode][(byte)(buf[0])]);
273                 keymap_act[mode][(byte)(buf[0])] = string_make(macro__buf);
274                 msg_print(_("キー配置を追加しました。", "Added a keymap."));
275             }
276         } else if (i == '9') {
277             prt(_("コマンド: キー配置の削除", "Command: Remove a keymap"), 16, 0);
278             prt(_("押すキー: ", "Keypress: "), 18, 0);
279             do_cmd_macro_aux_keymap(buf);
280             string_free(keymap_act[mode][(byte)(buf[0])]);
281             keymap_act[mode][(byte)(buf[0])] = NULL;
282             msg_print(_("キー配置を削除しました。", "Removed a keymap."));
283         } else if (i == '0') {
284             prt(_("コマンド: マクロ行動の入力", "Command: Enter a new action"), 16, 0);
285             clear_from(20);
286             c_prt(TERM_L_RED,
287                 _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
288                     "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."),
289                 22, 0);
290             prt(_("マクロ行動: ", "Action: "), 20, 0);
291             tmp[80] = '\0';
292             if (!askfor(buf, 80))
293                 continue;
294
295             text_to_ascii(macro__buf, buf);
296         } else {
297             bell();
298         }
299
300         msg_erase();
301     }
302
303     screen_load();
304 }