| 1 | /* |
| 2 | * ===================================================================================== |
| 3 | * |
| 4 | * Filename: ui.c |
| 5 | * |
| 6 | * Description: Routines to handle the user interface |
| 7 | * |
| 8 | * Version: 1.0 |
| 9 | * Created: 24/04/2017 16:41:15 |
| 10 | * Revision: none |
| 11 | * Compiler: gcc |
| 12 | * |
| 13 | * Author: Jerome Benoit (fraggle), jerome.benoit@piment-noir.org |
| 14 | * Organization: Piment Noir |
| 15 | * |
| 16 | * ===================================================================================== |
| 17 | */ |
| 18 | |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include "ui.h" |
| 22 | #include "debug.h" |
| 23 | #include "list.h" |
| 24 | |
| 25 | /* in all print routine, y and x are the coordinates of the first character of the shape |
| 26 | * which can be a space ' ' */ |
| 27 | |
| 28 | /* FIXME: one can split this shape in building blocks and build it using them */ |
| 29 | void print_board(int y, int x) { |
| 30 | |
| 31 | mvprintw(y, x, " A | B | C | D | E | F | G | H"); |
| 32 | mvprintw(y+1, x, " -----+----+----+----+----+----+----+-----"); |
| 33 | mvprintw(y+2, x, "1| | | | | | | | |"); |
| 34 | mvprintw(y+3, x, " | | | | | | | | |"); |
| 35 | mvprintw(y+4, x, " +----+----+----+----+----+----+----+----+"); |
| 36 | mvprintw(y+5, x, "2| | | | | | | | |"); |
| 37 | mvprintw(y+6, x, " | | | | | | | | |"); |
| 38 | mvprintw(y+7, x, " +----+----+----+----+----+----+----+----+"); |
| 39 | mvprintw(y+8, x, "3| | | | | | | | |"); |
| 40 | mvprintw(y+9, x, " | | | | | | | | |"); |
| 41 | mvprintw(y+10, x, " +----+----+----+----+----+----+----+----+"); |
| 42 | mvprintw(y+11, x, "4| | | | | | | | |"); |
| 43 | mvprintw(y+12, x, " | | | | | | | | |"); |
| 44 | mvprintw(y+13, x, " +----+----+----+----+----+----+----+----+"); |
| 45 | mvprintw(y+14, x, "5| | | | | | | | |"); |
| 46 | mvprintw(y+15, x, " | | | | | | | | |"); |
| 47 | mvprintw(y+16, x, " +----+----+----+----+----+----+----+----+"); |
| 48 | mvprintw(y+17, x, "6| | | | | | | | |"); |
| 49 | mvprintw(y+18, x, " | | | | | | | | |"); |
| 50 | mvprintw(y+19, x, " +----+----+----+----+----+----+----+----+"); |
| 51 | mvprintw(y+20, x, "7| | | | | | | | |"); |
| 52 | mvprintw(y+21, x, " | | | | | | | | |"); |
| 53 | mvprintw(y+22, x, " +----+----+----+----+----+----+----+----+"); |
| 54 | mvprintw(y+23, x, "8| | | | | | | | |"); |
| 55 | mvprintw(y+24, x, " | | | | | | | | |"); |
| 56 | mvprintw(y+25, x, " -----+----+----+----+----+----+----+-----"); |
| 57 | } |
| 58 | |
| 59 | static void set_default_colors() { |
| 60 | |
| 61 | init_pair(1, COLOR_WHITE, COLOR_BLACK); |
| 62 | attron(COLOR_PAIR(1)); |
| 63 | } |
| 64 | |
| 65 | static void print_o(int y, int x, unsigned int type) { |
| 66 | |
| 67 | if (type == black) { |
| 68 | init_color(COLOR_CYAN, 0, 0, 0); /* redefine to a dark black color */ |
| 69 | init_pair(2, COLOR_WHITE, COLOR_CYAN); |
| 70 | attron(COLOR_PAIR(2)); |
| 71 | } else { |
| 72 | init_pair(3, COLOR_BLACK, COLOR_WHITE); |
| 73 | attron(COLOR_PAIR(3)); |
| 74 | } |
| 75 | mvprintw(y, x, "/\\"); |
| 76 | mvprintw(y+1, x, "\\/"); |
| 77 | /* reset to default */ |
| 78 | set_default_colors(); |
| 79 | } |
| 80 | |
| 81 | /* used for pawn placement hints */ |
| 82 | static void print_x(int y, int x, unsigned int type) { |
| 83 | |
| 84 | if (type == hint_allowed) { |
| 85 | init_pair(4, COLOR_GREEN, COLOR_BLACK); |
| 86 | attron(COLOR_PAIR(4)); |
| 87 | } else { |
| 88 | init_pair(5, COLOR_RED, COLOR_BLACK); |
| 89 | attron(COLOR_PAIR(5)); |
| 90 | } |
| 91 | mvprintw(y, x, "\\/"); |
| 92 | mvprintw(y+1, x,"/\\"); |
| 93 | /* reset to default */ |
| 94 | set_default_colors(); |
| 95 | } |
| 96 | |
| 97 | /* y = 1: y -> 0 x = 1: 1 -> 1 |
| 98 | * y > 1: y -> y + 3 x > 1: x -> x + 5 */ |
| 99 | static int remap_y(int y) { |
| 100 | |
| 101 | if (y == 1) { |
| 102 | return 2; |
| 103 | } else if (y > 1 && y <= board_size) { |
| 104 | return (remap_y(y - 1) + 3); |
| 105 | } else { |
| 106 | return -1; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static int remap_x(int x) { |
| 111 | |
| 112 | if (x == 1) { |
| 113 | return 3; |
| 114 | } else if (x > 1 && x <= board_size) { |
| 115 | return (remap_x(x - 1) + 5); |
| 116 | } else { |
| 117 | return -1; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void print_pawns(int base_y, int base_x, unsigned int pawn_array[board_size][board_size]) { |
| 122 | |
| 123 | for (int i = 1; i <= board_size; i++) { |
| 124 | for (int j = 1; j <= board_size; j++) { |
| 125 | if (!is_box_type(i, j, pawn_array, empty)) { |
| 126 | print_o(base_y + remap_y(i), base_x + remap_x(j), get_box_value(i, j, pawn_array)); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void print_shots_list(int base_y, int base_x, struct shots_list_s* shots_list) { |
| 133 | struct shots_list_s* list_counter; |
| 134 | |
| 135 | list_for_each_entry(list_counter, &shots_list->list, list) { |
| 136 | print_x(base_y + remap_y(list_counter->y), base_x + remap_x(list_counter->x), list_counter->type); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | int map_col_letter_to_index(char c) { |
| 141 | |
| 142 | if (c == 'a' || c == 'A') { |
| 143 | return 1; |
| 144 | } else if (c == 'b' || c == 'B') { |
| 145 | return 2; |
| 146 | } else if (c == 'c' || c == 'C') { |
| 147 | return 3; |
| 148 | } else if (c == 'd' || c == 'D') { |
| 149 | return 4; |
| 150 | } else if (c == 'e' || c == 'E') { |
| 151 | return 5; |
| 152 | } else if (c == 'f' || c == 'F') { |
| 153 | return 6; |
| 154 | } else if (c == 'g' || c == 'G') { |
| 155 | return 7; |
| 156 | } else if (c == 'h' || c == 'H') { |
| 157 | return 8; |
| 158 | } else { |
| 159 | return -1; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* return capital letters */ |
| 164 | char map_col_index_to_letter(int index) { |
| 165 | |
| 166 | if (index == 1) { |
| 167 | return 'A'; |
| 168 | } else if (index == 2) { |
| 169 | return 'B'; |
| 170 | } else if (index == 3) { |
| 171 | return 'C'; |
| 172 | } else if (index == 4) { |
| 173 | return 'D'; |
| 174 | } else if (index == 5) { |
| 175 | return 'E'; |
| 176 | } else if (index == 6) { |
| 177 | return 'F'; |
| 178 | } else if (index == 7) { |
| 179 | return 'G'; |
| 180 | } else if (index == 9) { |
| 181 | return 'H'; |
| 182 | } else { |
| 183 | return -1; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | int prompt_values(WINDOW* windows, int base_y, int base_x, const char* msg, int* y, char* x) { |
| 188 | mvwprintw(windows, base_y, base_x, "%s\n", msg); |
| 189 | int retval = mvwscanw(windows, base_y + 1, base_x + strlen(msg)/2, "%d%c", y, x); |
| 190 | return (retval == 1) ? 0 : 1; |
| 191 | } |