summaryrefslogtreecommitdiff
path: root/src/ui.c
blob: ee27567bf8d525a4ec258a077e8193edbbbee866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include "ui.h"
#include "cardstack.h"

// Definition of ncurses color pair identifiers
#define CP_WHITE_ON_BLACK	1
#define CP_YELLOW_ON_BLACK	2
#define CP_BLUE_ON_BLACK	3
#define CP_MAGENTA_ON_BLACK	4
#define CP_RED_ON_BLACK		5

#define KEY_ESCAPE	27
#define KEY_RETURN	10

#define KEY_VI_LEFT	'h'
#define KEY_VI_RIGHT	'l'
#define KEY_VI_UP	'k'
#define KEY_VI_DOWN	'j'

static bool colors = false;
static WINDOW *w_table_cards;
static WINDOW *w_stack_points;
static WINDOW *w_current_state;
static WINDOW *w_hand_cards;

static void draw_card(WINDOW *w, const uint8_t row, const uint8_t col, const bool highlight, const card c)
{
	unsigned color_pair = 0;

	// If card is 0, draw a placeholder (empty space) instead of a card. This is used to remove a card that does not longer exist but has visible remains on the screen.
	if (0 == c)
	{
		mvwhline(w, row, col, ' ', 7);
		mvwhline(w, row+1, col, ' ', 7);
		mvwhline(w, row+2, col, ' ', 7);
		mvwhline(w, row+3, col, ' ', 7);
		mvwhline(w, row+4, col, ' ', 7);
		return;
	}

	if (highlight)
		wattron(w, A_BOLD);

	if (colors)
	{
		switch (card_get_points(c))
		{
			case 7:
				color_pair = CP_MAGENTA_ON_BLACK;
				break;
			case 5:
				color_pair = CP_RED_ON_BLACK;
				break;
			case 3:
				color_pair = CP_YELLOW_ON_BLACK;
				break;
			case 2:
				color_pair = CP_BLUE_ON_BLACK;
				break;
			default:
				color_pair = CP_WHITE_ON_BLACK;
		}

		wattron(w, COLOR_PAIR(color_pair));
	}

	mvwaddch(w, row, col, ACS_ULCORNER); // Upper left corner
	mvwhline(w, row, col+1, ACS_HLINE, 5); // Upper horizontal line
	mvwaddch(w, row, col+6, ACS_URCORNER); // Upper right corner
	mvwvline(w, row+1, col, ACS_VLINE, 3); // Left vertical line
	mvwvline(w, row+1, col+6, ACS_VLINE, 3); // Right vertical line
	mvwaddch(w, row+4, col, ACS_LLCORNER); // Lower left corner
	mvwhline(w, row+4, col+1, ACS_HLINE, 5); // Lower horizontal line
	mvwaddch(w, row+4, col+6, ACS_LRCORNER); // Lower right corner
	mvwprintw(w, row+1, col+1, " %3d ", c); // Card number, 3 digits wide, filled with spaces, if number has fewer digits

	// Clear the rest of the card first
	mvwprintw(w, row+2, col+1, "     ");
	mvwprintw(w, row+3, col+1, "     ");

	switch (card_get_points(c))
	{
		case 7:
			// "** **"
			// " *** "
			mvwhline(w, row+2, col+1, ACS_DIAMOND, 2);
			mvwhline(w, row+2, col+4, ACS_DIAMOND, 2);
			mvwhline(w, row+3, col+2, ACS_DIAMOND, 3);
			break;
		case 5:
			// "* * *"
			// " * * "
			mvwaddch(w, row+2, col+1, ACS_DIAMOND);
			mvwaddch(w, row+2, col+3, ACS_DIAMOND);
			mvwaddch(w, row+2, col+5, ACS_DIAMOND);
			mvwaddch(w, row+3, col+2, ACS_DIAMOND);
			mvwaddch(w, row+3, col+4, ACS_DIAMOND);
			break;
		case 3:
			// " * * "
			// "  *  "
			mvwaddch(w, row+2, col+2, ACS_DIAMOND);
			mvwaddch(w, row+2, col+4, ACS_DIAMOND);
			mvwaddch(w, row+3, col+3, ACS_DIAMOND);
			break;
		case 2:
			// " * * "
			// "     "
			mvwaddch(w, row+2, col+2, ACS_DIAMOND);
			mvwaddch(w, row+2, col+4, ACS_DIAMOND);
			break;
		default:
			// "  *  "
			// "     "
			mvwaddch(w, row+2, col+3, ACS_DIAMOND);
	}

	if (colors)
	{
		wattroff(w, COLOR_PAIR(color_pair));
	}
	
	if (highlight)
		wattroff(w, A_BOLD);
}

