1 /* src/client/draw_wins.c */
4 #include <ncurses.h> /* attri_t, chtype */
5 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int16_t */
6 #include <stdio.h> /* for sprintf() */
7 #include <stdlib.h> /* free() */
8 #include <string.h> /* strlen(), strtok() */
9 #include "../common/try_malloc.h" /* for try_malloc() */
10 #include "command_db.h" /* for get_command_longdsc */
11 #include "keybindings.h" /* struct KeyBinding, get_keyname_to_keycode() */
12 #include "wincontrol.h" /* struct WinConf, get_winconf_by_win() */
13 #include "windows.h" /* struct Win */
14 #include "world.h" /* global world */
18 /* Apply to the winmap of Win "w" the new sizes "new_size_y" and "new_size_x"
19 * to the degree that they extend it. Re-shape the window content accordingly.
21 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x);
23 /* In Win "w", write "ch" to coordinate "y"/"x". */
24 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch);
26 /* Add "text" into window "win". Break text at right window edge. Also break at
27 * newlines. If "text" ends in a newline, ignore it.
29 static void add_text_with_linebreaks(struct Win * win, char * text);
31 /* Add "line" into window "w". Apply ncurses attribute "attri" to all
32 * characters drawn. If "attri" is non-zero, fill the entire line until the
33 * right window edge with empty characters, so "attri" applies on those too.
35 static void add_line(struct Win * w, char * line, attr_t attri);
37 /* Write "text" with add_text_with_linebreaks() as not starting from the top but
38 * from bottom of "win". Draw only what fits in window (avoid scroll hints).
40 static void draw_text_from_bottom(struct Win * win, char * text);
42 /* Return keybinding list line via "kb_pp", iterate pointer pointed to by it. */
43 static char * get_kb_line_and_iterate(struct KeyBinding ** kb_pp);
45 /* Draw from line "start" on config view for keybindings defined at "kb". */
46 static void draw_keybinding_config(struct Win * w, struct KeyBindingDB * kb,
49 /* Draw into window "w" from line "start" on a "title" followed by an empty
50 * line followed by a list of all keybindings starting at kb_p.
52 static uint16_t draw_titled_keybinding_list(char * title, struct Win * w,
54 struct KeyBinding * kb_p);
58 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x)
60 char * f_name = "try_resize_winmap()";
61 if (w->winmapsize.y >= new_size_y && w->winmapsize.x >= new_size_x)
65 if (w->winmapsize.y > new_size_y)
67 new_size_y = w->winmapsize.y;
69 else if (w->winmapsize.x > new_size_x)
71 new_size_x = w->winmapsize.x;
73 chtype * old_winmap = w->winmap;
74 uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
75 w->winmap = try_malloc(new_size, f_name);
77 for (y = 0; y < new_size_y; y++)
79 for (x = 0; y < w->winmapsize.y && x < w->winmapsize.x; x++)
81 chtype ch = old_winmap[(y * w->winmapsize.x) + x];
82 w->winmap[(y * new_size_x) + x] = ch;
84 for (; x < new_size_x; x++)
86 w->winmap[(y * new_size_x) + x] = ' ';
90 w->winmapsize.y = new_size_y;
91 w->winmapsize.x = new_size_x;
96 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch)
98 w->winmap[(y * w->winmapsize.x) + x] = ch;
103 static void add_text_with_linebreaks(struct Win * win, char * text)
107 for (y = win->winmapsize.y; ; y++)
109 try_resize_winmap(win, y + 1, win->framesize.x);
110 for (x = 0; x < win->framesize.x; x++)
117 else if ('\0' == text[z])
123 set_ch_on_yx(win, y, x, text[z]);
125 if ('\n' == text[z+1])
130 else if ('\0' == text[z+1])
140 static void add_line(struct Win * w, char * line, attr_t attri)
142 uint16_t y = w->winmapsize.y;
143 uint16_t len_line = strlen(line);
145 && w->winmapsize.x < w->framesize.x && w->framesize.x > len_line)
147 try_resize_winmap(w, y + 1, w->framesize.x);
151 try_resize_winmap(w, y + 1, strlen(line));
154 for (; x < len_line; x++)
156 set_ch_on_yx(w, y, x, line[x] | attri);
160 for (; x < w->framesize.x; x++)
162 set_ch_on_yx(w, y, x, ' ' | attri);
169 static void draw_text_from_bottom(struct Win * win, char * text)
171 /* Determine number of lines text would have in a window of win's width,
172 * but infinite height. Treat \n and \0 as control chars for incrementing
173 * y and stopping the loop. Make sure +they* don't count as cell space.
178 for (y = 0; 0 == toggle; y++)
180 for (x = 0; x < win->framesize.x; x++)
187 if ('\n' == text[z+1])
192 else if (0 == text[z+1])
201 /* Depending on what's bigger, determine start point in window or text. */
202 uint16_t start_y = 0;
203 if (y < win->framesize.y)
205 start_y = win->framesize.y - y;
207 else if (y > win->framesize.y)
209 uint16_t offset = y - win->framesize.y;
210 for (y = 0; y < offset; y++)
212 for (x = 0; x < win->framesize.x; x++)
219 if ('\n' == text[z+1])
226 text = text + (sizeof(char) * (z + 1));
229 try_resize_winmap(win, start_y, 1);
230 add_text_with_linebreaks(win, text);
235 static char * get_kb_line_and_iterate(struct KeyBinding ** kb_pp)
237 char * f_name = "get_kb_line_and_iterate()";
238 struct KeyBinding * kb_p = * kb_pp;
239 char * keyname = get_keyname_to_keycode(kb_p->key);
240 char * cmd_dsc = get_command_longdsc(kb_p->command);
241 uint16_t size = 9 + 1 + strlen(cmd_dsc) + 1;
242 char * line = try_malloc(size, f_name);
243 sprintf(line, "%-9s %s", keyname, cmd_dsc);
245 * kb_pp = kb_p->next;
251 static void draw_keybinding_config(struct Win * w, struct KeyBindingDB * kb,
256 add_line(w, "(none)", 0);
259 struct KeyBinding * kb_p = kb->kbs;
261 for (y = start; 0 != kb_p; y++)
264 if (y - start == kb->select)
269 attri = attri | A_BLINK;
272 char * kb_line = get_kb_line_and_iterate(&kb_p);
273 add_line(w, kb_line, attri);
280 static uint16_t draw_titled_keybinding_list(char * title, struct Win * w,
282 struct KeyBinding * kb_p)
286 for (y = start; (0 == state || 0 != kb_p); y++)
290 add_line(w, title, 0);
293 state = 1 + (0 == kb_p);
296 char * kb_line = get_kb_line_and_iterate(&kb_p);
297 add_line(w, kb_line, 0);
302 char * none = "(none)";
303 add_line(w, none, 0);
311 extern void draw_win_log(struct Win * win)
318 draw_text_from_bottom(win, log);
323 extern void draw_win_map(struct Win * win)
325 try_resize_winmap(win, world.map.size.y, world.map.size.x);
328 for (y = 0; y < world.map.size.y; y++)
330 for (x = 0; x < world.map.size.x; x++)
332 set_ch_on_yx(win, y, x, world.map.cells[z]);
340 extern void draw_win_info(struct Win * win)
342 char * dsc_turn = "Turn: ";
343 char * dsc_hitpoints = "\nHitpoints: ";
344 char * dsc_score = "\nScore: ";
345 uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
346 + 5 + 3 + 5; /* Max strlens of strings of numbers to use. */
348 sprintf(text, "%s%d%s%d%s%d",
349 dsc_turn, world.turn,
350 dsc_hitpoints, world.player_lifepoints,
351 dsc_score, world.score);
352 add_text_with_linebreaks(win, text);
357 extern void draw_win_inventory(struct Win * win)
359 win->center.y = world.player_inventory_select;
360 char inventory_copy[strlen(world.player_inventory) + 1];
361 sprintf(inventory_copy, "%s", world.player_inventory);
362 char * foo = inventory_copy;
366 char * object = strtok(foo, "\n");
373 if (i == world.player_inventory_select)
377 add_line(win, object, attri);
384 extern void draw_win_available_keybindings(struct Win * win)
386 char * title = "Active window's keybindings:";
387 struct KeyBinding * kb_p;
388 struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
393 else if (1 == wc->view)
395 kb_p = world.kb_wingeom.kbs;
397 else if (2 == wc->view)
399 kb_p = world.kb_winkeys.kbs;
401 uint16_t offset = draw_titled_keybinding_list(title, win, 0, kb_p);
402 add_line(win, " ", 0);
403 struct KeyBinding * kbs_glo = world.kb_global.kbs;
404 draw_titled_keybinding_list("Global keybindings", win, offset + 1, kbs_glo);
409 extern void draw_win_keybindings_global(struct Win * win)
411 win->center.y = world.kb_global.select;
412 draw_keybinding_config(win, &world.kb_global, 0);
417 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
419 win->center.y = world.kb_wingeom.select;
420 draw_keybinding_config(win, &world.kb_wingeom, 0);
425 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
427 win->center.y = world.kb_winkeys.select;
428 draw_keybinding_config(win, &world.kb_winkeys, 0);
433 extern void draw_winconf_keybindings(struct Win * win)
435 struct WinConf * wc = get_winconf_by_win(win);
436 char * title = "Window's keybindings:";
437 add_line(win, title, 0);
438 add_line(win, " ", 0);
439 draw_keybinding_config(win, &wc->kb, 2);
440 win->center.y = wc->kb.select + 2;
445 extern void draw_winconf_geometry(struct Win * win)
447 struct WinConf * wcp = get_winconf_by_win(win);
448 char * title = "Window's geometry:\n";
449 char * h_d = "\nHeight to save: ";
450 char * h_pos = " (width in cells)";
451 char * h_neg = " (negative diff: cells to screen width)";
452 char * w_d = "\n\nWidth to save: ";
453 char * w_pos = " (height in cells)";
454 char * w_neg = " (negative diff: cells to screen height)";
457 if (1 == wcp->height_type)
461 if (1 == wcp->width_type)
465 uint16_t maxl = strlen(title)
466 + strlen(h_t) + strlen(h_d) + 6 /* 6 = n of chars to */
467 + strlen(w_t) + strlen(w_d) + 6 + 1; /* write max int16_t */
469 sprintf(text, "%s%s%d%s%s%d%s", title, h_d, wcp->height, h_t,
470 w_d, wcp->width, w_t);
471 add_text_with_linebreaks(win, text);