home · contact · privacy
63fb8a0324c5ded1b1150dc5f4a5de533be12d92
[plomrogue] / src / client / draw_wins.c
1 /* src/client/draw_wins.c
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  */
7
8 #define _POSIX_C_SOURCE 200809L /* strdup() */
9 #include "draw_wins.h"
10 #include <ncurses.h> /* attr_t, chtype, init_pair(), A_REVERSE, COLOR_PAIR() */
11 #include <stddef.h> /* NULL */
12 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
13 #include <stdio.h> /* sprintf() */
14 #include <stdlib.h> /* free() */
15 #include <string.h> /* memset(), strcmp(), strchr(), strdup/(), strlen() */
16 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
17 #include "../common/try_malloc.h" /* try_malloc() */
18 #include "keybindings.h" /* struct KeyBindingDB, get_keyname_to_keycode() */
19 #include "windows.h" /* yx_uint16, Win */
20 #include "world.h" /* global world */
21
22
23
24 /* Apply to the winmap of Win "w" the new sizes "new_size_y" and "new_size_x"
25  * to the degree that they extend it. Re-shape the window content accordingly.
26  */
27 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x);
28
29 /* In Win "w", write "ch" to coordinate "y"/"x". */
30 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch);
31
32 /* Add "line" into window "w" with "attri" set on all chars. add_line() selects
33  * one of the other three functions based on the "win"->linebreak mode.
34  *
35  * "wide" writes lines to full width without in-between breaks. "long" breaks
36  * lines on the right window border. "compact" replaces newlines with printable
37  * separator characters. "offset" and "last" are only relevant to the "compact"
38  * mode. "offset" reports the horizontal offset at which the line is drawn from
39  * the lowest existing winmap line on; it is updated to the next offset after
40  * the new line, to be used by the next add_line_compact() call. "last" marks
41  * the line to add as the last in the window, so no further separator is added.
42  */
43 static void add_line(struct Win * win, char * line, attr_t attri,
44                      uint16_t * offset, uint8_t last);
45 static void add_line_wide(struct Win * win, char * line, attr_t attri);
46 static void add_line_long(struct Win * win, char * line, attr_t attri);
47 static void add_line_compact(struct Win * win, char * line, attr_t attri,
48                              uint16_t * offset, uint8_t last);
49
50 /* Add linebreaks-containing "text" to "win", formatted as per "win"->linebreak.
51  *
52  * draw_text_from_bottom() prepends empty lines to "text" to push its last line
53  * to the low frame edge when the frame is of greater height than the winmap
54  * would be otherwise; else, set "win"->center.y to the winmap's lowest line.
55  */
56 static void add_text_with_linebreaks(struct Win * win, char * text);
57 static void draw_text_from_bottom(struct Win * win, char * text);
58
59 /* Draw from line "start" on config view for keybindings defined at "kb". */
60 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
61                                    uint16_t offset);
62
63 /* String building helper functions to draw_winconf_geometry(). */
64 static char * get_keyname_to_command(char * command_name);
65 static char * winconf_geom_helper(struct Win * win, char axis, char * sep,
66                                   char * newlines, char * value_prefix);
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 + 1, __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 void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
260                                    uint16_t offset)
261 {
262     if (0 == kbdb->n_of_kbs)
263     {
264         add_line(win, "(none)", 0, &offset, 0);
265         return;
266     }
267     uint8_t kb_n;
268     for (kb_n = 0; kb_n < kbdb->n_of_kbs; kb_n++)
269     {
270         attr_t attri = 0;
271         if (kb_n == kbdb->select)
272         {
273             attri = A_REVERSE;
274             if (1 == kbdb->edit)
275             {
276                 attri = attri | A_BLINK;
277             }
278             win->center.y = win->winmap_size.y;
279         }
280         struct KeyBinding kb = kbdb->kbs[kb_n];
281         char * keyname = get_keyname_to_keycode(kb.keycode);
282         uint16_t size = strlen(keyname) + 3 + strlen(kb.command->dsc_long) + 1;
283         char * kb_line = try_malloc(size, __func__);
284         int test = sprintf(kb_line, "%s - %s", keyname, kb.command->dsc_long);
285         exit_trouble(test < 0, __func__, "sprintf");
286         free(keyname);
287         add_line(win, kb_line, attri, &offset, (kbdb->n_of_kbs == kb_n + 1));
288         free(kb_line);
289     }
290 }
291
292
293
294 static char * get_keyname_to_command(char * command_name)
295 {
296     uint8_t i = 0;
297     for (i = 0; i < world.kb_wingeom.n_of_kbs; i++)
298     {
299         if (!strcmp(world.kb_wingeom.kbs[i].command->dsc_short, command_name))
300         {
301             return get_keyname_to_keycode(world.kb_wingeom.kbs[i].keycode);
302         }
303     }
304     return "UNDEFINED";
305 }
306
307
308
309 static char * winconf_geom_helper(struct Win * win, char axis, char * sep,
310                                   char * newlines, char * value_prefix)
311 {
312     char * p0 = 'v'==axis? "Height" : "Width";
313     char * p1 = " to save (grow/shrink/toggle positivity with ";
314     char * p2 = get_keyname_to_command('v'==axis? "grow_v" : "grow_h");
315     char * p4 = get_keyname_to_command('v'==axis? "shri_v" : "shri_v");
316     char * p6 = get_keyname_to_command('v'==axis? "to_height_t" : "to_width_t");
317     char p8[6 + 1];                            /* 6: int16_t value max strlen */
318     int test = sprintf(p8,"%d", 'v'==axis?win->target_height:win->target_width);
319     exit_trouble(test < 0, __func__, "sprintf");
320     char * p9 = " (";
321     char * p10 = "in_cells";
322     if (1 == ('v'==axis? win->target_height_type : win->target_width_type))
323     {
324         p10 = "non-positive diff: cells to screen maximum";
325     }
326     char * p11 = ")";
327     uint8_t size =   strlen(p0) + strlen(p1) + strlen(p2) + strlen(sep)
328                    + strlen(p4) + strlen(sep) + strlen(p6) +strlen(value_prefix)
329                    + strlen(p8) + strlen(p9) + strlen(p10) + strlen(p11)
330                    + strlen(newlines);
331     char * msg = try_malloc(size, __func__);
332     sprintf(msg, "%s%s%s%s%s%s%s%s%s%s%s%s%s", p0, p1, p2, sep, p4, sep, p6,
333             value_prefix, p8, p9, p10, p11, newlines);
334     return msg;
335 }
336
337
338
339 extern void draw_win_log(struct Win * win)
340 {
341     if (!world.log)
342     {
343         return;
344     }
345     draw_text_from_bottom(win, world.log);
346 }
347
348
349
350 extern void draw_win_map(struct Win * win)
351 {
352     init_pair(1, COLOR_WHITE, COLOR_BLUE);
353     init_pair(2, COLOR_BLUE, COLOR_BLACK);
354     attr_t attr_fov = 0;
355     attr_t attr_mem = COLOR_PAIR(2);
356     attr_t attr_sha = COLOR_PAIR(1);
357     try_resize_winmap(win, world.map.length, world.map.length * 2);
358     uint16_t x, y, z;
359     for (y = 0, z = 0; y < world.map.length; y++)
360     {
361         for (x = 0; x < world.map.length; x++)
362         {
363             attr_t attr_c = ' ' == world.mem_map[z] ? attr_sha : attr_mem;
364             chtype c = world.mem_map[z] | attr_c;
365             set_ch_on_yx(win, y, x * 2 + (y % 2), c);
366             if (x + (y % 2) < world.map.length)
367             {
368                 set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | attr_c);
369             }
370             z++;
371         }
372     }
373     for (y = 0, z = 0; y < world.map.length; y++)
374     {
375         for (x = 0; x < world.map.length; x++)
376         {
377             if (' ' != world.map.cells[z])
378             {
379                 chtype c = world.map.cells[z] | attr_fov;
380                 set_ch_on_yx(win, y, x * 2 + (y % 2), c);
381                 if (x + (y % 2) < world.map.length)
382                 {
383                     set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | attr_fov);
384                 }
385             }
386             z++;
387         }
388     }
389 }
390
391
392
393 extern void draw_win_info(struct Win * win)
394 {
395     char * dsc_turn      = "Turn: ";
396     char * dsc_hitpoints = "\nHitpoints: ";
397     uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3;
398     char * text = try_malloc(maxl + 1, __func__);
399     int test = sprintf(text, "%s%d%s%d", dsc_turn, world.turn, dsc_hitpoints,
400                                          world.player_lifepoints);
401     exit_trouble(test < 0, __func__, "sprintf");
402     add_text_with_linebreaks(win, text);
403     free(text);
404 }
405
406
407
408 extern void draw_win_inventory(struct Win * win)
409 {
410     char * limit;
411     char * text_copy = strdup(world.player_inventory);
412     char * start = text_copy;
413     uint8_t i = 0;
414     uint8_t found_selection = 0;
415     uint16_t offset = 0;
416     while (NULL != (limit = strchr(start, '\n')))
417     {
418         (* limit) = '\0';
419         attr_t attri = 0;
420         if (i == world.player_inventory_select)
421         {
422             found_selection = 1;
423             attri = A_REVERSE;
424             win->center.y = win->winmap_size.y;
425         }
426         add_line(win, start, attri, &offset, 0);
427         start = limit + 1;
428         i++;
429     }
430     win->center.y = !found_selection ? win->winmap_size.y : win->center.y;
431     add_line(win, start, !found_selection * A_REVERSE, &offset, 1);
432     free(text_copy);
433 }
434
435
436
437 extern void draw_win_terrain_stack(struct Win * win)
438 {
439     if (world.things_below_player)
440     {
441         add_text_with_linebreaks(win, world.things_below_player);
442     }
443 }
444
445
446
447 extern void draw_win_keybindings_global(struct Win * win)
448 {
449     win->center.y = world.kb_global.select;
450     draw_keybinding_config(win, &world.kb_global, 0);
451 }
452
453
454
455 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
456 {
457     win->center.y = world.kb_wingeom.select;
458     draw_keybinding_config(win, &world.kb_wingeom, 0);
459 }
460
461
462
463 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
464 {
465     win->center.y = world.kb_winkeys.select;
466     draw_keybinding_config(win, &world.kb_winkeys, 0);
467 }
468
469
470
471 extern void draw_winconf_keybindings(struct Win * win)
472 {
473     char * title = "Window's keys:";
474     uint16_t offset = 0;
475     add_line(win, title, 0, &offset, 0);
476     add_line(win, " ", 0, &offset, 0);
477     draw_keybinding_config(win, &win->kb, offset);
478 }
479
480
481
482 extern void draw_winconf_geometry(struct Win * win)
483 {
484     char * sep = "/";
485     char * newlines = "\n\n";
486     char * value_prefix = "): ";
487     char * title = "Window's geometry:\n\nShift up/down with ";
488     char * key_shift_b = get_keyname_to_command("shift_b");
489     char * key_shift_f = get_keyname_to_command("shift_f");
490     char * height = winconf_geom_helper(win, 'v', sep, newlines, value_prefix);
491     char * width = winconf_geom_helper(win, 'h', sep, newlines, value_prefix);
492     char * breaks_title = "Linebreak type (toggle with ";
493     char * key_to_break = get_keyname_to_command("to_break");
494     char * breaks_type = "long";
495     if (win->linebreak)
496     {
497         breaks_type = (1 == win->linebreak) ? "wide" : "compact";
498     }
499     uint16_t text_size =   strlen(title) + strlen(key_shift_b) + strlen(sep)
500                          + strlen(key_shift_f) + strlen(newlines)
501                          + strlen(height) + strlen(width)
502                          + strlen(breaks_title) + strlen(key_to_break)
503                          + strlen(value_prefix) + strlen(breaks_type);
504     char * text = try_malloc(text_size + 1, __func__);
505     int test = sprintf(text, "%s%s%s%s%s%s%s%s%s%s%s", title, key_shift_b, sep,
506                        key_shift_f, newlines, height, width, breaks_title,
507                        key_to_break, value_prefix, breaks_type);
508     free(height);
509     free(width);
510     exit_trouble(test < 0, __func__, "sprintf");
511     add_text_with_linebreaks(win, text);
512     free(text);
513 }