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" /* get_command_data() */
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 struct Command * command_data = get_command_data(kb_p->command);
241 char * cmd_dsc = command_data->dsc_long;
242 uint16_t size = 9 + 1 + strlen(cmd_dsc) + 1;
243 char * line = try_malloc(size, f_name);
244 sprintf(line, "%-9s %s", keyname, cmd_dsc);
246 * kb_pp = kb_p->next;
252 static void draw_keybinding_config(struct Win * w, struct KeyBindingDB * kb,
257 add_line(w, "(none)", 0);
260 struct KeyBinding * kb_p = kb->kbs;
262 for (y = start; 0 != kb_p; y++)
265 if (y - start == kb->select)
270 attri = attri | A_BLINK;
273 char * kb_line = get_kb_line_and_iterate(&kb_p);
274 add_line(w, kb_line, attri);
281 static uint16_t draw_titled_keybinding_list(char * title, struct Win * w,
283 struct KeyBinding * kb_p)
287 for (y = start; (0 == state || 0 != kb_p); y++)
291 add_line(w, title, 0);
294 state = 1 + (0 == kb_p);
297 char * kb_line = get_kb_line_and_iterate(&kb_p);
298 add_line(w, kb_line, 0);
303 char * none = "(none)";
304 add_line(w, none, 0);
312 extern void draw_win_log(struct Win * win)
319 draw_text_from_bottom(win, log);
324 extern void draw_win_map(struct Win * win)
326 try_resize_winmap(win, world.map.size.y, world.map.size.x);
329 for (y = 0; y < world.map.size.y; y++)
331 for (x = 0; x < world.map.size.x; x++)
333 set_ch_on_yx(win, y, x, world.map.cells[z]);
341 extern void draw_win_info(struct Win * win)
343 char * dsc_turn = "Turn: ";
344 char * dsc_hitpoints = "\nHitpoints: ";
345 char * dsc_score = "\nScore: ";
346 uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
347 + 5 + 3 + 5; /* Max strlens of strings of numbers to use. */
349 sprintf(text, "%s%d%s%d%s%d",
350 dsc_turn, world.turn,
351 dsc_hitpoints, world.player_lifepoints,
352 dsc_score, world.score);
353 add_text_with_linebreaks(win, text);
358 extern void draw_win_inventory(struct Win * win)
360 win->center.y = world.player_inventory_select;
361 char inventory_copy[strlen(world.player_inventory) + 1];
362 sprintf(inventory_copy, "%s", world.player_inventory);
363 char * foo = inventory_copy;
367 char * object = strtok(foo, "\n");
374 if (i == world.player_inventory_select)
378 add_line(win, object, attri);
385 extern void draw_win_available_keybindings(struct Win * win)
387 char * title = "Active window's keybindings:";
388 struct KeyBinding * kb_p;
389 struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
394 else if (1 == wc->view)
396 kb_p = world.kb_wingeom.kbs;
398 else if (2 == wc->view)
400 kb_p = world.kb_winkeys.kbs;
402 uint16_t offset = draw_titled_keybinding_list(title, win, 0, kb_p);
403 add_line(win, " ", 0);
404 struct KeyBinding * kbs_glo = world.kb_global.kbs;
405 draw_titled_keybinding_list("Global keybindings", win, offset + 1, kbs_glo);
410 extern void draw_win_keybindings_global(struct Win * win)
412 win->center.y = world.kb_global.select;
413 draw_keybinding_config(win, &world.kb_global, 0);
418 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
420 win->center.y = world.kb_wingeom.select;
421 draw_keybinding_config(win, &world.kb_wingeom, 0);
426 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
428 win->center.y = world.kb_winkeys.select;
429 draw_keybinding_config(win, &world.kb_winkeys, 0);
434 extern void draw_winconf_keybindings(struct Win * win)
436 struct WinConf * wc = get_winconf_by_win(win);
437 char * title = "Window's keybindings:";
438 add_line(win, title, 0);
439 add_line(win, " ", 0);
440 draw_keybinding_config(win, &wc->kb, 2);
441 win->center.y = wc->kb.select + 2;
446 extern void draw_winconf_geometry(struct Win * win)
448 struct WinConf * wcp = get_winconf_by_win(win);
449 char * title = "Window's geometry:\n";
450 char * h_d = "\nHeight to save: ";
451 char * h_pos = " (width in cells)";
452 char * h_neg = " (negative diff: cells to screen width)";
453 char * w_d = "\n\nWidth to save: ";
454 char * w_pos = " (height in cells)";
455 char * w_neg = " (negative diff: cells to screen height)";
458 if (1 == wcp->height_type)
462 if (1 == wcp->width_type)
466 uint16_t maxl = strlen(title)
467 + strlen(h_t) + strlen(h_d) + 6 /* 6 = n of chars to */
468 + strlen(w_t) + strlen(w_d) + 6 + 1; /* write max int16_t */
470 sprintf(text, "%s%s%d%s%s%d%s", title, h_d, wcp->height, h_t,
471 w_d, wcp->width, w_t);
472 add_text_with_linebreaks(win, text);