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 from line "start" on config view for keybindings defined at "kb". */
47 static void draw_kb_view(struct World * world, struct Win * win,
48 char * f_name, struct KeyBiData * kb, uint8_t start);
50 /* Draw into window "win" from line "start" on a "title" followed by an empty
51 * line followed by a list of all keybindings starting at kb_p.
53 static uint16_t draw_titled_keybinding_list(struct World * world,
54 struct Win * win, uint16_t start,
56 struct KeyBinding * kb_p);
63 static void draw_with_linebreaks(struct Win * win, char * text,
70 for (y = start_y; y < win->frame.size.y; y++)
76 for (x = 0; x < win->frame.size.x; x++)
88 mvwaddch(win->frame.curses_win, y, x, text[z]);
90 if ('\n' == text[z+1])
95 else if (0 == text[z+1])
107 static void draw_line(struct Win * win, uint16_t y, char * line, attr_t attri,
111 for (; x < win->frame.size.x && x < strlen(line); x++)
113 mvwaddch(win->frame.curses_win, y, x, line[x] | attri);
117 for (; x < win->frame.size.x; x++)
119 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);
126 static void draw_text_from_bottom (struct Win * win, char * text)
128 /* Determine number of lines text would have in a window of win's width,
129 * but infinite height. Treat \n and \0 as control chars for incrementing
130 * y and stopping the loop. Make sure +they* don't count as cell space.
133 uint16_t x, y, offset;
135 for (y = 0; 0 == toggle; y++)
137 for (x = 0; x < win->frame.size.x; x++)
144 if ('\n' == text[z+1])
149 else if (0 == text[z+1])
158 /* Depending on what's bigger, determine start point in window or text. */
159 uint16_t start_y = 0;
160 if (y < win->frame.size.y)
162 start_y = win->frame.size.y - y;
164 else if (y > win->frame.size.y)
166 offset = y - win->frame.size.y;
167 for (y = 0; y < offset; y++)
169 for (x = 0; x < win->frame.size.x; x++)
176 if ('\n' == text[z+1])
183 text = text + (sizeof(char) * (z + 1));
186 draw_with_linebreaks(win, text, start_y);
191 static void draw_map_objects(struct World * world, struct MapObj * start,
192 struct Map * map, struct Win * win)
195 struct MapObjDef * d;
198 for (i = 0; i < 2; i++)
200 for (o = start; o != 0; o = o->next)
202 if ( ( (0 == i && 0 == o->lifepoints) /* Draw in-animate */
203 || (1 == i && 0 < o->lifepoints)) /* objects first. */
204 && o->pos.y >= map->offset.y
205 && o->pos.y < map->offset.y + win->frame.size.y
206 && o->pos.x >= map->offset.x
207 && o->pos.x < map->offset.x + win->frame.size.x)
209 d = get_map_object_def(world, o->type);
211 mvwaddch(win->frame.curses_win,
212 o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);
220 static char * get_kb_line_and_iterate(struct World * world,
221 struct KeyBinding ** kb_pp)
223 char * f_name = "get_kb_line_and_iterate()";
224 struct KeyBinding * kb_p = * kb_pp;
225 char * keyname = get_name_to_keycode(world, kb_p->key);
226 char * cmd_dsc = get_command_longdsc(world, kb_p->name);
227 uint16_t size = 9 + 1 + strlen(cmd_dsc) + 1;
228 char * line = try_malloc(size, world, f_name);
229 sprintf(line, "%-9s %s", keyname, cmd_dsc);
231 * kb_pp = kb_p->next;
237 static void draw_kb_view(struct World * world, struct Win * win,
238 char * f_name, struct KeyBiData * kb, uint8_t start)
242 draw_line(win, start, "(none)", 0, 0);
244 char * err_hint = trouble_msg(world, f_name, "draw_scroll_hint()");
245 uint16_t kb_max = get_n_of_keybs(kb->kbs) - 1;
247 offset = center_offset(kb->select, kb_max, win->frame.size.y - 1 - start);
248 uint16_t y_border = win->frame.size.y + offset - 1 - start;
249 struct KeyBinding * kb_p = get_keyb_of_n(kb->kbs, offset + (offset > 0));
250 for (y = start; 0 != kb_p && y < win->frame.size.y; y++)
252 if (start == y && offset > 0)
254 uint8_t test = draw_scroll_hint(&win->frame, y, offset + 1, '^');
255 exit_err(test, world, err_hint);
258 else if (win->frame.size.y == y + 1 && kb_max > y_border)
260 uint16_t pos = kb_max - (offset + win->frame.size.y) + 2 + start;
261 uint8_t test = draw_scroll_hint(&win->frame, y, pos, 'v');
262 exit_err(test, world, err_hint);
266 if (y - start == kb->select - offset)
271 attri = attri | A_BLINK;
274 char * kb_line = get_kb_line_and_iterate(world, &kb_p);
275 draw_line(win, y, kb_line, attri, 1);
283 static uint16_t draw_titled_keybinding_list(struct World * world,
284 struct Win * win, uint16_t start,
286 struct KeyBinding * kb_p)
291 for (y = start; y < win->frame.size.y && (0 == state || 0 != kb_p); y++)
295 for (x = 0; x < win->frame.size.x; x++)
297 if (i == strlen(title))
300 state = 1 + (0 == kb_p);
304 mvwaddch(win->frame.curses_win, y, x, title[i]);
309 char * kb_line = get_kb_line_and_iterate(world, &kb_p);
310 draw_line(win, y, kb_line, 0, 0);
315 draw_line(win, y, "(none)", 0, 0);
323 extern void draw_win_log(struct Win * win)
325 struct World * world = (struct World *) win->data;
326 draw_text_from_bottom(win, world->log);
331 extern void draw_win_map(struct Win * win)
333 struct World * world = (struct World *) win->data;
334 struct Map * map = world->map;
335 char * cells = map->cells;
336 uint16_t width_map_av = map->size.x - map->offset.x;
337 uint16_t height_map_av = map->size.y - map->offset.y;
339 for (y = 0; y < win->frame.size.y; y++)
341 z = map->offset.x + (map->offset.y + y) * (map->size.x);
342 for (x = 0; x < win->frame.size.x; x++)
344 if (y < height_map_av && x < width_map_av)
346 mvwaddch(win->frame.curses_win, y, x, cells[z]);
351 draw_map_objects(world, world->map_objs, map, win);
356 extern void draw_win_info(struct Win * win)
358 struct World * world = (struct World *) win->data;
359 char * dsc_turn = "Turn: ";
360 char * dsc_hitpoints = "\nHitpoints: ";
361 char * dsc_score = "\nScore: ";
362 uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
363 + 10 + 5 + 10; /* max strlens of numbers to be used */
365 struct MapObj * player = get_player(world);
366 sprintf(text, "%s%d%s%d%s%d",
367 dsc_turn, world->turn,
368 dsc_hitpoints, player->lifepoints,
369 dsc_score, world->score);
370 draw_with_linebreaks(win, text, 0);
375 extern void draw_win_inventory(struct Win * win)
377 mvwaddstr(win->frame.curses_win, 0, 0, "(empty)");
382 extern void draw_win_available_keybindings(struct Win * win)
384 struct World * world = (struct World *) win->data;
385 char * title = "Active window's keybindings:";
386 struct KeyBinding * kb_p;
387 struct WinConf * wc = get_winconf_by_win(world, world->wmeta->active);
392 else if (1 == wc->view)
394 kb_p = world->kb_wingeom.kbs;
396 else if (2 == wc->view)
398 kb_p = world->kb_winkeys.kbs;
400 uint16_t offset = draw_titled_keybinding_list(world, win, 0, title, kb_p);
401 draw_titled_keybinding_list(world, win, offset + 1, "Global keybindings:",
402 world->kb_global.kbs);
407 extern void draw_win_keybindings_global(struct Win * win)
409 char * f_name = "draw_win_keybindings_global()";
410 struct World * world = (struct World *) win->data;
411 draw_kb_view(world, win, f_name, &world->kb_global, 0);
416 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
418 char * f_name = "draw_win_keybindings_winconf_geometry()";
419 struct World * world = (struct World *) win->data;
420 draw_kb_view(world, win, f_name, &world->kb_wingeom, 0);
425 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
427 char * f_name = "draw_win_keybindings_winconf_keybindings()";
428 struct World * world = (struct World *) win->data;
429 draw_kb_view(world, win, f_name, &world->kb_winkeys, 0);
434 extern void draw_winconf_keybindings(struct Win * win)
436 char * f_name = "draw_winconf_keybindings()";
437 struct World * world = (struct World *) win->data;
438 struct WinConf * wc = get_winconf_by_win(world, win);
439 char * title = "Window's keybindings:";
440 uint8_t title_space = strlen(title) / win->frame.size.x + 2;
441 mvwaddstr(win->frame.curses_win, 0, 0, title);
442 draw_kb_view(world, win, f_name, &wc->kb, title_space);
447 extern void draw_winconf_geometry(struct Win * win)
449 struct World * world = (struct World *) win->data;
450 struct WinConf * wcp = get_winconf_by_win(world, win);
451 char * title = "Window's geometry:\n";
452 char * h_d = "\nWidth to save: ";
453 char * h_pos = " (height in cells)";
454 char * h_neg = " (negative diff: cells to maximum height)";
455 char * w_d = "\n\nHeight to save: ";
456 char * w_pos = " (width in cells)";
457 char * w_neg = " (negative diff: cells to maximum width)";
460 if (1 == wcp->height_type)
464 if (1 == wcp->width_type)
468 uint16_t maxl = strlen(title)
469 + strlen(h_t) + strlen(h_d) + 6
470 + strlen(w_t) + strlen(w_d) + 6 + 1;
472 sprintf(text, "%s%s%d%s%s%d%s", title, h_d, wcp->height, h_t,
473 w_d, wcp->width, w_t);
474 draw_with_linebreaks(win, text, 0);