-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor.cpp
More file actions
294 lines (238 loc) · 7.78 KB
/
Editor.cpp
File metadata and controls
294 lines (238 loc) · 7.78 KB
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
#include "Globals.h"
#include "Blocks.h"
#include "Circle.h"
#include "Button.h"
#include "Editor.hpp"
Editor::Editor(){}
Editor::Editor(Font *font)
{
this->font = font;
rcEdit = Create_Rectangle(46, 310, 466, 158, colorBorde, Color(34,38,38));
rcCube = Create_Rectangle(100, 342, 32,32, colorBorde);
idBlock = Create_Label(106, 320, "00", font, 15);
label_1 = Create_Label(220, 310, "Modo Edicion", font, 20);
label_2 = Create_Label(150, 340, "Rueda para cambiar Bloque", font, 15);
label_3 = Create_Label(150, 360, "Click en Numeros para Cambiar-Nivel", font, 15);
label_4 = Create_Label(150, 390, "Editando Nivel: 00", font, 15);
Guardar = new BUTTON(310, 386, 70, 25, "Guardar", font, 14);
Cargar = new BUTTON(400, 386, 70, 25, "Cargar", font, 14);
Salir = new BUTTON(230, 425, 70, 25, "Salir", font, 14);
}
Editor::~Editor()
{
//dtor
}
void Editor::SetBlock(BLOCK **block, int TOTAL){
this->block = block;
this->TOTAL = TOTAL;
}
void Editor::SetLevel(CIRCLE **level, int LEVELS){
this->level = level;
this->LEVELS = LEVELS;
}
void Editor::Set_rcGame(RectangleShape *rcGame){
this->rcGame = rcGame;
}
Color Editor::data_getColors(int idColor){
return data[idColor].colors;
}
int Editor::data_getGolpes(int idColor){
return data[idColor].Golpes;
}
void Editor::copyToArray(int level, BLOCK **block, int TOTAL){
int filas = 0;
int colms = 0;
for(int index=0; index < TOTAL; index++){
colms = index % MAX_COLMS;
filas = index / MAX_COLMS;
ARRAY_LEVEL[level][filas][colms] = block[(TOTAL -1) -index]->get_idColor();
}
}
void Editor::SaveToTexto(int FILAS){
ofstream mySave;
mySave.open(fileSave);
if(mySave.is_open()){
mySave << "GameLevel {" << endl;
for(int level=0; level < MAX_LEVEL; level++){
mySave << " Level: " << fillZero(2) << level \
<< " filas: " << fillZero(2) << FILAS << endl;
for(int filas=0; filas < MAX_FILAS; filas++){
mySave << " " << fillZero(2) << filas << " { ";
for(int colms=0; colms < MAX_COLMS; colms++){
mySave << fillZero(2) << ARRAY_LEVEL[level][filas][colms] << " ";
}
mySave << " }" << endl;
}
}
mySave << "}";
mySave.close();
}
}
void Editor::loadFromFile(){
fstream myLoad;
string buffer;
int myLevel = 0;
int myFilas = 0;
int filas = 0;
int colms = 0;
int idBlock = 0;
myLoad.open(fileSave);
if(myLoad.is_open()){
while( !myLoad.eof() ){
myLoad >> buffer;
if(buffer == "Level:"){
myLoad >> myLevel;
continue;
}
if(buffer == "filas:"){
myLoad >> myFilas;
cout << "Level(" << myLevel << ") filas(" << myFilas << ")" << endl;
for(filas = 0; filas < MAX_FILAS; filas++){
myLoad >> filas;
myLoad >> buffer; //. La llave {
for(colms = 0; colms < MAX_COLMS; colms++){
myLoad >> idBlock;
ARRAY_LEVEL[myLevel][filas][colms] = idBlock;
//cout << idBlock <<", ";
}
myLoad >> buffer; //. la llave }
//cout << endl;
}
}
}
myLoad.close();
}
}
void Editor::CopyFromArray(int level, BLOCK **block, int TOTAL){
int filas = 0;
int colms = 0;
int index = TOTAL -1;
int idColor = 0;
for(filas=0; filas < MAX_FILAS; filas++){
for(colms=0; colms < MAX_COLMS; colms++){
idColor = ARRAY_LEVEL[level][filas][colms];
block[index]->set_idColor(idColor, data_getColors(idColor));
block[index]->setActivo(true);
index--;
}
}
}
/***** SELECCIONADOR DE LOS NIVELES A EDITAR *******/
bool Editor::isMouseInLevel(RenderWindow *win, CIRCLE **level, int TOTAL, int *indice){
bool Result = false;
*indice = -1;
for(int index=0; index < TOTAL; index++){
level[index]->Update(&*win);
if(level[index]->isSelected() ){
*indice = index;
Result = true;
}
}
return Result;
}
/****** EDITOR DE BLOQUES DE NIVELES ******/
bool Editor::isMouseInBlock(RenderWindow *win, BLOCK **block, int TOTAL, int *index){
bool Result = false;
RectangleShape rcShape;
Rect<float> rcBlock;
int px = Mouse::getPosition(*win).x; //. posicion relativa a la win
int py = Mouse::getPosition(*win).y;
for(int ndx = 0; ndx < TOTAL; ndx++){
rcShape = block[ndx]->getRectangle();
rcBlock = Rect<float>(rcShape.getPosition().x - rcShape.getOrigin().x, \
rcShape.getPosition().y - rcShape.getOrigin().y, \
rcShape.getSize().x, \
rcShape.getSize().y);
if(rcBlock.contains(Vector2f(px, py))){
*index = ndx;
Result = true;
break;
}
}
return Result;
}
void Editor::Process(RenderWindow *win, Event evn){
if(evn.type == Event::MouseWheelMoved){
if(Keyboard::isKeyPressed(Keyboard::LControl)){
iFondo += evn.mouseWheel.delta;
iFondo = min(iFondo, 39);
iFondo = max(iFondo, 00);
cout << "Fondo: " << iFondo << endl;
CambiarFondo(rcGame, iFondo);
} else {
Wheel += evn.mouseWheel.delta;
Wheel = min( Wheel, NUM_COLORS -1);
Wheel = max( Wheel, 0);
cout << "Wheel: " << Wheel << endl;
rcCube.setFillColor( data_getColors(Wheel) );
ostringstream buffer;
buffer << setfill('0') << setw(2) << Wheel;
idBlock.setString(buffer.str());
}
}
//. Mouse en los Bloques
if(isMouseInBlock(win, block, TOTAL, &index)){
//. Seleccionar index
cout << index << endl;
//. insertar bloque
if(Mouse::isButtonPressed(Mouse::Left)){
block[index]->set_idColor(Wheel, data_getColors(Wheel));
}
//. eliminar bloque
if(Mouse::isButtonPressed(Mouse::Right)){
block[index]->set_idColor(0, (Color)0);
}
//. Mouse en los Niveles
} else if(isMouseInLevel(win, level, LEVELS, &Selecto)){
if(Mouse::isButtonPressed(Mouse::Left)){
//. Asegura que el Click Cambia una sola vez.
if(Selecto != -1){
if(SELECTO != Selecto){
//. Desmarca el Anterior
level[SELECTO]->Set_Normal();
//. MArca el Actual Seleccionado.
level[Selecto]->Set_Pressed();
//. Copia los bloques Actuales al Array
copyToArray(SELECTO, block, TOTAL);
SELECTO = Selecto;
cout << "Nivel Selecto: " << Selecto << endl;
//. Copia desde el Array los bloques.-
CopyFromArray(Selecto, block, TOTAL);
ostringstream buffer;
buffer << "Editando Nivel: " << fillZero(2) << Selecto +1;
label_4.setString(buffer.str());
} //. Fin, Cambio de Nivel
} //. Fin, posible Seleccion
} //. Mouse Pressed
}
Guardar->Update(win);
if(Guardar->isPressed()){
SELECTO = ((SELECTO != -1) ? SELECTO : 0);
cout << "Guardar Archivo: " << SELECTO << endl;
copyToArray(SELECTO, block, TOTAL);
SaveToTexto();
}
Cargar->Update(win);
if(Cargar->isPressed()){
SELECTO = ((SELECTO != -1) ? SELECTO : 0);
cout << "Cargar Archivo: " << SELECTO << endl;
loadFromFile();
CopyFromArray(SELECTO, block, TOTAL);
}
Salir->Update(win);
if(Salir->isPressed()){
ModoEditar = false;
}
}
void Editor::Display(RenderWindow *win){
win->draw(rcEdit);
win->draw(rcCube);
win->draw(label_1);
win->draw(label_2);
win->draw(label_3);
win->draw(label_4);
win->draw(idBlock);
Guardar->Display(win);
Cargar->Display(win);
Salir->Display(win);
}