home · contact · privacy
Fixed README typo.
[plomrogue] / src / draw_wins.c
1 /* draw_wins.c */
2
3 #include "draw_wins.h"
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(),
12                           * get_player()
13                           */
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() */
19
20
21
22 /* Write "text" into window "win" as far as possible. Start on row "start_y".
23  * Break lines at newlines.
24  */
25 static void draw_with_linebreaks(struct Win * win, char * text,
26                                  uint16_t start_y);
27
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).
31  */
32 static void draw_line(struct Win * win, uint16_t y, char * line, attr_t attri,
33                       uint8_t fill);
34
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);
37
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);
41
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);
45
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);
49
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.
52  */
53 static uint16_t draw_titled_keybinding_list(struct World * world,
54                                             struct Win * win, uint16_t start,
55                                             char * title,
56                                             struct KeyBinding * kb_p);
57
58
59
60
61
62
63 static void draw_with_linebreaks(struct Win * win, char * text,
64                                  uint16_t start_y)
65 {
66     uint16_t x, y;
67     char toggle;
68     char fin = 0;
69     int16_t z = -1;
70     for (y = start_y; y < win->frame.size.y; y++)
71     {
72         if (0 == fin)
73         {
74             toggle = 0;
75         }
76         for (x = 0; x < win->frame.size.x; x++)
77         {
78             if (0 == toggle)
79             {
80                 z++;
81                 if ('\n' == text[z])
82                 {
83                     toggle = 1;
84                     continue;
85                 }
86                 else
87                 {
88                     mvwaddch(win->frame.curses_win, y, x, text[z]);
89                 }
90                 if ('\n' == text[z+1])
91                 {
92                     z++;
93                     toggle = 1;
94                 }
95                 else if (0 == text[z+1])
96                 {
97                     toggle = 1;
98                     fin = 1;
99                 }
100             }
101         }
102     }
103 }
104
105
106
107 static void draw_line(struct Win * win, uint16_t y, char * line, attr_t attri,
108                       uint8_t fill)
109 {
110     uint16_t x = 0;
111     for (; x < win->frame.size.x && x < strlen(line); x++)
112     {
113         mvwaddch(win->frame.curses_win, y, x, line[x] | attri);
114     }
115     if (0 != fill)
116     {
117         for (; x < win->frame.size.x; x++)
118         {
119             mvwaddch(win->frame.curses_win, y, x, ' ' | attri);
120         }
121     }
122 }
123
124
125
126 static void draw_text_from_bottom (struct Win * win, char * text)
127 {
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.
131      */
132     char toggle = 0;
133     uint16_t x, y, offset;
134     int16_t z = -1;
135     for (y = 0; 0 == toggle; y++)
136     {
137         for (x = 0; x < win->frame.size.x; x++)
138         {
139             z++;
140             if ('\n' == text[z])
141             {
142                 break;
143             }
144             if ('\n' == text[z+1])
145             {
146                 z++;
147                 break;
148             }
149             else if (0 == text[z+1])
150             {
151                 toggle = 1;
152                 break;
153             }
154         }
155     }
156     z = -1;
157
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)
161     {
162         start_y = win->frame.size.y - y;
163     }
164     else if (y > win->frame.size.y)
165     {
166         offset = y - win->frame.size.y;
167         for (y = 0; y < offset; y++)
168         {
169             for (x = 0; x < win->frame.size.x; x++)
170             {
171                 z++;
172                 if ('\n' == text[z])
173                 {
174                     break;
175                 }
176                 if ('\n' == text[z+1])
177                 {
178                     z++;
179                     break;
180                 }
181             }
182         }
183         text = text + (sizeof(char) * (z + 1));
184     }
185
186     draw_with_linebreaks(win, text, start_y);
187 }
188
189
190
191 static void draw_map_objects(struct World * world, struct MapObj * start,
192                              struct Map * map, struct Win * win)
193 {
194     struct MapObj * o;
195     struct MapObjDef * d;
196     char c;
197     uint8_t i;
198     for (i = 0; i < 2; i++)
199     {
200         for (o = start; o != 0; o = o->next)
201         {
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)
208             {
209                 d = get_map_object_def(world, o->type);
210                 c = d->char_on_map;
211                 mvwaddch(win->frame.curses_win,
212                          o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);
213             }
214         }
215     }
216 }
217
218
219
220 static char * get_kb_line_and_iterate(struct World * world,
221                                       struct KeyBinding ** kb_pp)
222 {
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);
230     free(keyname);
231     * kb_pp = kb_p->next;
232     return line;
233 }
234
235
236
237 static void draw_kb_view(struct World * world, struct Win * win,
238                          char * f_name, struct KeyBiData * kb, uint8_t start)
239 {
240     if (0 == kb->kbs)
241     {
242         draw_line(win, start, "(none)", 0, 0);
243     }
244     char * err_hint = trouble_msg(world, f_name, "draw_scroll_hint()");
245     uint16_t kb_max = get_n_of_keybs(kb->kbs) - 1;
246     uint16_t y, offset;
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++)
251     {
252         if (start == y && offset > 0)
253         {
254             uint8_t test = draw_scroll_hint(&win->frame, y, offset + 1, '^');
255             exit_err(test, world, err_hint);
256             continue;
257         }
258         else if (win->frame.size.y == y + 1 && kb_max > y_border)
259         {
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);
263             continue;
264         }
265         attr_t attri = 0;
266         if (y - start == kb->select - offset)
267         {
268             attri = A_REVERSE;
269             if (1 == kb->edit)
270             {
271                 attri = attri | A_BLINK;
272             }
273         }
274         char * kb_line = get_kb_line_and_iterate(world, &kb_p);
275         draw_line(win, y, kb_line, attri, 1);
276         free(kb_line);
277     }
278     free(err_hint);
279 }
280
281
282
283 static uint16_t draw_titled_keybinding_list(struct World * world,
284                                             struct Win * win, uint16_t start,
285                                             char * title,
286                                             struct KeyBinding * kb_p)
287 {
288     uint16_t x, y;
289     uint16_t i = 0;
290     uint8_t state = 0;
291     for (y = start; y < win->frame.size.y && (0 == state || 0 != kb_p); y++)
292     {
293         if (0 == state)
294         {
295             for (x = 0; x < win->frame.size.x; x++)
296             {
297                 if (i == strlen(title))
298                 {
299                     y++;
300                     state = 1 + (0 == kb_p);
301                     i = 0;
302                     break;
303                 }
304                 mvwaddch(win->frame.curses_win, y, x, title[i]);
305                 i++;
306             }
307             continue;
308         }
309         char * kb_line = get_kb_line_and_iterate(world, &kb_p);
310         draw_line(win, y, kb_line, 0, 0);
311         free(kb_line);
312     }
313     if (2 == state)
314     {
315         draw_line(win, y, "(none)", 0, 0);
316         y++;
317     }
318     return y;
319 }
320
321
322
323 extern void draw_win_log(struct Win * win)
324 {
325     struct World * world = (struct World *) win->data;
326     draw_text_from_bottom(win, world->log);
327 }
328
329
330
331 extern void draw_win_map(struct Win * win)
332 {
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;
338     uint16_t x, y, z;
339     for (y = 0; y < win->frame.size.y; y++)
340     {
341         z = map->offset.x + (map->offset.y + y) * (map->size.x);
342         for (x = 0; x < win->frame.size.x; x++)
343         {
344             if (y < height_map_av && x < width_map_av)
345             {
346                 mvwaddch(win->frame.curses_win, y, x, cells[z]);
347                 z++;
348             }
349         }
350     }
351     draw_map_objects(world, world->map_objs, map, win);
352 }
353
354
355
356 extern void draw_win_info(struct Win * win)
357 {
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 */
364     char text[maxl + 1];
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);
371 }
372
373
374
375 extern void draw_win_inventory(struct Win * win)
376 {
377     mvwaddstr(win->frame.curses_win, 0, 0, "(empty)");
378 }
379
380
381
382 extern void draw_win_available_keybindings(struct Win * win)
383 {
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);
388     if     (0 == wc->view)
389     {
390         kb_p = wc->kb.kbs;
391     }
392     else if (1 == wc->view)
393     {
394         kb_p = world->kb_wingeom.kbs;
395     }
396     else if (2 == wc->view)
397     {
398         kb_p = world->kb_winkeys.kbs;
399     }
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);
403 }
404
405
406
407 extern void draw_win_keybindings_global(struct Win * win)
408 {
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);
412 }
413
414
415
416 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
417 {
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);
421 }
422
423
424
425 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
426 {
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);
430 }
431
432
433
434 extern void draw_winconf_keybindings(struct Win * win)
435 {
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);
443 }
444
445
446
447 extern void draw_winconf_geometry(struct Win * win)
448 {
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)";
458     char * h_t = h_pos;
459     char * w_t = w_pos;
460     if      (1 == wcp->height_type)
461     {
462         h_t = h_neg;
463     }
464     if     (1 == wcp->width_type)
465     {
466         w_t = w_neg;
467     }
468     uint16_t maxl = strlen(title)
469                     + strlen(h_t) + strlen(h_d) + 6
470                     + strlen(w_t) + strlen(w_d) + 6 + 1;
471     char text[maxl + 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);
475 }