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