static void draw_cardstack(WINDOW *w, const uint8_t row, const uint8_t col, const bool highlight, const cardstack cs)
{
	// Clear old card stack first
	mvwhline(w, row, col, ' ', 15);
	mvwhline(w, row+1, col, ' ', 15);
	mvwhline(w, row+2, col, ' ', 15);
	mvwhline(w, row+3, col, ' ', 15);
	mvwhline(w, row+4, col, ' ', 15);

	for (uint8_t i = 0; i < MAX_CARDSTACK_SIZE; i++)
	{
		if (cs[i] != 0)
		{
			draw_card(w, row, i*2, highlight, cs[i]);
		}
	}
}

/**
 * Displays the table cards window. Draws table stacks and can highlight a stack.
 * @param[in] ts The table stacks to display
 * @param[in] highlight If true, a stack will be highlighted
 * @param[in] highlighted_stack The stack to highlight. Only used, if highlight is true
*/
void ui_display_wnd_table_cards(const tablestacks ts, const bool highlight, const uint8_t highlighted_stack)
{
	wattron(w_table_cards, A_BOLD);
	mvwprintw(w_table_cards, 0, 2, "Table Cards:");
	wattroff(w_table_cards, A_BOLD);

	for (uint8_t i = 0; i < NUM_TABLESTACKS; i++)
	{
		if (highlight && i == highlighted_stack)
		{
			draw_cardstack(w_table_cards, 1 + i*5, 0, true, ts[i]);
			continue;
		}

		draw_cardstack(w_table_cards, 1 + i*5, 0, false, ts[i]);
	}

	wrefresh(w_table_cards);
}

/**
 * Displays the stack points window.
 * @param[in] ts The table stacks used for calculation of stack points
 * @param[in] highlight If true, stack poins will be highlighted
 * @param[in] highlighted_points The stack points to highlight. Only used, if highlight is true
*/
void ui_display_wnd_stack_points(const tablestacks ts, const bool highlight, const uint8_t highlighted_points)
{
	wattron(w_stack_points, A_BOLD);
	mvwprintw(w_stack_points, 0, 0, "Pts:");
	wattroff(w_stack_points, A_BOLD);

	for (uint8_t i = 0; i < NUM_TABLESTACKS; i++)
	{
		if (highlight && i == highlighted_points)
		{
			wattron(w_stack_points, A_BOLD);
			mvwprintw(w_stack_points, 3 + i*5, 1, "%2d", cardstack_get_points(ts[i]));
			wattroff(w_stack_points, A_BOLD);
			continue;
		}

		mvwprintw(w_stack_points, 3 + i*5, 1, "%2d", cardstack_get_points(ts[i]));
	}

	wrefresh(w_stack_points);
}

