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 static void draw_with_linebreaks(struct Win * win, char * text,
24 /* Write "text" not starting from the top but from the bottom of "win". */
25 static void draw_text_from_bottom(struct Win * win, char * text);
27 /* Draw onto "map" in "win" the objects in the chain at "start". */
28 static void draw_map_objects(struct World * world, struct MapObj * start,
29 struct Map * map, struct Win * win);
31 /* Draw from line "start" on config view for keybindings defined at "kb". */
32 static void draw_kb_view(struct World * world, struct Win * win,
33 char * f_name, struct KeyBiData * kb, uint8_t start);
37 static void draw_with_linebreaks(struct Win * win, char * text,
44 for (y = start_y; y < win->frame.size.y; y++)
50 for (x = 0; x < win->frame.size.x; x++)
62 mvwaddch(win->frame.curses_win, y, x, text[z]);
64 if ('\n' == text[z+1])
69 else if (0 == text[z+1])
81 static void draw_text_from_bottom (struct Win * win, char * text)
83 /* Determine number of lines text would have in a window of win's width,
84 * but infinite height. Treat \n and \0 as control chars for incrementing
85 * y and stopping the loop. Make sure +they* don't count as cell space.
88 uint16_t x, y, offset;
90 for (y = 0; 0 == toggle; y++)
92 for (x = 0; x < win->frame.size.x; x++)
99 if ('\n' == text[z+1])
104 else if (0 == text[z+1])
113 /* Depending on what's bigger, determine start point in window or text. */
114 uint16_t start_y = 0;
115 if (y < win->frame.size.y)
117 start_y = win->frame.size.y - y;
119 else if (y > win->frame.size.y)
121 offset = y - win->frame.size.y;
122 for (y = 0; y < offset; y++)
124 for (x = 0; x < win->frame.size.x; x++)
131 if ('\n' == text[z+1])
138 text = text + (sizeof(char) * (z + 1));
141 draw_with_linebreaks(win, text, start_y);
146 static void draw_map_objects(struct World * world, struct MapObj * start,
147 struct Map * map, struct Win * win)
150 struct MapObjDef * d;
152 for (o = start; o != 0; o = o->next)
154 if ( o->pos.y >= map->offset.y
155 && o->pos.y < map->offset.y + win->frame.size.y
156 && o->pos.x >= map->offset.x
157 && o->pos.x < map->offset.x + win->frame.size.x)
159 d = get_map_obj_def (world, o->type);
161 mvwaddch(win->frame.curses_win,
162 o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);
169 static void draw_kb_view(struct World * world, struct Win * win,
170 char * f_name, struct KeyBiData * kb, uint8_t start)
172 char * err_hint = trouble_msg(world, f_name, "draw_scroll_hint()");
173 uint16_t kb_max = get_n_of_keybs(kb->kbs) - 1;
174 uint16_t y, x, offset;
175 offset = center_offset(kb->select, kb_max, win->frame.size.y - 1 - start);
176 uint8_t keydescwidth = 9 + 1; /* get_name_to_keycode()'s max length + \0 */
177 char keydesc[keydescwidth];
178 uint16_t nav_max = kb_max + start;
179 uint16_t y_border = win->frame.size.y + offset - 1 - start;
180 for (y = start; y <= nav_max && y < win->frame.size.y; y++)
183 if (start == y && offset > 0)
185 uint8_t test = draw_scroll_hint(&win->frame, y, offset + 1, '^');
186 exit_err(test, world, err_hint);
189 else if (win->frame.size.y == y + 1 && kb_max > y_border)
191 uint16_t pos = kb_max - (offset + win->frame.size.y) + 2 + start;
192 uint8_t test = draw_scroll_hint(&win->frame, y, pos, 'v');
193 exit_err(test, world, err_hint);
198 if (y - start == kb->select - offset)
203 attri = attri | A_BLINK;
207 struct KeyBinding * kb_p;
208 kb_p = get_keyb_of_n(kb->kbs, (y - start) + offset);
209 char * keyname = get_name_to_keycode(world, kb_p->key);
210 snprintf(keydesc, keydescwidth, "%-9s", keyname);
212 char * cmd_dsc = get_command_longdsc(world, kb_p->name);
213 uint8_t dsclen = strlen(keydesc);
214 for (x = 0; x < win->frame.size.x; x++)
218 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
221 else if (dsclen < x && x < strlen(cmd_dsc) + strlen(keydesc) + 1)
223 chtype ch = cmd_dsc[x - strlen(keydesc) - 1] | attri;
224 mvwaddch(win->frame.curses_win, y, x, ch);
227 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);
236 extern void draw_win_log(struct Win * win)
238 struct World * world = (struct World *) win->data;
239 draw_text_from_bottom(win, world->log);
244 extern void draw_win_map(struct Win * win)
246 struct World * world = (struct World *) win->data;
247 struct Map * map = world->map;
248 struct Player * player = world->player;
249 char * cells = map->cells;
250 uint16_t width_map_av = map->size.x - map->offset.x;
251 uint16_t height_map_av = map->size.y - map->offset.y;
253 for (y = 0; y < win->frame.size.y; y++)
255 z = map->offset.x + (map->offset.y + y) * (map->size.x);
256 for (x = 0; x < win->frame.size.x; x++)
258 if (y < height_map_av && x < width_map_av)
260 mvwaddch(win->frame.curses_win, y, x, cells[z]);
265 draw_map_objects (world, (struct MapObj *) world->item, map, win);
266 draw_map_objects (world, (struct MapObj *) world->monster, map, win);
267 if ( player->pos.y >= map->offset.y
268 && player->pos.y < map->offset.y + win->frame.size.y
269 && player->pos.x >= map->offset.x
270 && player->pos.x < map->offset.x + win->frame.size.x)
272 mvwaddch(win->frame.curses_win,
273 player->pos.y - map->offset.y, player->pos.x - map->offset.x,
280 extern void draw_win_info(struct Win * win)
282 struct World * world = (struct World *) win->data;
283 char * dsc_turn = "Turn: ";
284 char * dsc_hitpoints = "\nHitpoints: ";
285 char * dsc_score = "\nScore: ";
286 uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
287 + 10 + 5 + 10; /* max strlens of numbers to be used */
289 sprintf(text, "%s%d%s%d%s%d",
290 dsc_turn, world->turn,
291 dsc_hitpoints, world->player->hitpoints,
292 dsc_score, world->score);
293 draw_with_linebreaks(win, text, 0);
298 extern void draw_win_keybindings_global(struct Win * win)
300 char * f_name = "draw_win_keybindings_global()";
301 struct World * world = (struct World *) win->data;
302 draw_kb_view(world, win, f_name, &world->kb_global, 0);
307 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
309 char * f_name = "draw_win_keybindings_winconf_geometry()";
310 struct World * world = (struct World *) win->data;
311 draw_kb_view(world, win, f_name, &world->kb_wingeom, 0);
316 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
318 char * f_name = "draw_win_keybindings_winconf_keybindings()";
319 struct World * world = (struct World *) win->data;
320 draw_kb_view(world, win, f_name, &world->kb_winkeys, 0);
325 extern void draw_winconf_keybindings(struct Win * win)
327 char * f_name = "draw_winconf_keybindings()";
328 struct World * world = (struct World *) win->data;
329 struct WinConf * wc = get_winconf_by_win(world, win);
330 char * title = "Window's keybindings:";
331 uint8_t title_space = strlen(title) / win->frame.size.x + 2;
332 mvwaddstr(win->frame.curses_win, 0, 0, "Window's keybindings:");
333 draw_kb_view(world, win, f_name, &wc->kb, title_space);
338 extern void draw_winconf_geometry(struct Win * win)
340 struct World * world = (struct World *) win->data;
341 struct WinConf * wcp = get_winconf_by_win(world, win);
342 char * title = "Window's geometry:\n";
343 char * h_d = "\nWidth to save: ";
344 char * h_pos = " (height in cells)";
345 char * h_neg = " (negative diff: cells to maximum height)";
346 char * w_d = "\n\nHeight to save: ";
347 char * w_pos = " (width in cells)";
348 char * w_neg = " (negative diff: cells to maximum width)";
351 if (1 == wcp->height_type)
355 if (1 == wcp->width_type)
359 uint16_t maxl = strlen(title)
360 + strlen(h_t) + strlen(h_d) + 6
361 + strlen(w_t) + strlen(w_d) + 6 + 1;
363 sprintf(text, "%s%s%d%s%s%d%s", title, h_d, wcp->height, h_t,
364 w_d, wcp->width, w_t);
365 draw_with_linebreaks(win, text, 0);