8 #include "keybindings.h"
11 void draw_with_linebreaks (struct Win * win, char * text, uint16_t start_y) {
12 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
17 for (y = start_y; y < win->frame.size.y; y++) {
20 for (x = 0; x < win->frame.size.x; x++) {
23 if ('\n' == text[z]) {
27 mvwaddch(win->frame.curses_win, y, x, text[z]);
28 if ('\n' == text[z+1]) {
31 else if (0 == text[z+1]) {
35 void draw_text_from_bottom (struct Win * win, char * text) {
36 // Draw text from end/bottom to the top.
38 uint16_t x, y, offset;
40 for (y = 0; 0 == toggle; y++) // Determine number of lines text would have in
41 for (x = 0; x < win->frame.size.x; x++) { // a window of available width, but infinite height.
43 if ('\n' == text[z]) // Treat \n and \0 as control characters for incrementing y and stopping
44 break; // the loop. Make sure they don't count as cell space themselves.
45 if ('\n' == text[z+1]) {
48 else if (0 == text[z+1]) {
53 if (y < win->frame.size.y) // Depending on what is bigger, determine start point in window or in text.
54 start_y = win->frame.size.y - y;
55 else if (y > win->frame.size.y) {
56 offset = y - win->frame.size.y;
57 for (y = 0; y < offset; y++)
58 for (x = 0; x < win->frame.size.x; x++) {
62 if ('\n' == text[z+1]) {
65 text = text + (sizeof(char) * (z + 1)); }
66 draw_with_linebreaks(win, text, start_y); }
68 void draw_log_win (struct Win * win) {
69 // Draw log text from world struct in win->data from bottom to top.
70 struct World * world = (struct World *) win->data;
71 draw_text_from_bottom(win, world->log); }
73 void draw_map_win (struct Win * win) {
74 // Draw map determined by win->data Map struct into window. Respect offset.
75 struct World * world = (struct World *) win->data;
76 struct Map * map = world->map;
77 struct Player * player = world->player;
78 struct Monster * monster;
79 char * cells = map->cells;
80 uint16_t width_map_av = map->size.x - map->offset.x;
81 uint16_t height_map_av = map->size.y - map->offset.y;
83 for (y = 0; y < win->frame.size.y; y++) {
84 z = map->offset.x + (map->offset.y + y) * (map->size.x);
85 for (x = 0; x < win->frame.size.x; x++) {
86 if (y < height_map_av && x < width_map_av) {
87 mvwaddch(win->frame.curses_win, y, x, cells[z]);
89 if ( player->pos.y >= map->offset.y && player->pos.y < map->offset.y + win->frame.size.y
90 && player->pos.x >= map->offset.x && player->pos.x < map->offset.x + win->frame.size.x)
91 mvwaddch(win->frame.curses_win, player->pos.y - map->offset.y, player->pos.x - map->offset.x, '@');
92 for (monster = world->monster; monster != 0; monster = monster->next)
93 if ( monster->pos.y >= map->offset.y && monster->pos.y < map->offset.y + win->frame.size.y
94 && monster->pos.x >= map->offset.x && monster->pos.x < map->offset.x + win->frame.size.x)
95 mvwaddch(win->frame.curses_win, monster->pos.y - map->offset.y, monster->pos.x - map->offset.x, monster->name); }
97 void draw_info_win (struct Win * win) {
98 // Draw info window by appending win->data integer value to "Turn: " display.
99 struct World * world = (struct World *) win->data;
100 uint16_t count = world->turn;
102 snprintf(text, 100, "Turn: %d", count);
103 draw_with_linebreaks(win, text, 0); }
105 void draw_keys_win (struct Win * win) {
106 // Draw keybindings window.
107 struct World * world = (struct World *) win->data;
108 uint16_t offset = 0, y, x;
109 if (world->keyswindata->max >= win->frame.size.y) {
110 if (world->keyswindata->select > win->frame.size.y / 2) {
111 if (world->keyswindata->select < (world->keyswindata->max - (win->frame.size.y / 2)))
112 offset = world->keyswindata->select - (win->frame.size.y / 2);
114 offset = world->keyswindata->max - win->frame.size.y + 1; } }
115 uint8_t keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
116 char * keydesc = malloc(keydescwidth), * keyname;
118 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++) {
119 if (0 == y && offset > 0) {
120 draw_scroll_hint(&win->frame, y, offset + 1, '^');
122 else if (win->frame.size.y == y + 1 && 0 < world->keyswindata->max - (win->frame.size.y + offset - 1)) {
123 draw_scroll_hint(&win->frame, y, world->keyswindata->max - (offset + win->frame.size.y) + 2, 'v');
126 if (y == world->keyswindata->select - offset) {
128 if (1 == world->keyswindata->edit)
129 attri = attri | A_BLINK; }
130 keyname = get_keyname(world->keybindings[y + offset].key);
131 snprintf(keydesc, keydescwidth, "%-9s", keyname);
133 for (x = 0; x < win->frame.size.x; x++)
134 if (x < strlen(keydesc))
135 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
136 else if (strlen(keydesc) < x && x < strlen(world->keybindings[y + offset].name) + strlen(keydesc) + 1)
137 mvwaddch(win->frame.curses_win, y, x,
138 world->keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
140 mvwaddch(win->frame.curses_win, y, x, ' ' | attri); }