4 #include <stdlib.h> /* for free() */
5 #include <stdint.h> /* for uint16_t */
6 #include <string.h> /* for strlen() */
7 #include <ncurses.h> /* for attri_t, chtype */
8 #include "windows.h" /* for struct Win */
9 #include "misc.h" /* for try_malloc() */
10 #include "keybindings.h" /* for struct KeyBinding, for get_name_to_keycode() */
11 #include "map_objects.h" /* for structs MapObj, get_map_object_def(),
14 #include "map.h" /* for Map struct */
15 #include "main.h" /* for world global */
16 #include "command_db.h" /* for get_command_longdesc() */
17 #include "wincontrol.h" /* for WinConf struct, get_winconf_by_win() */
21 /* Apply to the winmap of Win "w" the new sizes "new_size_y" and "new_size_x"
22 * to the degree that they extend it. Re-shape the window content accordingly.
24 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x);
26 /* In Win "w", write "ch" to coordinate "y"/"x". */
27 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch);
29 /* Add "text" into window "win". Break text at right window edge. Also break at
32 static void add_text_with_linebreaks(struct Win * win, char * text);
34 /* Add "line" into window "w". Apply ncurses attribute "attri" to all
35 * characters drawn. If "fill" is non-zero, fill the entire line until the
36 * right window edge with empty characters ("attri" also applied on these).
38 static void add_line(struct Win * w, char * line, attr_t attri, uint8_t fill);
40 /* Write "text" with add_text_with_linebreaks() as not starting from the top but
41 * from bottom of "win". Draw only what fits in window (avoid scroll hints).
43 static void draw_text_from_bottom(struct Win * win, char * text);
45 /* Draw onto "map" in "win" the objects in the chain at "start". */
46 static void draw_map_objects(struct MapObj * start, struct Map * map,
49 /* Return keybinding list line via "kb_pp", iterate pointer pointed to by it. */
50 static char * get_kb_line_and_iterate(struct KeyBinding ** kb_pp);
52 /* Draw from line "start" on config view for keybindings defined at "kb". */
53 static void draw_kb_view(struct Win * w, struct KeyBiData * kb, uint8_t start);
55 /* Draw into window "w" from line "start" on a "title" followed by an empty
56 * line followed by a list of all keybindings starting at kb_p.
58 static uint16_t draw_titled_keybinding_list(char * title, struct Win * w,
60 struct KeyBinding * kb_p);
64 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x)
66 char * f_name = "try_resize_winmap()";
67 if (w->winmapsize.y >= new_size_y && w->winmapsize.x >= new_size_x)
71 if (w->winmapsize.y > new_size_y)
73 new_size_y = w->winmapsize.y;
75 else if (w->winmapsize.x > new_size_x)
77 new_size_x = w->winmapsize.x;
79 chtype * old_winmap = w->winmap;
80 uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
81 w->winmap = try_malloc(new_size, f_name);
83 for (y = 0; y < new_size_y; y++)
85 for (x = 0; y < w->winmapsize.y && x < w->winmapsize.x; x++)
87 chtype ch = old_winmap[(y * w->winmapsize.x) + x];
88 w->winmap[(y * new_size_x) + x] = ch;
90 for (; x < new_size_x; x++)
92 w->winmap[(y * new_size_x) + x] = ' ';
96 w->winmapsize.y = new_size_y;
97 w->winmapsize.x = new_size_x;
102 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch)
104 w->winmap[(y * w->winmapsize.x) + x] = ch;
109 static void add_text_with_linebreaks(struct Win * win, char * text)
113 for (y = win->winmapsize.y; ; y++)
115 try_resize_winmap(win, y + 1, win->framesize.x);
116 for (x = 0; x < win->framesize.x; x++)
125 set_ch_on_yx(win, y, x, text[z]);
127 if ('\n' == text[z+1])
132 else if (0 == text[z+1])
142 static void add_line(struct Win * w, char * line, attr_t attri, uint8_t fill)
144 uint16_t y = w->winmapsize.y;
145 uint16_t len_line = strlen(line);
147 && w->winmapsize.x < w->framesize.x && w->framesize.x > len_line)
149 try_resize_winmap(w, y + 1, w->framesize.x);
153 try_resize_winmap(w, y + 1, strlen(line));
156 for (; x < len_line; x++)
158 set_ch_on_yx(w, y, x, line[x] | attri);
162 for (; x < w->framesize.x; x++)
164 set_ch_on_yx(w, y, x, ' ' | attri);
171 static void draw_text_from_bottom(struct Win * win, char * text)
173 /* Determine number of lines text would have in a window of win's width,
174 * but infinite height. Treat \n and \0 as control chars for incrementing
175 * y and stopping the loop. Make sure +they* don't count as cell space.
180 for (y = 0; 0 == toggle; y++)
182 for (x = 0; x < win->framesize.x; x++)
189 if ('\n' == text[z+1])
194 else if (0 == text[z+1])
203 /* Depending on what's bigger, determine start point in window or text. */
204 uint16_t start_y = 0;
205 if (y < win->framesize.y)
207 start_y = win->framesize.y - y;
209 else if (y > win->framesize.y)
211 uint16_t offset = y - win->framesize.y;
212 for (y = 0; y < offset; y++)
214 for (x = 0; x < win->framesize.x; x++)
221 if ('\n' == text[z+1])
228 text = text + (sizeof(char) * (z + 1));
231 try_resize_winmap(win, start_y, 1);
232 add_text_with_linebreaks(win, text);
237 static void draw_map_objects(struct MapObj * start, struct Map * map,
241 struct MapObjDef * d;
244 for (i = 0; i < 2; i++)
246 for (o = start; o != 0; o = o->next)
248 if (( (0 == i && 0 == o->lifepoints) /* Draw in-animate */
249 || (1 == i && 0 < o->lifepoints))) /* objects first. */
251 d = get_map_object_def(o->type);
253 set_ch_on_yx(win, o->pos.y, o->pos.x, c);
261 static char * get_kb_line_and_iterate(struct KeyBinding ** kb_pp)
263 char * f_name = "get_kb_line_and_iterate()";
264 struct KeyBinding * kb_p = * kb_pp;
265 char * keyname = get_name_to_keycode(kb_p->key);
266 char * cmd_dsc = get_command_longdsc(kb_p->name);
267 uint16_t size = 9 + 1 + strlen(cmd_dsc) + 1;
268 char * line = try_malloc(size, f_name);
269 sprintf(line, "%-9s %s", keyname, cmd_dsc);
271 * kb_pp = kb_p->next;
277 static void draw_kb_view(struct Win * w, struct KeyBiData * kb, uint8_t start)
281 add_line(w, "(none)", 0, 0);
284 struct KeyBinding * kb_p = kb->kbs;
286 for (y = start; 0 != kb_p; y++)
289 if (y - start == kb->select)
294 attri = attri | A_BLINK;
297 char * kb_line = get_kb_line_and_iterate(&kb_p);
298 add_line(w, kb_line, attri, 1);
305 static uint16_t draw_titled_keybinding_list(char * title, struct Win * w,
307 struct KeyBinding * kb_p)
311 for (y = start; (0 == state || 0 != kb_p); y++)
315 add_line(w, title, 0, 0);
317 add_line(w, " ", 0, 0);
318 state = 1 + (0 == kb_p);
321 char * kb_line = get_kb_line_and_iterate(&kb_p);
322 add_line(w, kb_line, 0, 0);
327 char * none = "(none)";
328 add_line(w, none, 0, 0);
336 extern void draw_win_log(struct Win * win)
338 draw_text_from_bottom(win, world.log);
343 extern void draw_win_map(struct Win * win)
345 struct Map * map = world.map;
346 char * cells = map->cells;
347 try_resize_winmap(win, map->size.y, map->size.x);
350 for (y = 0; y < map->size.y; y++)
352 for (x = 0; x < map->size.x; x++)
354 set_ch_on_yx(win, y, x, cells[z]);
358 draw_map_objects(world.map_objs, map, win);
363 extern void draw_win_info(struct Win * win)
365 char * dsc_turn = "Turn: ";
366 char * dsc_hitpoints = "\nHitpoints: ";
367 char * dsc_score = "\nScore: ";
368 uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
369 + 10 + 5 + 10; /* max strlens of numbers to be used */
371 struct MapObj * player = get_player();
372 sprintf(text, "%s%d%s%d%s%d",
373 dsc_turn, world.turn,
374 dsc_hitpoints, player->lifepoints,
375 dsc_score, world.score);
376 add_text_with_linebreaks(win, text);
381 extern void draw_win_inventory(struct Win * win)
383 struct MapObj * player = get_player();
384 if (NULL == player->owns)
386 add_line(win, "(none)", 0, 0);
389 win->center.y = world.inventory_select;
390 struct MapObj * owned = player->owns;
392 for (y = 0; NULL != owned; y++)
395 if (y == world.inventory_select)
399 struct MapObjDef * mod = get_map_object_def(owned->type);
400 add_line(win, mod->name, attri, 0);
407 extern void draw_win_available_keybindings(struct Win * win)
409 char * title = "Active window's keybindings:";
410 struct KeyBinding * kb_p;
411 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
416 else if (1 == wc->view)
418 kb_p = world.kb_wingeom.kbs;
420 else if (2 == wc->view)
422 kb_p = world.kb_winkeys.kbs;
424 uint16_t offset = draw_titled_keybinding_list(title, win, 0, kb_p);
425 add_line(win, " ", 0, 0);
426 struct KeyBinding * kbs_glo = world.kb_global.kbs;
427 draw_titled_keybinding_list("Global keybindings", win, offset + 1, kbs_glo);
432 extern void draw_win_keybindings_global(struct Win * win)
434 win->center.y = world.kb_global.select;
435 draw_kb_view(win, &world.kb_global, 0);
440 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
442 win->center.y = world.kb_wingeom.select;
443 draw_kb_view(win, &world.kb_wingeom, 0);
448 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
450 win->center.y = world.kb_winkeys.select;
451 draw_kb_view(win, &world.kb_winkeys, 0);
456 extern void draw_winconf_keybindings(struct Win * win)
458 struct WinConf * wc = get_winconf_by_win(win);
459 char * title = "Window's keybindings:";
460 add_line(win, title, 0, 0);
461 add_line(win, " ", 0, 0);
462 draw_kb_view(win, &wc->kb, 2);
463 win->center.y = wc->kb.select + 2;
468 extern void draw_winconf_geometry(struct Win * win)
470 struct WinConf * wcp = get_winconf_by_win(/*&world, */win);
471 char * title = "Window's geometry:\n";
472 char * h_d = "\nHeight to save: ";
473 char * h_pos = " (width in cells)";
474 char * h_neg = " (negative diff: cells to screen width)";
475 char * w_d = "\n\nWidth to save: ";
476 char * w_pos = " (height in cells)";
477 char * w_neg = " (negative diff: cells to screen height)";
480 if (1 == wcp->height_type)
484 if (1 == wcp->width_type)
488 uint16_t maxl = strlen(title)
489 + strlen(h_t) + strlen(h_d) + 6
490 + strlen(w_t) + strlen(w_d) + 6 + 1;
492 sprintf(text, "%s%s%d%s%s%d%s", title, h_d, wcp->height, h_t,
493 w_d, wcp->width, w_t);
494 add_text_with_linebreaks(win, text);