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() */
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. */
27 static void draw_line(struct Win * win, uint16_t y, char * line);
29 /* Write "text" not starting from the top but from the bottom of "win". */
30 static void draw_text_from_bottom(struct Win * win, char * text);
32 /* Draw onto "map" in "win" the objects in the chain at "start". */
33 static void draw_map_objects(struct World * world, struct MapObj * start,
34 struct Map * map, struct Win * win);
36 /* Draw from line "start" on config view for keybindings defined at "kb". */
37 static void draw_kb_view(struct World * world, struct Win * win,
38 char * f_name, struct KeyBiData * kb, uint8_t start);
40 /* Draw into window "win" from line "start" on a "title" followed by an empty
41 * line followed by a list of all keybindings starting at kb_p.
43 static uint16_t draw_titled_keybinding_list(struct World * world,
44 struct Win * win, uint16_t start,
46 struct KeyBinding * kb_p);
53 static void draw_with_linebreaks(struct Win * win, char * text,
60 for (y = start_y; y < win->frame.size.y; y++)
66 for (x = 0; x < win->frame.size.x; x++)
78 mvwaddch(win->frame.curses_win, y, x, text[z]);
80 if ('\n' == text[z+1])
85 else if (0 == text[z+1])
97 static void draw_line(struct Win * win, uint16_t y, char * line)
100 for (; x < win->frame.size.x && x < strlen(line); x++)
102 mvwaddch(win->frame.curses_win, y, x, line[x]);
108 static void draw_text_from_bottom (struct Win * win, char * text)
110 /* Determine number of lines text would have in a window of win's width,
111 * but infinite height. Treat \n and \0 as control chars for incrementing
112 * y and stopping the loop. Make sure +they* don't count as cell space.
115 uint16_t x, y, offset;
117 for (y = 0; 0 == toggle; y++)
119 for (x = 0; x < win->frame.size.x; x++)
126 if ('\n' == text[z+1])
131 else if (0 == text[z+1])
140 /* Depending on what's bigger, determine start point in window or text. */
141 uint16_t start_y = 0;
142 if (y < win->frame.size.y)
144 start_y = win->frame.size.y - y;
146 else if (y > win->frame.size.y)
148 offset = y - win->frame.size.y;
149 for (y = 0; y < offset; y++)
151 for (x = 0; x < win->frame.size.x; x++)
158 if ('\n' == text[z+1])
165 text = text + (sizeof(char) * (z + 1));
168 draw_with_linebreaks(win, text, start_y);
173 static void draw_map_objects(struct World * world, struct MapObj * start,
174 struct Map * map, struct Win * win)
177 struct MapObjDef * d;
179 for (o = start; o != 0; o = o->next)
181 if ( o->pos.y >= map->offset.y
182 && o->pos.y < map->offset.y + win->frame.size.y
183 && o->pos.x >= map->offset.x
184 && o->pos.x < map->offset.x + win->frame.size.x)
186 d = get_map_obj_def (world, o->type);
188 mvwaddch(win->frame.curses_win,
189 o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);
196 static void draw_kb_view(struct World * world, struct Win * win,
197 char * f_name, struct KeyBiData * kb, uint8_t start)
199 char * err_hint = trouble_msg(world, f_name, "draw_scroll_hint()");
200 uint16_t kb_max = get_n_of_keybs(kb->kbs) - 1;
201 uint16_t y, x, offset;
202 offset = center_offset(kb->select, kb_max, win->frame.size.y - 1 - start);
203 uint8_t keydescwidth = 9 + 1; /* get_name_to_keycode()'s max length + \0 */
204 char keydesc[keydescwidth];
205 uint16_t nav_max = kb_max + start;
206 uint16_t y_border = win->frame.size.y + offset - 1 - start;
207 for (y = start; y <= nav_max && y < win->frame.size.y; y++)
210 if (start == y && offset > 0)
212 uint8_t test = draw_scroll_hint(&win->frame, y, offset + 1, '^');
213 exit_err(test, world, err_hint);
216 else if (win->frame.size.y == y + 1 && kb_max > y_border)
218 uint16_t pos = kb_max - (offset + win->frame.size.y) + 2 + start;
219 uint8_t test = draw_scroll_hint(&win->frame, y, pos, 'v');
220 exit_err(test, world, err_hint);
225 if (y - start == kb->select - offset)
230 attri = attri | A_BLINK;
234 struct KeyBinding * kb_p;
235 kb_p = get_keyb_of_n(kb->kbs, (y - start) + offset);
236 char * keyname = get_name_to_keycode(world, kb_p->key);
237 snprintf(keydesc, keydescwidth, "%-9s", keyname);
239 char * cmd_dsc = get_command_longdsc(world, kb_p->name);
240 uint8_t dsclen = strlen(keydesc);
241 for (x = 0; x < win->frame.size.x; x++)
245 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
248 else if (dsclen < x && x < strlen(cmd_dsc) + strlen(keydesc) + 1)
250 chtype ch = cmd_dsc[x - strlen(keydesc) - 1] | attri;
251 mvwaddch(win->frame.curses_win, y, x, ch);
254 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);
263 static uint16_t draw_titled_keybinding_list(struct World * world,
264 struct Win * win, uint16_t start,
266 struct KeyBinding * kb_p)
271 for (y = start; y < win->frame.size.y && (0 == state || 0 != kb_p); y++)
275 for (x = 0; x < win->frame.size.x; x++)
277 if (i == strlen(title))
280 state = 1 + (0 == kb_p);
284 mvwaddch(win->frame.curses_win, y, x, title[i]);
289 char * keyname = get_name_to_keycode(world, kb_p->key);
290 char * cmd_dsc = get_command_longdsc(world, kb_p->name);
291 char line[9 + 1 + strlen(cmd_dsc) + 1];
292 sprintf(line, "%-9s %s", keyname, cmd_dsc);
295 draw_line(win, y, line);
299 char * line = "(none)";
300 draw_line(win, y, line);
308 extern void draw_win_log(struct Win * win)
310 struct World * world = (struct World *) win->data;
311 draw_text_from_bottom(win, world->log);
316 extern void draw_win_map(struct Win * win)
318 struct World * world = (struct World *) win->data;
319 struct Map * map = world->map;
320 struct Player * player = world->player;
321 char * cells = map->cells;
322 uint16_t width_map_av = map->size.x - map->offset.x;
323 uint16_t height_map_av = map->size.y - map->offset.y;
325 for (y = 0; y < win->frame.size.y; y++)
327 z = map->offset.x + (map->offset.y + y) * (map->size.x);
328 for (x = 0; x < win->frame.size.x; x++)
330 if (y < height_map_av && x < width_map_av)
332 mvwaddch(win->frame.curses_win, y, x, cells[z]);
337 draw_map_objects (world, (struct MapObj *) world->item, map, win);
338 draw_map_objects (world, (struct MapObj *) world->monster, map, win);
339 if ( player->pos.y >= map->offset.y
340 && player->pos.y < map->offset.y + win->frame.size.y
341 && player->pos.x >= map->offset.x
342 && player->pos.x < map->offset.x + win->frame.size.x)
344 mvwaddch(win->frame.curses_win,
345 player->pos.y - map->offset.y, player->pos.x - map->offset.x,
352 extern void draw_win_info(struct Win * win)
354 struct World * world = (struct World *) win->data;
355 char * dsc_turn = "Turn: ";
356 char * dsc_hitpoints = "\nHitpoints: ";
357 char * dsc_score = "\nScore: ";
358 uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
359 + 10 + 5 + 10; /* max strlens of numbers to be used */
361 sprintf(text, "%s%d%s%d%s%d",
362 dsc_turn, world->turn,
363 dsc_hitpoints, world->player->hitpoints,
364 dsc_score, world->score);
365 draw_with_linebreaks(win, text, 0);
370 extern void draw_win_available_keybindings(struct Win * win)
372 struct World * world = (struct World *) win->data;
373 char * title = "Active window's keybindings:";
374 struct KeyBinding * kb_p;
375 struct WinConf * wc = get_winconf_by_win(world, world->wmeta->active);
380 else if (1 == wc->view)
382 kb_p = world->kb_wingeom.kbs;
384 else if (2 == wc->view)
386 kb_p = world->kb_winkeys.kbs;
388 uint16_t offset = draw_titled_keybinding_list(world, win, 0, title, kb_p);
389 draw_titled_keybinding_list(world, win, offset + 1, "Global keybindings:",
390 world->kb_global.kbs);
395 extern void draw_win_keybindings_global(struct Win * win)
397 char * f_name = "draw_win_keybindings_global()";
398 struct World * world = (struct World *) win->data;
399 draw_kb_view(world, win, f_name, &world->kb_global, 0);
404 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
406 char * f_name = "draw_win_keybindings_winconf_geometry()";
407 struct World * world = (struct World *) win->data;
408 draw_kb_view(world, win, f_name, &world->kb_wingeom, 0);
413 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
415 char * f_name = "draw_win_keybindings_winconf_keybindings()";
416 struct World * world = (struct World *) win->data;
417 draw_kb_view(world, win, f_name, &world->kb_winkeys, 0);
422 extern void draw_winconf_keybindings(struct Win * win)
424 char * f_name = "draw_winconf_keybindings()";
425 struct World * world = (struct World *) win->data;
426 struct WinConf * wc = get_winconf_by_win(world, win);
427 char * title = "Window's keybindings:";
428 uint8_t title_space = strlen(title) / win->frame.size.x + 2;
429 mvwaddstr(win->frame.curses_win, 0, 0, title);
430 draw_kb_view(world, win, f_name, &wc->kb, title_space);
435 extern void draw_winconf_geometry(struct Win * win)
437 struct World * world = (struct World *) win->data;
438 struct WinConf * wcp = get_winconf_by_win(world, win);
439 char * title = "Window's geometry:\n";
440 char * h_d = "\nWidth to save: ";
441 char * h_pos = " (height in cells)";
442 char * h_neg = " (negative diff: cells to maximum height)";
443 char * w_d = "\n\nHeight to save: ";
444 char * w_pos = " (width in cells)";
445 char * w_neg = " (negative diff: cells to maximum width)";
448 if (1 == wcp->height_type)
452 if (1 == wcp->width_type)
456 uint16_t maxl = strlen(title)
457 + strlen(h_t) + strlen(h_d) + 6
458 + strlen(w_t) + strlen(w_d) + 6 + 1;
460 sprintf(text, "%s%s%d%s%s%d%s", title, h_d, wcp->height, h_t,
461 w_d, wcp->width, w_t);
462 draw_with_linebreaks(win, text, 0);