-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
914 lines (673 loc) · 27.7 KB
/
main.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
#include "imgui/imgui.h"
#include "rlImGui/imgui_impl_raylib.h"
#include "pixel-draw.h"
#include "raylib.h"
#include <cctype>
#include <charconv>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <cstring>
#include <cmath>
#include "IconsFontAwesome5.h"
#include <raylib.h>
#include <raymath.h>
/*#define STB_IMAGE_IMPLEMENTATION*/
#include "stb/stb_image.h"
/*#define STB_IMAGE_WRITE_IMPLEMENTATION*/
#include "stb/stb_image_write.h"
using std::vector, std::string;
#define MAX_OUTPUT_FILE_NAME_SIZE 20
// global logic in order to make web build as well
char g_outputFileName[MAX_OUTPUT_FILE_NAME_SIZE];
int g_screenWidth = 1280;
int g_screenHeight = 900;
int g_canvasWidth = 512;
int g_canvasHeight = 512;
int g_pixelBlockSize = g_canvasWidth/32; // basic 32x32
int g_canvasPixelsSize = g_canvasWidth * g_canvasHeight;
float g_imGuiColorFloat[4]{0.0f, 0.0f, 0.0f, 1.0f}; // BLACK Color
float g_canvasScale = 1.0f;
float g_canvasScaleMin = 0.2f;
float g_canvasScaleMax = 5.0f;
float g_guiTitleBarHeight = 20.0f;
float g_guiLayerWidgetHeight = 50.0f;
float g_guiOffsetBetweenLayerWidgets = 5.0f;
// using texture like global increase performance.
Texture2D g_mainCanvasTexture;
Texture2D g_transparentTexture;
ImFont* g_iconFont;
ImFont* g_textFont;
Font g_rlFontIcons;
ImGuiIO *io;
bool g_isHoldingLMB;
bool g_isMouseDraggingCanvas = false;
bool g_canInteractWithCanvas = true;
bool g_isSaveImageWindowOpen = false;
bool g_isNewCanvasWindowOpen = false;
bool g_isColorPickerPressed;
Vector2 g_canvasPos;
Vector2 g_canvasPosBeforeDrag;
Vector2 g_LMBHoldingFirstPos;
Vector2 g_LMBHoldingFirstPosOnCanvas;
Vector2 g_secondLastMousePos;
Vector2 g_lastMousePos;
Vector2 g_secondLastMousePosOnCanvas;
Vector2 g_lastMousePosOnCanvas;
Vector2 g_verticalCenterLineStart;
Vector2 g_verticalCenterLineEnd;
Vector2 g_horizontalCenterLineStart;
Vector2 g_horizontalCenterLineEnd;
Color *g_canvasPixels = new Color[g_canvasPixelsSize]();
/*vector<vector<Color>> g_undoCanvasColorPixels;*/
/*vector<vector<Color>> g_redoCanvasColorPixels;*/
struct Layer{
char name[32];
bool isSelected = false;
bool isActive = true;
// () ensures that each index has default zero value
Color *mainLayerPixels = new Color[g_canvasPixelsSize]();
Color *tmpLayerPixels = new Color[g_canvasPixelsSize]();
vector<vector<Color>> undoLayerColorPixels{};
vector<vector<Color>> redoLayerColorPixels{};
};
vector<Layer> g_layerVector = {{"1"}};
int g_selectedLayerIndex = 0;
PixelDraw g_pixelDraw(g_canvasWidth, g_canvasHeight, g_pixelBlockSize, g_canvasPixels,g_layerVector[0].mainLayerPixels, g_layerVector[0].tmpLayerPixels);
void CenterCanvasPos(){
g_canvasPos.x = g_screenWidth/2-(g_canvasWidth*g_canvasScale)/2;
g_canvasPos.y = g_screenHeight/2-(g_canvasHeight*g_canvasScale)/2;
}
void SetTransparentTexture(){
Color* transparentTexturePixels = new Color[g_canvasPixelsSize];
int countX = 0;
int countY = 0;
bool isGray = true;
for(int i = 0; i < g_canvasWidth; i++){
if(countX >= g_pixelBlockSize){
isGray = !isGray;
countX = 0;
}
countX++;
for(int j = 0; j < g_canvasHeight; j++){
if(countY >= g_pixelBlockSize){
isGray = !isGray;
countY = 0;
}
transparentTexturePixels[i+j*g_canvasWidth] = isGray ? GRAY : LIGHTGRAY;
countY++;
}
}
UpdateTexture(g_transparentTexture,transparentTexturePixels);
}
void CreateCanvas(int pixelSize){
g_pixelBlockSize = pixelSize;
g_pixelDraw = PixelDraw(g_canvasWidth, g_canvasHeight,
g_pixelBlockSize, g_canvasPixels, g_layerVector[g_selectedLayerIndex].mainLayerPixels,
g_layerVector[g_selectedLayerIndex].tmpLayerPixels);
CenterCanvasPos();
g_pixelDraw.ClearLayerPixels();
SetTransparentTexture();
g_layerVector.clear();
}
void SavePixelArt(char fileName[10]){
stbi_write_png(fileName, g_canvasWidth, g_canvasHeight, 4, g_canvasPixels, g_canvasWidth*4);
}
void AddLayerPixelsToUndo(){
vector<Color> prevArr(g_layerVector[g_selectedLayerIndex].mainLayerPixels,
g_layerVector[g_selectedLayerIndex].mainLayerPixels+g_canvasPixelsSize);
// Copying C-array content to std::array
/*copy(g_mainCanvasPixels, g_mainCanvasPixels+g_pixelsSize, prevArr.begin());*/
/*memcpy(prevArr, &g_mainCanvasPixels, g_pixelsSize * sizeof(Color));*/
// delete the limited step undo
if(g_layerVector[g_selectedLayerIndex].undoLayerColorPixels.size() >= 30){
g_layerVector[g_selectedLayerIndex].undoLayerColorPixels.erase(g_layerVector[g_selectedLayerIndex].undoLayerColorPixels.begin());
}
g_layerVector[g_selectedLayerIndex].undoLayerColorPixels.push_back(prevArr);
}
void UpdateCanvas(){
// To remove previous canvas pixels color that are currently blank on some layers
g_pixelDraw.ClearCanvasPixels();
// Updating canvas pixels according to layer ordering
for(int i = 0; i < g_canvasWidth; i++){
for(int j = 0; j < g_canvasHeight; j++){
for(int l = 0; l < g_layerVector.size(); l++){
if(g_layerVector[l].isActive && g_layerVector[l].mainLayerPixels[i+j*g_canvasWidth] != BLANK){
g_canvasPixels[i + j * g_canvasWidth] = g_layerVector[l].mainLayerPixels[i+j*g_canvasWidth];
break;
}
}
}
}
UpdateTexture(g_mainCanvasTexture, g_canvasPixels);
}
void UpdateTMPLayerPixels(){
memcpy(g_layerVector[g_selectedLayerIndex].tmpLayerPixels,g_layerVector[g_selectedLayerIndex].mainLayerPixels, g_canvasPixelsSize * sizeof(Color));
}
/*void UpdateMainCanvasPixels(){*/
/* // simply can copy from tmp to main*/
/* memcpy(g_mainCanvasPixels, g_tmpCanvasPixels, g_canvasPixelsSize * sizeof(Color));*/
/* UpdateTexture(g_mainCanvasTexture, g_mainCanvasPixels);*/
/*}*/
/*void InterpolateBrush(){*/
/* float timeDelta = GetFrameTime();*/
/* float time = 0.5f;*/
/* while(timeDelta < time){*/
/* Vector2 intermidiate = Vector2Lerp(g_prevLastMousePos, g_lastMousePos, timeDelta/time);*/
/* g_pixelDraw.DrawWithBrush(intermidiate.x, intermidiate.y, g_pixelDraw.curDrawingColor);*/
/* timeDelta += GetFrameTime();*/
/* }*/
/*}*/
void Draw() {
switch (g_pixelDraw.curTool) {
case Line:
g_pixelDraw.DrawWithLine(g_LMBHoldingFirstPosOnCanvas.x, g_LMBHoldingFirstPosOnCanvas.y, g_lastMousePosOnCanvas.x, g_lastMousePosOnCanvas.y);
break;
case Brush:
g_pixelDraw.DrawWithBrush(g_secondLastMousePosOnCanvas.x, g_secondLastMousePosOnCanvas.y, g_lastMousePosOnCanvas.x, g_lastMousePosOnCanvas.y);
break;
case Rect:
g_pixelDraw.DrawRectangle(g_LMBHoldingFirstPosOnCanvas.x, g_LMBHoldingFirstPosOnCanvas.y, g_lastMousePosOnCanvas.x, g_lastMousePosOnCanvas.y);
break;
case Eraser:
g_pixelDraw.Erase(g_lastMousePosOnCanvas.x, g_lastMousePosOnCanvas.y);
break;
case Circle:
// The first mouse position is the center, if user is holding left alt - don't delete intermidiate stepsinclude "IconsFontAwesome5.h"
if(IsKeyDown(KEY_LEFT_CONTROL)){
g_pixelDraw.DrawCenteredCircle(g_LMBHoldingFirstPosOnCanvas.x, g_LMBHoldingFirstPosOnCanvas.y,
g_lastMousePosOnCanvas.x, g_lastMousePosOnCanvas.y, IsKeyDown(KEY_LEFT_ALT));
}
// Center is calculated as the midpoint of two mouse vectors, if user is holding left alt - don't delete intermidiate steps
else{
g_pixelDraw.DrawAndStretchCircle(g_LMBHoldingFirstPosOnCanvas.x, g_LMBHoldingFirstPosOnCanvas.y,
g_lastMousePosOnCanvas.x, g_lastMousePosOnCanvas.y, IsKeyDown(KEY_LEFT_ALT));
}
break;
case ColorPicker:
// Changes tool raylib color
g_pixelDraw.SetColorFromPos(g_lastMousePosOnCanvas.x, g_lastMousePosOnCanvas.y);
// To change imgui color
g_imGuiColorFloat[0] = {g_pixelDraw.curDrawingColor.r/255.0f};
g_imGuiColorFloat[1] = {g_pixelDraw.curDrawingColor.g/255.0f};
g_imGuiColorFloat[2] = {g_pixelDraw.curDrawingColor.b/255.0f};
g_imGuiColorFloat[3] = {g_pixelDraw.curDrawingColor.a/255.0f};
break;
case Fill:
g_pixelDraw.FillWithColor(g_lastMousePosOnCanvas.x, g_lastMousePosOnCanvas.y, g_pixelDraw.curDrawingColor);
break;
}
UpdateCanvas();
}
void AddLayerPixelsToRedo(){
vector<Color> curArr(g_layerVector[g_selectedLayerIndex].mainLayerPixels,
g_layerVector[g_selectedLayerIndex].mainLayerPixels+g_canvasPixelsSize);
// Copying C-array content to std::array
/*copy(g_mainCanvasPixels, g_mainCanvasPixels+g_pixelsSize, prevArr.begin());*/
/*memcpy(prevArr, &g_mainCanvasPixels, g_pixelsSize * sizeof(Color));*/
// delete the limited step undo
if(g_layerVector[g_selectedLayerIndex].redoLayerColorPixels.size() >= 30){
g_layerVector[g_selectedLayerIndex].redoLayerColorPixels.erase(g_layerVector[g_selectedLayerIndex].redoLayerColorPixels.begin());
}
g_layerVector[g_selectedLayerIndex].redoLayerColorPixels.push_back(curArr);
}
void StepsControl(){
if(IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_Z)){
// If Left Shift is down - will not go to undo
if(IsKeyDown(KEY_LEFT_SHIFT)){
// REDO FUNCTIONALITY
if(g_layerVector[g_selectedLayerIndex].redoLayerColorPixels.size()){
// Adding current canvas to undo
AddLayerPixelsToUndo();
auto step = g_layerVector[g_selectedLayerIndex].redoLayerColorPixels.back();
g_layerVector[g_selectedLayerIndex].redoLayerColorPixels.pop_back();
memcpy(g_layerVector[g_selectedLayerIndex].mainLayerPixels, step.data(), g_canvasPixelsSize * sizeof(Color));
UpdateTMPLayerPixels();
UpdateCanvas();
/*UpdateTexture(g_tmpCanvasTexture, g_canvasPixels);*/
std::cout<<"pop from redo"<<'\n';
}
return;
}
if(g_layerVector[g_selectedLayerIndex].undoLayerColorPixels.size()){
// UNDO FUNCTIONALITY
// Adding current canvas to redo
AddLayerPixelsToRedo();
auto prevStep = g_layerVector[g_selectedLayerIndex].undoLayerColorPixels.back();
g_layerVector[g_selectedLayerIndex].undoLayerColorPixels.pop_back();
std::cout<<"undo size: "<<g_layerVector[g_selectedLayerIndex].undoLayerColorPixels.size()<<'\n';
memcpy(g_layerVector[g_selectedLayerIndex].mainLayerPixels, prevStep.data(), g_canvasPixelsSize * sizeof(Color));
UpdateTMPLayerPixels();
/*memcpy(g_mainCanvasPixels, prev, g_pixelsSize * sizeof(Color));*/
/*memcpy(g_layerVector[g_se], g_mainCanvasPixels, g_canvasPixelsSize * sizeof(Color));*/
UpdateCanvas();
/*UpdateTexture(g_tmpCanvasTexture, g_tmpCanvasPixels);*/
}
}
}
// Doesn't work yet
/*void ControlCursor(){*/
/*DrawTextCodepoint(g_rlFontIcons, 0xf2c0, g_lastMousePos, 20, BLACK);*/
/*DrawTextCodepoint(Font font, int codepoint, Vector2 position, float fontSize, Color tint)*/
/*std::cout<<" : " ICON_FA_BRUSH<<'\n';*/
/*DrawTextCodepoint(g_rlFontIcons, 0xf6ac, g_lastMousePos, 20, BLACK);*/
/*}*/
void SetMousePositions(){
g_secondLastMousePos = g_lastMousePos;
g_secondLastMousePosOnCanvas = g_lastMousePosOnCanvas;
g_lastMousePos = GetMousePosition();
if(g_isMouseDraggingCanvas){
g_lastMousePosOnCanvas = (g_canvasPosBeforeDrag - g_lastMousePos) / g_canvasScale;
}
else{
g_lastMousePosOnCanvas = (g_canvasPos - g_lastMousePos) / g_canvasScale;
}
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
g_isHoldingLMB = true;
g_LMBHoldingFirstPosOnCanvas = g_lastMousePosOnCanvas;
g_LMBHoldingFirstPos = g_lastMousePos;
}
if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){
g_isHoldingLMB = false;
}
}
void ControlGUIHotKeys(){
if(IsKeyDown(KEY_LEFT_CONTROL)){
if(IsKeyPressed(KEY_S)){
g_isSaveImageWindowOpen = true;
}
}
}
void UpdateCenterLines(){
g_verticalCenterLineStart = Vector2{g_canvasPos.x+(g_canvasWidth*g_canvasScale/2), g_canvasPos.y};
g_verticalCenterLineEnd = Vector2{g_canvasPos.x+(g_canvasWidth*g_canvasScale/2), (float)g_canvasPos.y+g_canvasHeight*g_canvasScale};
g_horizontalCenterLineStart = Vector2{g_canvasPos.x, g_canvasPos.y+(g_canvasHeight*g_canvasScale/2)};
g_horizontalCenterLineEnd = Vector2{(float)g_canvasWidth*g_canvasScale+g_canvasPos.x, g_canvasPos.y+(g_canvasHeight*g_canvasScale/2)};
}
void ControlCanvasTransform(){
// Scale change on mouse scroll
Vector2 mouseWheel = GetMouseWheelMoveV();
if(mouseWheel.y > 0.1f && !io->WantCaptureMouse){
if(g_canvasScale+0.2f > g_canvasScaleMax){
return;
}
g_canvasScale += 0.2f;
CenterCanvasPos();
}
else if(mouseWheel.y < -0.1f && !io->WantCaptureMouse){
if(g_canvasScale-0.2f < g_canvasScaleMin){
return;
}
g_canvasScale -= 0.2f;
CenterCanvasPos();
}
if(IsKeyDown(KEY_LEFT_ALT) && g_isHoldingLMB){
if(!g_isMouseDraggingCanvas){
g_canvasPosBeforeDrag = g_canvasPos;
g_isMouseDraggingCanvas = true;
}
Vector2 differenceVector = g_LMBHoldingFirstPos-g_lastMousePos;
g_canvasPos = g_canvasPosBeforeDrag+differenceVector;
}
else if(g_isMouseDraggingCanvas){
g_isMouseDraggingCanvas = false;
}
// have to update center highlighting lines after canvas transformation
UpdateCenterLines();
}
void ChangeLayer(int layerIndex){
/*memcpy(g_layerVector[g_selectedLayerIndex].g_mainLayerPixels, g_mainCanvasPixels, g_canvasPixelsSize * sizeof(Color));*/
g_selectedLayerIndex = layerIndex;
/*memcpy(g_mainCanvasPixels, g_layerVector[g_selectedLayerIndex].g_mainLayerPixels, g_canvasPixelsSize * sizeof(Color));*/
/*memcpy(g_tmpCanvasPixels, g_layerVector[g_selectedLayerIndex].g_tmpLayerPixels, g_canvasPixelsSize * sizeof(Color));*/
/*g_undoCanvasColorPixels = g_layerVector[g_selectedLayerIndex].undoCanvasColorPixels;*/
/*g_redoCanvasColorPixels = g_layerVector[g_selectedLayerIndex].redoCanvasColorPixels;*/
g_pixelDraw.ChangeLayer(g_layerVector[g_selectedLayerIndex].mainLayerPixels, g_layerVector[g_selectedLayerIndex].tmpLayerPixels);
}
void ControlPopUpWindows(){
if(g_isSaveImageWindowOpen){
ImGui::Begin("File",&g_isSaveImageWindowOpen);
ImGui::InputText("Enter the file name", g_outputFileName, MAX_OUTPUT_FILE_NAME_SIZE);
if(ImGui::Button("Save to PNG")){
SavePixelArt(g_outputFileName);
// Closing window after confirming
g_isSaveImageWindowOpen = false;
}
ImGui::End();
}
if(g_isNewCanvasWindowOpen){
ImGui::Begin("Canvas",&g_isNewCanvasWindowOpen);
/*ImGui::InputInt("Width", &g_canvasWidth, 0, 0);*/
/*ImGui::InputInt("Height", &g_canvasHeight, 0,0);*/
const char* items[] = {"8x8", "16x16", "32x32", "64x64", "128x128", "256x256"};
static int selectedItemIndex = 0;
if(ImGui::BeginListBox("Pixel size")){
for (int n = 0; n < IM_ARRAYSIZE(items); n++)
{
const bool isSelected = (selectedItemIndex == n);
if (ImGui::Selectable(items[n], isSelected))
selectedItemIndex = n;
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (isSelected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndListBox();
}
auto v = items[selectedItemIndex];
if(ImGui::Button("Create Canvas")){
switch (selectedItemIndex){
case 0:
CreateCanvas(g_canvasWidth/8);
break;
case 1:
CreateCanvas(g_canvasWidth/16);
break;
case 2:
CreateCanvas(g_canvasWidth/32);
break;
case 3:
CreateCanvas(g_canvasWidth/64);
break;
case 4:
CreateCanvas(g_canvasWidth/128);
break;
case 5:
CreateCanvas(g_canvasWidth/256);
break;
}
// Closing window after confirming
g_isNewCanvasWindowOpen = false;
}
ImGui::End();
}
}
/*void ChangeLayerNameIfRequired(int layerIndex){*/
/**/
/* for(int i = 0 ; i < g_layerVector.size(); i++){*/
/* if(layerIndex == i) */
/* continue;*/
/**/
/* if(strcmp(g_layerVector[i].name,g_layerVector[layerIndex].name) == 0){*/
/**/
/* int len = strlen(g_layerVector[i].name);*/
/* std::cout<<len<<'\n';*/
/* char lastChar = g_layerVector[i].name[len-1];*/
/* if(isdigit(lastChar)){*/
/* }*/
/* else{*/
/* strcat(g_layerVector[layerIndex].name, " 1");*/
/* }*/
/* return;*/
/* }*/
/* }*/
/*}*/
void DrawAndControlLayersGUILogic(){
bool isLayersWindowOpened = true;
ImGui::Begin("Layers", &isLayersWindowOpened);
ImVec2 layersWinPos = ImGui::GetWindowPos();
ImVec2 mousePos = ImGui::GetMousePos();
// Calculating mouse pos inside the layer's window without the titlebar height and item offset
float mouseYRelativeToFirstLayerWidget = mousePos.y - layersWinPos.y-g_guiTitleBarHeight-g_guiOffsetBetweenLayerWidgets;
for(int i = 0; i < g_layerVector.size();i++){
// Pushing Unique ID in order to change state of each individual Layer Widget (Selectable, InputText, CheckBox)
// Only the last element of the array changes without this feature
ImGui::PushID(i); // ID by layer index inside of a layer vector
const bool isSelected = (i == g_selectedLayerIndex);
// Getting widget drawing cursor here in order to use it while drawing
// next overlapping widgets
ImVec2 cursorPos = ImGui::GetCursorPos();
// Layer selectable indexation text
/*const char* indexChar = std::to_string(i+1).c_str(); */
ImGui::Selectable("##selectable text", isSelected, ImGuiSelectableFlags_AllowOverlap,ImVec2{ImGui::GetWindowWidth(),g_guiLayerWidgetHeight});
/*if(ImGui::InputText("##textInput", g_layerVector[i].name, sizeof(g_layerVector[i].name), ImGuiInputTextFlags_)){*/
/*}*/
int curItem = mouseYRelativeToFirstLayerWidget / (g_guiLayerWidgetHeight+g_guiOffsetBetweenLayerWidgets);
// Logic to drag selectable layers and swap their places
if(ImGui::IsItemFocused() && ImGui::IsMouseDragging(0)){
if(curItem >= 0 && curItem != i && curItem < g_layerVector.size()){
// TODO swap changes items multiple times, have to fix.
std::swap(g_layerVector[i], g_layerVector[curItem]);
// Swapped the indexed so we change the layerIndex on the index of the cur position
g_selectedLayerIndex = curItem;
UpdateCanvas();
std::cout<<"layers swapped"<<'\n';
}
}
// Selecting the item on double click
if(ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)){
ChangeLayer(i);
}
ImGui::SetCursorPos(ImVec2{cursorPos.x+35, cursorPos.y});
ImGui::InputText("##layer input text", g_layerVector[i].name, sizeof(g_layerVector[i].name), ImGuiInputTextFlags_AutoSelectAll);
ImGui::SameLine();
if(ImGui::Checkbox("##layer check box", &g_layerVector[i].isActive)){
//If layer visibility state changed then we need to update canvas pixels
UpdateCanvas();
}
// Flushing ID
ImGui::PopID();
ImGui::SetCursorPosY(cursorPos.y+g_guiLayerWidgetHeight+10);
}
if(ImGui::Button("Create layer")){
Layer nLayer = Layer{"New layer"};
g_layerVector.insert(g_layerVector.begin(), nLayer);
/*ChangeLayerNameIfRequired(0);*/
// Appending to the beginning, so current layer index will be index++
g_selectedLayerIndex++;
}
ImGui::End();
}
void DrawAndControlGUI() {
ImGui_ImplRaylib_ProcessEvents();
ImGui_ImplRaylib_NewFrame();
ImGui::NewFrame();
ImGui::PushFont(g_textFont);
bool isFunctionsPanelOpened = true;
ImGui::Begin("Functions panel", &isFunctionsPanelOpened, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
ImGui::SetWindowPos(ImVec2(0.0f, 0.0f));
ImGui::SetWindowSize(ImVec2(g_screenWidth, 30));
if(ImGui::BeginMenuBar()){
if(ImGui::BeginMenu("File")){
if(ImGui::MenuItem("New Canvas", "Ctrl+N")){
g_isNewCanvasWindowOpen = true;
}
if(ImGui::MenuItem("Save image", "Ctrl+S")){
g_isSaveImageWindowOpen = true;
}
ImGui::EndMenu();
}
if(ImGui::BeginMenu("Donate")){
if(ImGui::MenuItem("Patreon")){
OpenURL("https://www.patreon.com/SoFarDevelopment");
}
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
ImGui::End(); // Functions panel
ControlPopUpWindows();
ImGui::Begin("Tool panel");
ImGui::SetWindowPos(ImVec2(0.0f, 30.0f));
if (ImGui::Button("Clear Canvas")) {
g_pixelDraw.ClearLayerPixels();
UpdateTexture(g_mainCanvasTexture, g_canvasPixels);
/*UpdateTexture(g_tmpCanvasTexture, g_mainCanvasPixels);*/
}
ImGui::SliderInt("Tool Size", &g_pixelDraw.curToolSize, 1, 10);
ImGui::PopFont();
/*---- ICONS SECTION -----*/
// Color choosing for drawing
// Changing color only on release for optimization reasons
if (ImGui::ColorPicker4(ICON_FA_PALETTE " Brush Color", g_imGuiColorFloat)) {
if (!g_isColorPickerPressed) {
g_isColorPickerPressed = true;
}
} else {
if (g_isColorPickerPressed) {
unsigned char r = g_imGuiColorFloat[0] * 255;
unsigned char g = g_imGuiColorFloat[1] * 255;
unsigned char b = g_imGuiColorFloat[2] * 255;
unsigned char a = g_imGuiColorFloat[3] * 255;
g_pixelDraw.curDrawingColor = {r, g, b, a};
g_isColorPickerPressed = false;
}
}
if (ImGui::Button(ICON_FA_PENCIL_RULER " Line")) {
g_pixelDraw.curTool = Tool::Line;
}
if (ImGui::Button(ICON_FA_PAINT_BRUSH " Brush")) {
g_pixelDraw.curTool = Tool::Brush;
}
if(ImGui::CollapsingHeader(ICON_FA_SHAPES " Brush Shapes")){
if (ImGui::Button("Brush: Circle")) {
g_pixelDraw.curBrushShape = BrushShape::CircleBrush;
}
if (ImGui::Button("Brush: Square")) {
g_pixelDraw.curBrushShape = BrushShape::SquareBrush;
}
}
if (ImGui::Button(ICON_FA_ERASER " Eraser")) {
g_pixelDraw.curTool = Tool::Eraser;
}
if (ImGui::Button(ICON_FA_EYE_DROPPER "Color Picker")) {
g_pixelDraw.curTool = Tool::ColorPicker;
}
if (ImGui::Button(ICON_FA_CIRCLE " Circle")) {
g_pixelDraw.curTool = Tool::Circle;
}
if (ImGui::Button(ICON_FA_FILL " Fill")) {
g_pixelDraw.curTool = Tool::Fill;
}
if (ImGui::Button( " Rectangle")){
g_pixelDraw.curTool = Tool::Rect;
}
ImGui::Checkbox("Mirror X Axis", &g_pixelDraw.xAxisMirror);
ImGui::Checkbox("Mirror Y Axis", &g_pixelDraw.yAxisMirror);
ImGui::End();
bool isScrollbarsOpen = true;
/*-----HORIZONTAL VIEW SCROLLBAR--------*/
ImGui::Begin("Horizontal bar",&isScrollbarsOpen, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar);
ImGui::PushItemWidth(g_screenWidth-17);
ImGui::SetWindowPos(ImVec2(0, g_screenHeight-40));
ImGui::SetWindowSize(ImVec2(g_screenWidth, 40));
ImGui::SliderFloat("##Horizontal view", &g_canvasPos.x, 0-g_canvasWidth, g_screenWidth, "%.0f");
ImGui::End();
/*-----VERTICAL VIEW SCROLLBAR--------*/
/*ImGui::Begin("Vertical bar",&isScrollbarsOpen, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar);*/
/*ImGui::PushItemWidth(g_screenWidth-17);*/
/*ImGui::SetWindowPos(ImVec2(400, 0));*/
/*ImGui::SliderFloat("##Vertical view", &g_canvasPos.y, 0-g_canvasHeight, g_screenHeight, "%.0f");*/
/*ImGui::End();*/
DrawAndControlLayersGUILogic();
ImGui::Render();
ImGui_ImplRaylib_RenderDrawData(ImGui::GetDrawData());
}
void SetupStyles(ImGuiIO io){
ImGuiStyle* style = &ImGui::GetStyle();
style->Colors[ImGuiCol_Border] = ImVec4(0.5f, 0.5f, 0.5f, 1.0f);
style->Colors[ImGuiCol_Button] = ImVec4(0.0f,0.0f,0.0f,1.0f);
style->Colors[ImGuiCol_Header] = ImVec4(0.0f,0.0f,0.0f,1.0f);
style->Colors[ImGuiCol_FrameBg] = ImVec4(0.0f,0.0f,0.0f,1.0f);
/*style->Colors[ImGuiCol_TitleBg] = ImVec4(0.0f,0.0f,0.0f,1.0f);*/
/*style->FrameBorderSize = 1;*/
style->FrameRounding = 3;
style->ChildRounding = 3;
io.Fonts->AddFontDefault();
float baseFontSize = 25.0f;
float iconFontSize = baseFontSize * 2.0f / 3.0f;
/*static const ImWchar rangesFixed[] = {*/
/* 0x0020, 0x00FF, // Basic Latin + Latin Supplement*/
/* 0x2026, 0x2026, // ellipsis*/
/* 0*/
/*};*/
static const ImWchar icons_ranges[] = {ICON_MIN_FA, ICON_MAX_16_FA, 0};
ImFontConfig icons_config;
icons_config.MergeMode = true;
/*io.Fonts->TexDesiredWidth = 2;*/
g_iconFont = io.Fonts->AddFontFromFileTTF( "fonts/" FONT_ICON_FILE_NAME_FAS, iconFontSize, &icons_config, icons_ranges );
g_textFont = io.Fonts->AddFontFromFileTTF( "fonts/louis-george-cafe.ttf", 20);
int codepoints[2] = {0xf2b9, 0xf6ad};
g_rlFontIcons = LoadFontEx("fonts/" FONT_ICON_FILE_NAME_FAS, iconFontSize, codepoints, 2);
io.Fonts->Build();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
/*ImGui::StyleColorsDark();*/
/*Font rlFontIcons = LoadFontEx("fonts/" FONT_ICON_FILE_NAME_FAS, iconFontSize, ICON_MIN_FA, ICON_MAX_16_FA);*/
ImGui_ImplRaylib_Init();
Imgui_ImplRaylib_BuildFontAtlas();
}
void ShouldHighlightCenter(){
if(IsKeyDown(KEY_LEFT_SHIFT)){
DrawLineEx(g_verticalCenterLineStart, g_verticalCenterLineEnd, 3.0f,BLACK);
DrawLineEx(g_horizontalCenterLineStart, g_horizontalCenterLineEnd, 3.0f,BLACK);
}
}
// buggy
/*void DrawSizeCursor(){*/
/* DrawCircleLines(g_lastMousePos.x, g_lastMousePos.y, g_brushSize, BLACK);*/
/*}*/
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void) {
// Initialization
//--------------------------------------------------------------------------------------
/*SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI);*/
InitWindow(g_screenWidth, g_screenHeight, "Pixelater");
CenterCanvasPos();
ImGui::CreateContext();
io = &ImGui::GetIO(); (void)io;
SetupStyles(*io);
/*g_pixelDraw.ClearLayerPixels();*/
Image g_image = {g_canvasPixels, g_canvasWidth, g_canvasHeight, 1,
PIXELFORMAT_UNCOMPRESSED_R8G8B8A8};
g_mainCanvasTexture = LoadTextureFromImage(g_image);
g_transparentTexture = LoadTextureFromImage(g_image);
SetTransparentTexture();
/*ToggleFullscreen();*/
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
ClearBackground(WHITE);
DrawTextureEx(g_transparentTexture, Vector2{g_canvasPos.x,g_canvasPos.y}, 0.0f, g_canvasScale,WHITE);
BeginDrawing();
DrawAndControlGUI();
StepsControl();
ControlGUIHotKeys();
/*ControlCursor();*/
SetMousePositions();
ControlCanvasTransform();
ShouldHighlightCenter();
/*DrawSizeCursor();*/
DrawTextureEx(g_mainCanvasTexture, Vector2{g_canvasPos.x,g_canvasPos.y}, 0.0f, g_canvasScale,WHITE);
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) && !io->WantCaptureMouse) {
Draw();
}
if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){
UpdateTMPLayerPixels();
// TODO make layer system work with undo/redo
// Means that we can delete redo after undo's
if(g_layerVector[g_selectedLayerIndex].redoLayerColorPixels.size()){
g_layerVector[g_selectedLayerIndex].redoLayerColorPixels.clear();
}
AddLayerPixelsToUndo();
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
ImGui_ImplRaylib_Shutdown();
ImGui::DestroyContext();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}