-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaintb.cpp
478 lines (381 loc) · 11.1 KB
/
paintb.cpp
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include "paintb.h"
// canvas:
canvas::canvas(int rows, int cols) {
this->rows = rows;
this->cols = cols;
head = new FS;
head->frameString = new unsigned char[(rows + 2) * (cols + 3)];
head->next = NULL;
head->prev = NULL;
head->index = 0;
// initialize framestring
head->frameString[0] = 218;
for(int c = 1; c < cols + 1; c++) {
head->frameString[c] = 196;
}
head->frameString[cols + 1] = 191;
head->frameString[cols + 2] = '\n';
for(int r = 1; r < rows + 1; r++) {
head->frameString[r * (cols + 3)] = 179;
for(int c = 1; c < cols + 1; c++) {
head->frameString[c + r * (cols + 3)] = ' ';
}
head->frameString[(r + 1) * (cols + 3) - 2] = 179;
head->frameString[(r + 1) * (cols + 3) - 1] = '\n';
}
head->frameString[(cols + 3) * (rows + 1)] = 192;
for(int c = 1; c < cols + 1; c++) {
head->frameString[(cols + 3) * (rows + 1) + c] = 196;
}
head->frameString[(cols + 3) * (rows + 2) - 2] = 217;
head->frameString[(cols + 3) * (rows + 2) - 1] = '\0';
// framestring initialized
}
canvas::~canvas() {
delete head->frameString;
delete head;
}
int canvas::rawPos(int row, int col) {
return (col + 1) + (row + 1) * (cols + 3);
}
int canvas::logRow(int rawPos) {
int row = rawPos / (cols + 3);
if(row == 0 || row + 1 == rows) {return -1;}
return row - 1;
}
int canvas::logCol(int rawPos) {
int col = rawPos % (cols + 3);
if(col == 0 || col > cols) {return -1;}
return col - 1;
}
void canvas::setBlock(int row, int col, int key) {
if(row < 0 || row >= rows || col < 0 || col >= cols) {return; }
if(0 <= key && key < 256){
head->frameString[rawPos(row, col)] = key;
}
}
void canvas::print_fs() {
std::cout << head->frameString << std::endl;
}
// cursor:
canvasCRS::canvasCRS(int rows, int cols) : canvas(rows, cols) {
focusedMode = false;
mouseInput = false;
block = 219;
display = NULL;
displayColor = NULL;
// initializing color support
color = 7;
colorHead = new FS;
colorHead->frameString = new unsigned char[(rows + 2) * (cols + 3)];
colorHead->next = NULL;
colorHead->prev = NULL;
for(int i =0; i < (rows + 2) * (cols + 3); i++) {
colorHead->frameString[i] = 7;
}
// color support initialized
// initializing console manipulation
cohan = GetStdHandle(STD_OUTPUT_HANDLE);
cihan = GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode(cihan, &consoleMode_prev);
cfi_prev.cbSize = sizeof(CONSOLE_FONT_INFOEX);
GetCurrentConsoleFontEx(cohan, FALSE, &cfi_prev); // to restore later
cfi.cbSize = sizeof(CONSOLE_FONT_INFOEX);
cfi.dwFontSize.X = 4;
cfi.dwFontSize.Y = 6;
wcscpy(cfi.FaceName, L"Terminal");
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
cfi.nFont = 0;
topLeft.X = 0;
topLeft.Y = 0;
cci.dwSize = 25;
cci.bVisible = FALSE;
// console manipulation initialized
}
canvasCRS::~canvasCRS() {
SetConsoleMode(cihan , consoleMode_prev); // restoring previous consome mode
SetCurrentConsoleFontEx(cohan, FALSE, &cfi_prev);
cls();
delete colorHead->frameString;
delete colorHead;
if(display != NULL) {
delete display;
}
}
void canvasCRS::cls() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
if(!GetConsoleScreenBufferInfo(cohan, &csbi)) {
// handle failure
return;
}
DWORD written = 0;
DWORD length = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(cohan, TEXT(' '),
length, topLeft, &written); // fill the console with spaces
FillConsoleOutputAttribute(cohan, csbi.wAttributes,
length, topLeft, &written); // clear background color formatting, if any
SetConsoleCursorPosition(cohan, topLeft);
//reset console cursor
}
void canvasCRS::print() {
if(focusedMode == false) {
print_fs();
// TODO: add a proper unfocused print functionality here
return;
}
SetConsoleCursorInfo(cohan, &cci);
// in case the console resized, making the console cursor invisible
for(int i = 0;i < (rows + 2) * (cols + 3); i++) {
if(display[i] == head->frameString[i] &&
displayColor[i] == colorHead->frameString[i]) {
continue;
}
display[i] = head->frameString[i];
displayColor[i] = colorHead->frameString[i];
jumpTo.X = i % (cols + 3);
jumpTo.Y = i / (cols + 3);
SetConsoleCursorPosition(cohan, jumpTo);
SetConsoleTextAttribute(cohan, displayColor[i]);
WriteConsole(cohan, &display[i],
1, NULL, NULL); // print
}
}
void canvasCRS::printFull(){
std::cout.flush();
cls();
SetConsoleCursorPosition(cohan, topLeft);
// reset the console cursor
SetConsoleCursorInfo(cohan, &cci);
// in case the console resized, making the console cursor invisible
for(int i = 0;i < (rows + 2) * (cols + 3); i++) {
SetConsoleTextAttribute(cohan, displayColor[i]);
WriteConsole(cohan, &display[i],
1, NULL, NULL); // print
}
SetConsoleTextAttribute(cohan, 7);
}
void canvasCRS::refreshDisplayString() {
displayColor = new unsigned char[(rows + 2) * (cols + 3)];
for(int i = 0;i < (rows + 2) * (cols + 3); i++) {
displayColor[i] = colorHead->frameString[i];
}
display = new unsigned char[(rows + 2) * (cols + 3)];
for(int i = 0;i < (rows + 2) * (cols + 3); i++) {
display[i] = head->frameString[i];
}
}
void canvasCRS::maximize() {
HWND thisWH = GetForegroundWindow();
ShowWindow(thisWH, SW_MAXIMIZE);
Sleep(100);
}
void canvasCRS::setFocusedMode(bool state) {
if(focusedMode == state) {return; }
if(focusedMode == true){
delete display;
SetCurrentConsoleFontEx(cohan, FALSE, &cfi_prev); // restoring font
focusedMode = false;
return;
}
refreshDisplayString(); // initializing display and displayColor FS
SetCurrentConsoleFontEx(cohan, FALSE, &cfi); // setting font
// saving the position to move the cursor back to
jumpBack.X = 0;
jumpBack.Y = rows + 2;
focusedMode = true;
}
void canvasCRS::setMouseInput(bool state) {
if(mouseInput == state) { return; }
if(state == false) {
SetConsoleMode(cihan , consoleMode_prev);
SetCurrentConsoleFontEx(cohan, FALSE, &cfi_prev);
state = false;
return;
}
mouseInput = true;
SetConsoleMode(cihan ,ENABLE_EXTENDED_FLAGS | ENABLE_MOUSE_INPUT);
}
unsigned char canvasCRS::inputLoop() {
if(focusedMode == false) { return 0; }
INPUT_RECORD inputRecord;
DWORD numOfEventsRead;
unsigned char out;
unsigned char outColor;
while(true) {
ReadConsoleInput(cihan, &inputRecord, 1, &numOfEventsRead);
switch(inputRecord.EventType) {
case WINDOW_BUFFER_SIZE_EVENT:
SetConsoleCursorPosition(cohan, topLeft);
cls();
WriteConsole(cohan, head->frameString,
(rows + 2) * (cols + 3), NULL, NULL); // print
WriteConsole(cohan, "\n", 1, NULL, NULL); // newline
continue;
case MOUSE_EVENT:
if(mouseInput == false) { continue; }
break;
case KEY_EVENT:
if(inputRecord.Event.KeyEvent.bKeyDown) {
SetConsoleCursorPosition(cohan, jumpBack);
// moveing the console cursor to jumpBack position
SetConsoleTextAttribute(cohan, 7);
// resetting output color
return inputRecord.Event.KeyEvent.uChar.AsciiChar;
}
default:
continue;
}
switch(inputRecord.Event.MouseEvent.dwButtonState){
case FROM_LEFT_1ST_BUTTON_PRESSED:
out = block;
outColor = color;
break;
case RIGHTMOST_BUTTON_PRESSED:
out = ' ';
outColor = 7;
break;
default:
continue;
}
int Y = inputRecord.Event.MouseEvent.dwMousePosition.Y;
int X = inputRecord.Event.MouseEvent.dwMousePosition.X;
if(Y == 0 || Y >= rows + 1 || X == 0 || X >= cols + 1) { continue; }
// TODO: make a paint(int size) function and call it here
head->frameString[rawPos(Y - 1, X - 1)] = out;
colorHead->frameString[rawPos(Y - 1, X - 1)] = outColor;
print();
redrawConsole();
}
}
void canvasCRS::changeColor(int newColor) {
if(newColor == -1) { color = 7; }
if(0 < newColor && newColor < 16) {
color = newColor;
}
}
void canvasCRS::zoomIn() {
switch(cfi.dwFontSize.X){
case 4:
cfi.dwFontSize.X = 8;
cfi.dwFontSize.Y = 12;
break;
case 8:
cfi.dwFontSize.X = 12;
cfi.dwFontSize.Y = 16;
break;
default:
return;
}
SetCurrentConsoleFontEx(cohan, FALSE, &cfi);
}
void canvasCRS::zoomOut() {
switch(cfi.dwFontSize.X){
case 8:
cfi.dwFontSize.X = 4;
cfi.dwFontSize.Y = 6;
break;
case 12:
cfi.dwFontSize.X = 8;
cfi.dwFontSize.Y = 12;
break;
default:
return;
}
SetCurrentConsoleFontEx(cohan, FALSE, &cfi);
}
int canvasCRS::loadCanvas(std::string fileName) {
std::ifstream file(fileName.data());
if(!file) {return -1; }
int compKey = 0;
file >> compKey;
if(compKey != COMP_KEY) {return -2; }
file >> rows >> cols;
// creating new framestring and colorstring
delete head->frameString;
delete colorHead->frameString;
head->frameString = new unsigned char[(rows + 2) * (cols + 3)];
colorHead->frameString = new unsigned char[(rows + 2) * (cols + 3)];
// initializing frameStringColor
for(int i =0; i < (rows + 2) * (cols + 3); i++) {
colorHead->frameString[i] = 7;
}
// frameStringColor initialized!
// initializing framestring border
head->frameString[0] = 218;
for(int c = 1; c < cols + 1; c++) {
head->frameString[c] = 196;
}
head->frameString[cols + 1] = 191;
head->frameString[cols + 2] = '\n';
for(int r = 1; r < rows + 1; r++) {
head->frameString[r * (cols + 3)] = 179;
head->frameString[(r + 1) * (cols + 3) - 2] = 179;
head->frameString[(r + 1) * (cols + 3) - 1] = '\n';
}
head->frameString[(cols + 3) * (rows + 1)] = 192;
for(int c = 1; c < cols + 1; c++) {
head->frameString[(cols + 3) * (rows + 1) + c] = 196;
}
head->frameString[(cols + 3) * (rows + 2) - 2] = 217;
head->frameString[(cols + 3) * (rows + 2) - 1] = '\0';
// framestring border initialized!
// framsetring and colorstring craetion done!
file.ignore(1);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
head->frameString[rawPos(i, j)] = (unsigned char)file.get();
}
}
file.ignore(1);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
colorHead->frameString[rawPos(i, j)] = (unsigned char)file.get();
}
}
// reloading display string
if(display != NULL){
delete display;
display = new unsigned char[(rows + 2) * (cols + 3)];
delete displayColor;
displayColor = new unsigned char[(rows + 2) * (cols + 3)];
refreshDisplayString(); // refresh displayString and displaColorString
}
// reloading display string done!
file.close();
return 0;
}
int canvasCRS::saveCanvas(std::string fileName) {
std::ifstream checkIfOpen(fileName.data());
if(checkIfOpen) {return -1;} // checking whther the file already exists
checkIfOpen.close();
(void)saveCanvasForce(fileName);
return 0;
}
int canvasCRS::saveCanvasForce(std::string fileName) {
std::ofstream file(fileName.data());
if(!file) {return -1; }
file << COMP_KEY << "\n" << rows << " " << cols << "\n";
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
file << head->frameString[rawPos(i, j)];
}
}
file << "\n";
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
file << colorHead->frameString[rawPos(i, j)];
}
}
file.close();
return 0;
}
void canvasCRS::redrawConsole() { // redraw console window
HWND winHandle = GetForegroundWindow(); // get window handle
InvalidateRect(winHandle, NULL, FALSE); // queue window for updating
UpdateWindow(winHandle); // update window
}