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, Player */
12 #include "map.h" /* for Map struct */
13 #include "main.h" /* for World struct */
14 #include "rexit.h" /* for err_exit() */
15 #include "command_db.h" /* for get_command_longdesc() */
16 #include "wincontrol.h" /* for WinConf struct, get_winconf_by_win() */
20 /* Write "text" into window "win" as far as possible. Start on row "start_y".
21 * Break lines at newlines.
23 static void draw_with_linebreaks(struct Win * win, char * text,
26 /* Write "line" into window "win" at line "y" as far as it fits into it; apply
27 * ncurses attribute "attri" to all characters drawn; if "fill" is non-zero,
28 * fill the entire line with empty characters ("attri" also applied on these).
30 static void draw_line(struct Win * win, uint16_t y, char * line, attr_t attri,
33 /* Write "text" not starting from the top but from the bottom of "win". */
34 static void draw_text_from_bottom(struct Win * win, char * text);
36 /* Draw onto "map" in "win" the objects in the chain at "start". */
37 static void draw_map_objects(struct World * world, struct MapObj * start,
38 struct Map * map, struct Win * win);
40 /* Return keybinding list line via "kb_pp", iterate pointer pointed to by it. */
41 static char * get_kb_line_and_iterate(struct World * world,
42 struct KeyBinding ** kb_pp);
44 /* Draw from line "start" on config view for keybindings defined at "kb". */
45 static void draw_kb_view(struct World * world, struct Win * win,
46 char * f_name, struct KeyBiData * kb, uint8_t start);
48 /* Draw into window "win" from line "start" on a "title" followed by an empty
49 * line followed by a list of all keybindings starting at kb_p.
51 static uint16_t draw_titled_keybinding_list(struct World * world,
52 struct Win * win, uint16_t start,
54 struct KeyBinding * kb_p);
61 static void draw_with_linebreaks(struct Win * win, char * text,
68 for (y = start_y; y < win->frame.size.y; y++)
74 for (x = 0; x < win->frame.size.x; x++)
86 mvwaddch(win->frame.curses_win, y, x, text[z]);
88 if ('\n' == text[z+1])
93 else if (0 == text[z+1])
105 static void draw_line(struct Win * win, uint16_t y, char * line, attr_t attri,
109 for (; x < win->frame.size.x && x < strlen(line); x++)
111 mvwaddch(win->frame.curses_win, y, x, line[x] | attri);
115 for (; x < win->frame.size.x; x++)
117 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);
124 static void draw_text_from_bottom (struct Win * win, char * text)
126 /* Determine number of lines text would have in a window of win's width,
127 * but infinite height. Treat \n and \0 as control chars for incrementing
128 * y and stopping the loop. Make sure +they* don't count as cell space.
131 uint16_t x, y, offset;
133 for (y = 0; 0 == toggle; y++)
135 for (x = 0; x < win->frame.size.x; x++)
142 if ('\n' == text[z+1])
147 else if (0 == text[z+1])
156 /* Depending on what's bigger, determine start point in window or text. */
157 uint16_t start_y = 0;
158 if (y < win->frame.size.y)
160 start_y = win->frame.size.y - y;
162 else if (y > win->frame.size.y)
164 offset = y - win->frame.size.y;
165 for (y = 0; y < offset; y++)
167 for (x = 0; x < win->frame.size.x; x++)
174 if ('\n' == text[z+1])
181 text = text + (sizeof(char) * (z + 1));
184 draw_with_linebreaks(win, text, start_y);
189 static void draw_map_objects(struct World * world, struct MapObj * start,
190 struct Map * map, struct Win * win)
193 struct MapObjDef * d;
195 for (o = start; o != 0; o = o->next)
197 if ( o->pos.y >= map->offset.y
198 && o->pos.y < map->offset.y + win->frame.size.y
199 && o->pos.x >= map->offset.x
200 && o->pos.x < map->offset.x + win->frame.size.x)
202 d = get_map_obj_def (world, o->type);
204 mvwaddch(win->frame.curses_win,
205 o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);
212 static char * get_kb_line_and_iterate(struct World * world,
213 struct KeyBinding ** kb_pp)
215 char * f_name = "get_kb_line_and_iterate()";
216 struct KeyBinding * kb_p = * kb_pp;
217 char * keyname = get_name_to_keycode(world, kb_p->key);
218 char * cmd_dsc = get_command_longdsc(world, kb_p->name);
219 uint16_t size = 9 + 1 + strlen(cmd_dsc) + 1;
220 char * line = try_malloc(size, world, f_name);
221 sprintf(line, "%-9s %s", keyname, cmd_dsc);
223 * kb_pp = kb_p->next;
229 static void draw_kb_view(struct World * world, struct Win * win,
230 char * f_name, struct KeyBiData * kb, uint8_t start)
234 draw_line(win, start, "(none)", 0, 0);
236 char * err_hint = trouble_msg(world, f_name, "draw_scroll_hint()");
237 uint16_t kb_max = get_n_of_keybs(kb->kbs) - 1;
239 offset = center_offset(kb->select, kb_max, win->frame.size.y - 1 - start);
240 uint16_t y_border = win->frame.size.y + offset - 1 - start;
241 struct KeyBinding * kb_p = get_keyb_of_n(kb->kbs, offset + (offset > 0));
242 for (y = start; 0 != kb_p && y < win->frame.size.y; y++)
244 if (start == y && offset > 0)
246 uint8_t test = draw_scroll_hint(&win->frame, y, offset + 1, '^');
247 exit_err(test, world, err_hint);
250 else if (win->frame.size.y == y + 1 && kb_max > y_border)
252 uint16_t pos = kb_max - (offset + win->frame.size.y) + 2 + start;
253 uint8_t test = draw_scroll_hint(&win->frame, y, pos, 'v');
254 exit_err(test, world, err_hint);
258 if (y - start == kb->select - offset)
263 attri = attri | A_BLINK;
266 char * kb_line = get_kb_line_and_iterate(world, &kb_p);
267 draw_line(win, y, kb_line, attri, 1);
275 static uint16_t draw_titled_keybinding_list(struct World * world,
276 struct Win * win, uint16_t start,
278 struct KeyBinding * kb_p)
283 for (y = start; y < win->frame.size.y && (0 == state || 0 != kb_p); y++)
287 for (x = 0; x < win->frame.size.x; x++)
289 if (i == strlen(title))
292 state = 1 + (0 == kb_p);
296 mvwaddch(win->frame.curses_win, y, x, title[i]);
301 char * kb_line = get_kb_line_and_iterate(world, &kb_p);
302 draw_line(win, y, kb_line, 0, 0);
307 draw_line(win, y, "(none)", 0, 0);
315 extern void draw_win_log(struct Win * win)
317 struct World * world = (struct World *) win->data;
318 draw_text_from_bottom(win, world->log);
323 extern void draw_win_map(struct Win * win)
325 struct World * world = (struct World *) win->data;
326 struct Map * map = world->map;
327 struct Player * player = world->player;
328 char * cells = map->cells;
329 uint16_t width_map_av = map->size.x - map->offset.x;
330 uint16_t height_map_av = map->size.y - map->offset.y;
332 for (y = 0; y < win->frame.size.y; y++)
334 z = map->offset.x + (map->offset.y + y) * (map->size.x);
335 for (x = 0; x < win->frame.size.x; x++)
337 if (y < height_map_av && x < width_map_av)
339 mvwaddch(win->frame.curses_win, y, x, cells[z]);
344 draw_map_objects (world, (struct MapObj *) world->item, map, win);
345 draw_map_objects (world, (struct MapObj *) world->monster, map, win);
346 if ( player->pos.y >= map->offset.y
347 && player->pos.y < map->offset.y + win->frame.size.y
348 && player->pos.x >= map->offset.x
349 && player->pos.x < map->offset.x + win->frame.size.x)
351 mvwaddch(win->frame.curses_win,
352 player->pos.y - map->offset.y, player->pos.x - map->offset.x,
359 extern void draw_win_info(struct Win * win)
361 struct World * world = (struct World *) win->data;
362 char * dsc_turn = "Turn: ";
363 char * dsc_hitpoints = "\nHitpoints: ";
364 char * dsc_score = "\nScore: ";
365 uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
366 + 10 + 5 + 10; /* max strlens of numbers to be used */
368 sprintf(text, "%s%d%s%d%s%d",
369 dsc_turn, world->turn,
370 dsc_hitpoints, world->player->hitpoints,
371 dsc_score, world->score);
372 draw_with_linebreaks(win, text, 0);
377 extern void draw_win_available_keybindings(struct Win * win)
379 struct World * world = (struct World *) win->data;
380 char * title = "Active window's keybindings:";
381 struct KeyBinding * kb_p;
382 struct WinConf * wc = get_winconf_by_win(world, world->wmeta->active);
387 else if (1 == wc->view)
389 kb_p = world->kb_wingeom.kbs;
391 else if (2 == wc->view)
393 kb_p = world->kb_winkeys.kbs;
395 uint16_t offset = draw_titled_keybinding_list(world, win, 0, title, kb_p);
396 draw_titled_keybinding_list(world, win, offset + 1, "Global keybindings:",
397 world->kb_global.kbs);
402 extern void draw_win_keybindings_global(struct Win * win)
404 char * f_name = "draw_win_keybindings_global()";
405 struct World * world = (struct World *) win->data;
406 draw_kb_view(world, win, f_name, &world->kb_global, 0);
411 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
413 char * f_name = "draw_win_keybindings_winconf_geometry()";
414 struct World * world = (struct World *) win->data;
415 draw_kb_view(world, win, f_name, &world->kb_wingeom, 0);
420 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
422 char * f_name = "draw_win_keybindings_winconf_keybindings()";
423 struct World * world = (struct World *) win->data;
424 draw_kb_view(world, win, f_name, &world->kb_winkeys, 0);
429 extern void draw_winconf_keybindings(struct Win * win)
431 char * f_name = "draw_winconf_keybindings()";
432 struct World * world = (struct World *) win->data;
433 struct WinConf * wc = get_winconf_by_win(world, win);
434 char * title = "Window's keybindings:";
435 uint8_t title_space = strlen(title) / win->frame.size.x + 2;
436 mvwaddstr(win->frame.curses_win, 0, 0, title);
437 draw_kb_view(world, win, f_name, &wc->kb, title_space);
442 extern void draw_winconf_geometry(struct Win * win)
444 struct World * world = (struct World *) win->data;
445 struct WinConf * wcp = get_winconf_by_win(world, win);
446 char * title = "Window's geometry:\n";
447 char * h_d = "\nWidth to save: ";
448 char * h_pos = " (height in cells)";
449 char * h_neg = " (negative diff: cells to maximum height)";
450 char * w_d = "\n\nHeight to save: ";
451 char * w_pos = " (width in cells)";
452 char * w_neg = " (negative diff: cells to maximum width)";
455 if (1 == wcp->height_type)
459 if (1 == wcp->width_type)
463 uint16_t maxl = strlen(title)
464 + strlen(h_t) + strlen(h_d) + 6
465 + strlen(w_t) + strlen(w_d) + 6 + 1;
467 sprintf(text, "%s%s%d%s%s%d%s", title, h_d, wcp->height, h_t,
468 w_d, wcp->width, w_t);
469 draw_with_linebreaks(win, text, 0);