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_keys_win (struct Win * win) {
103 // Draw keybindings window.
104 struct World * world = (struct World *) win->data;
105 uint16_t offset = 0, y, x;
106 if (world->keyswindata->max >= win->frame.size.y) {
107 if (world->keyswindata->select > win->frame.size.y / 2) {
108 if (world->keyswindata->select < (world->keyswindata->max - (win->frame.size.y / 2)))
109 offset = world->keyswindata->select - (win->frame.size.y / 2);
111 offset = world->keyswindata->max - win->frame.size.y + 1; } }
112 uint8_t keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
113 char * keydesc = malloc(keydescwidth), * keyname;
115 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++) {
116 if (0 == y && offset > 0) {
117 draw_scroll_hint(&win->frame, y, offset + 1, '^');
119 else if (win->frame.size.y == y + 1 && 0 < world->keyswindata->max - (win->frame.size.y + offset - 1)) {
120 draw_scroll_hint(&win->frame, y, world->keyswindata->max - (offset + win->frame.size.y) + 2, 'v');
123 if (y == world->keyswindata->select - offset) {
125 if (1 == world->keyswindata->edit)
126 attri = attri | A_BLINK; }
127 keyname = get_keyname(world->keybindings[y + offset].key);
128 snprintf(keydesc, keydescwidth, "%-9s", keyname);
130 for (x = 0; x < win->frame.size.x; x++)
131 if (x < strlen(keydesc))
132 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
133 else if (strlen(keydesc) < x && x < strlen(world->keybindings[y + offset].name) + strlen(keydesc) + 1)
134 mvwaddch(win->frame.curses_win, y, x,
135 world->keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
137 mvwaddch(win->frame.curses_win, y, x, ' ' | attri); }