Skip to content

Commit 5c95a8b

Browse files
committed
* Totaly rework typebox system
now work as user expect * Adding posobolity to set length to win * Totaly reworked system of cheching win * Some small issues for better UX
1 parent 1786b2c commit 5c95a8b

File tree

8 files changed

+213
-116
lines changed

8 files changed

+213
-116
lines changed

src/baseGUI.cpp

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -252,23 +252,27 @@ void typeBox::writeString(char* str, bool freeData){
252252
}
253253

254254
// Moving part after caret at end
255-
for(Uint8 i = length; i > caret; --i){
255+
for(Sint8 i = length; i >= caret; --i){
256256
buffer[i + clipboardSize] = buffer[i];
257257
}
258258

259259
// Coping main clipboard text
260-
strcpy_s(buffer + caret, clipboardSize, str);
260+
for(Uint8 i=0; i < clipboardSize; ++i){
261+
buffer[caret + i] = str[i];
262+
}
263+
264+
length += clipboardSize;
265+
caret += clipboardSize;
266+
267+
updateTexture();
261268

262269
if(freeData){
263270
SDL_free(str);
264271
}
265272
};
266273

267-
void typeBox::enterAction(SDL_TextEditingEvent code){
268-
269-
}
270-
271-
/*void typeBox::press(SDL_Keycode code){
274+
void typeBox::press(SDL_Keycode code){
275+
static SDL_Keycode preCode;
272276
switch (code)
273277
{
274278
case SDLK_BACKSPACE:
@@ -280,6 +284,16 @@ void typeBox::enterAction(SDL_TextEditingEvent code){
280284
length--;
281285
}
282286
break;
287+
288+
case SDLK_DELETE:
289+
// Coping after caret
290+
if(caret < length){
291+
for(Uint8 t = caret + 1; t <= length; t++){
292+
buffer[t] = buffer[t+1];
293+
}
294+
length--;
295+
}
296+
break;
283297

284298
case SDLK_LEFT:
285299
if(caret > 0){
@@ -288,43 +302,38 @@ void typeBox::enterAction(SDL_TextEditingEvent code){
288302
break;
289303

290304
case SDLK_RIGHT:
291-
if(caret < length-1){
305+
if(caret+1 < length){
292306
swapChar(buffer + caret++, buffer + caret + 1);
293307
}
294308
break;
295309

310+
// Control-v mode
296311
case SDLK_PASTE:
297312
// Inserting text from clipboard
298313
writeString(SDL_GetClipboardText(), true);
299314
break;
300-
301-
default:
302-
// Checking if normal letter
303-
if(code > 27 && code < 200){
304-
// Checking, if letter can be placed
305-
if(length < bufferSize){
306-
// Copying all letters after caret
307-
for(Uint8 t = ++length; t > caret; --t){
308-
buffer[t] = buffer[t-1];
309-
}
310-
// Setting new letter
311-
buffer[caret++] = code;
312-
}
315+
316+
case SDLK_v:
317+
if(preCode == SDLK_LCTRL){
318+
writeString(SDL_GetClipboardText(), true);
313319
}
314-
}
320+
break;
315321

322+
case SDLK_LCTRL:
323+
if(preCode == SDLK_v){
324+
writeString(SDL_GetClipboardText(), true);
325+
}
326+
break;
327+
};
316328
updateTexture();
329+
preCode = code;
317330
};
318-
//*/
319331

