7 #include "cmd-io/cmd-process-screen.h"
8 #include "cmd-visual/cmd-draw.h"
9 #include "core/asking-player.h"
10 #include "core/player-redraw-types.h"
11 #include "core/stuff-handler.h"
12 #include "core/visuals-reseter.h"
13 #include "game-option/special-options.h"
14 #include "io/files-util.h"
15 #include "io/input-key-acceptor.h"
16 #include "system/player-type-definition.h"
17 #include "term/gameterm.h"
18 #include "term/screen-processor.h"
19 #include "term/term-color-types.h"
20 #include "util/angband-files.h"
21 #include "view/display-messages.h"
23 // Encode the screen colors
24 static char hack[17] = "dwsorgbuDWvyRGBU";
26 static concptr tags[4] = { "HEADER_START:", "HEADER_END:", "FOOTER_START:", "FOOTER_END:", };
27 static concptr html_head[3] = { "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n", "<pre>", 0, };
28 static concptr html_foot[3] = { "</pre>\n", "</body>\n</html>\n", 0, };
31 * @brief 一時ファイルを読み込み、ファイルに書き出す
32 * @param fff ファイルへの参照ポインタ
33 * @param tempfff 一時ファイルへの参照ポインタ
35 * @param buf_size バッファサイズ
37 * @todo io/ 以下に移したいところだが、このファイルの行数も大したことがないので一旦保留
39 static void read_temporary_file(FILE *fff, FILE *tmpfff, char buf[], size_t buf_size, int num_tag)
41 bool is_first_line = true;
42 int next_tag = num_tag + 1;
43 while (!angband_fgets(tmpfff, buf, buf_size))
47 if (strncmp(buf, tags[num_tag], strlen(tags[num_tag])) == 0)
48 is_first_line = false;
53 if (strncmp(buf, tags[next_tag], strlen(tags[next_tag])) == 0)
56 fprintf(fff, "%s\n", buf);
65 * @param fff 記念撮影ファイルへの参照ポインタ
67 static void screen_dump_one_line(int wid, int y, FILE *fff)
69 TERM_COLOR a = 0, old_a = 0;
71 for (TERM_LEN x = 0; x < wid - 1; x++)
74 (void)(term_what(x, y, &a, &c));
77 case '&': cc = "&"; break;
78 case '<': cc = "<"; break;
79 case '>': cc = ">"; break;
81 case 0x1f: c = '.'; break;
82 case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
87 if ((y == 0 && x == 0) || a != old_a)
89 int rv = angband_color_table[a][1];
90 int gv = angband_color_table[a][2];
91 int bv = angband_color_table[a][3];
92 fprintf(fff, "%s<font color=\"#%02x%02x%02x\">",
93 ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
98 fprintf(fff, "%s", cc);
100 fprintf(fff, "%c", c);
106 * @brief 記念撮影を行方向にスイープする
109 * @param fff 記念撮影ファイルへの参照ポインタ
111 static void screen_dump_lines(int wid, int hgt, FILE *fff)
113 for (TERM_LEN y = 0; y < hgt; y++)
118 screen_dump_one_line(wid, y, fff);
124 * @brief ファイルへ書き込めない場合にエラーを表示する
125 * @param fff ダンプファイルへの参照ポインタ
127 * @return ファイルへ書き込めるならTRUE、書き込めないならFALSE
129 static bool check_screen_html_can_open(FILE *fff, char *filename, int message)
131 if (fff) return true;
132 if (message == 0) return false;
134 msg_format(_("ファイル %s を開けませんでした。", "Failed to open file %s."), filename);
141 * @brief HTMLヘッダを書き込む
142 * @param tmpfff 一時ファイルへの参照ポインタ
143 * @param fff 記念撮影ファイルへの参照ポインタ
145 * @param buf_size バッファサイズ
147 static void write_html_header(FILE *tmpfff, FILE *fff, char buf[], size_t buf_size)
151 read_temporary_file(fff, tmpfff, buf, buf_size, 0);
155 for (int i = 0; html_head[i]; i++)
156 fputs(html_head[i], fff);
161 * @brief HTMLフッタを書き込む
162 * @param tmpfff 一時ファイルへの参照ポインタ
163 * @param fff 記念撮影ファイルへの参照ポインタ
165 * @param buf_size バッファサイズ
167 static void write_html_footer(FILE *tmpfff, FILE *fff, char buf[], size_t buf_size)
169 fprintf(fff, "</font>");
172 for (int i = 0; html_foot[i]; i++)
173 fputs(html_foot[i], fff);
178 read_temporary_file(fff, tmpfff, buf, buf_size, 2);
179 angband_fclose(tmpfff);
186 void do_cmd_save_screen_html_aux(char *filename, int message)
189 term_get_size(&wid, &hgt);
191 fff = angband_fopen(filename, "w");
192 if (!check_screen_html_can_open(fff, filename, message)) return;
194 if (message) screen_save();
197 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "htmldump.prf");
199 tmpfff = angband_fopen(buf, "r");
200 write_html_header(tmpfff, fff, buf, sizeof(buf));
201 screen_dump_lines(wid, hgt, fff);
202 write_html_footer(tmpfff, fff, buf, sizeof(buf));
206 msg_print(_("画面(記念撮影)をファイルに書き出しました。", "Screen dump saved."));
216 * @brief HTML方式で記念撮影する / Save a screen dump to a file
219 static void do_cmd_save_screen_html(void)
221 char buf[1024], tmp[256] = "screen.html";
223 if (!get_string(_("ファイル名: ", "File name: "), tmp, 80))
225 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, tmp);
229 do_cmd_save_screen_html_aux(buf, 1);
234 * @brief 記念撮影の方式を問い合わせる
235 * @param html_dump HTMLダンプするか否か
236 * @return ダンプするならTRUE、キャンセルならFALSE
238 static bool ask_html_dump(bool *html_dump)
243 if (c == 'Y' || c == 'y')
249 if (c == 'H' || c == 'h')
265 * @brief ファイルへ書き込めない場合にエラーを表示する
266 * @param fff ダンプファイルへの参照ポインタ
268 * @return ファイルへ書き込めるならTRUE、書き込めないならFALSE
270 static bool check_screen_text_can_open(FILE *fff, char buf[])
272 if (fff) return true;
274 msg_format(_("ファイル %s を開けませんでした。", "Failed to open file %s."), buf);
281 * @brief テキスト方式で記念撮影する
284 * @return 記念撮影に成功したらTRUE、ファイルが開けなかったらFALSE
285 * @todo どこかバグっていて、(恐らく初期化されていない)変な文字列まで出力される
287 static bool do_cmd_save_screen_text(int wid, int hgt)
293 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "dump.txt");
294 fff = angband_fopen(buf, "w");
295 if (!check_screen_text_can_open(fff, buf)) return false;
298 for (TERM_LEN y = 0; y < hgt; y++)
301 for (x = 0; x < wid - 1; x++)
303 (void)(term_what(x, y, &a, &c));
308 fprintf(fff, "%s\n", buf);
312 for (TERM_LEN y = 0; y < hgt; y++)
315 for (x = 0; x < wid - 1; x++)
317 (void)(term_what(x, y, &a, &c));
318 buf[x] = hack[a & 0x0F];
322 fprintf(fff, "%s\n", buf);
327 msg_print(_("画面(記念撮影)をファイルに書き出しました。", "Screen dump saved."));
335 * @brief 記念撮影のためにグラフィック使用をOFFにする
336 * @param creature_ptr プレーヤーへの参照ポインタ
337 * @return 記念撮影直前のグラフィックオプション
339 static bool update_use_graphics(player_type *creature_ptr)
341 if (!use_graphics) return true;
343 use_graphics = false;
344 reset_visuals(creature_ptr);
345 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
346 handle_stuff(creature_ptr);
352 * Save a screen dump to a file
353 * @param creature_ptr プレーヤーへの参照ポインタ
355 void do_cmd_save_screen(player_type *creature_ptr)
357 prt(_("記念撮影しますか? [(y)es/(h)tml/(n)o] ", "Save screen dump? [(y)es/(h)tml/(n)o] "), 0, 0);
359 if (!ask_html_dump(&html_dump)) return;
362 term_get_size(&wid, &hgt);
364 bool old_use_graphics = update_use_graphics(creature_ptr);
368 do_cmd_save_screen_html();
369 do_cmd_redraw(creature_ptr);
371 else if (!do_cmd_save_screen_text(wid, hgt))
376 if (old_use_graphics) return;
379 reset_visuals(creature_ptr);
380 creature_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
381 handle_stuff(creature_ptr);
386 * @brief 白文字だけ画面に描画する
388 * @param fff 記念撮影ファイルへの参照ポインタ
392 * @return ファイルが読み込めなくなったらFALSEで抜ける
394 static bool draw_white_characters(char buf[], FILE *fff, int wid, int hgt)
397 for (TERM_LEN y = 0; okay; y++)
399 if (!fgets(buf, 1024, fff)) okay = false;
401 if (buf[0] == '\n' || buf[0] == '\0') break;
402 if (y >= hgt) continue;
404 for (TERM_LEN x = 0; x < wid - 1; x++)
406 if (buf[x] == '\n' || buf[x] == '\0') break;
408 term_draw(x, y, TERM_WHITE, buf[x]);
417 * @brief 白以外の文字を画面に描画する
419 * @param fff 記念撮影ファイルへの参照ポインタ
422 * @param 白文字が途中で読み込めなくなっていたらTRUE
425 static void draw_colored_characters(char buf[], FILE *fff, int wid, int hgt, bool okay)
427 TERM_COLOR a = TERM_DARK;
429 for (TERM_LEN y = 0; okay; y++)
431 if (!fgets(buf, 1024, fff)) okay = false;
433 if (buf[0] == '\n' || buf[0] == '\0') break;
434 if (y >= hgt) continue;
436 for (TERM_LEN x = 0; x < wid - 1; x++)
438 if (buf[x] == '\n' || buf[x] == '\0') break;
440 (void)(term_what(x, y, &a, &c));
441 for (int i = 0; i < 16; i++)
443 if (hack[i] == buf[x]) a = (byte)i;
446 term_draw(x, y, a, c);
453 * @brief Load a screen dump from a file
456 void do_cmd_load_screen(void)
461 term_get_size(&wid, &hgt);
462 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "dump.txt");
463 fff = angband_fopen(buf, "r");
466 msg_format(_("%s を開くことができませんでした。", "Failed to open %s."), buf);
473 bool okay = draw_white_characters(buf, fff, wid, hgt);
474 draw_colored_characters(buf, fff, wid, hgt, okay);
477 prt(_("ファイルに書き出された画面(記念撮影)をロードしました。", "Screen dump loaded."), 0, 0);