home · contact · privacy
Add auto-mapping / map memory.
[plomrogue] / src / client / draw_wins.c
1 /* src/client/draw_wins.c */
2
3 #define _POSIX_C_SOURCE 200809L /* strdup() */
4 #include "draw_wins.h"
5 #include <ncurses.h> /* attr_t, chtype, init_pair(), A_REVERSE, COLOR_PAIR() */
6 #include <stddef.h> /* NULL */
7 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
8 #include <stdio.h> /* sprintf() */
9 #include <stdlib.h> /* free() */
10 #include <string.h> /* memset(), strchr(), strdup/(), strlen() */
11 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
12 #include "../common/try_malloc.h" /* try_malloc() */
13 #include "keybindings.h" /* struct KeyBindingDB, get_keyname_to_keycode() */
14 #include "windows.h" /* yx_uint16, Win, get_win_by_id() */
15 #include "world.h" /* global world */
16
17
18
19 /* Apply to the winmap of Win "w" the new sizes "new_size_y" and "new_size_x"
20  * to the degree that they extend it. Re-shape the window content accordingly.
21  */
22 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x);
23
24 /* In Win "w", write "ch" to coordinate "y"/"x". */
25 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch);
26
27 /* Add "line" into window "w" with "attri" set on all chars. add_line() selects
28  * one of the other three functions based on the "win"->linebreak mode.
29  *
30  * "wide" writes lines to full width without in-between breaks. "long" breaks
31  * lines on the right window border. "compact" replaces newlines with printable
32  * separator characters. "offset" and "last" are only relevant to the "compact"
33  * mode. "offset" reports the horizontal offset at which the line is drawn from
34  * the lowest existing winmap line on; it is updated to the next offset after
35  * the new line, to be used by the next add_line_compact() call. "last" marks
36  * the line to add as the last in the window, so no further separator is added.
37  */
38 static void add_line(struct Win * win, char * line, attr_t attri,
39                      uint16_t * offset, uint8_t last);
40 static void add_line_wide(struct Win * win, char * line, attr_t attri);
41 static void add_line_long(struct Win * win, char * line, attr_t attri);
42 static void add_line_compact(struct Win * win, char * line, attr_t attri,
43                              uint16_t * offset, uint8_t last);
44
45 /* Add linebreaks-containing "text" to "win", formatted as per "win"->linebreak.
46  *
47  * draw_text_from_bottom() prepends empty lines to "text" to push its last line
48  * to the low frame edge when the frame is of greater height than the winmap
49  * would be otherwise; else, set "win"->center.y to the winmap's lowest line.
50  */
51 static void add_text_with_linebreaks(struct Win * win, char * text);
52 static void draw_text_from_bottom(struct Win * win, char * text);
53
54 /* Return a properly formatted keybinding list line for "kb". */
55 static char * get_kb_line(struct KeyBinding * kb);
56
57 /* Draw from line "start" on config view for keybindings defined at "kb". */
58 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
59                                    uint16_t offset);
60
61 /* Draw into window "w" from line "start" on a "title" followed by an empty
62  * line followed by a list of all keybindings starting in "kbdb".
63  */
64 static void draw_titled_keybinding_list(char * title, struct Win * win,
65                                         uint16_t * offset, uint8_t last,
66                                         struct KeyBindingDB * kbdb);
67
68
69
70 static void try_resize_winmap(struct Win * win, int new_size_y, int new_size_x)
71 {
72     if (win->winmap_size.y >= new_size_y && win->winmap_size.x >= new_size_x)
73     {
74         return;
75     }
76     if      (win->winmap_size.y > new_size_y)
77     {
78         new_size_y = win->winmap_size.y;
79     }
80     else if (win->winmap_size.x > new_size_x)
81     {
82         new_size_x = win->winmap_size.x;
83     }
84     chtype * old_winmap = win->winmap;
85     uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
86     win->winmap = try_malloc(new_size, __func__);
87     uint16_t y, x;
88     for (y = 0; y < new_size_y; y++)
89     {
90         for (x = 0; y < win->winmap_size.y && x < win->winmap_size.x; x++)
91         {
92             chtype ch = old_winmap[(y * win->winmap_size.x) + x];
93             win->winmap[(y * new_size_x) + x] = ch;
94         }
95         for (; x < new_size_x; x++)
96         {
97             win->winmap[(y * new_size_x) + x] = ' ';
98         }
99     }
100     free(old_winmap);
101     win->winmap_size.y = new_size_y;
102     win->winmap_size.x = new_size_x;
103 }
104
105
106
107 static void set_ch_on_yx(struct Win * win, int y, int x, chtype ch)
108 {
109     win->winmap[(y * win->winmap_size.x) + x] = ch;
110 }
111
112
113
114 static void add_line(struct Win * win, char * line, attr_t attri,
115                      uint16_t * offset, uint8_t last)
116 {
117     exit_err(strlen(line) > UINT16_MAX, "Line in add_line() too big.");
118     if      (0 == win->linebreak)
119     {
120         add_line_long(win, line, attri);
121     }
122     else if (1 == win->linebreak)
123     {
124         add_line_wide(win, line, attri);
125     }
126     else if (2 == win->linebreak)
127     {
128         add_line_compact(win, line, attri, offset, last);
129     }
130 }
131
132
133
134 static void add_line_wide(struct Win * win, char * line, attr_t attri)
135 {
136     uint16_t y_start = win->winmap_size.y;
137     uint16_t len_line = strlen(line);
138     try_resize_winmap(win, y_start + 1, win->frame_size.x);
139     try_resize_winmap(win, y_start + 1, len_line);
140     uint16_t x;
141     for (x = 0; x < len_line; x++)
142     {
143         set_ch_on_yx(win, y_start, x, line[x] | attri);
144     }
145     if (attri)
146     {
147         for (; x < win->frame_size.x; x++)
148         {
149             set_ch_on_yx(win, y_start, x, ' ' | attri);
150         }
151     }
152 }
153
154
155
156 static void add_line_long(struct Win * win, char * line, attr_t attri)
157 {
158     uint16_t y_start = win->winmap_size.y;
159     uint16_t len_line = strlen(line);
160     try_resize_winmap(win, y_start + 1, win->frame_size.x);
161     uint16_t x, y, z;
162     for (z = 0, y = y_start; z < len_line; y++)
163     {
164         for (x = 0; x < win->winmap_size.x && z < len_line; x++, z++)
165         {
166             set_ch_on_yx(win, y, x, line[z] | attri);
167         }
168         if (z < len_line)
169         {
170             try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
171         }
172     }
173 }
174
175
176
177 static void add_line_compact(struct Win * win, char * line, attr_t attri,
178                              uint16_t * offset, uint8_t last)
179 {
180     uint16_t y_start = win->winmap_size.y - (win->winmap_size.y > 0);
181     try_resize_winmap(win, y_start + 1, win->frame_size.x);
182     uint16_t len_line = strlen(line);
183     char * separator = last ? "" : " / ";
184     uint32_t len_line_new = len_line + strlen(separator);
185     char * line_new = try_malloc(len_line_new, __func__);
186     int test = sprintf(line_new, "%s%s", line, separator);
187     exit_trouble(test < 0, __func__, "sprintf");
188     uint16_t x = 0;
189     uint16_t y;
190     uint32_t z;
191     for (z = 0, y = y_start; z < len_line_new; y++)
192     {
193         for (x = * offset; x < win->winmap_size.x && z < len_line_new; x++, z++)
194         {
195             set_ch_on_yx(win, y, x, line_new[z] | attri);
196             if (z + 1 == (uint32_t) len_line)
197             {
198                attri = 0;
199             }
200         }
201         * offset = 0;
202         if (z < len_line_new)
203         {
204             try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
205         }
206     }
207     free(line_new);
208     *offset = x;
209 }
210
211
212
213 static void add_text_with_linebreaks(struct Win * win, char * text)
214 {
215     char * limit;
216     uint16_t offset = 0;
217     char * text_copy = strdup(text);
218     char * start = text_copy;
219     while (NULL != (limit = strchr(start, '\n')))
220     {
221         (* limit) = '\0';
222         add_line(win, start, 0, &offset, 0);
223         start = limit + 1;
224     }
225     add_line(win, start, 0, &offset, 1);
226     free(text_copy);
227 }
228
229
230
231 static void draw_text_from_bottom(struct Win * win, char * text)
232 {
233     add_text_with_linebreaks(win, text);
234     if (win->winmap_size.y > win->frame_size.y)
235     {
236         win->center.y = win->winmap_size.y - 1;
237     }
238     else if (win->winmap_size.y < win->frame_size.y)
239     {
240         uint16_t new_y_start = win->frame_size.y - win->winmap_size.y;
241         memset(&win->winmap_size, 0, sizeof(struct yx_uint16));
242         free(win->winmap);
243         win->winmap = NULL;
244         do
245         {
246             add_line_wide(win, " ", 0);
247         }
248         while (new_y_start > win->winmap_size.y);
249         if (2 == win->linebreak) /* add_text_with_linebreaks() will start not */
250         {                        /* not after, but within the last line then. */
251             add_line_wide(win, " ", 0);
252         }
253         add_text_with_linebreaks(win, text);
254     }
255 }
256
257
258
259 static char * get_kb_line(struct KeyBinding * kb)
260 {
261     char * keyname = get_keyname_to_keycode(kb->keycode);
262     uint16_t size = strlen(keyname) + 3 + strlen(kb->command->dsc_long) + 1;
263     char * kb_line = try_malloc(size, __func__);
264     int test = sprintf(kb_line, "%s - %s", keyname, kb->command->dsc_long);
265     exit_trouble(test < 0, __func__, "sprintf");
266     free(keyname);
267     return kb_line;
268 }
269
270
271
272 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
273                                    uint16_t offset)
274 {
275     if (0 == kbdb->n_of_kbs)
276     {
277         add_line(win, "(none)", 0, &offset, 0);
278         return;
279     }
280     uint8_t kb_n;
281     for (kb_n = 0; kb_n < kbdb->n_of_kbs; kb_n++)
282     {
283         attr_t attri = 0;
284         if (kb_n == kbdb->select)
285         {
286             attri = A_REVERSE;
287             if (1 == kbdb->edit)
288             {
289                 attri = attri | A_BLINK;
290             }
291             win->center.y = win->winmap_size.y;
292         }
293         char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
294         add_line(win, kb_line, attri, &offset, (kbdb->n_of_kbs == kb_n + 1));
295         free(kb_line);
296     }
297 }
298
299
300
301 static void draw_titled_keybinding_list(char * title, struct Win * win,
302                                         uint16_t * offset, uint8_t last,
303                                         struct KeyBindingDB * kbdb)
304 {
305     uint8_t state = 0;
306     uint8_t kb_n = 0;
307     while (0 == state || kb_n < kbdb->n_of_kbs)
308     {
309         if (0 == state)
310         {
311             add_line(win, title, 0, offset, 0);
312             add_line(win, " ", 0, offset, 0);
313             state = 1 + (0 == kbdb->n_of_kbs);
314             continue;
315         }
316         char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
317         add_line(win, kb_line, 0, offset, (last * kbdb->n_of_kbs == kb_n + 1));
318         free(kb_line);
319         kb_n++;
320     }
321     if (2 == state)
322     {
323         add_line(win, "(none)", 0, offset, last);
324     }
325 }
326
327
328
329 extern void draw_win_log(struct Win * win)
330 {
331     if (!world.log)
332     {
333         return;
334     }
335     draw_text_from_bottom(win, world.log);
336 }
337
338
339
340 extern void draw_win_map(struct Win * win)
341 {
342     init_pair(1, COLOR_WHITE, COLOR_BLUE);
343     init_pair(2, COLOR_BLUE, COLOR_BLACK);
344     attr_t attr_fov = 0;
345     attr_t attr_mem = COLOR_PAIR(2);
346     attr_t attr_sha = COLOR_PAIR(1);
347     try_resize_winmap(win, world.map.length, world.map.length * 2);
348     uint16_t x, y, z;
349     for (y = 0, z = 0; y < world.map.length; y++)
350     {
351         for (x = 0; x < world.map.length; x++)
352         {
353             attr_t attr_c = ' ' == world.mem_map[z] ? attr_sha : attr_mem;
354             chtype c = world.mem_map[z] | attr_c;
355             set_ch_on_yx(win, y, x * 2 + (y % 2), c);
356             if (x + (y % 2) < world.map.length)
357             {
358                 set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | attr_c);
359             }
360             z++;
361         }
362     }
363     for (y = 0, z = 0; y < world.map.length; y++)
364     {
365         for (x = 0; x < world.map.length; x++)
366         {
367             if (' ' != world.map.cells[z])
368             {
369                 chtype c = world.map.cells[z] | attr_fov;
370                 set_ch_on_yx(win, y, x * 2 + (y % 2), c);
371                 if (x + (y % 2) < world.map.length)
372                 {
373                     set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | attr_fov);
374                 }
375             }
376             z++;
377         }
378     }
379 }
380
381
382
383 extern void draw_win_info(struct Win * win)
384 {
385     char * dsc_turn      = "Turn: ";
386     char * dsc_hitpoints = "\nHitpoints: ";
387     uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3;
388     char * text = try_malloc(maxl + 1, __func__);
389     int test = sprintf(text, "%s%d%s%d", dsc_turn, world.turn, dsc_hitpoints,
390                                          world.player_lifepoints);
391     exit_trouble(test < 0, __func__, "sprintf");
392     add_text_with_linebreaks(win, text);
393     free(text);
394 }
395
396
397
398 extern void draw_win_inventory(struct Win * win)
399 {
400     char * limit;
401     char * text_copy = strdup(world.player_inventory);
402     char * start = text_copy;
403     uint8_t i = 0;
404     uint8_t found_selection = 0;
405     uint16_t offset = 0;
406     while (NULL != (limit = strchr(start, '\n')))
407     {
408         (* limit) = '\0';
409         attr_t attri = 0;
410         if (i == world.player_inventory_select)
411         {
412             found_selection = 1;
413             attri = A_REVERSE;
414             win->center.y = win->winmap_size.y;
415         }
416         add_line(win, start, attri, &offset, 0);
417         start = limit + 1;
418         i++;
419     }
420     win->center.y = !found_selection ? win->winmap_size.y : win->center.y;
421     add_line(win, start, !found_selection * A_REVERSE, &offset, 1);
422     free(text_copy);
423 }
424
425
426
427 extern void draw_win_available_keybindings(struct Win * win)
428 {
429     char * title = "Active window's keys:";
430     struct Win * win_active = get_win_by_id(world.winDB.active);
431     struct KeyBindingDB * kbdb = &win_active->kb;
432     if      (1 == win_active->view)
433     {
434         kbdb = &world.kb_wingeom;
435     }
436     else if (2 == win_active->view)
437     {
438         kbdb = &world.kb_winkeys;
439     }
440     uint16_t offset = 0;
441     draw_titled_keybinding_list(title, win, &offset, 0, kbdb);
442     add_line(win, " ", 0, &offset, 0);
443     title = "Global keys:";
444     draw_titled_keybinding_list(title, win, &offset, 1, &world.kb_global);
445 }
446
447
448
449
450 extern void draw_win_keybindings_global(struct Win * win)
451 {
452     win->center.y = world.kb_global.select;
453     draw_keybinding_config(win, &world.kb_global, 0);
454 }
455
456
457
458 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
459 {
460     win->center.y = world.kb_wingeom.select;
461     draw_keybinding_config(win, &world.kb_wingeom, 0);
462 }
463
464
465
466 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
467 {
468     win->center.y = world.kb_winkeys.select;
469     draw_keybinding_config(win, &world.kb_winkeys, 0);
470 }
471
472
473
474 extern void draw_winconf_keybindings(struct Win * win)
475 {
476     char * title = "Window's keys:";
477     uint16_t offset = 0;
478     add_line(win, title, 0, &offset, 0);
479     add_line(win, " ", 0, &offset, 0);
480     draw_keybinding_config(win, &win->kb, offset);
481 }
482
483
484
485 extern void draw_winconf_geometry(struct Win * win)
486 {
487     char * title = "Window's geometry:\n\n";
488     char * h_title = "Height to save: ";
489     char h_value[6 + 1];                       /* 6: int16_t value max strlen */
490     int test = sprintf(h_value, "%d", win->target_height);
491     exit_trouble(test < 0, __func__, "sprintf");
492     char * h_plus = " (width in cells)\n\n";
493     char * h_minus = " (negative diff: cells to screen width)\n\n";
494     char * h_type = (1 == win->target_height_type) ? h_minus : h_plus;
495     char * w_title = "Width to save: ";
496     char w_value[6 + 1];
497     test = sprintf(w_value, "%d", win->target_width);
498     exit_trouble(test < 0, __func__, "sprintf");
499     char * w_plus = "(height in cells)\n\n";
500     char * w_minus = " (negative diff: cells to screen height)\n\n";
501     char * w_type = (1 == win->target_width_type)  ? w_minus : w_plus;
502     char * breaks_title = "Linebreak type: ";
503     char * breaks_type = (1 == win->linebreak) ? "wide" : "long";
504     breaks_type        = (2 == win->linebreak) ? "compact" : breaks_type;
505     uint16_t text_size =   strlen(title)
506                          + strlen(h_title) + strlen(h_value) + strlen(h_type)
507                          + strlen(w_title) + strlen(w_value) + strlen(w_type)
508                          + strlen(breaks_title) + strlen(breaks_type);
509     char * text = try_malloc(text_size + 1, __func__);
510     test = sprintf(text, "%s%s%s%s%s%s%s%s%s", title, h_title, h_value, h_type,
511                         w_title, w_value, w_type, breaks_title, breaks_type);
512     exit_trouble(test < 0, __func__, "sprintf");
513     add_text_with_linebreaks(win, text);
514     free(text);
515 }