-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathElements.cpp
1007 lines (807 loc) · 22.8 KB
/
Elements.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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Elements.h"
// Forward declarations
class Vanity {
public:
static void Rerender();
static SDL_Texture* LoadImage(std::string imagePath);
static int GetViewportWidth();
static int GetViewportHeight();
static void SetActiveTextbox(Textbox* textbox);
};
class Button;
// ELEMENTS - Common methods for every element type
Element::Element() {
display = true;
x = 0;
y = 0;
width = 0;
height = 0;
};
int Element::GetX() {
return this->x;
}
int Element::GetY() {
return this->y;
}
int Element::GetWidth() {
return this->width;
}
int Element::GetHeight() {
return this->height;
}
int Element::GetComputedWidth() {
return this->width;
}
int Element::GetComputedHeight() {
return this->height;
}
bool Element::GetDisplayState() {
return this->display;
}
void Element::SetWidth(int width) {
this->width = width;
}
void Element::SetHeight(int height) {
this->height = height;
}
void Element::SetDimensions(int width, int height) {
this->width = width;
this->height = height;
}
void Element::SetWidth(std::string percentage) {
for (int i = 0; i < percentage.length(); i++) {
std::string value(1, percentage[i]);
if (value == "%") {
int perc = std::stoi(percentage);
auto parent = this->GetParent();
if (parent) {
this->SetComputedWidth(parent->GetWidth() / 100 * perc);
}
else {
this->SetComputedWidth(Vanity::GetViewportWidth() / 100 * perc);
}
}
}
}
void Element::SetHeight(std::string percentage) {
for (int i = 0; i < percentage.length(); i++) {
std::string value(1, percentage[i]);
if (value == "%") {
int perc = std::stoi(percentage);
auto parent = this->GetParent();
if (parent) {
this->SetComputedHeight(parent->GetHeight() / 100 * perc);
}
else {
this->SetComputedHeight(Vanity::GetViewportHeight() / 100 * perc);
}
}
}
}
void Element::SetDimensions(std::string percentageWidth, std::string percentageHeight) {
this->SetWidth(percentageWidth);
this->SetHeight(percentageHeight);
}
void Element::SetComputedWidth(int width) {
this->width = width;
}
void Element::SetComputedHeight(int height) {
this->height = height;
}
void Element::SetPosition(int x, int y) {
this->x = x;
this->y = y;
Vanity::Rerender();
}
void Element::SetPositionX(int value) {
this->x = value;
}
void Element::SetPositionY(int value) {
this->y = value;
}
void Element::Show() {
this->display = true;
Vanity::Rerender();
}
void Element::Hide() {
this->display = false;
Vanity::Rerender();
}
// PARENTABLE
Parentable::Parentable() {
parent = nullptr;
}
Division* Parentable::GetParent() {
return this->parent;
}
void Parentable::SetParent(Division* parent) {
this->parent = parent;
Vanity::Rerender();
}
Color::Color() {
SDL_Color white = { 255, 255, 255, 255 };
SDL_Color fadedWhite = { 255, 255, 255, 180 };
color = white;
hoverColor = fadedWhite;
}
SDL_Color Color::GetColor() {
return this->color;
}
SDL_Color Color::GetHoverColor() {
return this->hoverColor;
}
void Color::SetColor(SDL_Color color) {
this->color = color;
}
void Color::SetHoverColor(SDL_Color color) {
this->hoverColor = color;
}
// BORDER
Border::Border() {
// Thickness
BorderThickness bt = { 2, 2, 2, 2}; // Thickness for top, right, bottom and left border
borderThickness = bt;
// Colors
SDL_Color white = { 255, 255, 255, 255 };
BorderColors bc = { white, white, white, white };
borderColors = bc;
}
BorderThickness Border::GetBorderThickness() {
return this->borderThickness;
}
BorderColors Border::GetBorderColors() {
return this->borderColors;
}
int Border::GetBorderThicknessLeft() {
return this->borderThickness.left;
}
int Border::GetBorderThicknessTop() {
return this->borderThickness.top;
}
int Border::GetBorderThicknessRight() {
return this->borderThickness.right;
}
int Border::GetBorderThicknessBottom() {
return this->borderThickness.bottom;
}
SDL_Color Border::GetBorderColorLeft() {
return this->borderColors.left;
}
SDL_Color Border::GetBorderColorTop() {
return this->borderColors.top;
}
SDL_Color Border::GetBorderColorRight() {
return this->borderColors.right;
}
SDL_Color Border::GetBorderColorBottom() {
return this->borderColors.bottom;
}
void Border::SetBorderThickness(BorderThickness borderThickness) {
this->borderThickness = borderThickness;
}
void Border::SetBorderThicknessLeft(int thickness) {
this->borderThickness.left = thickness;
}
void Border::SetBorderThicknessTop(int thickness) {
this->borderThickness.top = thickness;
}
void Border::SetBorderThicknessRight(int thickness) {
this->borderThickness.right = thickness;
}
void Border::SetBorderThicknessBottom(int thickness) {
this->borderThickness.bottom = thickness;
}
void Border::SetBorderColors(BorderColors borderColors) {
this->borderColors = borderColors;
}
void Border::SetBorderColorLeft(SDL_Color color) {
this->borderColors.left = color;
}
void Border::SetBorderColorTop(SDL_Color color) {
this->borderColors.top = color;
}
void Border::SetBorderColorRight(SDL_Color color) {
this->borderColors.right = color;
}
void Border::SetBorderColorBottom(SDL_Color color) {
this->borderColors.bottom = color;
}
// FONT
Font::Font() {
font = nullptr;
fontColor = SDL_Color{ 255, 255, 255, 255 };
fontSize = 15;
}
TTF_Font* Font::GetFont() {
return this->font;
}
int Font::GetFontSize() {
return this->fontSize;
}
SDL_Color Font::GetFontColor() {
return this->fontColor;
}
void Font::SetFont(std::string path) {
if (!(this->font = TTF_OpenFont(path.c_str(), this->fontSize))) {
char t[] = "Font error";
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, t, TTF_GetError(), nullptr);
exit(0);
}
}
void Font::SetFontSize(int size) {
this->fontSize = size;
}
void Font::SetFontColor(SDL_Color* color) {
this->fontColor = *color;
}
// BACKGROUND IMAGE
BackgroundImage::BackgroundImage() {
image = nullptr;
displayBackgroundImage = true;
}
SDL_Texture* BackgroundImage::GetBackgroundImage() {
return this->image;
}
bool BackgroundImage::GetBackgroundImageDisplayState() {
return this->displayBackgroundImage;
}
void BackgroundImage::SetBackgroundImage(std::string imagePath) {
this->image = Vanity::LoadImage(imagePath);
}
void BackgroundImage::ShowBackgroundImage() {
this->displayBackgroundImage = true;
}
void BackgroundImage::HideBackgroundImage() {
this->displayBackgroundImage = false;
}
// ANIMATION
Animation::Animation() {
animationActive = false;
transitionTargetX = NULL;
transitionTargetY = NULL;
animationTimespanMs = 1000;
animationStartTimestamp = NULL;
animationStyle = "linear";
animationRebound = false;
}
bool Animation::GetAnimationState() {
return this->animationActive;
}
void Animation::SetAnimation(int x, int y, std::string style, int timespan, bool rebound) {
this->transitionTargetX = x;
this->transitionTargetY = y;
this->animationStyle = style;
this->animationTimespanMs = timespan;
this->animationRebound = rebound;
}
void Animation::SetAnimationRebound(bool value) {
this->animationRebound = value;
}
void Animation::SetTransitionTarget(int x, int y) {
this->transitionTargetX = x;
this->transitionTargetY = y;
}
void Animation::SetAnimationState(bool state) {
this->animationActive = state;
}
void Animation::CalculateNextAnimationStep(int& x, int& y, Button& element) {
Uint32 elapsedMs = SDL_GetTicks() - this->animationStartTimestamp;
// Calculates how many steps should be taken from element's origin point based on elapsed time
float animationStep = (float)elapsedMs / 16.66;
// Calculate the actual coordinate to transelate towards relative to element's position
float posXDiff = x + this->transitionTargetX - x;
float posYDiff = y + this->transitionTargetY - y;
float stepX = 0, stepY = 0;
// Calculate next step on X and Y axis based on specified animation style/algorithm
if (this->animationStyle == "linear") {
stepX = posXDiff / ((float)this->animationTimespanMs / 16.66);
stepY = posYDiff / ((float)this->animationTimespanMs / 16.66);
}
else if (this->animationStyle == "easing") {
// TODO: Implement more animation styles/algorithms
}
// Calculate new position
x = x + stepX * animationStep;
y = y + stepY * animationStep;
// Check if animation has reached its end goal
if (elapsedMs >= this->animationTimespanMs) {
this->animationActive = false;
if (!this->animationRebound)
element.SetPosition(this->transitionTargetX, this->transitionTargetY);
}
}
void Animation::Animate() {
this->animationActive = true;
this->animationStartTimestamp = SDL_GetTicks();
}
// PADDING
ElementPadding::ElementPadding() {
Padding p = { 10, 15, 10, 15 }; // Thickness for top, right, bottom and left border
this->padding = p;
}
Padding ElementPadding::GetPadding() {
return this->padding;
}
int ElementPadding::GetPaddingTop() {
return this->padding.top;
}
int ElementPadding::GetPaddingRight() {
return this->padding.right;
}
int ElementPadding::GetPaddingBottom() {
return this->padding.bottom;
}
int ElementPadding::GetPaddingLeft() {
return this->padding.left;
}
void ElementPadding::SetPadding(Padding value) {
this->padding = value;
}
void ElementPadding::SetPaddingTop(int value) {
this->padding.top = value;
}
void ElementPadding::SetPaddingRight(int value) {
this->padding.right = value;
}
void ElementPadding::SetPaddingBottom(int value) {
this->padding.bottom = value;
}
void ElementPadding::SetPaddingLeft(int value) {
this->padding.left = value;
}
// TOUCHED
Touched::Touched() {
this->touched = false;
}
bool Touched::GetTouched() {
return this->touched;
}
void Touched::SetTouched(bool value) {
this->touched = value;
}
Clickable::Clickable() {
this->clicked = false;
}
bool Clickable::Clicked() {
if (this->clicked) {
this->clicked = false;
return true;
}
return false;
}
void Clickable::SetClickedState(bool value) {
this->clicked = value;
Vanity::Rerender();
}
// BUTTON
Button::Button(std::string label, int width, int height, int x, int y, int fontSize, std::string fontPath) {
this->label = label;
this->width = width;
this->height = height;
this->x = x;
this->y = y;
this->display = true;
this->fontSize = fontSize;
SDL_Color c = { 0, 0, 0, 175 };
SDL_Color hc = { 25, 25, 25, 175 };
this->color = c;
this->hoverColor = hc;
this->SetFont(fontPath);
}
int Button::GetComputedWidth() {
return this->padding.left + this-> width + this->padding.right + this->borderThickness.left + this->borderThickness.right;
}
int Button::GetComputedHeight() {
return this->padding.top + this->height + this->padding.bottom + this->borderThickness.top + this->borderThickness.bottom;
}
std::string Button::GetLabel() {
return this->label;
}
void Button::SetComputedWidth(int width) {
this->width = width - this->padding.left - this->padding.right - this->borderThickness.left - this->borderThickness.right;
}
void Button::SetComputedHeight(int height) {
this->height = height - this->padding.top - this->padding.bottom - this->borderThickness.top - this->borderThickness.left;
}
void Button::SetLabel(std::string label) {
this->label = label;
Vanity::Rerender();
}
void Button::SetLabel(int label) {
this->label = std::to_string(label);
Vanity::Rerender();
}
void Button::SetLabel(double label) {
this->label = std::to_string(label);
Vanity::Rerender();
}
void Button::HorizontallyAlignCenter() {
int parentWidth = this->GetParent()->GetWidth();
this->SetPositionX(parentWidth / 2 - this->GetComputedWidth() / 2);
}
void Button::VerticallyAlignCenter() {
int parentHeight = this->GetParent()->GetHeight();
this->SetPositionY(parentHeight / 2 - this->GetComputedHeight() / 2);
}
void Button::AlignTop() {
this->SetPositionX(0);
}
void Button::AlignRight() {
int alignedPosition = this->GetParent()->GetWidth() - this->GetComputedWidth();
this->SetPositionX(alignedPosition);
}
void Button::AlignBottom() {
int alignedPosition = this->GetParent()->GetHeight() - this->GetComputedHeight();
this->SetPositionY(alignedPosition);
}
void Button::AlignLeft() {
this->SetPositionX(0);
}
// TEXTBOX
Textbox::Textbox(std::string placeholder, int width, int height, int x, int y, int fontSize, int limit, std::string fontPath) {
this->placeholder = placeholder;
this->width = width;
this->height = height;
this->x = x;
this->y = y;
this->display = true;
this->fontSize = fontSize;
SDL_Color c = { 0, 0, 0, 175 };
SDL_Color hc = { 25, 25, 25, 175 };
this->color = c;
this->charLimit = limit;
this->hoverColor = hc;
this->SetFont(fontPath);
}
int Textbox::GetComputedWidth() {
return this->padding.left + this->width + this->padding.right + this->borderThickness.left + this->borderThickness.right;
}
int Textbox::GetComputedHeight() {
return this->padding.top + this->height + this->padding.bottom + this->borderThickness.top + this->borderThickness.bottom;
}
std::string Textbox::GetPlaceholder() {
return this->placeholder;
}
std::string Textbox::GetValue() {
return this->value;
}
int Textbox::GetCharLimit() {
return this->charLimit;
}
void Textbox::SetPlaceholder(std::string placeholder) {
this->placeholder = placeholder;
Vanity::Rerender();
}
void Textbox::SetPlaceholder(int placeholder) {
this->placeholder = std::to_string(placeholder);
Vanity::Rerender();
}
void Textbox::SetPlaceholder(double placeholder) {
this->placeholder = std::to_string(placeholder);
Vanity::Rerender();
}
void Textbox::SetValue(std::string value) {
this->value = value;
Vanity::Rerender();
}
void Textbox::SetCharLimit(int limit) {
this->charLimit = limit;
}
void Textbox::SetComputedWidth(int width) {
this->width = width - this->padding.left - this->padding.right - this->borderThickness.left - this->borderThickness.right;
}
void Textbox::SetComputedHeight(int height) {
this->height = height - this->padding.top - this->padding.bottom - this->borderThickness.top - this->borderThickness.bottom;
}
void Textbox::Focus() {
Vanity::SetActiveTextbox(this);
}
// LABEL
Label::Label(std::string text, int x, int y, SDL_Color color, int fontSize, std::string fontPath) {
this->text = text;
this->x = x;
this->y = y;
this->color = color;
this->display = true;
this->fontSize = fontSize;
this->SetFont(fontPath);
ComputeDimensionsOfText(this->font, this->text.length(), this->width, this->height);
}
std::string Label::GetText() {
return this->text;
}
void Label::SetText(std::string text) {
this->text = text;
ComputeDimensionsOfText(this->font, this->text.length(), this->width, this->height);
Vanity::Rerender();
}
void Label::SetText(int text) {
this->text = std::to_string(text);
ComputeDimensionsOfText(this->font, this->text.length(), this->width, this->height);
Vanity::Rerender();
}
void Label::SetText(double text) {
this->text = std::to_string(text);
ComputeDimensionsOfText(this->font, this->text.length(), this->width, this->height);
Vanity::Rerender();
}
// CHECKBOX
Checkbox::Checkbox(int x, int y, int size, bool defaultState) {
this->x = x;
this->y = y;
this->size = size;
this->width = size;
this->height = size;
this->display = true;
this->checked = defaultState;
this->clickedLastFrame = false;
SDL_Color c = { 0, 0, 0, 175 };
SDL_Color hc = { 25, 25, 25, 175 };
SDL_Color cc = { 255, 255, 255, 255 };
this->color = c;
this->hoverColor = hc;
this->checkmarkColor = cc;
}
int Checkbox::GetComputedWidth() {
return this->width + this->borderThickness.left + this->borderThickness.right;
}
int Checkbox::GetComputedHeight() {
return this->height + this->borderThickness.top + this->borderThickness.bottom;
}
SDL_Color Checkbox::GetCheckmarkColor() {
return this->checkmarkColor;
}
int Checkbox::GetSize() {
return this->size;
}
bool Checkbox::GetClickedLastFrame() {
return this->clickedLastFrame;
}
void Checkbox::SetCheckmarkColor(SDL_Color* color) {
this->checkmarkColor = *color;
Vanity::Rerender();
}
void Checkbox::SetSize(int size) {
this->size = size;
this->x = size;
this->y = size;
Vanity::Rerender();
}
void Checkbox::SetState(bool state) {
this->checked = state;
Vanity::Rerender();
}
void Checkbox::SetClickedLastFrame(bool state) {
this->clickedLastFrame = state;
}
bool Checkbox::IsChecked() {
return this->checked;
}
// IMAGE
Image::Image(std::string imagePath, int x, int y, int width, int height) {
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->display = true;
this->image = Vanity::LoadImage(imagePath);
// Set border thickness to 0 to disable
BorderThickness bt = { 0, 0, 0, 0 }; // Thickness for top, right, bottom and left border
borderThickness = bt;
}
SDL_Texture* Image::GetImage() {
return this->image;
}
// SLIDER
Slider::Slider(int x, int y, int width, int height, int thumbWidth, int thumbHeight) {
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->thumbWidth = thumbWidth;
this->thumbHeight = thumbHeight;
this->display = true;
this->value = 50;
this->thumbPosision = x + width / 2 - thumbWidth / 2;
SDL_Color c = { 0, 0, 0, 120 };
SDL_Color hc = { 25, 25, 25, 100 };
SDL_Color tc = { 255, 255, 255, 255 };
this->color = c;
this->hoverColor = hc;
this->thumbColor = tc;
// Set border thickness to 0 to disable
BorderThickness bt = { 0, 0, 0, 0 }; // Thickness for top, right, bottom and left border
borderThickness = bt;
}
SDL_Color Slider::GetThumbColor() {
return this->thumbColor;
}
int Slider::GetThumbWidth() {
return this->thumbWidth;
}
int Slider::GetThumbHeight() {
return this->thumbHeight;
}
int Slider::GetValue() {
return this->value;
}
int Slider::GetThumbPosision() {
return this->thumbPosision;
}
void Slider::SetThumbColor(SDL_Color* color) {
this->thumbColor = *color;
Vanity::Rerender();
}
void Slider::SetThumbWidth(int width) {
this->thumbWidth = width;
Vanity::Rerender();
}
void Slider::SetThumbHeight(int height) {
this->thumbHeight = thumbHeight;
Vanity::Rerender();
}
void Slider::SetValue(int value) {
this->value = value;
Vanity::Rerender();
}
void Slider::SetThumbPosision(int posision) {
this->thumbPosision = posision;
Vanity::Rerender();
}
// DIVISION
Division::Division(int x, int y, int width, int height) {
this->x = x;
this->y = y;
this->width = width;
this->height = height;
SDL_Color color = { 0, 0, 0, 50 };
this->color = color;
this->autoExpand = true;
// Set border thickness to 0 to disable
BorderThickness bt = { 0, 0, 0, 0 }; // Thickness for top, right, bottom and left border
borderThickness = bt;
// Specify padding values since divisions should have different default padding values than other elements
this->padding = { 10, 10, 10, 10 };
this->elements = new std::vector<Element*>();
}
int Division::GetComputedWidth() {
return this->padding.left + this->width + this->padding.right + this->borderThickness.left + this->borderThickness.right;
}
int Division::GetComputedHeight() {
return this->padding.top + this->height + this->padding.bottom + this->borderThickness.top + this->borderThickness.bottom;
}
bool Division::GetAutoExpand() {
return this->autoExpand;
}
std::vector<Element*>* Division::GetElements() {
return this->elements;
}
std::vector<Button*>* Division::GetButtons() {
auto buttons = new std::vector<Button*>();
for (int i = 0; i < elements->size(); i++) {
if (Button* button = dynamic_cast<Button*>((*elements)[i])) {
buttons->push_back(button);
}
}
return buttons;
}
std::vector<Label*>* Division::GetLabels() {
auto labels = new std::vector<Label*>();
for (int i = 0; i < elements->size(); i++) {
if (Label* label = dynamic_cast<Label*>((*elements)[i])) {
labels->push_back(label);
}
}
return labels;
}
std::vector<Checkbox*>* Division::GetCheckboxes() {
auto checkboxes = new std::vector<Checkbox*>();
for (int i = 0; i < elements->size(); i++) {
if (Checkbox* checkbox = dynamic_cast<Checkbox*>((*elements)[i])) {
checkboxes->push_back(checkbox);
}
}
return checkboxes;
}
std::vector<Textbox*>* Division::GetTextboxes() {
auto textboxes = new std::vector<Textbox*>();
for (int i = 0; i < elements->size(); i++) {
if (Textbox* textbox = dynamic_cast<Textbox*>((*elements)[i])) {
textboxes->push_back(textbox);
}
}
return textboxes;
}
std::vector<Image*>* Division::GetImages() {
auto images = new std::vector<Image*>();
for (int i = 0; i < elements->size(); i++) {
if (Image* image = dynamic_cast<Image*>((*elements)[i])) {
images->push_back(image);
}
}
return images;
}
std::vector<Slider*>* Division::GetSliders() {
auto sliders = new std::vector<Slider*>();
for (int i = 0; i < elements->size(); i++) {
if (Slider* slider = dynamic_cast<Slider*>((*elements)[i])) {
sliders->push_back(slider);
}
}
return sliders;
}
std::vector<Division*>* Division::GetDivisions() {
auto divisions = new std::vector<Division*>();
for (int i = 0; i < elements->size(); i++) {
if (Division* division = dynamic_cast<Division*>((*elements)[i])) {
divisions->push_back(division);
}
}
return divisions;
}
Division* Division::SetAutoExpand(bool value) {
this->autoExpand = value;
return this;
}
void Division::SetComputedWidth(int width) {
this->width = width - this->padding.left - this->padding.right - this->borderThickness.left - this->borderThickness.right;
}
void Division::SetComputedHeight(int height) {
this->height = height - this->padding.top - this->padding.bottom - this->borderThickness.top - this->borderThickness.bottom;
}
Division* Division::AddChild(Element* element) {
element->SetParent(this);
this->elements->push_back(element);
if (this->autoExpand) {
int elementSpan = element->GetX() + element->GetComputedWidth();
if (this->GetComputedWidth() < elementSpan) {
// Division needs to expand in width to fit child element
this->SetWidth(elementSpan);
}
elementSpan = element->GetY() + element->GetComputedHeight();
if (this->GetComputedHeight() < elementSpan) {
// Division needs to expand in height to fit child element
this->SetHeight(elementSpan);
}
}
Vanity::Rerender();
return this;
}
void Division::HorizontallyAlignElementsCenter() {
int divWidth = this->width;
for (int i = 0; i < this->elements->size(); i++) {
auto element = (*elements)[i];
element->SetPositionX(divWidth / 2 - element->GetComputedWidth() / 2);
}
}
void Division::VerticallyAlignElementsCenter() {
int divHeight = this->height;
for (int i = 0; i < this->elements->size(); i++) {
auto element = (*elements)[i];
element->SetPositionY(divHeight / 2 - element->GetComputedHeight() / 2);
}
}
void Division::AlignElementsTop() {
for (int i = 0; i < this->elements->size(); i++) {
(*elements)[i]->SetPositionY(0);
}
}
void Division::AlignElementsRight() {
int divWidth = this->GetWidth();
for (int i = 0; i < this->elements->size(); i++) {
auto element = (*elements)[i];
element->SetPositionX(divWidth - element->GetComputedWidth());
}
}
void Division::AlignElementsBottom() {
int divHeight = this->GetHeight();
for (int i = 0; i < this->elements->size(); i++) {
auto element = (*elements)[i];
element->SetPositionY(divHeight - element->GetComputedHeight());
}