-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessMenuScreen.h
227 lines (206 loc) · 6.76 KB
/
ChessMenuScreen.h
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
#ifndef CHESSMENUSCREEN_H_INCLUDED
#define CHESSMENUSCREEN_H_INCLUDED
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Audio/Music.hpp>
#include <SFML/Audio/Sound.hpp>
#include "Chess.h"
#include "ChessScreens.h"
#include "ChessExceptions.h"
#include "ChessTimer.h"
#include <sstream>
using namespace std;
using namespace sf;
static int MAIN_PLAYER_COLOR=ChessPiece::COLOR_WHITE;
static int MINUTES_PER_TURN=1;
struct ChessMenu{
Font* fuente;
Text* txt_editor=NULL;
RectangleShape* option=NULL;
Text minutesText;
Text colorText;
RenderWindow* window;
RectangleShape button;
string name;
ChessMenu(){
fuente = new Font();
fuente->loadFromFile("./assets/Fuente.ttf");
option = new RectangleShape[6];
txt_editor = new Text[6];
for (int i= 0; i<6; i++){
txt_editor[i]= Text("", *fuente);
}
option[0]= RectangleShape({140,40});
option[1]= RectangleShape({40,40});
option[2]= RectangleShape({40,40});
option[3]= RectangleShape({250,80});
option[4]= RectangleShape({140,60});
option[5]= RectangleShape({80,80});
option[0].setFillColor(Color::Transparent);
option[1].setFillColor(Color::Transparent);
option[2].setFillColor(Color::Transparent);
option[3].setFillColor(Color::Transparent);
option[4].setFillColor(Color::Transparent);
option[5].setFillColor(Color::Transparent);
option[0].setPosition({350,357});
option[1].setPosition({390,460});
option[2].setPosition({490,460});
option[3].setPosition({180,600});
option[4].setPosition({885,700});
option[5].setPosition({945,32});
minutesText.setFont(*fuente);
minutesText.setColor(Color::Black);
minutesText.setCharacterSize(50);
minutesText.setPosition({450,435});
minutesText.setString("1");
colorText.setFont(*fuente);
colorText.setColor(Color::Black);
colorText.setCharacterSize(50);
colorText.setPosition({375,336});
colorText.setString("Blanco");
}
void renderMenu(RenderWindow &window){
for (int i= 0; i<6; i++){
window.draw(option[i]);
}
// window.draw(minutesShape);
// window.draw(minutesText);
}
void updateColor(){
string text="Blanco";
if(MAIN_PLAYER_COLOR==ChessPiece::COLOR_BLACK){
text="Negro";
}
cout<<"TEXT: "<<text<<"\n";
colorText.setString(text);
}
void updateMinutes(){
string text=to_string(MINUTES_PER_TURN);
cout<<"TEXT: "<<text<<"\n";
minutesText.setString(text);
}
};
struct ChessMenuScreen : public ChessScreen
{
static const int STATUS_PLAY=1;
static const int STATUS_INFO=2;
Music music;
Event event;
Texture texture;
Sprite sprite;
ChessMenu menu;
// list<botton> option;
int (*actions[6])(ChessMenu &);
ChessMenuScreen()
{
actions[0]=ActionChangeColor;
actions[1]=ActionSubstractTime;
actions[2]=ActionAddTime;
actions[3]=ActionPlay;
actions[4]=ActionClose;
actions[5]=ActionPause;
if (!music.openFromFile("./assets/sounds/MenuMusic.ogg")){
throw MenuMusicException();
exit(1);
}
if (!texture.loadFromFile("./assets/Chess_Game.png"))
{
throw ChessImageException();
exit(1);
}
sprite.setTexture(texture);
}
virtual int Run(RenderWindow &window){
if(!wasRun){
/**
Codigo a ejecutar solamente en la primer llamada al Run
El codigo aqui se usa en caso de requerir acciones que se ejecuten 1 sola ocasión en la primer llamada.
*/
wasRun=true;
music.setLoop(true);
}else{
/**
Codigo a ejecutar siempre despues de la primera llamada a Run.
Aquí debe ir todo código que reanude las acciones que el metodo Pause haya detenido.
*/
}
music.play();
music.setVolume(40.f);
bool running = true;
while (window.isOpen())
{
while (window.pollEvent(event))
{
switch(event.type)
{
case (Event::Closed):
window.close();
break;
case (Event::MouseButtonPressed):
if (event.mouseButton.button == Mouse::Left)
{
// return CHESS_GAME_SCREEN_NUMBER;
Vector2i posicion_mouse;
posicion_mouse=Mouse::getPosition(window);
for (int i= 0; i<6; i++){
if (menu.option[i].getGlobalBounds().contains(posicion_mouse.x, posicion_mouse.y)){
int operation=actions[i](menu);
if(operation==STATUS_INFO){
return CHESS_INFO_SCREEN_NUMBER;
}else if(operation==STATUS_PLAY){
return CHESS_GAME_SCREEN_NUMBER;
}
break;
}
}
}
break;
}
}
window.clear();
window.draw(sprite);
menu.renderMenu(window);
window.draw(menu.minutesText);
window.draw(menu.colorText);
window.display();
}
}
virtual void Pause(){
music.sf::SoundStream::pause();
}
static int ActionPlay(ChessMenu& menu){
cout<<"PLAY\n";
return STATUS_PLAY;
}
static int ActionClose(ChessMenu& menu){
exit(1);
}
static int ActionAddTime(ChessMenu& menu){
if(MINUTES_PER_TURN<5){
MINUTES_PER_TURN++;
menu.updateMinutes();
}
cout<<"ADD\n";
}
static int ActionSubstractTime(ChessMenu& menu){
if(MINUTES_PER_TURN>1){
MINUTES_PER_TURN--;
menu.updateMinutes();
}
cout<<"SUBSTRACT\n";
}
static int ActionChangeColor(ChessMenu& menu){
if(MAIN_PLAYER_COLOR==ChessPiece::COLOR_WHITE){
MAIN_PLAYER_COLOR=ChessPiece::COLOR_BLACK;
}else{
MAIN_PLAYER_COLOR=ChessPiece::COLOR_WHITE;
}
menu.updateColor();
cout<<"CHANGECOLOR: "<<MAIN_PLAYER_COLOR<<"\n";
}
static int ActionPause(ChessMenu& menu){
cout<<"INFO\n";
return STATUS_INFO;
}
};
#endif // CHESSMENUSCREEN_H_INCLUDED