1 /* src/client/draw_wins.c */
4 #include <ncurses.h> /* attri_t, chtype */
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int16_t */
7 #include <stdio.h> /* for sprintf() */
8 #include <stdlib.h> /* free() */
9 #include <string.h> /* strlen(), strtok() */
10 #include "../common/try_malloc.h" /* try_malloc() */
11 #include "keybindings.h" /* struct KeyBinding, get_keyname_to_keycode() */
12 #include "windows.h" /* struct Win, get_win_by_id() */
13 #include "world.h" /* global world */
17 /* Apply to the winmap of Win "w" the new sizes "new_size_y" and "new_size_x"
18 * to the degree that they extend it. Re-shape the window content accordingly.
20 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x);
22 /* In Win "w", write "ch" to coordinate "y"/"x". */
23 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch);
25 /* Add "text" into window "win". Break text at right window edge. Also break at
26 * newlines. If "text" ends in a newline, ignore it.
28 static void add_text_with_linebreaks(struct Win * win, char * text);
30 /* Add "line" into window "w". Apply ncurses attribute "attri" to all
31 * characters drawn. If "attri" is non-zero, fill the entire line until the
32 * right window edge with empty characters, so "attri" applies on those too.
34 static void add_line(struct Win * w, char * line, attr_t attri);
36 /* Write "text" with add_text_with_linebreaks() as not starting from the top but
37 * from bottom of "win". Draw only what fits in window (avoid scroll hints).
39 static void draw_text_from_bottom(struct Win * win, char * text);
41 /* Return keybinding list line via "kb_pp", iterate pointer pointed to by it. */
42 static char * get_kb_line_and_iterate(struct KeyBinding ** kb_pp);
44 /* Draw from line "start" on config view for keybindings defined at "kb". */
45 static void draw_keybinding_config(struct Win * w, struct KeyBindingDB * kb,
48 /* Draw into window "w" from line "start" on a "title" followed by an empty
49 * line followed by a list of all keybindings starting at kb_p.
51 static uint16_t draw_titled_keybinding_list(char * title, struct Win * w,
53 struct KeyBinding * kb_p);
57 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x)
59 char * f_name = "try_resize_winmap()";
60 if (w->winmap_size.y >= new_size_y && w->winmap_size.x >= new_size_x)
64 if (w->winmap_size.y > new_size_y)
66 new_size_y = w->winmap_size.y;
68 else if (w->winmap_size.x > new_size_x)
70 new_size_x = w->winmap_size.x;
72 chtype * old_winmap = w->winmap;
73 uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
74 w->winmap = try_malloc(new_size, f_name);
76 for (y = 0; y < new_size_y; y++)
78 for (x = 0; y < w->winmap_size.y && x < w->winmap_size.x; x++)
80 chtype ch = old_winmap[(y * w->winmap_size.x) + x];
81 w->winmap[(y * new_size_x) + x] = ch;
83 for (; x < new_size_x; x++)
85 w->winmap[(y * new_size_x) + x] = ' ';
89 w->winmap_size.y = new_size_y;
90 w->winmap_size.x = new_size_x;
95 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch)
97 w->winmap[(y * w->winmap_size.x) + x] = ch;
102 static void add_text_with_linebreaks(struct Win * win, char * text)
106 for (y = win->winmap_size.y; ; y++)
108 try_resize_winmap(win, y + 1, win->frame_size.x);
109 for (x = 0; x < win->frame_size.x; x++)
116 else if ('\0' == text[z])
122 set_ch_on_yx(win, y, x, text[z]);
124 if ('\n' == text[z+1])
129 else if ('\0' == text[z+1])
139 static void add_line(struct Win * w, char * line, attr_t attri)
141 uint16_t y = w->winmap_size.y;
142 uint16_t len_line = strlen(line);
144 && w->winmap_size.x < w->frame_size.x && w->frame_size.x > len_line)
146 try_resize_winmap(w, y + 1, w->frame_size.x);
150 try_resize_winmap(w, y + 1, strlen(line));
153 for (; x < len_line; x++)
155 set_ch_on_yx(w, y, x, line[x] | attri);
159 for (; x < w->frame_size.x; x++)
161 set_ch_on_yx(w, y, x, ' ' | attri);
168 static void draw_text_from_bottom(struct Win * win, char * text)
170 /* Determine number of lines text would have in a window of win's width,
171 * but infinite height. Treat \n and \0 as control chars for incrementing
172 * y and stopping the loop. Make sure +they* don't count as cell space.
177 for (y = 0; 0 == toggle; y++)
179 for (x = 0; x < win->frame_size.x; x++)
186 if ('\n' == text[z+1])
191 else if (0 == text[z+1])
200 /* Depending on what's bigger, determine start point in window or text. */
201 uint16_t start_y = 0;
202 if (y < win->frame_size.y)
204 start_y = win->frame_size.y - y;
206 else if (y > win->frame_size.y)
208 uint16_t offset = y - win->frame_size.y;
209 for (y = 0; y < offset; y++)
211 for (x = 0; x < win->frame_size.x; x++)
218 if ('\n' == text[z+1])
225 text = text + (sizeof(char) * (z + 1));
228 try_resize_winmap(win, start_y, 1);
229 add_text_with_linebreaks(win, text);
234 static char * get_kb_line_and_iterate(struct KeyBinding ** kb_pp)
236 char * f_name = "get_kb_line_and_iterate()";
237 struct KeyBinding * kb_p = * kb_pp;
238 char * keyname = get_keyname_to_keycode(kb_p->key);
239 struct Command * command = get_command_to_keycode(kb_p, kb_p->key);
240 uint16_t size = 9 + 1 + strlen(command->dsc_long) + 1;
241 char * line = try_malloc(size, f_name);
242 sprintf(line, "%-9s %s", keyname, command->dsc_long);
244 * kb_pp = kb_p->next;
250 static void draw_keybinding_config(struct Win * w, struct KeyBindingDB * kb,
255 add_line(w, "(none)", 0);
258 struct KeyBinding * kb_p = kb->kbs;
260 for (y = start; 0 != kb_p; y++)
263 if (y - start == kb->select)
268 attri = attri | A_BLINK;
271 char * kb_line = get_kb_line_and_iterate(&kb_p);
272 add_line(w, kb_line, attri);
279 static uint16_t draw_titled_keybinding_list(char * title, struct Win * w,
281 struct KeyBinding * kb_p)
285 for (y = start; (0 == state || 0 != kb_p); y++)
289 add_line(w, title, 0);
292 state = 1 + (0 == kb_p);
295 char * kb_line = get_kb_line_and_iterate(&kb_p);
296 add_line(w, kb_line, 0);
301 char * none = "(none)";
302 add_line(w, none, 0);
310 extern void draw_win_log(struct Win * win)
317 draw_text_from_bottom(win, log);
322 extern void draw_win_map(struct Win * win)
324 try_resize_winmap(win, world.map.size.y, world.map.size.x);
327 for (y = 0; y < world.map.size.y; y++)
329 for (x = 0; x < world.map.size.x; x++)
331 set_ch_on_yx(win, y, x, world.map.cells[z]);
339 extern void draw_win_info(struct Win * win)
341 char * dsc_turn = "Turn: ";
342 char * dsc_hitpoints = "\nHitpoints: ";
343 char * dsc_score = "\nScore: ";
344 uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
345 + 5 + 3 + 5; /* Max strlens of strings of numbers to use. */
347 sprintf(text, "%s%d%s%d%s%d",
348 dsc_turn, world.turn,
349 dsc_hitpoints, world.player_lifepoints,
350 dsc_score, world.player_score);
351 add_text_with_linebreaks(win, text);
356 extern void draw_win_inventory(struct Win * win)
358 win->center.y = world.player_inventory_select;
359 char inventory_copy[strlen(world.player_inventory) + 1];
360 sprintf(inventory_copy, "%s", world.player_inventory);
361 char * strtok_target = inventory_copy;
365 char * object = strtok(strtok_target, "\n");
366 strtok_target = NULL;
372 if (i == world.player_inventory_select)
376 add_line(win, object, attri);
383 extern void draw_win_available_keybindings(struct Win * win)
385 char * title = "Active window's keybindings:";
386 struct KeyBinding * kb_p;
387 struct Win * w = get_win_by_id(world.winDB.active);
392 else if (1 == w->view)
394 kb_p = world.kb_wingeom.kbs;
396 else if (2 == w->view)
398 kb_p = world.kb_winkeys.kbs;
400 uint16_t offset = draw_titled_keybinding_list(title, win, 0, kb_p);
401 add_line(win, " ", 0);
402 struct KeyBinding * kbs_glo = world.kb_global.kbs;
403 draw_titled_keybinding_list("Global keybindings", win, offset + 1, kbs_glo);
408 extern void draw_win_keybindings_global(struct Win * win)
410 win->center.y = world.kb_global.select;
411 draw_keybinding_config(win, &world.kb_global, 0);
416 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
418 win->center.y = world.kb_wingeom.select;
419 draw_keybinding_config(win, &world.kb_wingeom, 0);
424 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
426 win->center.y = world.kb_winkeys.select;
427 draw_keybinding_config(win, &world.kb_winkeys, 0);
432 extern void draw_winconf_keybindings(struct Win * win)
434 char * title = "Window's keybindings:";
435 add_line(win, title, 0);
436 add_line(win, " ", 0);
437 draw_keybinding_config(win, &win->kb, 2);
438 win->center.y = win->kb.select + 2;
443 extern void draw_winconf_geometry(struct Win * win)
445 char * title = "Window's geometry:\n";
446 char * h_d = "\nHeight to save: ";
447 char * h_pos = " (width in cells)";
448 char * h_neg = " (negative diff: cells to screen width)";
449 char * w_d = "\n\nWidth to save: ";
450 char * w_pos = " (height in cells)";
451 char * w_neg = " (negative diff: cells to screen height)";
454 if (1 == win->target_height_type)
458 if (1 == win->target_width_type)
462 uint16_t maxl = strlen(title)
463 + strlen(h_t) + strlen(h_d) + 6 /* 6 = n of chars to */
464 + strlen(w_t) + strlen(w_d) + 6 + 1; /* write max int16_t */
466 sprintf(text, "%s%s%d%s%s%d%s", title, h_d, win->target_height, h_t,
467 w_d, win->target_width, w_t);
468 add_text_with_linebreaks(win, text);