4 #include <stdlib.h> /* for free() */
5 #include <stdint.h> /* for uint16_t */
6 #include <string.h> /* for strlen() */
7 #include <ncurses.h> /* for mvwaddch() */
8 #include "windows.h" /* for structs Win, Frame, for draw_scroll_hint() */
9 #include "misc.h" /* for center_offset(), 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 struct */
16 #include "rexit.h" /* for err_exit() */
17 #include "command_db.h" /* for get_command_longdesc() */
18 #include "wincontrol.h" /* for WinConf struct, get_winconf_by_win() */
22 /* Write "text" into window "win" as far as possible. Start on row "start_y".
23 * Break lines at newlines.
25 static void draw_with_linebreaks(struct Win * win, char * text,
28 /* Write "line" into window "win" at line "y" as far as it fits into it; apply
29 * ncurses attribute "attri" to all characters drawn; if "fill" is non-zero,
30 * fill the entire line with empty characters ("attri" also applied on these).
32 static void draw_line(struct Win * win, uint16_t y, char * line, attr_t attri,
35 /* Write "text" not starting from the top but from the bottom of "win". */
36 static void draw_text_from_bottom(struct Win * win, char * text);
38 /* Draw onto "map" in "win" the objects in the chain at "start". */
39 static void draw_map_objects(struct World * world, struct MapObj * start,
40 struct Map * map, struct Win * win);
42 /* Return keybinding list line via "kb_pp", iterate pointer pointed to by it. */
43 static char * get_kb_line_and_iterate(struct World * world,
44 struct KeyBinding ** kb_pp);
46 /* Draw horizontal scroll hints in "frame" at the end line or a start line of y
47 * value "start" if the current "y" fits one of these lines and the number of
48 * lines "n_owned" and the "offset" make it appropriate.
50 * Return 1 if a scroll hint was drawn, else 0.
52 static uint8_t scroll_hint_helper(struct World * world, uint16_t start,
53 uint16_t y, uint16_t offset, uint16_t n_owned,
54 struct Frame * frame, char * f_name);
56 /* Draw from line "start" on config view for keybindings defined at "kb". */
57 static void draw_kb_view(struct World * world, struct Win * win,
58 char * f_name, struct KeyBiData * kb, uint8_t start);
60 /* Draw into window "win" from line "start" on a "title" followed by an empty
61 * line followed by a list of all keybindings starting at kb_p.
63 static uint16_t draw_titled_keybinding_list(struct World * world,
64 struct Win * win, uint16_t start,
66 struct KeyBinding * kb_p);
73 static void draw_with_linebreaks(struct Win * win, char * text,
80 for (y = start_y; y < win->frame.size.y; y++)
86 for (x = 0; x < win->frame.size.x; x++)
98 mvwaddch(win->frame.curses_win, y, x, text[z]);
100 if ('\n' == text[z+1])
105 else if (0 == text[z+1])
117 static void draw_line(struct Win * win, uint16_t y, char * line, attr_t attri,
121 for (; x < win->frame.size.x && x < strlen(line); x++)
123 mvwaddch(win->frame.curses_win, y, x, line[x] | attri);
127 for (; x < win->frame.size.x; x++)
129 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);
136 static void draw_text_from_bottom (struct Win * win, char * text)
138 /* Determine number of lines text would have in a window of win's width,
139 * but infinite height. Treat \n and \0 as control chars for incrementing
140 * y and stopping the loop. Make sure +they* don't count as cell space.
143 uint16_t x, y, offset;
145 for (y = 0; 0 == toggle; y++)
147 for (x = 0; x < win->frame.size.x; x++)
154 if ('\n' == text[z+1])
159 else if (0 == text[z+1])
168 /* Depending on what's bigger, determine start point in window or text. */
169 uint16_t start_y = 0;
170 if (y < win->frame.size.y)
172 start_y = win->frame.size.y - y;
174 else if (y > win->frame.size.y)
176 offset = y - win->frame.size.y;
177 for (y = 0; y < offset; y++)
179 for (x = 0; x < win->frame.size.x; x++)
186 if ('\n' == text[z+1])
193 text = text + (sizeof(char) * (z + 1));
196 draw_with_linebreaks(win, text, start_y);
201 static void draw_map_objects(struct World * world, struct MapObj * start,
202 struct Map * map, struct Win * win)
205 struct MapObjDef * d;
208 for (i = 0; i < 2; i++)
210 for (o = start; o != 0; o = o->next)
212 if ( ( (0 == i && 0 == o->lifepoints) /* Draw in-animate */
213 || (1 == i && 0 < o->lifepoints)) /* objects first. */
214 && o->pos.y >= map->offset.y
215 && o->pos.y < map->offset.y + win->frame.size.y
216 && o->pos.x >= map->offset.x
217 && o->pos.x < map->offset.x + win->frame.size.x)
219 d = get_map_object_def(world, o->type);
221 mvwaddch(win->frame.curses_win,
222 o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);
230 static char * get_kb_line_and_iterate(struct World * world,
231 struct KeyBinding ** kb_pp)
233 char * f_name = "get_kb_line_and_iterate()";
234 struct KeyBinding * kb_p = * kb_pp;
235 char * keyname = get_name_to_keycode(world, kb_p->key);
236 char * cmd_dsc = get_command_longdsc(world, kb_p->name);
237 uint16_t size = 9 + 1 + strlen(cmd_dsc) + 1;
238 char * line = try_malloc(size, world, f_name);
239 sprintf(line, "%-9s %s", keyname, cmd_dsc);
241 * kb_pp = kb_p->next;
247 static uint8_t scroll_hint_helper(struct World * world, uint16_t start,
248 uint16_t y, uint16_t offset, uint16_t n_owned,
249 struct Frame * frame, char * f_name)
252 char * err_hint = trouble_msg(world, f_name, "draw_scroll_hint()");
253 if (start == y && offset > 0)
255 uint8_t test = draw_scroll_hint(frame, y, offset + 1, '^');
256 exit_err(test, world, err_hint);
259 else if ( frame->size.y == y + 1
260 && n_owned > frame->size.y + offset - 1 - start)
262 uint8_t pos = n_owned - (offset + frame->size.y) + 2 + start;
263 uint8_t test = draw_scroll_hint(frame, y, pos, 'v');
264 exit_err(test, world, err_hint);
273 static void draw_kb_view(struct World * world, struct Win * win,
274 char * f_name, struct KeyBiData * kb, uint8_t start)
278 draw_line(win, start, "(none)", 0, 0);
281 uint16_t kb_max = get_n_of_keybs(kb->kbs) - 1;
283 offset = center_offset(kb->select, kb_max, win->frame.size.y - 1 - start);
284 struct KeyBinding * kb_p = get_keyb_of_n(kb->kbs, offset + (offset > 0));
286 for (y = start; 0 != kb_p && y < win->frame.size.y; y++)
288 if (scroll_hint_helper(world, start, y, offset, kb_max, &win->frame,
294 if (y - start == kb->select - offset)
299 attri = attri | A_BLINK;
302 char * kb_line = get_kb_line_and_iterate(world, &kb_p);
303 draw_line(win, y, kb_line, attri, 1);
311 static uint16_t draw_titled_keybinding_list(struct World * world,
312 struct Win * win, uint16_t start,
314 struct KeyBinding * kb_p)
319 for (y = start; y < win->frame.size.y && (0 == state || 0 != kb_p); y++)
323 for (x = 0; x < win->frame.size.x; x++)
325 if (i == strlen(title))
328 state = 1 + (0 == kb_p);
332 mvwaddch(win->frame.curses_win, y, x, title[i]);
337 char * kb_line = get_kb_line_and_iterate(world, &kb_p);
338 draw_line(win, y, kb_line, 0, 0);
343 draw_line(win, y, "(none)", 0, 0);
351 extern void draw_win_log(struct Win * win)
353 struct World * world = (struct World *) win->data;
354 draw_text_from_bottom(win, world->log);
359 extern void draw_win_map(struct Win * win)
361 struct World * world = (struct World *) win->data;
362 struct Map * map = world->map;
363 char * cells = map->cells;
364 uint16_t width_map_av = map->size.x - map->offset.x;
365 uint16_t height_map_av = map->size.y - map->offset.y;
367 for (y = 0; y < win->frame.size.y; y++)
369 z = map->offset.x + (map->offset.y + y) * (map->size.x);
370 for (x = 0; x < win->frame.size.x; x++)
372 if (y < height_map_av && x < width_map_av)
374 mvwaddch(win->frame.curses_win, y, x, cells[z]);
379 draw_map_objects(world, world->map_objs, map, win);
384 extern void draw_win_info(struct Win * win)
386 struct World * world = (struct World *) win->data;
387 char * dsc_turn = "Turn: ";
388 char * dsc_hitpoints = "\nHitpoints: ";
389 char * dsc_score = "\nScore: ";
390 uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
391 + 10 + 5 + 10; /* max strlens of numbers to be used */
393 struct MapObj * player = get_player(world);
394 sprintf(text, "%s%d%s%d%s%d",
395 dsc_turn, world->turn,
396 dsc_hitpoints, player->lifepoints,
397 dsc_score, world->score);
398 draw_with_linebreaks(win, text, 0);
403 extern void draw_win_inventory(struct Win * win)
405 struct World * world = (struct World *) win->data;
406 struct MapObj * player = get_player(world);
407 if (NULL == player->owns)
409 mvwaddstr(win->frame.curses_win, 0, 0, "(empty)");
412 char * f_name = "draw_win_inventory()";
413 struct MapObj * owned = player->owns;
415 for (n_owned = 0; NULL != owned->next; owned = owned->next, n_owned++);
416 uint8_t offset = center_offset(world->inventory_select, n_owned,
417 win->frame.size.y - 1);
419 for (i = 0, owned = player->owns; i < offset + (offset > 0);
420 i++, owned = owned->next);
422 for (y = 0; NULL != owned && y < win->frame.size.y; y++)
424 if (scroll_hint_helper(world, 0, y, offset, n_owned, &win->frame,
430 if (y == world->inventory_select - offset)
434 struct MapObjDef * mod = get_map_object_def(world, owned->type);
435 draw_line(win, y, mod->name, attri, 0);
442 extern void draw_win_available_keybindings(struct Win * win)
444 struct World * world = (struct World *) win->data;
445 char * title = "Active window's keybindings:";
446 struct KeyBinding * kb_p;
447 struct WinConf * wc = get_winconf_by_win(world, world->wmeta->active);
452 else if (1 == wc->view)
454 kb_p = world->kb_wingeom.kbs;
456 else if (2 == wc->view)
458 kb_p = world->kb_winkeys.kbs;
460 uint16_t offset = draw_titled_keybinding_list(world, win, 0, title, kb_p);
461 draw_titled_keybinding_list(world, win, offset + 1, "Global keybindings:",
462 world->kb_global.kbs);
467 extern void draw_win_keybindings_global(struct Win * win)
469 char * f_name = "draw_win_keybindings_global()";
470 struct World * world = (struct World *) win->data;
471 draw_kb_view(world, win, f_name, &world->kb_global, 0);
476 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
478 char * f_name = "draw_win_keybindings_winconf_geometry()";
479 struct World * world = (struct World *) win->data;
480 draw_kb_view(world, win, f_name, &world->kb_wingeom, 0);
485 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
487 char * f_name = "draw_win_keybindings_winconf_keybindings()";
488 struct World * world = (struct World *) win->data;
489 draw_kb_view(world, win, f_name, &world->kb_winkeys, 0);
494 extern void draw_winconf_keybindings(struct Win * win)
496 char * f_name = "draw_winconf_keybindings()";
497 struct World * world = (struct World *) win->data;
498 struct WinConf * wc = get_winconf_by_win(world, win);
499 char * title = "Window's keybindings:";
500 uint8_t title_space = strlen(title) / win->frame.size.x + 2;
501 mvwaddstr(win->frame.curses_win, 0, 0, title);
502 draw_kb_view(world, win, f_name, &wc->kb, title_space);
507 extern void draw_winconf_geometry(struct Win * win)
509 struct World * world = (struct World *) win->data;
510 struct WinConf * wcp = get_winconf_by_win(world, win);
511 char * title = "Window's geometry:\n";
512 char * h_d = "\nHeight to save: ";
513 char * h_pos = " (width in cells)";
514 char * h_neg = " (negative diff: cells to maximum width)";
515 char * w_d = "\n\nWidth to save: ";
516 char * w_pos = " (height in cells)";
517 char * w_neg = " (negative diff: cells to maximum height)";
520 if (1 == wcp->height_type)
524 if (1 == wcp->width_type)
528 uint16_t maxl = strlen(title)
529 + strlen(h_t) + strlen(h_d) + 6
530 + strlen(w_t) + strlen(w_d) + 6 + 1;
532 sprintf(text, "%s%s%d%s%s%d%s", title, h_d, wcp->height, h_t,
533 w_d, wcp->width, w_t);
534 draw_with_linebreaks(win, text, 0);