home · contact · privacy
In the client, different modes of treating linebreaks are avaiable for
[plomrogue] / src / client / draw_wins.c
1 /* src/client/draw_wins.c */
2
3 #include "draw_wins.h"
4 #include <ncurses.h> /* typedefs attr_t, chtype, define A_REVERSE */
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
7 #include <stdio.h> /* sprintf() */
8 #include <stdlib.h> /* free() */
9 #include <string.h> /* memset(), strchr(), strdup/(), strlen() */
10 #include "../common/rexit.h" /* exit_err() */
11 #include "../common/try_malloc.h" /* try_malloc() */
12 #include "../common/yx_uint16.h" /* struct yx_uint16 */
13 #include "keybindings.h" /* struct KeyBindingDB, get_keyname_to_keycode() */
14 #include "windows.h" /* struct 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, uint8_t linebreak_type);
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     char * f_name = "try_resize_winmap()";
73     if (win->winmap_size.y >= new_size_y && win->winmap_size.x >= new_size_x)
74     {
75         return;
76     }
77     if      (win->winmap_size.y > new_size_y)
78     {
79         new_size_y = win->winmap_size.y;
80     }
81     else if (win->winmap_size.x > new_size_x)
82     {
83         new_size_x = win->winmap_size.x;
84     }
85     chtype * old_winmap = win->winmap;
86     uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
87     win->winmap = try_malloc(new_size, f_name);
88     uint16_t y, x;
89     for (y = 0; y < new_size_y; y++)
90     {
91         for (x = 0; y < win->winmap_size.y && x < win->winmap_size.x; x++)
92         {
93             chtype ch = old_winmap[(y * win->winmap_size.x) + x];
94             win->winmap[(y * new_size_x) + x] = ch;
95         }
96         for (; x < new_size_x; x++)
97         {
98             win->winmap[(y * new_size_x) + x] = ' ';
99         }
100     }
101     free(old_winmap);
102     win->winmap_size.y = new_size_y;
103     win->winmap_size.x = new_size_x;
104 }
105
106
107
108 static void set_ch_on_yx(struct Win * win, int y, int x, chtype ch)
109 {
110     win->winmap[(y * win->winmap_size.x) + x] = ch;
111 }
112
113
114
115 static void add_line(struct Win * win, char * line, attr_t attri,
116                      uint16_t * offset, uint8_t last)
117 {
118     exit_err(strlen(line) > UINT16_MAX, "Line in add_line() too big.");
119     if      (0 == win->linebreak)
120     {
121         add_line_long(win, line, attri);
122     }
123     else if (1 == win->linebreak)
124     {
125         add_line_wide(win, line, attri);
126     }
127     else if (2 == win->linebreak)
128     {
129         add_line_compact(win, line, attri, offset, last);
130     }
131 }
132
133
134
135 static void add_line_wide(struct Win * win, char * line, attr_t attri)
136 {
137     uint16_t y_start = win->winmap_size.y;
138     uint16_t len_line = strlen(line);
139     try_resize_winmap(win, y_start + 1, win->frame_size.x);
140     try_resize_winmap(win, y_start + 1, len_line);
141     uint16_t x;
142     for (x = 0; x < len_line; x++)
143     {
144         set_ch_on_yx(win, y_start, x, line[x] | attri);
145     }
146     if (attri)
147     {
148         for (; x < win->frame_size.x; x++)
149         {
150             set_ch_on_yx(win, y_start, x, ' ' | attri);
151         }
152     }
153 }
154
155
156
157 static void add_line_long(struct Win * win, char * line, attr_t attri)
158 {
159     uint16_t y_start = win->winmap_size.y;
160     uint16_t len_line = strlen(line);
161     try_resize_winmap(win, y_start + 1, win->frame_size.x);
162     uint16_t x, y, z;
163     for (z = 0, y = y_start; z < len_line; y++)
164     {
165         for (x = 0; x < win->winmap_size.x && z < len_line; x++, z++)
166         {
167             set_ch_on_yx(win, y, x, line[z] | attri);
168         }
169         if (z < len_line)
170         {
171             try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
172         }
173     }
174 }
175
176
177
178 static void add_line_compact(struct Win * win, char * line, attr_t attri,
179                              uint16_t * offset, uint8_t last)
180 {
181     uint16_t y_start = win->winmap_size.y - (win->winmap_size.y > 0);
182     try_resize_winmap(win, y_start + 1, win->frame_size.x);
183     uint16_t len_line = strlen(line);
184     char * separator = last ? "" : " / ";
185     uint32_t len_line_new = len_line + strlen(separator);
186     char line_new[len_line_new];
187     sprintf(line_new, "%s%s", line, separator);
188     uint16_t x, y;
189     uint32_t z;
190     for (z = 0, y = y_start; z < len_line_new; y++)
191     {
192         for (x = * offset; x < win->winmap_size.x && z < len_line_new; x++, z++)
193         {
194             set_ch_on_yx(win, y, x, line_new[z] | attri);
195             if (z + 1 == (uint32_t) len_line)
196             {
197                attri = 0;
198             }
199         }
200         * offset = 0;
201         if (z < len_line_new)
202         {
203             try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
204         }
205     }
206     * offset = x;
207 }
208
209
210
211 static void add_text_with_linebreaks(struct Win * win, char * text)
212 {
213     char * limit;
214     uint16_t offset = 0;
215     char * text_copy = strdup(text);
216     char * start = text_copy;
217     while (NULL != (limit = strchr(start, '\n')))
218     {
219         (* limit) = '\0';
220         add_line(win, start, 0, &offset, 0);
221         start = limit + 1;
222     }
223     add_line(win, start, 0, &offset, 1);
224     free(text_copy);
225 }
226
227
228
229 static void draw_text_from_bottom(struct Win * win, char * text)
230 {
231     add_text_with_linebreaks(win, text);
232     if (win->winmap_size.y > win->frame_size.y)
233     {
234         win->center.y = win->winmap_size.y - 1;
235     }
236     else if (win->winmap_size.y < win->frame_size.y)
237     {
238         uint16_t new_y_start = win->frame_size.y - win->winmap_size.y;
239         memset(&win->winmap_size, 0, sizeof(struct yx_uint16));
240         free(win->winmap);
241         win->winmap = NULL;
242         do
243         {
244             add_line_wide(win, " ", 0);
245         }
246         while (new_y_start > win->winmap_size.y);
247         if (2 == win->linebreak) /* add_text_with_linebreaks() will start not */
248         {                        /* not after, but within the last line then. */
249             add_line_wide(win, " ", 0);
250         }
251         add_text_with_linebreaks(win, text);
252     }
253 }
254
255
256
257 static char * get_kb_line(struct KeyBinding * kb, uint8_t linebreak_type)
258 {
259     char * f_name = "get_kb_line()";
260     char * keyname = get_keyname_to_keycode(kb->keycode);
261     char * format = "%-9s %s";
262     uint16_t first_size = 9;
263     if (1 != linebreak_type)
264     {
265         format = "%s: %s";
266         first_size = strlen(keyname) + 1;
267     }
268     uint16_t size = first_size + 1 + strlen(kb->command->dsc_long) + 1;
269     char * kb_line = try_malloc(size, f_name);
270     sprintf(kb_line, format, keyname, kb->command->dsc_long);
271     free(keyname);
272     return kb_line;
273 }
274
275
276
277 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
278                                    uint16_t offset)
279 {
280     if (0 == kbdb->n_of_kbs)
281     {
282         add_line(win, "(none)", 0, &offset, 0);
283         return;
284     }
285     uint16_t kb_n;
286     for (kb_n = 0; kb_n < kbdb->n_of_kbs; kb_n++)
287     {
288         attr_t attri = 0;
289         if (kb_n == kbdb->select)
290         {
291             attri = A_REVERSE;
292             if (1 == kbdb->edit)
293             {
294                 attri = attri | A_BLINK;
295             }
296             win->center.y = win->winmap_size.y;
297         }
298         char * kb_line = get_kb_line(&kbdb->kbs[kb_n], win->linebreak);
299         add_line(win, kb_line, attri, &offset, (kbdb->n_of_kbs == kb_n + 1));
300         free(kb_line);
301     }
302 }
303
304
305
306 static void draw_titled_keybinding_list(char * title, struct Win * win,
307                                         uint16_t * offset, uint8_t last,
308                                         struct KeyBindingDB * kbdb)
309 {
310     uint8_t state = 0;
311     uint16_t kb_n = 0;
312     for (; (0 == state || kb_n < kbdb->n_of_kbs); kb_n++)
313     {
314         if (0 == state)
315         {
316             add_line(win, title, 0, offset, 0);
317             add_line(win, " ", 0, offset, 0);
318             state = 1 + (0 == kbdb->n_of_kbs);
319             continue;
320         }
321         char * kb_line = get_kb_line(&kbdb->kbs[kb_n], win->linebreak);
322         add_line(win, kb_line, 0, offset, (last * kbdb->n_of_kbs == kb_n + 1));
323         free(kb_line);
324     }
325     if (2 == state)
326     {
327         add_line(win, "(none)", 0, offset, last);
328     }
329 }
330
331
332
333 extern void draw_win_log(struct Win * win)
334 {
335     if (!world.log)
336     {
337         return;
338     }
339     draw_text_from_bottom(win, world.log);
340 }
341
342
343
344 extern void draw_win_map(struct Win * win)
345 {
346     try_resize_winmap(win, world.map.size.y, world.map.size.x);
347     uint16_t z = 0;
348     uint16_t x, y;
349     for (y = 0; y < world.map.size.y; y++)
350     {
351         for (x = 0; x < world.map.size.x; x++)
352         {
353             set_ch_on_yx(win, y, x, world.map.cells[z]);
354             z++;
355         }
356     }
357 }
358
359
360
361 extern void draw_win_info(struct Win * win)
362 {
363     char * dsc_turn      = "Turn: ";
364     char * dsc_hitpoints = "\nHitpoints: ";
365     uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3;
366     char text[maxl + 1];
367     sprintf(text, "%s%d%s%d",
368             dsc_turn, world.turn, dsc_hitpoints, world.player_lifepoints);
369     add_text_with_linebreaks(win, text);
370 }
371
372
373
374 extern void draw_win_inventory(struct Win * win)
375 {
376     char * limit;
377     char * text_copy = strdup(world.player_inventory);
378     char * start = text_copy;
379     uint8_t i = 0;
380     uint8_t found_selection = 0;
381     uint16_t offset = 0;
382     while (NULL != (limit = strchr(start, '\n')))
383     {
384         (* limit) = '\0';
385         attr_t attri = 0;
386         if (i == world.player_inventory_select)
387         {
388             found_selection = 1;
389             attri = A_REVERSE;
390             win->center.y = win->winmap_size.y;
391         }
392         add_line(win, start, attri, &offset, 0);
393         start = limit + 1;
394         i++;
395     }
396     win->center.y = !found_selection ? win->winmap_size.y : win->center.y;
397     add_line(win, start, !found_selection * A_REVERSE, &offset, 1);
398     free(text_copy);
399 }
400
401
402
403 extern void draw_win_available_keybindings(struct Win * win)
404 {
405     char * title = "Active window's keybindings:";
406     struct KeyBindingDB * kbdb;
407     struct Win * win_active = get_win_by_id(world.winDB.active);
408     if     (0 == win_active->view)
409     {
410         kbdb = &win_active->kb;
411     }
412     else if (1 == win_active->view)
413     {
414         kbdb = &world.kb_wingeom;
415     }
416     else if (2 == win_active->view)
417     {
418         kbdb = &world.kb_winkeys;
419     }
420     uint16_t offset = 0;
421     draw_titled_keybinding_list(title, win, &offset, 0, kbdb);
422     add_line(win, " ", 0, &offset, 0);
423     title = "Global keybindings:";
424     draw_titled_keybinding_list(title, win, &offset, 1, &world.kb_global);
425 }
426
427
428
429
430 extern void draw_win_keybindings_global(struct Win * win)
431 {
432     win->center.y = world.kb_global.select;
433     draw_keybinding_config(win, &world.kb_global, 0);
434 }
435
436
437
438 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
439 {
440     win->center.y = world.kb_wingeom.select;
441     draw_keybinding_config(win, &world.kb_wingeom, 0);
442 }
443
444
445
446 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
447 {
448     win->center.y = world.kb_winkeys.select;
449     draw_keybinding_config(win, &world.kb_winkeys, 0);
450 }
451
452
453
454 extern void draw_winconf_keybindings(struct Win * win)
455 {
456     char * title = "Window's keybindings:";
457     uint16_t offset = 0;
458     add_line(win, title, 0, &offset, 0);
459     add_line(win, " ", 0, &offset, 0);
460     draw_keybinding_config(win, &win->kb, offset);
461 }
462
463
464
465 extern void draw_winconf_geometry(struct Win * win)
466 {
467     char * title = "Window's geometry:\n\n";
468     char * h_title = "Height to save: ";
469     char h_value[6 + 1];                       /* 6: int16_t value max strlen */
470     sprintf(h_value, "%d", win->target_height);
471     char * h_plus = " (width in cells)\n\n";
472     char * h_minus = " (negative diff: cells to screen width)\n\n";
473     char * h_type = (1 == win->target_height_type) ? h_minus : h_plus;
474     char * w_title = "Width to save: ";
475     char w_value[6 + 1];
476     sprintf(w_value, "%d", win->target_width);
477     char * w_plus = "(height in cells)\n\n";
478     char * w_minus = " (negative diff: cells to screen height)\n\n";
479     char * w_type = (1 == win->target_width_type)  ? w_minus : w_plus;
480     char * breaks_title = "Linebreak type: ";
481     char * breaks_type = (1 == win->linebreak) ? "wide" : "long";
482     breaks_type        = (2 == win->linebreak) ? "compact" : breaks_type;
483     uint16_t text_size =   strlen(title)
484                          + strlen(h_title) + strlen(h_value) + strlen(h_type)
485                          + strlen(w_title) + strlen(w_value) + strlen(w_type)
486                          + strlen(breaks_title) + strlen(breaks_type);
487     char text[text_size + 1];
488     sprintf(text, "%s%s%s%s%s%s%s%s%s", title, h_title, h_value, h_type,
489             w_title, w_value, w_type, breaks_title, breaks_type);
490     add_text_with_linebreaks(win, text);
491 }