/**
 * Displays the current state window, showing all player names and their open cards and the client player's score.
 * @param[in] pnoc Array of (player name, open card) tuples. Array has to be sorted by open card, lowest open card first
 * @param[in] num_players The number of players to show a player name and open card for
 * @param[in] active_players The currently active player
 * @param[in] score The players score
*/
void ui_display_wnd_current_state(const pnoc_t pnoc[], const uint8_t num_players, const uint8_t active_player, const uint32_t score)
{
	wattron(w_current_state, A_BOLD);
	mvwprintw(w_current_state, 0, 0, "Current state:");
	mvwprintw(w_current_state, 0, 22, "Your Score: %3d", score);
	wattroff(w_current_state, A_BOLD);
	mvwprintw(w_current_state, 1, 1, "Player      Card");
	mvwprintw(w_current_state, 1, 22, "Player      Card");
	mvwvline(w_current_state, 1, 19, ACS_VLINE, 6); // Vertical line

	for (uint8_t i = 0; i < num_players; i++)
	{
		if (i == active_player)
			wattron(w_current_state, COLOR_PAIR(CP_YELLOW_ON_BLACK));
		
		if (i < 5)
		{
			mvwprintw(w_current_state, 2+i, 1, "%-s", pnoc[i].player_name);
			mvwprintw(w_current_state, 2+i, 13, "%3d", pnoc[i].open_card);
		}
		else
		{
			mvwprintw(w_current_state, 2+(i-5), 22, "%-s", pnoc[i].player_name);
			mvwprintw(w_current_state, 2+(i-5), 34, "%3d", pnoc[i].open_card);
		}

		if (i == active_player)
			wattroff(w_current_state, COLOR_PAIR(CP_YELLOW_ON_BLACK));
	}

	wrefresh(w_current_state);
}

/**
 * Displays the hand cards window.
 * @param[in] h The hand that will be displayed. h must be sorted in ascending order
 * @param[in] highlight If true, a card will be highlighted
 * @param[in] highlighted_card The card to highlight. Only used, if highlight is true
*/
void ui_display_wnd_hand_cards(const hand h, const bool highlight, const uint8_t highlighted_card)
{
	uint8_t num_zero_cards = 0;

	wattron(w_hand_cards, A_BOLD);
	mvwprintw(w_hand_cards, 0, 0, "Hand Cards:");
	wattroff(w_hand_cards, A_BOLD);

	// Clear old cards from screen first
	for (uint8_t i = 0; i < MAX_HAND_CARDS; i++)
	{
		if (i - num_zero_cards < 5) // Start with the first row of cards
			draw_card(w_hand_cards, 1, (i - num_zero_cards) * 8, false, 0);
		else // And then draw the second row
			draw_card(w_hand_cards, 6, (i - num_zero_cards - 5) * 8, false, 0);
	}

	for (uint8_t i = 0; i < MAX_HAND_CARDS; i++)
	{
		// Cound all 0 cards and don't draw them
		if (0 == h[i])
		{
			num_zero_cards++;
			continue;
		}

		if (highlight && i == highlighted_card)
		{
			if (i - num_zero_cards < 5) // Start with the first row of cards
				draw_card(w_hand_cards, 1, (i - num_zero_cards) * 8, true, h[i]);
			else // And then draw the second row
				draw_card(w_hand_cards, 6, (i - num_zero_cards - 5) * 8, true, h[i]);
			continue;
		}

		if (i - num_zero_cards < 5) // Start with the first row of cards
			draw_card(w_hand_cards, 1, (i - num_zero_cards) * 8, false, h[i]);
		else // And then draw the second row
			draw_card(w_hand_cards, 6, (i - num_zero_cards - 5) * 8, false, h[i]);
	}

	wrefresh(w_hand_cards);
}

