home · contact · privacy
MAJOR re-write. Split plomrogue into a server and a client. Re-wrote large parts
[plomrogue] / src / client / draw_wins.c
1 /* src/client/draw_wins.c */
2
3 #include "draw_wins.h"
4 #include <ncurses.h> /* attri_t, chtype */
5 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int16_t */
6 #include <stdio.h> /* for sprintf() */
7 #include <stdlib.h> /* free() */
8 #include <string.h> /* strlen(), strtok() */
9 #include "../common/try_malloc.h" /* for try_malloc() */
10 #include "command_db.h" /* for get_command_longdsc */
11 #include "keybindings.h" /* struct KeyBinding, get_name_to_keycode() */
12 #include "wincontrol.h" /* struct WinConf, get_winconf_by_win() */
13 #include "windows.h" /* struct Win */
14 #include "world.h" /* global world */
15
16
17
18 /* Apply to the winmap of Win "w" the new sizes "new_size_y" and "new_size_x"
19  * to the degree that they extend it. Re-shape the window content accordingly.
20  */
21 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x);
22
23 /* In Win "w", write "ch" to coordinate "y"/"x". */
24 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch);
25
26 /* Add "text" into window "win". Break text at right window edge. Also break at
27  * newlines. If "text" ends in a newline, ignore it.
28  */
29 static void add_text_with_linebreaks(struct Win * win, char * text);
30
31 /* Add "line" into window "w". Apply ncurses attribute "attri" to all
32  * characters drawn. If "attri" is non-zero, fill the entire line until the
33  * right window edge with empty characters, so "attri" applies on those too.
34  */
35 static void add_line(struct Win * w, char * line, attr_t attri);
36
37 /* Write "text" with add_text_with_linebreaks() as not starting from the top but
38  * from bottom of "win". Draw only what fits in window (avoid scroll hints).
39  */
40 static void draw_text_from_bottom(struct Win * win, char * text);
41
42 /* Return keybinding list line via "kb_pp", iterate pointer pointed to by it. */
43 static char * get_kb_line_and_iterate(struct KeyBinding ** kb_pp);
44
45 /* Draw from line "start" on config view for keybindings defined at "kb". */
46 static void draw_kb_view(struct Win * w, struct KeyBindingDB * kb,
47                          uint8_t start);
48
49 /* Draw into window "w" from line "start" on a "title" followed by an empty
50  * line followed by a list of all keybindings starting at kb_p.
51  */
52 static uint16_t draw_titled_keybinding_list(char * title, struct Win * w,
53                                             uint16_t start,
54                                             struct KeyBinding * kb_p);
55
56
57
58 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x)
59 {
60     char * f_name = "try_resize_winmap()";
61     if (w->winmapsize.y >= new_size_y && w->winmapsize.x >= new_size_x)
62     {
63         return;
64     }
65     if      (w->winmapsize.y > new_size_y)
66     {
67         new_size_y = w->winmapsize.y;
68     }
69     else if (w->winmapsize.x > new_size_x)
70     {
71         new_size_x = w->winmapsize.x;
72     }
73     chtype * old_winmap = w->winmap;
74     uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
75     w->winmap = try_malloc(new_size, f_name);
76     uint16_t y, x;
77     for (y = 0; y < new_size_y; y++)
78     {
79         for (x = 0; y < w->winmapsize.y && x < w->winmapsize.x; x++)
80         {
81             chtype ch = old_winmap[(y * w->winmapsize.x) + x];
82             w->winmap[(y * new_size_x) + x] = ch;
83         }
84         for (; x < new_size_x; x++)
85         {
86             w->winmap[(y * new_size_x) + x] = ' ';
87         }
88     }
89     free(old_winmap);
90     w->winmapsize.y = new_size_y;
91     w->winmapsize.x = new_size_x;
92 }
93
94
95
96 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch)
97 {
98     w->winmap[(y * w->winmapsize.x) + x] = ch;
99 }
100
101
102
103 static void add_text_with_linebreaks(struct Win * win, char * text)
104 {
105     uint16_t x, y;
106     int16_t z = -1;
107     for (y = win->winmapsize.y; ; y++)
108     {
109         try_resize_winmap(win, y + 1, win->framesize.x);
110         for (x = 0; x < win->framesize.x; x++)
111         {
112             z++;
113             if      ('\n' == text[z])
114             {
115                 break;
116             }
117             else if ('\0' == text[z])
118             {
119                 return;
120             }
121             else
122             {
123                 set_ch_on_yx(win, y, x, text[z]);
124             }
125             if      ('\n' == text[z+1])
126             {
127                 z++;
128                 break;
129             }
130             else if ('\0' == text[z+1])
131             {
132                 return;
133             }
134         }
135     }
136 }
137
138
139
140 static void add_line(struct Win * w, char * line, attr_t attri)
141 {
142     uint16_t y = w->winmapsize.y;
143     uint16_t len_line = strlen(line);
144     if (attri
145         && w->winmapsize.x < w->framesize.x && w->framesize.x > len_line)
146     {
147         try_resize_winmap(w, y + 1, w->framesize.x);
148     }
149     else
150     {
151         try_resize_winmap(w, y + 1, strlen(line));
152     }
153     uint16_t x = 0;
154     for (; x < len_line; x++)
155     {
156         set_ch_on_yx(w, y, x, line[x] | attri);
157     }
158     if (attri)
159     {
160         for (; x < w->framesize.x; x++)
161         {
162             set_ch_on_yx(w, y, x, ' ' | attri);
163         }
164     }
165 }
166
167
168
169 static void draw_text_from_bottom(struct Win * win, char * text)
170 {
171     /* Determine number of lines text would have in a window of win's width,
172      * but infinite height. Treat \n and \0 as control chars for incrementing
173      * y and stopping the loop. Make sure +they* don't count as cell space.
174      */
175     char toggle = 0;
176     uint16_t x, y;
177     int16_t z = -1;
178     for (y = 0; 0 == toggle; y++)
179     {
180         for (x = 0; x < win->framesize.x; x++)
181         {
182             z++;
183             if ('\n' == text[z])
184             {
185                 break;
186             }
187             if ('\n' == text[z+1])
188             {
189                 z++;
190                 break;
191             }
192             else if (0 == text[z+1])
193             {
194                 toggle = 1;
195                 break;
196             }
197         }
198     }
199     z = -1;
200
201     /* Depending on what's bigger, determine start point in window or text. */
202     uint16_t start_y = 0;
203     if (y < win->framesize.y)
204     {
205         start_y = win->framesize.y - y;
206     }
207     else if (y > win->framesize.y)
208     {
209         uint16_t offset = y - win->framesize.y;
210         for (y = 0; y < offset; y++)
211         {
212             for (x = 0; x < win->framesize.x; x++)
213             {
214                 z++;
215                 if ('\n' == text[z])
216                 {
217                     break;
218                 }
219                 if ('\n' == text[z+1])
220                 {
221                     z++;
222                     break;
223                 }
224             }
225         }
226         text = text + (sizeof(char) * (z + 1));
227     }
228
229     try_resize_winmap(win, start_y, 1);
230     add_text_with_linebreaks(win, text);
231 }
232
233
234
235 static char * get_kb_line_and_iterate(struct KeyBinding ** kb_pp)
236 {
237     char * f_name = "get_kb_line_and_iterate()";
238     struct KeyBinding * kb_p = * kb_pp;
239     char * keyname = get_name_to_keycode(kb_p->key);
240     char * cmd_dsc = get_command_longdsc(kb_p->name);
241     uint16_t size = 9 + 1 + strlen(cmd_dsc) + 1;
242     char * line = try_malloc(size, f_name);
243     sprintf(line, "%-9s %s", keyname, cmd_dsc);
244     free(keyname);
245     * kb_pp = kb_p->next;
246     return line;
247 }
248
249
250
251 static void draw_kb_view(struct Win * w, struct KeyBindingDB * kb,
252                          uint8_t start)
253 {
254     if (0 == kb->kbs)
255     {
256         add_line(w, "(none)", 0);
257         return;
258     }
259     struct KeyBinding * kb_p = kb->kbs;
260     uint16_t y;
261     for (y = start; 0 != kb_p; y++)
262     {
263         attr_t attri = 0;
264         if (y - start == kb->select)
265         {
266             attri = A_REVERSE;
267             if (1 == kb->edit)
268             {
269                 attri = attri | A_BLINK;
270             }
271         }
272         char * kb_line = get_kb_line_and_iterate(&kb_p);
273         add_line(w, kb_line, attri);
274         free(kb_line);
275     }
276 }
277
278
279
280 static uint16_t draw_titled_keybinding_list(char * title, struct Win * w,
281                                             uint16_t start,
282                                             struct KeyBinding * kb_p)
283 {
284     uint16_t y;
285     uint8_t state = 0;
286     for (y = start; (0 == state || 0 != kb_p); y++)
287     {
288         if (0 == state)
289         {
290             add_line(w, title, 0);
291             y++;
292             add_line(w, " ", 0);
293             state = 1 + (0 == kb_p);
294             continue;
295         }
296         char * kb_line = get_kb_line_and_iterate(&kb_p);
297         add_line(w, kb_line, 0);
298         free(kb_line);
299     }
300     if (2 == state)
301     {
302         char * none = "(none)";
303         add_line(w, none, 0);
304         y++;
305     }
306     return y;
307 }
308
309
310
311 extern void draw_win_log(struct Win * win)
312 {
313     char * log = "";
314     if (world.log)
315     {
316         log = world.log;
317     }
318     draw_text_from_bottom(win, log);
319 }
320
321
322
323 extern void draw_win_map(struct Win * win)
324 {
325     try_resize_winmap(win, world.map.size.y, world.map.size.x);
326     uint16_t z = 0;
327     uint16_t x, y;
328     for (y = 0; y < world.map.size.y; y++)
329     {
330         for (x = 0; x < world.map.size.x; x++)
331         {
332             set_ch_on_yx(win, y, x, world.map.cells[z]);
333             z++;
334         }
335     }
336 }
337
338
339
340 extern void draw_win_info(struct Win * win)
341 {
342     char * dsc_turn      = "Turn: ";
343     char * dsc_hitpoints = "\nHitpoints: ";
344     char * dsc_score     = "\nScore: ";
345     uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
346                     + 5 + 3 + 5; /* Max strlens of strings of numbers to use. */
347     char text[maxl + 1];
348     sprintf(text, "%s%d%s%d%s%d",
349             dsc_turn, world.turn,
350             dsc_hitpoints, world.player_lifepoints,
351             dsc_score, world.score);
352     add_text_with_linebreaks(win, text);
353 }
354
355
356
357 extern void draw_win_inventory(struct Win * win)
358 {
359     win->center.y = world.player_inventory_select;
360     char inventory_copy[strlen(world.player_inventory) + 1];
361     sprintf(inventory_copy, "%s", world.player_inventory);
362     char * foo = inventory_copy;
363     uint8_t i = 0;
364     while (1)
365     {
366         char * object = strtok(foo, "\n");
367         foo = NULL;
368         if (NULL == object)
369         {
370             return;
371         }
372         attr_t attri = 0;
373         if (i == world.player_inventory_select)
374         {
375             attri = A_REVERSE;
376         }
377         add_line(win, object, attri);
378         i++;
379     }
380 }
381
382
383
384 extern void draw_win_available_keybindings(struct Win * win)
385 {
386     char * title = "Active window's keybindings:";
387     struct KeyBinding * kb_p;
388     struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
389     if     (0 == wc->view)
390     {
391         kb_p = wc->kb.kbs;
392     }
393     else if (1 == wc->view)
394     {
395         kb_p = world.kb_wingeom.kbs;
396     }
397     else if (2 == wc->view)
398     {
399         kb_p = world.kb_winkeys.kbs;
400     }
401     uint16_t offset = draw_titled_keybinding_list(title, win, 0, kb_p);
402     add_line(win, " ", 0);
403     struct KeyBinding * kbs_glo = world.kb_global.kbs;
404     draw_titled_keybinding_list("Global keybindings", win, offset + 1, kbs_glo);
405 }
406
407
408
409 extern void draw_win_keybindings_global(struct Win * win)
410 {
411     win->center.y = world.kb_global.select;
412     draw_kb_view(win, &world.kb_global, 0);
413 }
414
415
416
417 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
418 {
419     win->center.y = world.kb_wingeom.select;
420     draw_kb_view(win, &world.kb_wingeom, 0);
421 }
422
423
424
425 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
426 {
427     win->center.y = world.kb_winkeys.select;
428     draw_kb_view(win, &world.kb_winkeys, 0);
429 }
430
431
432
433 extern void draw_winconf_keybindings(struct Win * win)
434 {
435     struct WinConf * wc = get_winconf_by_win(win);
436     char * title = "Window's keybindings:";
437     add_line(win, title, 0);
438     add_line(win, " ", 0);
439     draw_kb_view(win, &wc->kb, 2);
440     win->center.y = wc->kb.select + 2;
441 }
442
443
444
445 extern void draw_winconf_geometry(struct Win * win)
446 {
447     struct WinConf * wcp = get_winconf_by_win(win);
448     char * title = "Window's geometry:\n";
449     char * h_d   = "\nHeight to save: ";
450     char * h_pos = " (width in cells)";
451     char * h_neg = " (negative diff: cells to screen width)";
452     char * w_d   = "\n\nWidth to save: ";
453     char * w_pos = " (height in cells)";
454     char * w_neg = " (negative diff: cells to screen height)";
455     char * h_t = h_pos;
456     char * w_t = w_pos;
457     if (1 == wcp->height_type)
458     {
459         h_t = h_neg;
460     }
461     if (1 == wcp->width_type)
462     {
463         w_t = w_neg;
464     }
465     uint16_t maxl = strlen(title)
466                     + strlen(h_t) + strlen(h_d) + 6      /* 6 = n of chars to */
467                     + strlen(w_t) + strlen(w_d) + 6 + 1; /* write max int16_t */
468     char text[maxl + 1];
469     sprintf(text, "%s%s%d%s%s%d%s", title, h_d, wcp->height, h_t,
470                                            w_d, wcp->width,  w_t);
471     add_text_with_linebreaks(win, text);
472 }