1 /* src/client/draw_wins.c
3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
8 #define _POSIX_C_SOURCE 200809L /* strdup() */
10 #include <ncurses.h> /* attr_t, chtype, init_pair(), A_REVERSE, COLOR_PAIR() */
11 #include <stddef.h> /* NULL */
12 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
13 #include <stdio.h> /* sprintf() */
14 #include <stdlib.h> /* free() */
15 #include <string.h> /* memset(), strchr(), strdup/(), strlen() */
16 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
17 #include "../common/try_malloc.h" /* try_malloc() */
18 #include "keybindings.h" /* struct KeyBindingDB, get_keyname_to_keycode() */
19 #include "windows.h" /* yx_uint16, Win, get_win_by_id() */
20 #include "world.h" /* global world */
24 /* Apply to the winmap of Win "w" the new sizes "new_size_y" and "new_size_x"
25 * to the degree that they extend it. Re-shape the window content accordingly.
27 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x);
29 /* In Win "w", write "ch" to coordinate "y"/"x". */
30 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch);
32 /* Add "line" into window "w" with "attri" set on all chars. add_line() selects
33 * one of the other three functions based on the "win"->linebreak mode.
35 * "wide" writes lines to full width without in-between breaks. "long" breaks
36 * lines on the right window border. "compact" replaces newlines with printable
37 * separator characters. "offset" and "last" are only relevant to the "compact"
38 * mode. "offset" reports the horizontal offset at which the line is drawn from
39 * the lowest existing winmap line on; it is updated to the next offset after
40 * the new line, to be used by the next add_line_compact() call. "last" marks
41 * the line to add as the last in the window, so no further separator is added.
43 static void add_line(struct Win * win, char * line, attr_t attri,
44 uint16_t * offset, uint8_t last);
45 static void add_line_wide(struct Win * win, char * line, attr_t attri);
46 static void add_line_long(struct Win * win, char * line, attr_t attri);
47 static void add_line_compact(struct Win * win, char * line, attr_t attri,
48 uint16_t * offset, uint8_t last);
50 /* Add linebreaks-containing "text" to "win", formatted as per "win"->linebreak.
52 * draw_text_from_bottom() prepends empty lines to "text" to push its last line
53 * to the low frame edge when the frame is of greater height than the winmap
54 * would be otherwise; else, set "win"->center.y to the winmap's lowest line.
56 static void add_text_with_linebreaks(struct Win * win, char * text);
57 static void draw_text_from_bottom(struct Win * win, char * text);
59 /* Return a properly formatted keybinding list line for "kb". */
60 static char * get_kb_line(struct KeyBinding * kb);
62 /* Draw from line "start" on config view for keybindings defined at "kb". */
63 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
66 /* Draw into window "w" from line "start" on a "title" followed by an empty
67 * line followed by a list of all keybindings starting in "kbdb".
69 static void draw_titled_keybinding_list(char * title, struct Win * win,
70 uint16_t * offset, uint8_t last,
71 struct KeyBindingDB * kbdb);
75 static void try_resize_winmap(struct Win * win, int new_size_y, int new_size_x)
77 if (win->winmap_size.y >= new_size_y && win->winmap_size.x >= new_size_x)
81 if (win->winmap_size.y > new_size_y)
83 new_size_y = win->winmap_size.y;
85 else if (win->winmap_size.x > new_size_x)
87 new_size_x = win->winmap_size.x;
89 chtype * old_winmap = win->winmap;
90 uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
91 win->winmap = try_malloc(new_size, __func__);
93 for (y = 0; y < new_size_y; y++)
95 for (x = 0; y < win->winmap_size.y && x < win->winmap_size.x; x++)
97 chtype ch = old_winmap[(y * win->winmap_size.x) + x];
98 win->winmap[(y * new_size_x) + x] = ch;
100 for (; x < new_size_x; x++)
102 win->winmap[(y * new_size_x) + x] = ' ';
106 win->winmap_size.y = new_size_y;
107 win->winmap_size.x = new_size_x;
112 static void set_ch_on_yx(struct Win * win, int y, int x, chtype ch)
114 win->winmap[(y * win->winmap_size.x) + x] = ch;
119 static void add_line(struct Win * win, char * line, attr_t attri,
120 uint16_t * offset, uint8_t last)
122 exit_err(strlen(line) > UINT16_MAX, "Line in add_line() too big.");
123 if (0 == win->linebreak)
125 add_line_long(win, line, attri);
127 else if (1 == win->linebreak)
129 add_line_wide(win, line, attri);
131 else if (2 == win->linebreak)
133 add_line_compact(win, line, attri, offset, last);
139 static void add_line_wide(struct Win * win, char * line, attr_t attri)
141 uint16_t y_start = win->winmap_size.y;
142 uint16_t len_line = strlen(line);
143 try_resize_winmap(win, y_start + 1, win->frame_size.x);
144 try_resize_winmap(win, y_start + 1, len_line);
146 for (x = 0; x < len_line; x++)
148 set_ch_on_yx(win, y_start, x, line[x] | attri);
152 for (; x < win->frame_size.x; x++)
154 set_ch_on_yx(win, y_start, x, ' ' | attri);
161 static void add_line_long(struct Win * win, char * line, attr_t attri)
163 uint16_t y_start = win->winmap_size.y;
164 uint16_t len_line = strlen(line);
165 try_resize_winmap(win, y_start + 1, win->frame_size.x);
167 for (z = 0, y = y_start; z < len_line; y++)
169 for (x = 0; x < win->winmap_size.x && z < len_line; x++, z++)
171 set_ch_on_yx(win, y, x, line[z] | attri);
175 try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
182 static void add_line_compact(struct Win * win, char * line, attr_t attri,
183 uint16_t * offset, uint8_t last)
185 uint16_t y_start = win->winmap_size.y - (win->winmap_size.y > 0);
186 try_resize_winmap(win, y_start + 1, win->frame_size.x);
187 uint16_t len_line = strlen(line);
188 char * separator = last ? "" : " / ";
189 uint32_t len_line_new = len_line + strlen(separator);
190 char * line_new = try_malloc(len_line_new + 1, __func__);
191 int test = sprintf(line_new, "%s%s", line, separator);
192 exit_trouble(test < 0, __func__, "sprintf");
196 for (z = 0, y = y_start; z < len_line_new; y++)
198 for (x = * offset; x < win->winmap_size.x && z < len_line_new; x++, z++)
200 set_ch_on_yx(win, y, x, line_new[z] | attri);
201 if (z + 1 == (uint32_t) len_line)
207 if (z < len_line_new)
209 try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
218 static void add_text_with_linebreaks(struct Win * win, char * text)
222 char * text_copy = strdup(text);
223 char * start = text_copy;
224 while (NULL != (limit = strchr(start, '\n')))
227 add_line(win, start, 0, &offset, 0);
230 add_line(win, start, 0, &offset, 1);
236 static void draw_text_from_bottom(struct Win * win, char * text)
238 add_text_with_linebreaks(win, text);
239 if (win->winmap_size.y > win->frame_size.y)
241 win->center.y = win->winmap_size.y - 1;
243 else if (win->winmap_size.y < win->frame_size.y)
245 uint16_t new_y_start = win->frame_size.y - win->winmap_size.y;
246 memset(&win->winmap_size, 0, sizeof(struct yx_uint16));
251 add_line_wide(win, " ", 0);
253 while (new_y_start > win->winmap_size.y);
254 if (2 == win->linebreak) /* add_text_with_linebreaks() will start not */
255 { /* not after, but within the last line then. */
256 add_line_wide(win, " ", 0);
258 add_text_with_linebreaks(win, text);
264 static char * get_kb_line(struct KeyBinding * kb)
266 char * keyname = get_keyname_to_keycode(kb->keycode);
267 uint16_t size = strlen(keyname) + 3 + strlen(kb->command->dsc_long) + 1;
268 char * kb_line = try_malloc(size, __func__);
269 int test = sprintf(kb_line, "%s - %s", keyname, kb->command->dsc_long);
270 exit_trouble(test < 0, __func__, "sprintf");
277 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
280 if (0 == kbdb->n_of_kbs)
282 add_line(win, "(none)", 0, &offset, 0);
286 for (kb_n = 0; kb_n < kbdb->n_of_kbs; kb_n++)
289 if (kb_n == kbdb->select)
294 attri = attri | A_BLINK;
296 win->center.y = win->winmap_size.y;
298 char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
299 add_line(win, kb_line, attri, &offset, (kbdb->n_of_kbs == kb_n + 1));
306 static void draw_titled_keybinding_list(char * title, struct Win * win,
307 uint16_t * offset, uint8_t last,
308 struct KeyBindingDB * kbdb)
312 while (0 == state || kb_n < kbdb->n_of_kbs)
316 add_line(win, title, 0, offset, 0);
317 add_line(win, " ", 0, offset, 0);
318 state = 1 + (0 == kbdb->n_of_kbs);
321 char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
322 add_line(win, kb_line, 0, offset, (last * kbdb->n_of_kbs == kb_n + 1));
328 add_line(win, "(none)", 0, offset, last);
334 extern void draw_win_log(struct Win * win)
340 draw_text_from_bottom(win, world.log);
345 extern void draw_win_map(struct Win * win)
347 init_pair(1, COLOR_WHITE, COLOR_BLUE);
348 init_pair(2, COLOR_BLUE, COLOR_BLACK);
350 attr_t attr_mem = COLOR_PAIR(2);
351 attr_t attr_sha = COLOR_PAIR(1);
352 try_resize_winmap(win, world.map.length, world.map.length * 2);
354 for (y = 0, z = 0; y < world.map.length; y++)
356 for (x = 0; x < world.map.length; x++)
358 attr_t attr_c = ' ' == world.mem_map[z] ? attr_sha : attr_mem;
359 chtype c = world.mem_map[z] | attr_c;
360 set_ch_on_yx(win, y, x * 2 + (y % 2), c);
361 if (x + (y % 2) < world.map.length)
363 set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | attr_c);
368 for (y = 0, z = 0; y < world.map.length; y++)
370 for (x = 0; x < world.map.length; x++)
372 if (' ' != world.map.cells[z])
374 chtype c = world.map.cells[z] | attr_fov;
375 set_ch_on_yx(win, y, x * 2 + (y % 2), c);
376 if (x + (y % 2) < world.map.length)
378 set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | attr_fov);
388 extern void draw_win_info(struct Win * win)
390 char * dsc_turn = "Turn: ";
391 char * dsc_hitpoints = "\nHitpoints: ";
392 uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3;
393 char * text = try_malloc(maxl + 1, __func__);
394 int test = sprintf(text, "%s%d%s%d", dsc_turn, world.turn, dsc_hitpoints,
395 world.player_lifepoints);
396 exit_trouble(test < 0, __func__, "sprintf");
397 add_text_with_linebreaks(win, text);
403 extern void draw_win_inventory(struct Win * win)
406 char * text_copy = strdup(world.player_inventory);
407 char * start = text_copy;
409 uint8_t found_selection = 0;
411 while (NULL != (limit = strchr(start, '\n')))
415 if (i == world.player_inventory_select)
419 win->center.y = win->winmap_size.y;
421 add_line(win, start, attri, &offset, 0);
425 win->center.y = !found_selection ? win->winmap_size.y : win->center.y;
426 add_line(win, start, !found_selection * A_REVERSE, &offset, 1);
432 extern void draw_win_available_keybindings(struct Win * win)
434 char * title = "Active window's keys:";
435 struct Win * win_active = get_win_by_id(world.winDB.active);
436 struct KeyBindingDB * kbdb = &win_active->kb;
437 if (1 == win_active->view)
439 kbdb = &world.kb_wingeom;
441 else if (2 == win_active->view)
443 kbdb = &world.kb_winkeys;
446 draw_titled_keybinding_list(title, win, &offset, 0, kbdb);
447 add_line(win, " ", 0, &offset, 0);
448 title = "Global keys:";
449 draw_titled_keybinding_list(title, win, &offset, 1, &world.kb_global);
455 extern void draw_win_keybindings_global(struct Win * win)
457 win->center.y = world.kb_global.select;
458 draw_keybinding_config(win, &world.kb_global, 0);
463 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
465 win->center.y = world.kb_wingeom.select;
466 draw_keybinding_config(win, &world.kb_wingeom, 0);
471 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
473 win->center.y = world.kb_winkeys.select;
474 draw_keybinding_config(win, &world.kb_winkeys, 0);
479 extern void draw_winconf_keybindings(struct Win * win)
481 char * title = "Window's keys:";
483 add_line(win, title, 0, &offset, 0);
484 add_line(win, " ", 0, &offset, 0);
485 draw_keybinding_config(win, &win->kb, offset);
490 extern void draw_winconf_geometry(struct Win * win)
492 char * title = "Window's geometry:\n\n";
493 char * h_title = "Height to save: ";
494 char h_value[6 + 1]; /* 6: int16_t value max strlen */
495 int test = sprintf(h_value, "%d", win->target_height);
496 exit_trouble(test < 0, __func__, "sprintf");
497 char * h_plus = " (width in cells)\n\n";
498 char * h_minus = " (negative diff: cells to screen width)\n\n";
499 char * h_type = (1 == win->target_height_type) ? h_minus : h_plus;
500 char * w_title = "Width to save: ";
502 test = sprintf(w_value, "%d", win->target_width);
503 exit_trouble(test < 0, __func__, "sprintf");
504 char * w_plus = "(height in cells)\n\n";
505 char * w_minus = " (negative diff: cells to screen height)\n\n";
506 char * w_type = (1 == win->target_width_type) ? w_minus : w_plus;
507 char * breaks_title = "Linebreak type: ";
508 char * breaks_type = (1 == win->linebreak) ? "wide" : "long";
509 breaks_type = (2 == win->linebreak) ? "compact" : breaks_type;
510 uint16_t text_size = strlen(title)
511 + strlen(h_title) + strlen(h_value) + strlen(h_type)
512 + strlen(w_title) + strlen(w_value) + strlen(w_type)
513 + strlen(breaks_title) + strlen(breaks_type);
514 char * text = try_malloc(text_size + 1, __func__);
515 test = sprintf(text, "%s%s%s%s%s%s%s%s%s", title, h_title, h_value, h_type,
516 w_title, w_value, w_type, breaks_title, breaks_type);
517 exit_trouble(test < 0, __func__, "sprintf");
518 add_text_with_linebreaks(win, text);