uint8_t ui_choose_card(hand h)
{
	int key;
	uint8_t chosen_card_idx = 0;
	int8_t i;	// Has to be signed for modulo calculation

	hand_sort(h);

	// Select card with lowest index as default
	if (0 == h[chosen_card_idx])
	{
		i = (chosen_card_idx + 1) % MAX_HAND_CARDS;
		while(0 == h[i])
			i = (i + 1) % MAX_HAND_CARDS;
		chosen_card_idx = i;
	}

	ui_display_wnd_hand_cards(h, true, chosen_card_idx);

	while (KEY_RETURN != (key = wgetch(w_hand_cards)))
	{
		switch (key)
		{
			case KEY_VI_LEFT:
				// Fall through
			case KEY_LEFT:
				i = (chosen_card_idx - 1 + MAX_HAND_CARDS) % MAX_HAND_CARDS;
				while(0 == h[i])
					i = (i - 1 + MAX_HAND_CARDS) % MAX_HAND_CARDS;
				chosen_card_idx = i;
				break;

			case KEY_VI_RIGHT:
				// Fall through
			case KEY_RIGHT:
				i = (chosen_card_idx + 1) % MAX_HAND_CARDS;
				while(0 == h[i])
					i = (i + 1) % MAX_HAND_CARDS;
				chosen_card_idx = i;
				break;

			default:
				// No default
				break;
		}

		ui_display_wnd_hand_cards(h, true, chosen_card_idx);
	}

	return chosen_card_idx;
}

uint8_t ui_choose_stack(const tablestacks ts)
{
	int key;
	uint8_t chosen_stack_idx = 0;

	ui_display_wnd_table_cards(ts, true, chosen_stack_idx);
	ui_display_wnd_stack_points(ts, true, chosen_stack_idx);
	
	while (KEY_RETURN != (key = wgetch(w_table_cards)))
	{
		switch (key)
		{
			case KEY_VI_UP:
				// Fall through
			case KEY_LEFT:
				// Fall through
			case KEY_UP:
				if (0 == chosen_stack_idx)
					chosen_stack_idx = NUM_TABLESTACKS - 1;
				else
					chosen_stack_idx--;
				break;

			case KEY_VI_DOWN:
				// Fall through
			case KEY_RIGHT:
				// Fall through
			case KEY_DOWN:
				if (NUM_TABLESTACKS - 1 == chosen_stack_idx)
					chosen_stack_idx = 0;
				else
					chosen_stack_idx++;
				break;

			default:
				// No default
				break;
		}

		ui_display_wnd_table_cards(ts, true, chosen_stack_idx);
		ui_display_wnd_stack_points(ts, true, chosen_stack_idx);
	}

	return chosen_stack_idx;
}

/**
 * Initializes ncurses.
*/
void ui_init(void)
{
	initscr();	// Start curses mode
	if (TRUE == has_colors())
	{
		colors = true;
		start_color();

		init_pair(CP_WHITE_ON_BLACK, COLOR_WHITE, COLOR_BLACK);
		init_pair(CP_YELLOW_ON_BLACK, COLOR_YELLOW, COLOR_BLACK);
		init_pair(CP_BLUE_ON_BLACK, COLOR_BLUE, COLOR_BLACK);
		init_pair(CP_MAGENTA_ON_BLACK, COLOR_MAGENTA, COLOR_BLACK);
		init_pair(CP_RED_ON_BLACK, COLOR_RED, COLOR_BLACK);
		bkgd(COLOR_PAIR(0));
	}

	raw();		// Line buffering disabled
	noecho();	// Don't echo() while we do getch
	curs_set(0);	// Make the cursor invisible
	clear();
	refresh();

	// Window positions and sizes
	w_table_cards = newwin(21, 15, 0, 0);
	w_stack_points = newwin(19, 4, 0, 17);
	w_current_state = newwin(10, 38, 0, 24);
	w_hand_cards = newwin(11, 39, 10, 24);

	// Set input settings for windows
	keypad(w_table_cards, true); // We get F1, F2 etc..
	keypad(w_hand_cards, true); // We get F1, F2 etc..

	// Draw user interface
	mvvline(0, 22, ACS_VLINE, 21); // Vertical line
	refresh();
}

/**
 * Finalizes ncurses.
*/
void ui_fini(void)
{
	delwin(w_table_cards);
	delwin(w_stack_points);
	delwin(w_current_state);
	delwin(w_hand_cards);
	endwin();	// End curses mode
}