home · contact · privacy
Define server messages and server message arguments in confclient/commands, instead...
[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" /* get_command_data() */
11 #include "keybindings.h" /* struct KeyBinding, get_keyname_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_keybinding_config(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_keyname_to_keycode(kb_p->key);
240     struct Command * command_data = get_command_data(kb_p->command);
241     char * cmd_dsc = command_data->dsc_long;
242     uint16_t size = 9 + 1 + strlen(cmd_dsc) + 1;
243     char * line = try_malloc(size, f_name);
244     sprintf(line, "%-9s %s", keyname, cmd_dsc);
245     free(keyname);
246     * kb_pp = kb_p->next;
247     return line;
248 }
249
250
251
252 static void draw_keybinding_config(struct Win * w, struct KeyBindingDB * kb,
253                                    uint8_t start)
254 {
255     if (0 == kb->kbs)
256     {
257         add_line(w, "(none)", 0);
258         return;
259     }
260     struct KeyBinding * kb_p = kb->kbs;
261     uint16_t y;
262     for (y = start; 0 != kb_p; y++)
263     {
264         attr_t attri = 0;
265         if (y - start == kb->select)
266         {
267             attri = A_REVERSE;
268             if (1 == kb->edit)
269             {
270                 attri = attri | A_BLINK;
271             }
272         }
273         char * kb_line = get_kb_line_and_iterate(&kb_p);
274         add_line(w, kb_line, attri);
275         free(kb_line);
276     }
277 }
278
279
280
281 static uint16_t draw_titled_keybinding_list(char * title, struct Win * w,
282                                             uint16_t start,
283                                             struct KeyBinding * kb_p)
284 {
285     uint16_t y;
286     uint8_t state = 0;
287     for (y = start; (0 == state || 0 != kb_p); y++)
288     {
289         if (0 == state)
290         {
291             add_line(w, title, 0);
292             y++;
293             add_line(w, " ", 0);
294             state = 1 + (0 == kb_p);
295             continue;
296         }
297         char * kb_line = get_kb_line_and_iterate(&kb_p);
298         add_line(w, kb_line, 0);
299         free(kb_line);
300     }
301     if (2 == state)
302     {
303         char * none = "(none)";
304         add_line(w, none, 0);
305         y++;
306     }
307     return y;
308 }
309
310
311
312 extern void draw_win_log(struct Win * win)
313 {
314     char * log = "";
315     if (world.log)
316     {
317         log = world.log;
318     }
319     draw_text_from_bottom(win, log);
320 }
321
322
323
324 extern void draw_win_map(struct Win * win)
325 {
326     try_resize_winmap(win, world.map.size.y, world.map.size.x);
327     uint16_t z = 0;
328     uint16_t x, y;
329     for (y = 0; y < world.map.size.y; y++)
330     {
331         for (x = 0; x < world.map.size.x; x++)
332         {
333             set_ch_on_yx(win, y, x, world.map.cells[z]);
334             z++;
335         }
336     }
337 }
338
339
340
341 extern void draw_win_info(struct Win * win)
342 {
343     char * dsc_turn      = "Turn: ";
344     char * dsc_hitpoints = "\nHitpoints: ";
345     char * dsc_score     = "\nScore: ";
346     uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
347                     + 5 + 3 + 5; /* Max strlens of strings of numbers to use. */
348     char text[maxl + 1];
349     sprintf(text, "%s%d%s%d%s%d",
350             dsc_turn, world.turn,
351             dsc_hitpoints, world.player_lifepoints,
352             dsc_score, world.score);
353     add_text_with_linebreaks(win, text);
354 }
355
356
357
358 extern void draw_win_inventory(struct Win * win)
359 {
360     win->center.y = world.player_inventory_select;
361     char inventory_copy[strlen(world.player_inventory) + 1];
362     sprintf(inventory_copy, "%s", world.player_inventory);
363     char * foo = inventory_copy;
364     uint8_t i = 0;
365     while (1)
366     {
367         char * object = strtok(foo, "\n");
368         foo = NULL;
369         if (NULL == object)
370         {
371             return;
372         }
373         attr_t attri = 0;
374         if (i == world.player_inventory_select)
375         {
376             attri = A_REVERSE;
377         }
378         add_line(win, object, attri);
379         i++;
380     }
381 }
382
383
384
385 extern void draw_win_available_keybindings(struct Win * win)
386 {
387     char * title = "Active window's keybindings:";
388     struct KeyBinding * kb_p;
389     struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
390     if     (0 == wc->view)
391     {
392         kb_p = wc->kb.kbs;
393     }
394     else if (1 == wc->view)
395     {
396         kb_p = world.kb_wingeom.kbs;
397     }
398     else if (2 == wc->view)
399     {
400         kb_p = world.kb_winkeys.kbs;
401     }
402     uint16_t offset = draw_titled_keybinding_list(title, win, 0, kb_p);
403     add_line(win, " ", 0);
404     struct KeyBinding * kbs_glo = world.kb_global.kbs;
405     draw_titled_keybinding_list("Global keybindings", win, offset + 1, kbs_glo);
406 }
407
408
409
410 extern void draw_win_keybindings_global(struct Win * win)
411 {
412     win->center.y = world.kb_global.select;
413     draw_keybinding_config(win, &world.kb_global, 0);
414 }
415
416
417
418 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
419 {
420     win->center.y = world.kb_wingeom.select;
421     draw_keybinding_config(win, &world.kb_wingeom, 0);
422 }
423
424
425
426 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
427 {
428     win->center.y = world.kb_winkeys.select;
429     draw_keybinding_config(win, &world.kb_winkeys, 0);
430 }
431
432
433
434 extern void draw_winconf_keybindings(struct Win * win)
435 {
436     struct WinConf * wc = get_winconf_by_win(win);
437     char * title = "Window's keybindings:";
438     add_line(win, title, 0);
439     add_line(win, " ", 0);
440     draw_keybinding_config(win, &wc->kb, 2);
441     win->center.y = wc->kb.select + 2;
442 }
443
444
445
446 extern void draw_winconf_geometry(struct Win * win)
447 {
448     struct WinConf * wcp = get_winconf_by_win(win);
449     char * title = "Window's geometry:\n";
450     char * h_d   = "\nHeight to save: ";
451     char * h_pos = " (width in cells)";
452     char * h_neg = " (negative diff: cells to screen width)";
453     char * w_d   = "\n\nWidth to save: ";
454     char * w_pos = " (height in cells)";
455     char * w_neg = " (negative diff: cells to screen height)";
456     char * h_t = h_pos;
457     char * w_t = w_pos;
458     if (1 == wcp->height_type)
459     {
460         h_t = h_neg;
461     }
462     if (1 == wcp->width_type)
463     {
464         w_t = w_neg;
465     }
466     uint16_t maxl = strlen(title)
467                     + strlen(h_t) + strlen(h_d) + 6      /* 6 = n of chars to */
468                     + strlen(w_t) + strlen(w_d) + 6 + 1; /* write max int16_t */
469     char text[maxl + 1];
470     sprintf(text, "%s%s%d%s%s%d%s", title, h_d, wcp->height, h_t,
471                                            w_d, wcp->width,  w_t);
472     add_text_with_linebreaks(win, text);
473 }