From da769df0a9613300c8e4ec8a2832c65e052d6e22 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Fri, 8 Apr 2022 23:09:03 -0700 Subject: add colours --- dartbot.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 12 deletions(-) diff --git a/dartbot.c b/dartbot.c index 8289ac8..dd48d1b 100644 --- a/dartbot.c +++ b/dartbot.c @@ -391,6 +391,49 @@ void test_curses_match(int start_points) endwin(); } +void init_colours() +{ + init_pair(1, 82, 0); // 140 + init_pair(2, 154, 0); // 100 + init_pair(3, 226, 0); // 60 + init_pair(4, 214, 0); // 40 + init_pair(5, 202, 0); // 20 + init_pair(6, 196, 0); // 0 + init_pair(7, 235, 0); // darts + init_pair(8, 244, 0); // visit +} + +int points_colour(int points) +{ + if (points >= 180) + return COLOR_PAIR(1) | A_BOLD; + else if (points >= 140) + return COLOR_PAIR(1); + else if (points >= 100) + return COLOR_PAIR(2); + else if (points >= 60) + return COLOR_PAIR(3); + else if (points >= 40) + return COLOR_PAIR(4); + else if (points >= 20) + return COLOR_PAIR(5); + else if (points > 0) + return COLOR_PAIR(6); + else + return COLOR_PAIR(6) | A_BOLD; +} + +void flushbuf(WINDOW *w, char *buf, int *buflen, int col) +{ + if (!(*buflen)) return; + + if (col) wattron(w, col); + waddstr(w, buf); + if (col) wattroff(w, col); + + *buflen = 0; +} + void draw2(WINDOW *w, struct leg *l1, struct leg *l2) { werase(w); @@ -407,34 +450,45 @@ void draw2(WINDOW *w, struct leg *l1, struct leg *l2) int start_visit = (LINES - 3 > n_visits) ? 0 : (n_visits - (LINES - 3)); if (start_visit == 0 && LINES - 3 > n_visits) { wmove(w, (offset++)-start_visit, 2); - sprintf(buf, "(%2d) %3d", 0, l1->start); - waddstr(w, buf); + buflen = sprintf(buf, "(%2d)", 0); + flushbuf(w, buf, &buflen, COLOR_PAIR(8)); + buflen = sprintf(buf, " %3d %3d", l1->start, l2->start); + flushbuf(w, buf, &buflen, 0); } for (int i = start_visit; i < n_visits; ++i) { wmove(w, offset+i-start_visit, 2); - buflen += sprintf(buf, "(%2d)", i+1); + buflen += sprintf(buf + buflen, "(%2d)", i+1); + flushbuf(w, buf, &buflen, COLOR_PAIR(8)); + buflen += sprintf(buf + buflen, " "); + flushbuf(w, buf, &buflen, 0); - struct visit *v; + struct visit *v = l1->visits + i; - if (i < l1->n_visits) { - v = l1->visits + i; - buflen += sprintf(buf + buflen, " %3d %3d", v->points, v->rem); - } + flushbuf(w, buf, &buflen, 0); + buflen += sprintf(buf + buflen, "%3d", v->points); + flushbuf(w, buf, &buflen, points_colour(v->points)); + + buflen += sprintf(buf + buflen, " %3d", v->rem); if (i < l2->n_visits) { v = l2->visits + i; - buflen += sprintf(buf + buflen, " %3d %3d", v->rem, v->points); + + buflen += sprintf(buf + buflen, " %3d ", v->rem); + + flushbuf(w, buf, &buflen, 0); + buflen += sprintf(buf + buflen, "%3d", v->points); + flushbuf(w, buf, &buflen, points_colour(v->points)); for (int j = 0; j < v->n_darts; ++j) { char *n = segment_name(v->darts[j]); buflen += sprintf(buf + buflen, j == 0 ? " %4s" :" %4s", n); free(n); } + flushbuf(w, buf, &buflen, COLOR_PAIR(7)); } - buflen = 0; - waddstr(w, buf); + flushbuf(w, buf, &buflen, 0); } wrefresh(w); @@ -443,6 +497,8 @@ void draw2(WINDOW *w, struct leg *l1, struct leg *l2) void test_curses_match2(int start_points) { initscr(); + start_color(); + init_colours(); curs_set(0); noecho(); @@ -475,7 +531,7 @@ int main() init_sectors(); init_centre_dists(); - test_curses_match(501); + //test_curses_match(501); test_curses_match2(501); test_match(501); -- cgit v1.2.3-70-g09d2