home · contact · privacy
Client: In window geometry config, show available keys.
[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(), 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, get_win_by_id() */
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 /* Return a properly formatted keybinding list line for "kb". */
60 static char * get_kb_line(struct KeyBinding * kb);
61
62 /* Draw from line "start" on config view for keybindings defined at "kb". */
63 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
64                                    uint16_t offset);
65
66 /* String building helper functions to draw_winconf_geometry(). */
67 static char * get_keyname_to_command(char * command_name);
68 static char * winconf_geom_helper(struct Win * win, char axis, char * sep,
69                                   char * newlines, char * value_prefix);
70
71
72
73 static void try_resize_winmap(struct Win * win, int new_size_y, int new_size_x)
74 {
75     if (win->winmap_size.y >= new_size_y && win->winmap_size.x >= new_size_x)
76     {
77         return;
78     }
79     if      (win->winmap_size.y > new_size_y)
80     {
81         new_size_y = win->winmap_size.y;
82     }
83     else if (win->winmap_size.x > new_size_x)
84     {
85         new_size_x = win->winmap_size.x;
86     }
87     chtype * old_winmap = win->winmap;
88     uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
89     win->winmap = try_malloc(new_size, __func__);
90     uint16_t y, x;
91     for (y = 0; y < new_size_y; y++)
92     {
93         for (x = 0; y < win->winmap_size.y && x < win->winmap_size.x; x++)
94         {
95             chtype ch = old_winmap[(y * win->winmap_size.x) + x];
96             win->winmap[(y * new_size_x) + x] = ch;
97         }
98         for (; x < new_size_x; x++)
99         {
100             win->winmap[(y * new_size_x) + x] = ' ';
101         }
102     }
103     free(old_winmap);
104     win->winmap_size.y = new_size_y;
105     win->winmap_size.x = new_size_x;
106 }
107
108
109
110 static void set_ch_on_yx(struct Win * win, int y, int x, chtype ch)
111 {
112     win->winmap[(y * win->winmap_size.x) + x] = ch;
113 }
114
115
116
117 static void add_line(struct Win * win, char * line, attr_t attri,
118                      uint16_t * offset, uint8_t last)
119 {
120     exit_err(strlen(line) > UINT16_MAX, "Line in add_line() too big.");
121     if      (0 == win->linebreak)
122     {
123         add_line_long(win, line, attri);
124     }
125     else if (1 == win->linebreak)
126     {
127         add_line_wide(win, line, attri);
128     }
129     else if (2 == win->linebreak)
130     {
131         add_line_compact(win, line, attri, offset, last);
132     }
133 }
134
135
136
137 static void add_line_wide(struct Win * win, char * line, attr_t attri)
138 {
139     uint16_t y_start = win->winmap_size.y;
140     uint16_t len_line = strlen(line);
141     try_resize_winmap(win, y_start + 1, win->frame_size.x);
142     try_resize_winmap(win, y_start + 1, len_line);
143     uint16_t x;
144     for (x = 0; x < len_line; x++)
145     {
146         set_ch_on_yx(win, y_start, x, line[x] | attri);
147     }
148     if (attri)
149     {
150         for (; x < win->frame_size.x; x++)
151         {
152             set_ch_on_yx(win, y_start, x, ' ' | attri);
153         }
154     }
155 }
156
157
158
159 static void add_line_long(struct Win * win, char * line, attr_t attri)
160 {
161     uint16_t y_start = win->winmap_size.y;
162     uint16_t len_line = strlen(line);
163     try_resize_winmap(win, y_start + 1, win->frame_size.x);
164     uint16_t x, y, z;
165     for (z = 0, y = y_start; z < len_line; y++)
166     {
167         for (x = 0; x < win->winmap_size.x && z < len_line; x++, z++)
168         {
169             set_ch_on_yx(win, y, x, line[z] | attri);
170         }
171         if (z < len_line)
172         {
173             try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
174         }
175     }
176 }
177
178
179
180 static void add_line_compact(struct Win * win, char * line, attr_t attri,
181                              uint16_t * offset, uint8_t last)
182 {
183     uint16_t y_start = win->winmap_size.y - (win->winmap_size.y > 0);
184     try_resize_winmap(win, y_start + 1, win->frame_size.x);
185     uint16_t len_line = strlen(line);
186     char * separator = last ? "" : " / ";
187     uint32_t len_line_new = len_line + strlen(separator);
188     char * line_new = try_malloc(len_line_new + 1, __func__);
189     int test = sprintf(line_new, "%s%s", line, separator);
190     exit_trouble(test < 0, __func__, "sprintf");
191     uint16_t x = 0;
192     uint16_t y;
193     uint32_t z;
194     for (z = 0, y = y_start; z < len_line_new; y++)
195     {
196         for (x = * offset; x < win->winmap_size.x && z < len_line_new; x++, z++)
197         {
198             set_ch_on_yx(win, y, x, line_new[z] | attri);
199             if (z + 1 == (uint32_t) len_line)
200             {
201                attri = 0;
202             }
203         }
204         * offset = 0;
205         if (z < len_line_new)
206         {
207             try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
208         }
209     }
210     free(line_new);
211     *offset = x;
212 }
213
214
215
216 static void add_text_with_linebreaks(struct Win * win, char * text)
217 {
218     char * limit;
219     uint16_t offset = 0;
220     char * text_copy = strdup(text);
221     char * start = text_copy;
222     while (NULL != (limit = strchr(start, '\n')))
223     {
224         (* limit) = '\0';
225         add_line(win, start, 0, &offset, 0);
226         start = limit + 1;
227     }
228     add_line(win, start, 0, &offset, 1);
229     free(text_copy);
230 }
231
232
233
234 static void draw_text_from_bottom(struct Win * win, char * text)
235 {
236     add_text_with_linebreaks(win, text);
237     if (win->winmap_size.y > win->frame_size.y)
238     {
239         win->center.y = win->winmap_size.y - 1;
240     }
241     else if (win->winmap_size.y < win->frame_size.y)
242     {
243         uint16_t new_y_start = win->frame_size.y - win->winmap_size.y;
244         memset(&win->winmap_size, 0, sizeof(struct yx_uint16));
245         free(win->winmap);
246         win->winmap = NULL;
247         do
248         {
249             add_line_wide(win, " ", 0);
250         }
251         while (new_y_start > win->winmap_size.y);
252         if (2 == win->linebreak) /* add_text_with_linebreaks() will start not */
253         {                        /* not after, but within the last line then. */
254             add_line_wide(win, " ", 0);
255         }
256         add_text_with_linebreaks(win, text);
257     }
258 }
259
260
261
262 static char * get_kb_line(struct KeyBinding * kb)
263 {
264     char * keyname = get_keyname_to_keycode(kb->keycode);
265     uint16_t size = strlen(keyname) + 3 + strlen(kb->command->dsc_long) + 1;
266     char * kb_line = try_malloc(size, __func__);
267     int test = sprintf(kb_line, "%s - %s", keyname, kb->command->dsc_long);
268     exit_trouble(test < 0, __func__, "sprintf");
269     free(keyname);
270     return kb_line;
271 }
272
273
274
275 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
276                                    uint16_t offset)
277 {
278     if (0 == kbdb->n_of_kbs)
279     {
280         add_line(win, "(none)", 0, &offset, 0);
281         return;
282     }
283     uint8_t kb_n;
284     for (kb_n = 0; kb_n < kbdb->n_of_kbs; kb_n++)
285     {
286         attr_t attri = 0;
287         if (kb_n == kbdb->select)
288         {
289             attri = A_REVERSE;
290             if (1 == kbdb->edit)
291             {
292                 attri = attri | A_BLINK;
293             }
294             win->center.y = win->winmap_size.y;
295         }
296         char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
297         add_line(win, kb_line, attri, &offset, (kbdb->n_of_kbs == kb_n + 1));
298         free(kb_line);
299     }
300 }
301
302
303
304 static char * get_keyname_to_command(char * command_name)
305 {
306     uint8_t i = 0;
307     for (i = 0; i < world.kb_wingeom.n_of_kbs; i++)
308     {
309         if (!strcmp(world.kb_wingeom.kbs[i].command->dsc_short, command_name))
310         {
311             return get_keyname_to_keycode(world.kb_wingeom.kbs[i].keycode);
312         }
313     }
314     return "UNDEFINED";
315 }
316
317
318
319 static char * winconf_geom_helper(struct Win * win, char axis, char * sep,
320                                   char * newlines, char * value_prefix)
321 {
322     char * p0 = 'v'==axis? "Height" : "Width";
323     char * p1 = " to save (grow/shrink/toggle positivity with ";
324     char * p2 = get_keyname_to_command('v'==axis? "grow_v" : "grow_h");
325     char * p4 = get_keyname_to_command('v'==axis? "shri_v" : "shri_v");
326     char * p6 = get_keyname_to_command('v'==axis? "to_height_t" : "to_width_t");
327     char p8[6 + 1];                            /* 6: int16_t value max strlen */
328     int test = sprintf(p8,"%d", 'v'==axis?win->target_height:win->target_width);
329     exit_trouble(test < 0, __func__, "sprintf");
330     char * p9 = " (";
331     char * p10 = "in_cells";
332     if (1 == ('v'==axis? win->target_height_type : win->target_width_type))
333     {
334         p10 = "non-positive diff: cells to screen maximum";
335     }
336     char * p11 = ")";
337     uint8_t size =   strlen(p0) + strlen(p1) + strlen(p2) + strlen(sep)
338                    + strlen(p4) + strlen(sep) + strlen(p6) + strlen(value_prefix)
339                    + strlen(p8) + strlen(p9) + strlen(p10) + strlen(p11)
340                    + strlen(newlines);
341     char * msg = try_malloc(size, __func__);
342     sprintf(msg, "%s%s%s%s%s%s%s%s%s%s%s%s%s", p0, p1, p2, sep, p4, sep, p6,
343             value_prefix, p8, p9, p10, p11, newlines);
344     return msg;
345 }
346
347
348
349 extern void draw_win_log(struct Win * win)
350 {
351     if (!world.log)
352     {
353         return;
354     }
355     draw_text_from_bottom(win, world.log);
356 }
357
358
359
360 extern void draw_win_map(struct Win * win)
361 {
362     init_pair(1, COLOR_WHITE, COLOR_BLUE);
363     init_pair(2, COLOR_BLUE, COLOR_BLACK);
364     attr_t attr_fov = 0;
365     attr_t attr_mem = COLOR_PAIR(2);
366     attr_t attr_sha = COLOR_PAIR(1);
367     try_resize_winmap(win, world.map.length, world.map.length * 2);
368     uint16_t x, y, z;
369     for (y = 0, z = 0; y < world.map.length; y++)
370     {
371         for (x = 0; x < world.map.length; x++)
372         {
373             attr_t attr_c = ' ' == world.mem_map[z] ? attr_sha : attr_mem;
374             chtype c = world.mem_map[z] | attr_c;
375             set_ch_on_yx(win, y, x * 2 + (y % 2), c);
376             if (x + (y % 2) < world.map.length)
377             {
378                 set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | attr_c);
379             }
380             z++;
381         }
382     }
383     for (y = 0, z = 0; y < world.map.length; y++)
384     {
385         for (x = 0; x < world.map.length; x++)
386         {
387             if (' ' != world.map.cells[z])
388             {
389                 chtype c = world.map.cells[z] | attr_fov;
390                 set_ch_on_yx(win, y, x * 2 + (y % 2), c);
391                 if (x + (y % 2) < world.map.length)
392                 {
393                     set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | attr_fov);
394                 }
395             }
396             z++;
397         }
398     }
399 }
400
401
402
403 extern void draw_win_info(struct Win * win)
404 {
405     char * dsc_turn      = "Turn: ";
406     char * dsc_hitpoints = "\nHitpoints: ";
407     uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3;
408     char * text = try_malloc(maxl + 1, __func__);
409     int test = sprintf(text, "%s%d%s%d", dsc_turn, world.turn, dsc_hitpoints,
410                                          world.player_lifepoints);
411     exit_trouble(test < 0, __func__, "sprintf");
412     add_text_with_linebreaks(win, text);
413     free(text);
414 }
415
416
417
418 extern void draw_win_inventory(struct Win * win)
419 {
420     char * limit;
421     char * text_copy = strdup(world.player_inventory);
422     char * start = text_copy;
423     uint8_t i = 0;
424     uint8_t found_selection = 0;
425     uint16_t offset = 0;
426     while (NULL != (limit = strchr(start, '\n')))
427     {
428         (* limit) = '\0';
429         attr_t attri = 0;
430         if (i == world.player_inventory_select)
431         {
432             found_selection = 1;
433             attri = A_REVERSE;
434             win->center.y = win->winmap_size.y;
435         }
436         add_line(win, start, attri, &offset, 0);
437         start = limit + 1;
438         i++;
439     }
440     win->center.y = !found_selection ? win->winmap_size.y : win->center.y;
441     add_line(win, start, !found_selection * A_REVERSE, &offset, 1);
442     free(text_copy);
443 }
444
445
446
447 extern void draw_win_active_windows_keys(struct Win * win)
448 {
449     struct Win * win_active = get_win_by_id(world.winDB.active);
450     struct KeyBindingDB * kbdb = &win_active->kb;
451     if      (1 == win_active->view)
452     {
453         kbdb = &world.kb_wingeom;
454     }
455     else if (2 == win_active->view)
456     {
457         kbdb = &world.kb_winkeys;
458     }
459     uint16_t offset = 0;
460     if (0 == kbdb->n_of_kbs)
461     {
462         add_line(win, "(none)", 0, &offset, 0);
463         return;
464     }
465     uint8_t kb_n;
466     for (kb_n = 0; kb_n < kbdb->n_of_kbs; kb_n++)
467     {
468         char * kb_line = get_kb_line(&kbdb->kbs[kb_n]);
469         add_line(win, kb_line, 0, &offset, (0 == kb_n + 1));
470         free(kb_line);
471     }
472
473 }
474
475
476
477 extern void draw_win_keybindings_global(struct Win * win)
478 {
479     win->center.y = world.kb_global.select;
480     draw_keybinding_config(win, &world.kb_global, 0);
481 }
482
483
484
485 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
486 {
487     win->center.y = world.kb_wingeom.select;
488     draw_keybinding_config(win, &world.kb_wingeom, 0);
489 }
490
491
492
493 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
494 {
495     win->center.y = world.kb_winkeys.select;
496     draw_keybinding_config(win, &world.kb_winkeys, 0);
497 }
498
499
500
501 extern void draw_winconf_keybindings(struct Win * win)
502 {
503     char * title = "Window's keys:";
504     uint16_t offset = 0;
505     add_line(win, title, 0, &offset, 0);
506     add_line(win, " ", 0, &offset, 0);
507     draw_keybinding_config(win, &win->kb, offset);
508 }
509
510
511
512 extern void draw_winconf_geometry(struct Win * win)
513 {
514     char * sep = "/";
515     char * newlines = "\n\n";
516     char * value_prefix = "): ";
517     char * title = "Window's geometry:\n\nShift up/down with ";
518     char * key_shift_b = get_keyname_to_command("shift_b");
519     char * key_shift_f = get_keyname_to_command("shift_f");
520     char * height = winconf_geom_helper(win, 'v', sep, newlines, value_prefix);
521     char * width = winconf_geom_helper(win, 'h', sep, newlines, value_prefix);
522     char * breaks_title = "Linebreak type (toggle with ";
523     char * key_to_break = get_keyname_to_command("to_break");
524     char * breaks_type = "long";
525     if (win->linebreak)
526     {
527         breaks_type = (1 == win->linebreak) ? "wide" : "compact";
528     }
529     uint16_t text_size =   strlen(title) + strlen(key_shift_b) + strlen(sep)
530                          + strlen(key_shift_f) + strlen(newlines)
531                          + strlen(height) + strlen(width)
532                          + strlen(breaks_title) + strlen(key_to_break)
533                          + strlen(value_prefix) + strlen(breaks_type);
534     char * text = try_malloc(text_size + 1, __func__);
535     int test = sprintf(text, "%s%s%s%s%s%s%s%s%s%s%s", title, key_shift_b, sep,
536                        key_shift_f, newlines, height, width, breaks_title,
537                        key_to_break, value_prefix, breaks_type);
538     free(height);
539     free(width);
540     exit_trouble(test < 0, __func__, "sprintf");
541     add_text_with_linebreaks(win, text);
542     free(text);
543 }