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 a properly formatted keybinding list line for "kb". */
42 static char * get_kb_line(struct KeyBinding * kb);
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 in "kbdb".
51 static uint16_t draw_titled_keybinding_list(char * title, struct Win * w,
53 struct KeyBindingDB * kbdb);
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(struct KeyBinding * kb)
236 char * f_name = "get_kb_line()";
237 char * keyname = get_keyname_to_keycode(kb->keycode);
238 uint16_t size = 9 + 1 + strlen(kb->command->dsc_long) + 1;
239 char * kb_line = try_malloc(size, f_name);
240 sprintf(kb_line, "%-9s %s", keyname, kb->command->dsc_long);
247 static void draw_keybinding_config(struct Win * w, struct KeyBindingDB * kbdb,
250 if (0 == kbdb->n_of_kbs)
252 add_line(w, "(none)", 0);
256 for (y = start, kb_n = 0; kb_n < kbdb->n_of_kbs; y++, kb_n++)
259 if (y - start == kbdb->select)
264 attri = attri | A_BLINK;
267 char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
268 add_line(w, kb_line, attri);
275 static uint16_t draw_titled_keybinding_list(char * title, struct Win * w,
277 struct KeyBindingDB * kbdb)
282 for (y = start; (0 == state || kb_n < kbdb->n_of_kbs); y++, kb_n++)
286 add_line(w, title, 0);
289 state = 1 + (0 == kbdb->n_of_kbs);
292 char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
293 add_line(w, kb_line, 0);
298 char * none = "(none)";
299 add_line(w, none, 0);
307 extern void draw_win_log(struct Win * win)
314 draw_text_from_bottom(win, log);
319 extern void draw_win_map(struct Win * win)
321 try_resize_winmap(win, world.map.size.y, world.map.size.x);
324 for (y = 0; y < world.map.size.y; y++)
326 for (x = 0; x < world.map.size.x; x++)
328 set_ch_on_yx(win, y, x, world.map.cells[z]);
336 extern void draw_win_info(struct Win * win)
338 char * dsc_turn = "Turn: ";
339 char * dsc_hitpoints = "\nHitpoints: ";
340 uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3;
342 sprintf(text, "%s%d%s%d",
343 dsc_turn, world.turn, dsc_hitpoints, world.player_lifepoints);
344 add_text_with_linebreaks(win, text);
349 extern void draw_win_inventory(struct Win * win)
351 win->center.y = world.player_inventory_select;
352 char inventory_copy[strlen(world.player_inventory) + 1];
353 sprintf(inventory_copy, "%s", world.player_inventory);
354 char * strtok_target = inventory_copy;
358 char * object = strtok(strtok_target, "\n");
359 strtok_target = NULL;
365 if (i == world.player_inventory_select)
369 add_line(win, object, attri);
376 extern void draw_win_available_keybindings(struct Win * win)
378 char * title = "Active window's keybindings:";
379 struct KeyBindingDB * kbdb;
380 struct Win * w = get_win_by_id(world.winDB.active);
385 else if (1 == w->view)
387 kbdb = &world.kb_wingeom;
389 else if (2 == w->view)
391 kbdb = &world.kb_winkeys;
393 uint16_t offset = draw_titled_keybinding_list(title, win, 0, kbdb);
394 add_line(win, " ", 0);
395 draw_titled_keybinding_list("Global keybindings", win, offset + 1,
401 extern void draw_win_keybindings_global(struct Win * win)
403 win->center.y = world.kb_global.select;
404 draw_keybinding_config(win, &world.kb_global, 0);
409 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
411 win->center.y = world.kb_wingeom.select;
412 draw_keybinding_config(win, &world.kb_wingeom, 0);
417 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
419 win->center.y = world.kb_winkeys.select;
420 draw_keybinding_config(win, &world.kb_winkeys, 0);
425 extern void draw_winconf_keybindings(struct Win * win)
427 char * title = "Window's keybindings:";
428 add_line(win, title, 0);
429 add_line(win, " ", 0);
430 draw_keybinding_config(win, &win->kb, 2);
431 win->center.y = win->kb.select + 2;
436 extern void draw_winconf_geometry(struct Win * win)
438 char * title = "Window's geometry:\n";
439 char * h_d = "\nHeight to save: ";
440 char * h_pos = " (width in cells)";
441 char * h_neg = " (negative diff: cells to screen width)";
442 char * w_d = "\n\nWidth to save: ";
443 char * w_pos = " (height in cells)";
444 char * w_neg = " (negative diff: cells to screen height)";
447 if (1 == win->target_height_type)
451 if (1 == win->target_width_type)
455 uint16_t maxl = strlen(title)
456 + strlen(h_t) + strlen(h_d) + 6 /* 6 = n of chars to */
457 + strlen(w_t) + strlen(w_d) + 6 + 1; /* write max int16_t */
459 sprintf(text, "%s%s%d%s%s%d%s", title, h_d, win->target_height, h_t,
460 w_d, win->target_width, w_t);
461 add_text_with_linebreaks(win, text);