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,
68 static void try_resize_winmap(struct Win * win, int new_size_y, int new_size_x)
70 if (win->winmap_size.y >= new_size_y && win->winmap_size.x >= new_size_x)
74 if (win->winmap_size.y > new_size_y)
76 new_size_y = win->winmap_size.y;
78 else if (win->winmap_size.x > new_size_x)
80 new_size_x = win->winmap_size.x;
82 chtype * old_winmap = win->winmap;
83 uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
84 win->winmap = try_malloc(new_size, __func__);
86 for (y = 0; y < new_size_y; y++)
88 for (x = 0; y < win->winmap_size.y && x < win->winmap_size.x; x++)
90 chtype ch = old_winmap[(y * win->winmap_size.x) + x];
91 win->winmap[(y * new_size_x) + x] = ch;
93 for (; x < new_size_x; x++)
95 win->winmap[(y * new_size_x) + x] = ' ';
99 win->winmap_size.y = new_size_y;
100 win->winmap_size.x = new_size_x;
105 static void set_ch_on_yx(struct Win * win, int y, int x, chtype ch)
107 win->winmap[(y * win->winmap_size.x) + x] = ch;
112 static void add_line(struct Win * win, char * line, attr_t attri,
113 uint16_t * offset, uint8_t last)
115 exit_err(strlen(line) > UINT16_MAX, "Line in add_line() too big.");
116 if (0 == win->linebreak)
118 add_line_long(win, line, attri);
120 else if (1 == win->linebreak)
122 add_line_wide(win, line, attri);
124 else if (2 == win->linebreak)
126 add_line_compact(win, line, attri, offset, last);
132 static void add_line_wide(struct Win * win, char * line, attr_t attri)
134 uint16_t y_start = win->winmap_size.y;
135 uint16_t len_line = strlen(line);
136 try_resize_winmap(win, y_start + 1, win->frame_size.x);
137 try_resize_winmap(win, y_start + 1, len_line);
139 for (x = 0; x < len_line; x++)
141 set_ch_on_yx(win, y_start, x, line[x] | attri);
145 for (; x < win->frame_size.x; x++)
147 set_ch_on_yx(win, y_start, x, ' ' | attri);
154 static void add_line_long(struct Win * win, char * line, attr_t attri)
156 uint16_t y_start = win->winmap_size.y;
157 uint16_t len_line = strlen(line);
158 try_resize_winmap(win, y_start + 1, win->frame_size.x);
160 for (z = 0, y = y_start; z < len_line; y++)
162 for (x = 0; x < win->winmap_size.x && z < len_line; x++, z++)
164 set_ch_on_yx(win, y, x, line[z] | attri);
168 try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
175 static void add_line_compact(struct Win * win, char * line, attr_t attri,
176 uint16_t * offset, uint8_t last)
178 uint16_t y_start = win->winmap_size.y - (win->winmap_size.y > 0);
179 try_resize_winmap(win, y_start + 1, win->frame_size.x);
180 uint16_t len_line = strlen(line);
181 char * separator = last ? "" : " / ";
182 uint32_t len_line_new = len_line + strlen(separator);
183 char * line_new = try_malloc(len_line_new + 1, __func__);
184 int test = sprintf(line_new, "%s%s", line, separator);
185 exit_trouble(test < 0, __func__, "sprintf");
189 for (z = 0, y = y_start; z < len_line_new; y++)
191 for (x = * offset; x < win->winmap_size.x && z < len_line_new; x++, z++)
193 set_ch_on_yx(win, y, x, line_new[z] | attri);
194 if (z + 1 == (uint32_t) len_line)
200 if (z < len_line_new)
202 try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
211 static void add_text_with_linebreaks(struct Win * win, char * text)
215 char * text_copy = strdup(text);
216 char * start = text_copy;
217 while (NULL != (limit = strchr(start, '\n')))
220 add_line(win, start, 0, &offset, 0);
223 add_line(win, start, 0, &offset, 1);
229 static void draw_text_from_bottom(struct Win * win, char * text)
231 add_text_with_linebreaks(win, text);
232 if (win->winmap_size.y > win->frame_size.y)
234 win->center.y = win->winmap_size.y - 1;
236 else if (win->winmap_size.y < win->frame_size.y)
238 uint16_t new_y_start = win->frame_size.y - win->winmap_size.y;
239 memset(&win->winmap_size, 0, sizeof(struct yx_uint16));
244 add_line_wide(win, " ", 0);
246 while (new_y_start > win->winmap_size.y);
247 if (2 == win->linebreak) /* add_text_with_linebreaks() will start not */
248 { /* not after, but within the last line then. */
249 add_line_wide(win, " ", 0);
251 add_text_with_linebreaks(win, text);
257 static char * get_kb_line(struct KeyBinding * kb)
259 char * keyname = get_keyname_to_keycode(kb->keycode);
260 uint16_t size = strlen(keyname) + 3 + strlen(kb->command->dsc_long) + 1;
261 char * kb_line = try_malloc(size, __func__);
262 int test = sprintf(kb_line, "%s - %s", keyname, kb->command->dsc_long);
263 exit_trouble(test < 0, __func__, "sprintf");
270 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
273 if (0 == kbdb->n_of_kbs)
275 add_line(win, "(none)", 0, &offset, 0);
279 for (kb_n = 0; kb_n < kbdb->n_of_kbs; kb_n++)
282 if (kb_n == kbdb->select)
287 attri = attri | A_BLINK;
289 win->center.y = win->winmap_size.y;
291 char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
292 add_line(win, kb_line, attri, &offset, (kbdb->n_of_kbs == kb_n + 1));
299 extern void draw_win_log(struct Win * win)
305 draw_text_from_bottom(win, world.log);
310 extern void draw_win_map(struct Win * win)
312 init_pair(1, COLOR_WHITE, COLOR_BLUE);
313 init_pair(2, COLOR_BLUE, COLOR_BLACK);
315 attr_t attr_mem = COLOR_PAIR(2);
316 attr_t attr_sha = COLOR_PAIR(1);
317 try_resize_winmap(win, world.map.length, world.map.length * 2);
319 for (y = 0, z = 0; y < world.map.length; y++)
321 for (x = 0; x < world.map.length; x++)
323 attr_t attr_c = ' ' == world.mem_map[z] ? attr_sha : attr_mem;
324 chtype c = world.mem_map[z] | attr_c;
325 set_ch_on_yx(win, y, x * 2 + (y % 2), c);
326 if (x + (y % 2) < world.map.length)
328 set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | attr_c);
333 for (y = 0, z = 0; y < world.map.length; y++)
335 for (x = 0; x < world.map.length; x++)
337 if (' ' != world.map.cells[z])
339 chtype c = world.map.cells[z] | attr_fov;
340 set_ch_on_yx(win, y, x * 2 + (y % 2), c);
341 if (x + (y % 2) < world.map.length)
343 set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | attr_fov);
353 extern void draw_win_info(struct Win * win)
355 char * dsc_turn = "Turn: ";
356 char * dsc_hitpoints = "\nHitpoints: ";
357 uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3;
358 char * text = try_malloc(maxl + 1, __func__);
359 int test = sprintf(text, "%s%d%s%d", dsc_turn, world.turn, dsc_hitpoints,
360 world.player_lifepoints);
361 exit_trouble(test < 0, __func__, "sprintf");
362 add_text_with_linebreaks(win, text);
368 extern void draw_win_inventory(struct Win * win)
371 char * text_copy = strdup(world.player_inventory);
372 char * start = text_copy;
374 uint8_t found_selection = 0;
376 while (NULL != (limit = strchr(start, '\n')))
380 if (i == world.player_inventory_select)
384 win->center.y = win->winmap_size.y;
386 add_line(win, start, attri, &offset, 0);
390 win->center.y = !found_selection ? win->winmap_size.y : win->center.y;
391 add_line(win, start, !found_selection * A_REVERSE, &offset, 1);
397 extern void draw_win_active_windows_keys(struct Win * win)
399 struct Win * win_active = get_win_by_id(world.winDB.active);
400 struct KeyBindingDB * kbdb = &win_active->kb;
401 if (1 == win_active->view)
403 kbdb = &world.kb_wingeom;
405 else if (2 == win_active->view)
407 kbdb = &world.kb_winkeys;
410 if (0 == kbdb->n_of_kbs)
412 add_line(win, "(none)", 0, &offset, 0);
416 for (kb_n = 0; kb_n < kbdb->n_of_kbs; kb_n++)
418 char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
419 add_line(win, kb_line, 0, &offset, (0 == kb_n + 1));
427 extern void draw_win_keybindings_global(struct Win * win)
429 win->center.y = world.kb_global.select;
430 draw_keybinding_config(win, &world.kb_global, 0);
435 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
437 win->center.y = world.kb_wingeom.select;
438 draw_keybinding_config(win, &world.kb_wingeom, 0);
443 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
445 win->center.y = world.kb_winkeys.select;
446 draw_keybinding_config(win, &world.kb_winkeys, 0);
451 extern void draw_winconf_keybindings(struct Win * win)
453 char * title = "Window's keys:";
455 add_line(win, title, 0, &offset, 0);
456 add_line(win, " ", 0, &offset, 0);
457 draw_keybinding_config(win, &win->kb, offset);
462 extern void draw_winconf_geometry(struct Win * win)
464 char * title = "Window's geometry:\n\n";
465 char * h_title = "Height to save: ";
466 char h_value[6 + 1]; /* 6: int16_t value max strlen */
467 int test = sprintf(h_value, "%d", win->target_height);
468 exit_trouble(test < 0, __func__, "sprintf");
469 char * h_plus = " (width in cells)\n\n";
470 char * h_minus = " (negative diff: cells to screen width)\n\n";
471 char * h_type = (1 == win->target_height_type) ? h_minus : h_plus;
472 char * w_title = "Width to save: ";
474 test = sprintf(w_value, "%d", win->target_width);
475 exit_trouble(test < 0, __func__, "sprintf");
476 char * w_plus = " (height in cells)\n\n";
477 char * w_minus = " (negative diff: cells to screen height)\n\n";
478 char * w_type = (1 == win->target_width_type) ? w_minus : w_plus;
479 char * breaks_title = "Linebreak type: ";
480 char * breaks_type = (1 == win->linebreak) ? "wide" : "long";
481 breaks_type = (2 == win->linebreak) ? "compact" : breaks_type;
482 uint16_t text_size = strlen(title)
483 + strlen(h_title) + strlen(h_value) + strlen(h_type)
484 + strlen(w_title) + strlen(w_value) + strlen(w_type)
485 + strlen(breaks_title) + strlen(breaks_type);
486 char * text = try_malloc(text_size + 1, __func__);
487 test = sprintf(text, "%s%s%s%s%s%s%s%s%s", title, h_title, h_value, h_type,
488 w_title, w_value, w_type, breaks_title, breaks_type);
489 exit_trouble(test < 0, __func__, "sprintf");
490 add_text_with_linebreaks(win, text);