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, get_map_object_def() */
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_object_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, world->map_objs, map, win);
345 if ( player->pos.y >= map->offset.y
346 && player->pos.y < map->offset.y + win->frame.size.y
347 && player->pos.x >= map->offset.x
348 && player->pos.x < map->offset.x + win->frame.size.x)
350 mvwaddch(win->frame.curses_win,
351 player->pos.y - map->offset.y, player->pos.x - map->offset.x,
358 extern void draw_win_info(struct Win * win)
360 struct World * world = (struct World *) win->data;
361 char * dsc_turn = "Turn: ";
362 char * dsc_hitpoints = "\nHitpoints: ";
363 char * dsc_score = "\nScore: ";
364 uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
365 + 10 + 5 + 10; /* max strlens of numbers to be used */
367 sprintf(text, "%s%d%s%d%s%d",
368 dsc_turn, world->turn,
369 dsc_hitpoints, world->player->hitpoints,
370 dsc_score, world->score);
371 draw_with_linebreaks(win, text, 0);
376 extern void draw_win_inventory(struct Win * win)
378 mvwaddstr(win->frame.curses_win, 0, 0, "(empty)");
383 extern void draw_win_available_keybindings(struct Win * win)
385 struct World * world = (struct World *) win->data;
386 char * title = "Active window's keybindings:";
387 struct KeyBinding * kb_p;
388 struct WinConf * wc = get_winconf_by_win(world, world->wmeta->active);
393 else if (1 == wc->view)
395 kb_p = world->kb_wingeom.kbs;
397 else if (2 == wc->view)
399 kb_p = world->kb_winkeys.kbs;
401 uint16_t offset = draw_titled_keybinding_list(world, win, 0, title, kb_p);
402 draw_titled_keybinding_list(world, win, offset + 1, "Global keybindings:",
403 world->kb_global.kbs);
408 extern void draw_win_keybindings_global(struct Win * win)
410 char * f_name = "draw_win_keybindings_global()";
411 struct World * world = (struct World *) win->data;
412 draw_kb_view(world, win, f_name, &world->kb_global, 0);
417 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
419 char * f_name = "draw_win_keybindings_winconf_geometry()";
420 struct World * world = (struct World *) win->data;
421 draw_kb_view(world, win, f_name, &world->kb_wingeom, 0);
426 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
428 char * f_name = "draw_win_keybindings_winconf_keybindings()";
429 struct World * world = (struct World *) win->data;
430 draw_kb_view(world, win, f_name, &world->kb_winkeys, 0);
435 extern void draw_winconf_keybindings(struct Win * win)
437 char * f_name = "draw_winconf_keybindings()";
438 struct World * world = (struct World *) win->data;
439 struct WinConf * wc = get_winconf_by_win(world, win);
440 char * title = "Window's keybindings:";
441 uint8_t title_space = strlen(title) / win->frame.size.x + 2;
442 mvwaddstr(win->frame.curses_win, 0, 0, title);
443 draw_kb_view(world, win, f_name, &wc->kb, title_space);
448 extern void draw_winconf_geometry(struct Win * win)
450 struct World * world = (struct World *) win->data;
451 struct WinConf * wcp = get_winconf_by_win(world, win);
452 char * title = "Window's geometry:\n";
453 char * h_d = "\nWidth to save: ";
454 char * h_pos = " (height in cells)";
455 char * h_neg = " (negative diff: cells to maximum height)";
456 char * w_d = "\n\nHeight to save: ";
457 char * w_pos = " (width in cells)";
458 char * w_neg = " (negative diff: cells to maximum width)";
461 if (1 == wcp->height_type)
465 if (1 == wcp->width_type)
469 uint16_t maxl = strlen(title)
470 + strlen(h_t) + strlen(h_d) + 6
471 + strlen(w_t) + strlen(w_d) + 6 + 1;
473 sprintf(text, "%s%s%d%s%s%d%s", title, h_d, wcp->height, h_t,
474 w_d, wcp->width, w_t);
475 draw_with_linebreaks(win, text, 0);