8 #include "keybindings.h"
10 void draw_with_linebreaks (struct Win * win, char * text, uint16_t start_y) {
11 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
16 for (y = start_y; y < win->frame.size.y; y++) {
19 for (x = 0; x < win->frame.size.x; x++) {
22 if ('\n' == text[z]) {
26 mvwaddch(win->frame.curses_win, y, x, text[z]);
27 if ('\n' == text[z+1]) {
30 else if (0 == text[z+1]) {
34 void draw_text_from_bottom (struct Win * win, char * text) {
35 // Draw text from end/bottom to the top.
37 uint16_t x, y, offset;
39 for (y = 0; 0 == toggle; y++) // Determine number of lines text would have in
40 for (x = 0; x < win->frame.size.x; x++) { // a window of available width, but infinite height.
42 if ('\n' == text[z]) // Treat \n and \0 as control characters for incrementing y and stopping
43 break; // the loop. Make sure they don't count as cell space themselves.
44 if ('\n' == text[z+1]) {
47 else if (0 == text[z+1]) {
52 if (y < win->frame.size.y) // Depending on what is bigger, determine start point in window or in text.
53 start_y = win->frame.size.y - y;
54 else if (y > win->frame.size.y) {
55 offset = y - win->frame.size.y;
56 for (y = 0; y < offset; y++)
57 for (x = 0; x < win->frame.size.x; x++) {
61 if ('\n' == text[z+1]) {
64 text = text + (sizeof(char) * (z + 1)); }
65 draw_with_linebreaks(win, text, start_y); }
67 void draw_log_win (struct Win * win) {
68 // Draw log text from world struct in win->data from bottom to top.
69 struct World * world = (struct World *) win->data;
70 draw_text_from_bottom(win, world->log); }
72 void draw_map_win (struct Win * win) {
73 // Draw map determined by win->data Map struct into window. Respect offset.
74 struct World * world = (struct World *) win->data;
75 struct Map * map = world->map;
76 struct Player * player = world->player;
77 struct Monster * monster = world->monster;
78 char * cells = map->cells;
79 uint16_t width_map_av = map->width - map->offset_x;
80 uint16_t height_map_av = map->height - map->offset_y;
82 for (y = 0; y < win->frame.size.y; y++) {
83 z = map->offset_x + (map->offset_y + y) * (map->width);
84 for (x = 0; x < win->frame.size.x; x++) {
85 if (y < height_map_av && x < width_map_av) {
86 if (z == (map->width * player->y) + player->x)
87 mvwaddch(win->frame.curses_win, y, x, '@');
88 else if (z == (map->width * monster->y) + monster->x)
89 mvwaddch(win->frame.curses_win, y, x, 'M');
91 mvwaddch(win->frame.curses_win, y, x, cells[z]);
94 void draw_info_win (struct Win * win) {
95 // Draw info window by appending win->data integer value to "Turn: " display.
96 struct World * world = (struct World *) win->data;
97 uint16_t count = world->turn;
99 snprintf(text, 100, "Turn: %d", count);
100 draw_with_linebreaks(win, text, 0); }
102 void draw_horizontal_scroll_hint (struct Win * win, uint16_t y, uint32_t more_lines, char dir) {
103 // Draw scroll hint line in win at row y, announce more_lines more lines in direction dir.
105 char phrase[] = "more lines";
106 char * scrolldesc = malloc((3 * sizeof(char)) + strlen(phrase) + 10); // 10 = max chars for uint32_t string
107 sprintf(scrolldesc, " %d %s ", more_lines, phrase);
109 if (win->frame.size.x > (strlen(scrolldesc) + 1))
110 offset = (win->frame.size.x - strlen(scrolldesc)) / 2;
111 for (x = 0; x < win->frame.size.x; x++)
112 if (x >= offset && x < strlen(scrolldesc) + offset)
113 mvwaddch(win->frame.curses_win, y, x, scrolldesc[x - offset] | A_REVERSE);
115 mvwaddch(win->frame.curses_win, y, x, dir | A_REVERSE);
118 void draw_keys_win (struct Win * win) {
119 // Draw keybindings window.
120 struct World * world = (struct World *) win->data;
121 uint16_t offset = 0, y, x;
122 if (world->keyswindata->max >= win->frame.size.y) {
123 if (world->keyswindata->select > win->frame.size.y / 2) {
124 if (world->keyswindata->select < (world->keyswindata->max - (win->frame.size.y / 2)))
125 offset = world->keyswindata->select - (win->frame.size.y / 2);
127 offset = world->keyswindata->max - win->frame.size.y + 1; } }
128 uint8_t keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
129 char * keydesc = malloc(keydescwidth), * keyname;
131 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++) {
132 if (0 == y && offset > 0) {
133 draw_horizontal_scroll_hint (win, y, offset + 1, '^');
135 else if (win->frame.size.y == y + 1 && 0 < world->keyswindata->max - (win->frame.size.y + offset - 1)) {
136 draw_horizontal_scroll_hint (win, y, world->keyswindata->max - (offset + win->frame.size.y) + 2, 'v');
139 if (y == world->keyswindata->select - offset) {
141 if (1 == world->keyswindata->edit)
142 attri = attri | A_BLINK; }
143 keyname = get_keyname(world->keybindings[y + offset].key);
144 snprintf(keydesc, keydescwidth, "%-9s", keyname);
146 for (x = 0; x < win->frame.size.x; x++)
147 if (x < strlen(keydesc))
148 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
149 else if (strlen(keydesc) < x && x < strlen(world->keybindings[y + offset].name) + strlen(keydesc) + 1)
150 mvwaddch(win->frame.curses_win, y, x,
151 world->keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
153 mvwaddch(win->frame.curses_win, y, x, ' ' | attri); }