320332
void typeBox::select(){
321-
/*buffer[length] = '|';
333+
buffer[length] = '|';
322334
caret = length++;
323335
buffer[length] = '\0';
324-
updateTexture();*/
325-
326-
// Setting rect for typing text
327-
SDL_SetTextInputRect(&backRect);
336+
updateTexture();
328337

329338
// Starting using keyboard
330339
SDL_StartTextInput();
@@ -334,11 +343,17 @@ void typeBox::removeSelect(){
334343
// Stoping entering any letters
335344
SDL_StopTextInput();
336345

337-
/*for(Uint8 t = caret; t <= length; t++){
346+
for(Uint8 t = caret; t <= length; t++){
338347
buffer[t] = buffer[t+1];
339348
}
340349
length--;
341-
updateTexture();*/
350+
updateTexture();
351+
};
352+
353+
void typeBox::updateCaret(){
354+
static char b[] = {' '};
355+
swapChar(buffer + caret, b);
356+
updateTexture();
342357
};
343358

344359
bool typeBox::in(int x, int y){

src/baseGUI.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ namespace GUI{
124124
typeBox(Uint8 size, float posX, float posY, const char* startText = "", ALIGNMENT_types newAligment = MIDLE_text, SDL_Color newColor = BLACK);
125125
~typeBox();
126126
void blit();
127-
//void enterText(SDL_TextInputEvent code);
128127
void writeString(char* str, bool freeData);
129-
void enterAction(SDL_TextEditingEvent code);
128+
void press(SDL_Keycode code);
129+
void updateCaret();
130+
130131
void select();
131132
void removeSelect();
132133
bool in(int mouseX, int mouseY);

src/entity.cpp

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "dataLoader.hpp"
66

77

8-
Field::Field(/* args */)
8+
Field::Field()
99
{
1010
data = (Uint8*)malloc(sizeof(Uint8) * fieldWidth * fieldWidth);
1111
reset();
@@ -157,7 +157,7 @@ void Field::AImove(){
157157
//
158158
data[maxPos] = CELL_ENEMY;
159159

160-
//Checking for win
160+
// Checking for win
161161
switch (checkWin(maxPos % fieldWidth, maxPos / fieldWidth))
162162
{
163163
case CELL_MY:
@@ -173,47 +173,69 @@ void Field::AImove(){
173173
}
174174
}
175175

176+
// New checkWin function
176177
// Return 0, if none win, 1, if win player, 2 if win bot(2 player)
177-
int Field::checkWin(coord X, coord Y){
178-
Uint8 state = data[Y * fieldWidth];
179-
for(coord x = 1; x < fieldWidth; ++x){
180-
state &= data[Y * fieldWidth + x];
181-
}
182-
if(state){
183-
return state;
184-
}
178+
Uint8 Field::checkWin(const coord X, const coord Y){
179+
// Flag, which save type for control win
180+
Uint8 state;
181+
// Finding first starting point for X
182+
for(Uint8 startX = MAX(0, X - winWidth + 1); startX <= MIN(X, fieldWidth - winWidth); ++startX){
183+
// Checking all line
184+
state = CELL_MY | CELL_ENEMY;
185185

186-
state = data[X];
187-
for(coord y = 1; y < fieldWidth; ++y){
188-
state &= data[y * fieldWidth + X];
186+
for(coord x = startX; (x < startX + winWidth) && state; ++x){
187+
state &= data[Y * fieldWidth + x];
188+
}
189+
190+
if(state){
191+
return state;
192+
}
189193
}
190-
if(state){
191-
return state;
194+
195+
// Finding same first starting point for Y
196+
for(Uint8 startY = MAX(0, Y - winWidth + 1); startY <= MIN(Y, fieldWidth - winWidth); ++startY){
197+
// Checking all collumn
198+
state = CELL_MY | CELL_ENEMY;
199+
200+
for(coord y = startY; (y < startY + winWidth) && state; ++y){
201+
state &= data[y * fieldWidth + X];
202+
}
203+
204+
if(state){
205+
return state;
206+
}
192207
}
193208

194-
// Checking diagonal
195-
if(X == Y){
196-
state = data[0];
197-
for(coord t = 1; t < fieldWidth; ++t){
198-
state &= data[(fieldWidth + 1) * t];
209+
// Checking primal diagonal
210+
for(Uint8 startT = MAX(X - Y, 0); startT <= MIN(fieldWidth - winWidth, fieldWidth + X - Y - winWidth); ++startT){
211+
state = CELL_MY | CELL_ENEMY;
212+
213+
for(Uint8 t = startT; (t < winWidth + startT) && state; ++t){
214+
state &= data[t + (Y + t - X) * fieldWidth];
199215
}
200216
if(state){
201217
return state;
202218
}
203219
}
204-
else if(fieldWidth - 1 == X + Y){
205-
state = data[fieldWidth - 1];
206-
for(coord t = fieldWidth; t > 1; --t){
207-
state &= data[(fieldWidth - 1) * t];
220+
221+
// Checking second diagonal
222+
for(Sint8 startT = MIN(X + Y, fieldWidth); startT+1 > MIN(winWidth, winWidth - X - Y); --startT){
223+
state = CELL_MY | CELL_ENEMY;
224+
225+
for(Sint8 t = startT; (t > startT - winWidth); --t){
226+
state &= data[X + Y + t * (fieldWidth-1)];
208227
}
209228
if(state){
210229
return state;
211230
}
212231
}
232+
233+
// Checking, is field full
213234
bool c = true;
214235
for(coord t=0; t < fieldWidth * fieldWidth; ++t){
215-
if(!data[t]){
236+
if(data[t] == CELL_EMPTY){
216237
c = false;
238+
break;
217239
}
218240
}
219241
if(c){

src/entity.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ enum cellTypes{
1313
class Field
1414
{
1515
private:
16-
Uint8 *data; // type of cell in grid from cellTypes
17-
Sint8 recSolve(Uint8 round);
16+
Uint8 *data; // Grid of cells type from cellTypes
17+
Sint8 recSolve(Uint8 round); // Function for solve game in singleplayer recursivly
1818
public:
19-
Field(/* args */);
19+
Field();
2020
~Field();
2121
void reset();
2222

23-
void clickSingle(Uint8 x, Uint8 y); // Clicking in singleplayer mode
24-
void clickTwo(Uint8 x, Uint8 y); // Clicking in two-player mode
25-
void clickMulti(Uint8 x, Uint8 y, TCPsocket sendPlace); // Clicking inmultiplayer mode
23+
void clickSingle(Uint8 x, Uint8 y); // Clicking in singleplayer mode
24+
void clickTwo(Uint8 x, Uint8 y); // Clicking in two-player mode
25+
void clickMulti(Uint8 x, Uint8 y, TCPsocket sendPlace); // Clicking in multiplayer mode
2626

2727
void AImove();
28-
int checkWin(Uint8 X, Uint8 Y);
28+
Uint8 checkWin(const Uint8 X, const Uint8 Y);
2929

3030
void blit();
3131
};

0 commit comments

Comments
 (0)