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;
78 char * cells = map->cells;
79 uint16_t width_map_av = map->size.x - map->offset.x;
80 uint16_t height_map_av = map->size.y - map->offset.y;
82 for (y = 0; y < win->frame.size.y; y++) {
83 z = map->offset.x + (map->offset.y + y) * (map->size.x);
84 for (x = 0; x < win->frame.size.x; x++) {
85 if (y < height_map_av && x < width_map_av) {
86 mvwaddch(win->frame.curses_win, y, x, cells[z]);
88 if ( player->pos.y >= map->offset.y && player->pos.y < map->offset.y + win->frame.size.y
89 && player->pos.x >= map->offset.x && player->pos.x < map->offset.x + win->frame.size.x)
90 mvwaddch(win->frame.curses_win, player->pos.y - map->offset.y, player->pos.x - map->offset.x, '@');
91 for (monster = world->monster; monster != 0; monster = monster->next)
92 if ( monster->pos.y >= map->offset.y && monster->pos.y < map->offset.y + win->frame.size.y
93 && monster->pos.x >= map->offset.x && monster->pos.x < map->offset.x + win->frame.size.x)
94 mvwaddch(win->frame.curses_win, monster->pos.y - map->offset.y, monster->pos.x - map->offset.x, monster->name); }
96 void draw_info_win (struct Win * win) {
97 // Draw info window by appending win->data integer value to "Turn: " display.
98 struct World * world = (struct World *) win->data;
99 uint16_t count = world->turn;
101 snprintf(text, 100, "Turn: %d", count);
102 draw_with_linebreaks(win, text, 0); }
104 void draw_keys_win (struct Win * win) {
105 // Draw keybindings window.
106 struct World * world = (struct World *) win->data;
107 uint16_t offset = 0, y, x;
108 if (world->keyswindata->max >= win->frame.size.y) {
109 if (world->keyswindata->select > win->frame.size.y / 2) {
110 if (world->keyswindata->select < (world->keyswindata->max - (win->frame.size.y / 2)))
111 offset = world->keyswindata->select - (win->frame.size.y / 2);
113 offset = world->keyswindata->max - win->frame.size.y + 1; } }
114 uint8_t keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
115 char * keydesc = malloc(keydescwidth), * keyname;
117 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++) {
118 if (0 == y && offset > 0) {
119 draw_scroll_hint(&win->frame, y, offset + 1, '^');
121 else if (win->frame.size.y == y + 1 && 0 < world->keyswindata->max - (win->frame.size.y + offset - 1)) {
122 draw_scroll_hint(&win->frame, y, world->keyswindata->max - (offset + win->frame.size.y) + 2, 'v');
125 if (y == world->keyswindata->select - offset) {
127 if (1 == world->keyswindata->edit)
128 attri = attri | A_BLINK; }
129 keyname = get_keyname(world->keybindings[y + offset].key);
130 snprintf(keydesc, keydescwidth, "%-9s", keyname);
132 for (x = 0; x < win->frame.size.x; x++)
133 if (x < strlen(keydesc))
134 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
135 else if (strlen(keydesc) < x && x < strlen(world->keybindings[y + offset].name) + strlen(keydesc) + 1)
136 mvwaddch(win->frame.curses_win, y, x,
137 world->keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
139 mvwaddch(win->frame.curses_win, y, x, ' ' | attri); }