-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUI.c
125 lines (111 loc) · 5.1 KB
/
UI.c
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
#include "chess.h"
struct GTKmainGrid tabGrid[64];
struct GTKmenuGrid menuGrid[4];
void drawBoard (GtkWidget* grid, char figurePlacement[]){
for (int i = 0; i < 64; i++){
GtkWidget* button = gtk_grid_get_child_at(GTK_GRID(grid), tabGrid[i].posX, tabGrid[i].posY);
GtkWidget* image = NULL;
if (figurePlacement[i] == 'P') image = gtk_image_new_from_file ("icons//Chess_plt60.png");
else if (figurePlacement[i] == 'p') image = gtk_image_new_from_file ("icons//Chess_pdt60.png");
else if (figurePlacement[i] == 'S') image = gtk_image_new_from_file ("icons//Chess_nlt60.png");
else if (figurePlacement[i] == 's') image = gtk_image_new_from_file ("icons//Chess_ndt60.png");
else if (figurePlacement[i] == 'B') image = gtk_image_new_from_file ("icons//Chess_blt60.png");
else if (figurePlacement[i] == 'b') image = gtk_image_new_from_file ("icons//Chess_bdt60.png");
else if (figurePlacement[i] == 'R') image = gtk_image_new_from_file("icons//Chess_rlt60.png");
else if (figurePlacement[i] == 'r') image = gtk_image_new_from_file("icons//Chess_rdt60.png");
else if (figurePlacement[i] == 'Q') image = gtk_image_new_from_file("icons//Chess_qlt60.png");
else if (figurePlacement[i] == 'q') image = gtk_image_new_from_file("icons//Chess_qdt60.png");
else if (figurePlacement[i] == 'K') image = gtk_image_new_from_file("icons//Chess_klt60.png");
else if (figurePlacement[i] == 'k') image = gtk_image_new_from_file("icons//Chess_kdt60.png");
else if (figurePlacement[i] == 'X') image = gtk_image_new_from_file("icons//Transparent_X2.png");
if (image != NULL) gtk_button_set_image(GTK_BUTTON(button), image);
else gtk_button_set_image(GTK_BUTTON(button), NULL);
}
}
void resetBoardColors(GtkWidget* grid){
int l = 0;
for (int j = 0; j < 64; j ++){
if (j % 8 == 0) l++;
l++;
GtkWidget* button = gtk_grid_get_child_at(GTK_GRID(grid), tabGrid[j].posX, tabGrid[j].posY);
if (l % 2 == 0) gtk_widget_set_name(button, "bright");
else gtk_widget_set_name(button, "dark");
}
}
void drawLegalMoves(GtkWidget* boardGrid, int legalMoveTab[]){
for (int i = 0; i < 64; i++){
GtkWidget* button = gtk_grid_get_child_at(GTK_GRID(boardGrid), tabGrid[i].posX, tabGrid[i].posY);
if(legalMoveTab[i] == 1){
gtk_widget_set_name(button, "legalMove");
}
else if (legalMoveTab[i] == 2){
gtk_widget_set_name(button, "legalMoveKill");
}else if (legalMoveTab[i] == 5){
gtk_widget_set_name(button, "AIMOVE");
}
}
}
void drawLastMove(GtkWidget* boardGrid){
GtkWidget* button = gtk_grid_get_child_at(GTK_GRID(boardGrid), tabGrid[iFrom].posX, tabGrid[iFrom].posY);
gtk_widget_set_name(button, "lastMoveDark");
button = gtk_grid_get_child_at(GTK_GRID(boardGrid), tabGrid[iWhere].posX, tabGrid[iWhere].posY);
gtk_widget_set_name(button, "lastMoveDark");
}
void myCSS(void){
GtkCssProvider *provider;
GdkDisplay *display;
GdkScreen *screen;
provider = gtk_css_provider_new ();
display = gdk_display_get_default ();
screen = gdk_display_get_default_screen (display);
gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
const gchar *myCssFile = "mystyle.css";
GError *error = 0;
gtk_css_provider_load_from_file(provider, g_file_new_for_path(myCssFile), &error);
g_object_unref (provider);
}
void changeTurnLabel(GtkWidget* label){
if (whoseTurn() == 'B') gtk_label_set_text(GTK_LABEL(label), "Black's Turn");
else gtk_label_set_text(GTK_LABEL(label), "White's Turn");
}
void changeTurnTimers(){
if (whoseTurn() == 'B'){
sec_expired_white = sec_expired;
sec_expired = sec_expired_black;
}
else{
sec_expired_black = sec_expired;
sec_expired = sec_expired_white;
}
}
void KGdrawVisitedTiles(GtkWidget* grid, char figurePlacement[]){
int l = 0;
for (int i = 0; i < 64; i++){
if (i % 8 == 0) l++;
l++;
GtkWidget* button = gtk_grid_get_child_at(GTK_GRID(grid), tabGrid[i].posX, tabGrid[i].posY);
if (figurePlacement[i] == 'X'){
if (l % 2 == 0) gtk_widget_set_name(button, "BrightTileVisited");
else gtk_widget_set_name(button, "DarkTileVisited");
}
}
}
void drawUI(GtkWidget* grid, char figurePlacement[], int legalMoveTab[]){
resetBoardColors(grid);
changeTurnLabel(label);
if(iFrom >= 0 && iWhere >= 0)
drawLastMove(grid);
if (UISelectedTile != -1){
drawLegalMoves(grid, legalMoveTab);
}
// gtk_test_widget_wait_for_draw(grid);
drawBoard(grid, figurePlacement);
}
void KGdrawUI(GtkWidget* grid, char figurePlacement[], int legalMoveTab[]){
drawBoard(grid, figurePlacement);
resetBoardColors(grid);
KGdrawVisitedTiles(grid, figurePlacement);
if (KGUISelectedTile != -1){
drawLegalMoves(grid, legalMoveTab);
}
}