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 "term/z-form.h"
15 #include "util/angband-files.h"
16 #include "util/int-char-converter.h"
17 #include "util/string-processor.h"
18 #include "view/display-messages.h"
21 * @brief マクロ情報をprefファイルに保存する
22 * @param filename ファイル名
24 static void macro_dump(FILE **fpp, std::string_view filename)
26 constexpr auto mark = "Macro Dump";
27 const auto &path = path_build(ANGBAND_DIR_USER, filename);
28 if (!open_auto_dump(fpp, path, mark)) {
32 auto_dump_printf(*fpp, _("\n# 自動マクロセーブ\n\n", "\n# Automatic macro dump\n\n"));
34 for (auto i = 0; i < active_macros; i++) {
36 ascii_to_text(buf, macro_actions[i], sizeof(buf));
37 auto_dump_printf(*fpp, "A:%s\n", buf);
38 ascii_to_text(buf, macro_patterns[i], sizeof(buf));
39 auto_dump_printf(*fpp, "P:%s\n", buf);
40 auto_dump_printf(*fpp, "\n");
43 close_auto_dump(fpp, mark);
47 * @brief マクロのトリガーキーを取得する /
48 * Hack -- ask for a "trigger" (see below)
49 * @param buf キー表記を保管するバッファ
52 * Note the complex use of the "inkey()" function from "util.c".
54 * Note that both "flush()" calls are extremely important.
57 static void do_cmd_macro_aux(char *buf)
73 ascii_to_text(tmp, buf, sizeof(tmp));
74 term_addstr(-1, TERM_WHITE, tmp);
78 * @brief マクロのキー表記からアスキーコードを得てターミナルに表示する /
79 * Hack -- ask for a keymap "trigger" (see below)
80 * @param buf キー表記を取得するバッファ
83 * Note that both "flush()" calls are extremely important. This may
84 * no longer be true, since "util.c" is much simpler now.
87 static void do_cmd_macro_aux_keymap(char *buf)
93 ascii_to_text(tmp, buf, sizeof(tmp));
94 term_addstr(-1, TERM_WHITE, tmp);
99 * @brief キーマップをprefファイルにダンプする
100 * @param filename ファイルネーム
103 static errr keymap_dump(std::string_view filename)
105 FILE *auto_dump_stream;
108 if (rogue_like_commands) {
109 mode = KEYMAP_MODE_ROGUE;
111 mode = KEYMAP_MODE_ORIG;
114 const auto &path = path_build(ANGBAND_DIR_USER, filename);
115 constexpr auto mark = "Keymap Dump";
116 if (!open_auto_dump(&auto_dump_stream, path, mark)) {
120 auto_dump_printf(auto_dump_stream, _("\n# 自動キー配置セーブ\n\n", "\n# Automatic keymap dump\n\n"));
121 for (int i = 0; i < 256; i++) {
123 act = keymap_act[mode][i];
130 ascii_to_text(key, buf, sizeof(key));
131 ascii_to_text(buf, act, sizeof(buf));
132 auto_dump_printf(auto_dump_stream, "A:%s\n", buf);
133 auto_dump_printf(auto_dump_stream, "C:%d:%s\n", mode, key);
136 close_auto_dump(&auto_dump_stream, mark);
141 * @brief マクロを設定するコマンドのメインルーチン /
142 * Interact with "macros"
145 * Note that the macro "action" must be defined before the trigger.
147 * Could use some helpful instructions on this page.
150 void do_cmd_macros(PlayerType *player_ptr)
153 static char macro_buf[1024];
154 FILE *auto_dump_stream;
155 BIT_FLAGS mode = rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG;
159 auto print_macro_menu = [] {
160 prt(_("[ マクロの設定 ]", "Interact with Macros"), 2, 0);
161 prt(_("(1) ユーザー設定ファイルのロード", "(1) Load a user pref file"), 4, 5);
162 prt(_("(2) ファイルにマクロを追加", "(2) Append macros to a file"), 5, 5);
163 prt(_("(3) マクロの確認", "(3) Query a macro"), 6, 5);
164 prt(_("(4) マクロの作成", "(4) Create a macro"), 7, 5);
165 prt(_("(5) マクロの削除", "(5) Remove a macro"), 8, 5);
166 prt(_("(6) ファイルにキー配置を追加", "(6) Append keymaps to a file"), 9, 5);
167 prt(_("(7) キー配置の確認", "(7) Query a keymap"), 10, 5);
168 prt(_("(8) キー配置の作成", "(8) Create a keymap"), 11, 5);
169 prt(_("(9) キー配置の削除", "(9) Remove a keymap"), 12, 5);
170 prt(_("(0) マクロ行動の入力", "(0) Enter a new action"), 13, 5);
174 const auto initial_filename = format("%s.prf", player_ptr->base_name);
176 msg_print(_("コマンド: ", "Command: "));
177 const auto key = inkey();
186 prt(_("コマンド: ユーザー設定ファイルのロード", "Command: Load a user pref file"), 16, 0);
187 prt(_("ファイル: ", "File: "), 18, 0);
188 const auto ask_result = askfor(70, initial_filename);
193 const auto err = process_pref_file(player_ptr, *ask_result, true);
194 const auto *mes = ask_result->data();
196 msg_format(_("標準の設定ファイル'%s'を読み込みました。", "Loaded default '%s'."), mes);
198 msg_format(_("'%s'の読み込みに失敗しました!", "Failed to load '%s'!"), mes);
200 msg_format(_("'%s'を読み込みました。", "Loaded '%s'."), mes);
206 prt(_("コマンド: マクロをファイルに追加する", "Command: Append macros to a file"), 16, 0);
207 prt(_("ファイル: ", "File: "), 18, 0);
208 const auto ask_result = askfor(70, initial_filename);
213 macro_dump(&auto_dump_stream, *ask_result);
214 msg_print(_("マクロを追加しました。", "Appended macros."));
218 prt(_("コマンド: マクロの確認", "Command: Query a macro"), 16, 0);
219 prt(_("マクロ行動が(もしあれば)下に表示されます:", "Current action (if any) shown below:"), 20, 0);
220 prt(_("トリガーキー: ", "Trigger: "), 18, 0);
221 do_cmd_macro_aux(buf);
222 const auto k = macro_find_exact(buf);
224 msg_print(_("そのキーにはマクロは定義されていません。", "Found no macro."));
228 // マクロの作成時に参照するためmacro_bufにコピーする
229 angband_strcpy(macro_buf, macro_actions[k].data(), sizeof(macro_buf));
231 angband_strcpy(tmp, macro_buf, sizeof(tmp));
232 ascii_to_text(buf, tmp, sizeof(buf));
234 msg_print(_("マクロを確認しました。", "Found a macro."));
238 prt(_("コマンド: マクロの作成", "Command: Create a macro"), 16, 0);
239 prt(_("トリガーキー: ", "Trigger: "), 18, 0);
240 do_cmd_macro_aux(buf);
242 _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
243 "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."),
245 prt(_("マクロ行動: ", "Action: "), 20, 0);
246 // 最後に参照したマクロデータを元に作成する(コピーを行えるように)
247 macro_buf[80] = '\0';
249 ascii_to_text(tmp, macro_buf, sizeof(tmp));
250 const auto ask_result = askfor(80, tmp);
255 text_to_ascii(macro_buf, *ask_result, sizeof(macro_buf));
256 macro_add(buf, macro_buf);
257 msg_print(_("マクロを追加しました。", "Added a macro."));
261 prt(_("コマンド: マクロの削除", "Command: Remove a macro"), 16, 0);
262 prt(_("トリガーキー: ", "Trigger: "), 18, 0);
263 do_cmd_macro_aux(buf);
265 msg_print(_("マクロを削除しました。", "Removed a macro."));
268 prt(_("コマンド: キー配置をファイルに追加する", "Command: Append keymaps to a file"), 16, 0);
269 prt(_("ファイル: ", "File: "), 18, 0);
270 const auto ask_result = askfor(80, initial_filename);
275 (void)keymap_dump(*ask_result);
276 msg_print(_("キー配置を追加しました。", "Appended keymaps."));
280 prt(_("コマンド: キー配置の確認", "Command: Query a keymap"), 16, 0);
281 prt(_("マクロ行動が(もしあれば)下に表示されます:", "Current action (if any) shown below:"), 20, 0);
282 prt(_("押すキー: ", "Keypress: "), 18, 0);
283 do_cmd_macro_aux_keymap(buf);
284 concptr act = keymap_act[mode][(byte)(buf[0])];
286 msg_print(_("キー配置は定義されていません。", "Found no keymap."));
290 // マクロの作成時に参照するためmacro_bufにコピーする
291 angband_strcpy(macro_buf, act, sizeof(macro_buf));
292 // too long macro must die
294 angband_strcpy(tmp, macro_buf, sizeof(tmp));
295 ascii_to_text(buf, tmp, sizeof(buf));
297 msg_print(_("キー配置を確認しました。", "Found a keymap."));
301 prt(_("コマンド: キー配置の作成", "Command: Create a keymap"), 16, 0);
302 prt(_("押すキー: ", "Keypress: "), 18, 0);
303 do_cmd_macro_aux_keymap(buf);
305 _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
306 "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."),
308 prt(_("行動: ", "Action: "), 20, 0);
309 // 最後に参照したマクロデータを元に作成する(コピーを行えるように)
310 macro_buf[80] = '\0';
312 ascii_to_text(tmp, macro_buf, sizeof(tmp));
313 const auto ask_result = askfor(80, tmp);
318 text_to_ascii(macro_buf, *ask_result, sizeof(macro_buf));
319 string_free(keymap_act[mode][(byte)(buf[0])]);
320 keymap_act[mode][(byte)(buf[0])] = string_make(macro_buf);
321 msg_print(_("キー配置を追加しました。", "Added a keymap."));
325 prt(_("コマンド: キー配置の削除", "Command: Remove a keymap"), 16, 0);
326 prt(_("押すキー: ", "Keypress: "), 18, 0);
327 do_cmd_macro_aux_keymap(buf);
328 string_free(keymap_act[mode][(byte)(buf[0])]);
329 keymap_act[mode][(byte)(buf[0])] = nullptr;
330 msg_print(_("キー配置を削除しました。", "Removed a keymap."));
333 prt(_("コマンド: マクロ行動の入力", "Command: Enter a new action"), 16, 0);
335 _("カーソルキーの左右でカーソル位置を移動。BackspaceかDeleteで一文字削除。",
336 "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."),
338 prt(_("マクロ行動: ", "Action: "), 20, 0);
339 const auto ask_result = askfor(80);
344 text_to_ascii(macro_buf, *ask_result, sizeof(macro_buf));