-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscg-controls.h
More file actions
1428 lines (1283 loc) · 37.7 KB
/
scg-controls.h
File metadata and controls
1428 lines (1283 loc) · 37.7 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
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
#pragma once
#include "scg-utility.h"
#include "scg-graph.h"
#include "scg-settings.h"
#include "scg-console.h"
#include <map>
#include <vector>
#include <string>
#include <thread>
#include <mutex>
using namespace std;
/*
All in all: Handling graphics' command:
1. Draw
2. 'HasChanges = true' (Unless you do this, the control will never be updated)
In this file should NOT contain direct "text_" color information!
*/
namespace scg {
// These are declarations.
class control;
class master;
using control_symbol = string; // Must use string to register controls -- like Windows.
class pre_render_event_args : public event_args {
public:
pre_render_event_args(master *MasterPointer = nullptr) : MasterPointer(MasterPointer) {
}
master *MasterPointer;
};
class resize_event_args : public event_args {
public:
resize_event_args(console_size NewHeight = 0, console_size NewWidth = 0) : NewHeight(NewHeight), NewWidth(NewWidth) {
}
bool Valid() const {
return NewHeight != 0 && NewWidth != 0;
}
console_size NewHeight, NewWidth;
};
/*
This is a pure virtual (abstract) class, which means you can't create
object from it -- You should inherit it.
Later: Put layout system here (by using something like +=)
*/
class control {
public:
struct control_place {
control *target;
coords pos;
control_symbol syn;
control_place(control *Target = nullptr, control_symbol Name = "", coords ControlPos = nullptr) : target(Target), syn(Name), pos(ControlPos) {
}
operator control&() {
return MyControl();
}
control& MyControl() {
if (target == nullptr) throw scg_exception("Incorrect controller");
return *target;
}
};
using controls = map<control_symbol, control_place>;
using controls_it = controls::iterator;
virtual client_area& GetClientArea() = 0;
virtual void ProcessKey(keyboard_state KeyInfo) = 0;
virtual control_symbol operator += (control_place others) {
sub_controls[others.syn] = others;
auto &omc = others.MyControl();
omc.my_coords = others.pos;
omc.father = this;
omc.my_symbol = others.syn;
current_active = sub_controls.find(others.syn);
return others.syn;
}
virtual bool operator -= (control_symbol others) {
if (!sub_controls.count(others)) return false;
// Get previous one's client area
auto &to_delete = sub_controls[others];
auto &td_client = to_delete.MyControl().GetClientArea();
td_client.Fillup(spixel(' ', background_master));
// Redraw all
this->GetClientArea().MergeWith(td_client, to_delete.pos.x, to_delete.pos.y);
for (auto &i : sub_controls) {
if (i.first == others) {
continue;
}
this->GetClientArea().MergeWith(i.second.MyControl().GetClientArea(), i.second.pos.x, i.second.pos.y);
}
// End of redraw
to_delete.MyControl().IsDeleted = true;
if (current_active->first == others) ActiveNext();
sub_controls.erase(others);
return true;
}
virtual void MoveControl(control_symbol ControlToMove, coords MoveTarget) {
if (!sub_controls.count(ControlToMove)) return;
auto &origin = sub_controls[ControlToMove];
auto &orect = origin.MyControl().GetClientArea();
auto &self_area = this->GetClientArea();
ClearControlArea(ControlToMove);
origin.pos = MoveTarget;
origin.MyControl().my_coords = MoveTarget;
origin.MyControl().HasChanges = true;
origin.MyControl().OnMove.RunEvent(event_args());
UpdateSubControls(&self_area, false, true);
}
virtual void ResizeControl(control_symbol ControlToMove, console_size NewHeight, console_size NewWidth) {
if (!sub_controls.count(ControlToMove)) return;
auto &origin = sub_controls[ControlToMove];
auto &orect = origin.MyControl().GetClientArea();
auto &self_area = this->GetClientArea();
ClearControlArea(ControlToMove);
origin.MyControl().OnResize.RunEvent(resize_event_args(NewHeight, NewWidth));
origin.MyControl().HasChanges = true;
UpdateSubControls(&self_area, false, true);
}
virtual void ActivePrevious() {
if (sub_controls.size() <= 0) return; // Already no element!
size_t yield = 0;
do {
if (current_active == sub_controls.begin()) current_active = sub_controls.end();
current_active--;
if (yield >= sub_controls.size()) return; // All elements are not ready
yield++;
} while ((!current_active->second.MyControl().Enabled) || (!current_active->second.MyControl().Visible));
}
virtual void ActiveNext() {
if (sub_controls.size() <= 0) return; // Already no element!
bool have_enable = false;
for (auto &i : sub_controls) {
if (i.second.MyControl().Enabled) {
have_enable = true;
break;
}
}
if (!have_enable) {
current_active = sub_controls.begin();
return; // Can't set!
}
size_t yield = 0;
do {
current_active++;
if (yield >= sub_controls.size()) return; // All elements are not ready
yield++;
if (current_active == sub_controls.end()) current_active = sub_controls.begin();
} while ((!current_active->second.MyControl().Enabled) || (!current_active->second.MyControl().Visible));
}
// Common event for all controls:
event<event_args> LostFocus;
event<event_args> GotFocus;
event<pre_render_event_args> PreRender; // Call at the first render
event<event_args> OnMove;
event<resize_event_args> OnResize;
// Warning: in AfterDraw the display will be not updated!
event<event_args> AfterDraw; // After drawing (mostly for input box)
//bool HasChanges = true; // For initial loader
property<bool> HasChanges = property<bool>(
[this](bool &tmp) -> bool {
return IHasChanges;
},
[this](bool sets, bool &tmp) {
IHasChanges = sets;
}
);
bool Enabled = true; // true if can be selected
// true if the control can be shown
property<bool> Visible = property<bool>(
[this](bool &tmp) -> bool {
return IVisible;
},
[this](bool sets, bool &tmp) {
if (IVisible != sets) {
IVisible = sets;
this->HasChanges = true;
}
}
);
spixel my_background = background_master;
property<bool> IsDeleted = property<bool>(
[this](bool &buf) {
return buf;
}, [this](bool sets, bool &buf) {
buf = sets;
if (sets == true) {
this->HasChanges = true; // Remove old client area
this->Enabled = false;
this->GetClientArea().Fillup(spixel(' ', 0, true)); // Fill it with transparent
}
}
);
protected:
// Will not validate
virtual void ClearControlArea(control_symbol ControlToMove) {
auto &origin = sub_controls[ControlToMove];
auto &orect = origin.MyControl().GetClientArea();
auto &self_area = this->GetClientArea();
for (console_pos i = origin.pos.x; i < origin.pos.x + orect.SizeH; i++) {
for (console_pos j = origin.pos.y; j < origin.pos.y + orect.SizeW; j++) {
self_area[i][j] = spixel(' ', my_background.color_info);
}
}
}
coords GetBaseCoords() {
if (father == nullptr) return my_coords;
return my_coords + father->GetBaseCoords();
}
coords my_coords;
// Unused now:
bool IProcessActive = false;
bool IVisible = true;
bool IHasChanges = true;
virtual bool CurrentActivationAvailable() {
return sub_controls.size() && (current_active != sub_controls.end());
}
virtual void UpdateSubControls(client_area *cl = nullptr, bool ProcessActive = false, bool ForceAll = false) {
auto &mc_area = (cl == nullptr) ? (this->GetClientArea()) : (*cl);
for (auto &i : sub_controls) {
if (ProcessActive) {
if (i.first == current_active->first) continue; // Draw active control at last
}
auto &mc = i.second.MyControl();
if ((!mc.Visible) && mc.HasChanges) {
auto &origin = i.second;
auto &orect = mc.GetClientArea();
auto &self_area = mc_area;
for (console_pos i = origin.pos.x; i < origin.pos.x + orect.SizeH; i++) {
for (console_pos j = origin.pos.y; j < origin.pos.y + orect.SizeW; j++) {
self_area[i][j] = spixel(' ', my_background.color_info);
}
}
mc.HasChanges = false;
HasChanges = true;
}
}
for (auto &i : sub_controls) {
if (ProcessActive) {
if (i.first == current_active->first) continue; // Draw active control at last
}
auto &mc = i.second.MyControl();
if (!mc.Visible) continue;
mc.UpdateSubControls();
if (mc.HasChanges || ForceAll) {
mc_area.MergeWith(mc.GetClientArea(), i.second.pos.x, i.second.pos.y);
mc.HasChanges = false;
HasChanges = true;
}
}
if (ProcessActive || ForceAll) {
auto &i = *current_active;
auto &mc = i.second.MyControl();
mc.UpdateSubControls();
if (mc.HasChanges) {
if (mc.Visible) {
mc_area.MergeWith(mc.GetClientArea(), i.second.pos.x, i.second.pos.y);
}
else {
// Actually active control should not be hidden ... It's undefined behavior.
auto &origin = i.second;
auto &orect = mc.GetClientArea();
auto &self_area = mc_area;
for (console_pos i = origin.pos.x; i < origin.pos.x + orect.SizeH; i++) {
for (console_pos j = origin.pos.y; j < origin.pos.y + orect.SizeW; j++) {
self_area[i][j] = spixel(' ', my_background.color_info);
}
}
}
mc.HasChanges = false;
HasChanges = true;
}
}
}
controls sub_controls;
controls_it current_active;
control *father = nullptr;
control_symbol my_symbol;
//client_area mc_area;
};
class invisible_control : public control {
public:
invisible_control() {
this->IVisible = false;
this->IProcessActive = false;
this->Enabled = property<bool>([](bool &in) -> bool {return false; }, [](bool set, bool &in) {});
this->Visible = property<bool>([](bool &in) -> bool {return false; }, [](bool set, bool &in) {});
}
virtual client_area& GetClientArea() {
return empty_area;
}
virtual void ProcessKey(keyboard_state KeyInfo) {
// Do nothing
}
private:
client_area empty_area = client_area(1, 1); // 0,0 seem to be fail
};
// For easier access
using control_set = control::control_place;
class Protection {
public:
static void RaiseError(string Description) {
ErrorRaised = true;
ResetConsole();
SetTextDisplay();
MoveAbsoluteCursor(coords(0, 0));
#ifdef _WIN32
system("color 1f");
#endif
printf("An critical error has occured and Console Graphics has been \n");
printf("shut down to protect your application. \n");
printf(" \n");
printf("Technial information: Error: \n%s", Description.c_str());
while (1) {
this_thread::yield();
}
}
private:
static bool ErrorRaised;
};
bool Protection::ErrorRaised = false;
/*
This class is about master application. It should have 1 only.
*/
class master : public control {
public:
master() : mc_area(client_area(height, width)) {
this->IProcessActive = true;
}
virtual client_area& GetClientArea() {
// Must be locked
render_lock.lock();
UpdateSubControls(&mc_area, true);
render_lock.unlock();
return mc_area;
}
void MasterRender() {
GetClientArea().Draw();
for (auto &i : sub_controls) {
i.second.MyControl().AfterDraw.RunEvent(event_args());
}
}
virtual void ProcessKey(keyboard_state KeyInfo) {
// Jump to another window if it is, or send it down to active one
if (CurrentActivationAvailable()) {
if (KeyInfo == switch_windows) {
current_active->second.MyControl().LostFocus.RunEvent(event_args());
ActiveNext();
current_active->second.MyControl().GotFocus.RunEvent(event_args());
}
else {
current_active->second.MyControl().ProcessKey(KeyInfo);
}
}
// After processing key, draw (Only for master!)
MasterRender();
}
// Call this for prompt bar
void BarPrompt(string PromptText, pixel_color Style) {
ClearPrompt(Style);
console_pos ptr = 0;
for (auto &i : PromptText) {
mc_area[height - 1][ptr++] = i;
}
GetClientArea().Draw();
}
void BarClean() {
for (console_pos i = 0; i < width; i++) mc_area[height - 1][i] = spixel(' ', 0);
}
#if defined(_WIN32)
string BarInput(string PromptText, pixel_color Style, int BufferSize = 2048) {
ClearPrompt(Style);
console_pos ptr = 0;
for (auto &i : PromptText) {
mc_area[height - 1][ptr++] = i;
}
GetClientArea().Draw();
// After this: move cursor and listen!
MoveAbsoluteCursor(coords(height - 1, ptr));
char *s = new char[BufferSize];
SetCursorDisplay(display_show, display_enable);
fgets(s, BufferSize - 1, stdin);
SetCursorDisplay(display_show, display_disable);
BarClean();
GetClientArea().Draw();
return s;
}
#else
// WARNING: DEPRECATED UNDER LINUX
string BarInput(string PromptText, pixel_color Style, int BufferSize = 2048, bool UseNormalMode = true) {
ClearPrompt(Style);
console_pos ptr = 0;
for (auto &i : PromptText) {
mc_area[height - 1][ptr++] = i;
}
GetClientArea().Draw();
// After this: move cursor and listen!
MoveAbsoluteCursor(coords(height - 1, ptr));
char *s = new char[BufferSize];
if (UseNormalMode) LinuxGetchSupporter::ResetConsoleSettings();
SetCursorDisplay(display_show, display_enable);
fgets(s, BufferSize - 1, stdin);
if (UseNormalMode) LinuxGetchSupporter::ApplySCGSettings();
SetCursorDisplay(display_show, display_disable);
BarClean();
GetClientArea().Draw();
return s;
}
#endif
// Call this after all of work
void MainLoop() {
try {
ResetConsole();
SetEscapeOutput();
SetCursorDisplay(display_show, display_disable);
if (sub_controls.size() <= 0) {
Protection::RaiseError("SCG Exception: Must have at least one sub-control");
return;
}
for (auto &i : sub_controls) {
i.second.MyControl().PreRender.RunEvent(pre_render_event_args(this));
}
current_active->second.MyControl().GotFocus.RunEvent(event_args());
GetClientArea().Draw(); // Initial drawer
while (true) {
ProcessKey(GetKeyboard());
//this_thread::yield();
}
}
catch (scg_exception e) {
Protection::RaiseError(string("SCG Exception:") + e.what());
}
catch (exception ne) {
Protection::RaiseError(string("C++ Exception:") + ne.what());
}
}
private:
void ClearPrompt(pixel_color Style) {
for (console_pos i = 0; i < width; i++) mc_area[height - 1][i] = spixel(' ', Style);
}
// Buffered
client_area mc_area;
mutex render_lock;
};
class window : public control {
public:
window(console_size Height, console_size Width) : mc_area(client_area(Height, Width)) {
my_background = background_window;
mc_area.Fillup(spixel(' ', background_window));
// For title bar
// Default LostFocus Drawer
this->CurrentStyle = inactive_window;
PreRender += [this](pre_render_event_args e) {
this->my_master = e.MasterPointer;
DrawBar(inactive_window);
// Send DOWN PreRender()
for (auto &i : sub_controls) {
i.second.MyControl().PreRender.RunEvent(pre_render_event_args(this->my_master));
}
HasChanges = true;
};
OnResize += [this](resize_event_args e) {
// Window don't invalidate its area!
mc_area.Resize(e.NewHeight, e.NewWidth);
//this->Width.fdata = e.NewWidth;
//this->Height.fdata = e.NewHeight;
mc_area.Fillup(client_area::pixel(' ', my_background.color_info));
DrawBar(this->CurrentStyle);
this->UpdateTitle(this->Title);
HasChanges = true;
};
LostFocus += [this](event_args e) {
// Set window title bar to inactive state
this->CurrentStyle = inactive_window;
DrawBar(inactive_window);
HasChanges = true;
};
// Default GotFocus Drawer
GotFocus += [this](event_args e) {
// Set window title bar to active state
this->CurrentStyle = active_window;
DrawBar(active_window);
HasChanges = true;
};
AfterDraw += [this](event_args e) {
for (auto &i : sub_controls) {
i.second.MyControl().AfterDraw.RunEvent(event_args());
}
};
}
virtual client_area& GetClientArea() {
// Should be like this: Update all sons
// Will become foreground if the order of dictionary of the name is bigger (like: 'Z' > 'A').
// (For window: Has its borders (1 line))
UpdateSubControls(&mc_area);
return mc_area;
}
virtual void ProcessKey(keyboard_state KeyInfo) {
// For active control ...
switch (KeyInfo) {
case go_prev_control:
if (CurrentActivationAvailable()) current_active->second.MyControl().LostFocus.RunEvent(event_args());
ActivePrevious();
if (CurrentActivationAvailable()) current_active->second.MyControl().GotFocus.RunEvent(event_args());
break;
case go_next_control:
if (CurrentActivationAvailable()) current_active->second.MyControl().LostFocus.RunEvent(event_args());
ActiveNext();
if (CurrentActivationAvailable()) current_active->second.MyControl().GotFocus.RunEvent(event_args());
break;
default:
if (CurrentActivationAvailable()) {
current_active->second.MyControl().ProcessKey(KeyInfo);
}
break;
}
}
// Get or set the title of the whole window
property<string> Title = property<string>([this](string &tmp) -> string {
return tmp;
}, [this](string sets, string &tmp) {
tmp = sets;
UpdateTitle(sets);
// Draw title bar
});
private:
void DrawBar(pixel_color Color) {
for (console_size i = 0; i < mc_area.SizeW; i++) mc_area[0][i].color_info = Color;
}
void UpdateTitle(string TitleText) {
if (mc_area.SizeW - 1 <= TitleText.length()) return;
for (size_t i = 0; i < TitleText.length(); i++) mc_area[0][i] = TitleText[i];
HasChanges = true;
}
pixel_color CurrentStyle;
client_area mc_area;
master *my_master;
};
// Insert more controls ....
// Label control (show some text).
class label : public control {
public:
label(string Text, console_size Height, console_size Width) : mc_area(client_area(Height, Width)) {
//this->Enabled = false; // Can't be selected!
mc_area.Fillup(spixel(' ', text_label));
this->Height = Height;
this->Width = Width;
//this->Style = text_label;
this->Text = Text;
this->PreRender += [this](event_args e) {
this->Enabled = false;
};
this->OnResize += [this](resize_event_args e) {
mc_area.Resize(e.NewHeight, e.NewWidth);
this->Width.fdata = e.NewWidth;
this->Height.fdata = e.NewHeight;
mc_area.Fillup(client_area::pixel(' ', my_background.color_info));
this->RedrawText();
};
}
virtual client_area& GetClientArea() {
// Do nothing for child
return mc_area;
}
virtual void ProcessKey(keyboard_state KeyInfo) {
// Do nothing for keys
}
virtual control_symbol operator += (control_place others) {
throw scg_exception("Cannot add sub control for a label");
}
virtual bool operator -= (control_symbol others) {
throw scg_exception("Cannot remove sub control from a label");
}
virtual void ActiveNext() {
throw scg_exception("Cannot active sub control of a label");
}
virtual void ActivePrevious() {
throw scg_exception("Cannot active sub control of a label");
}
property<console_size> Height = property<console_size>([this](console_size &buf) -> console_size {
return buf;
}, [this](console_size sets, console_size &buf) {
InvalidateText();
buf = sets;
RedrawText();
}
);
property<console_size> Width = property<console_size>([this](console_size &buf) -> console_size {
return buf;
}, [this](console_size sets, console_size &buf) {
InvalidateText();
buf = sets;
RedrawText();
}
);
property<string> Text = property<string>([this](string &buf) -> string {
return buf;
}, [this](string sets, string &buf) {
InvalidateText();
buf = sets;
RedrawText();
});
property<pixel_color> Style = property<pixel_color>([this](pixel_color &buf) -> pixel_color {
return buf;
}, [this](pixel_color sets, pixel_color &buf) {
buf = sets;
console_size MyHeight = Height, MyWidth = Width;
for (console_pos i = 0; i < MyHeight; i++) {
for (console_pos j = 0; j < MyWidth; j++) {
mc_area[i][j].color_info = sets;
}
}
HasChanges = true;
});
protected:
void InvalidateText() {
string TextData = this->Text;
console_size MyHeight = Height, MyWidth = Width;
console_pos CurrentX = 0, CurrentY = 0; // X: linear pos, Y: wide pos.
for (auto &i : TextData) {
if (i == '\n') {
CurrentX++;
continue;
}
if (i == '\t') {
CurrentY += tab_size;
continue;
}
if (CurrentY == MyWidth) {
CurrentX++;
CurrentY = 0;
}
if (CurrentX >= MyHeight) {
break; // Break the rest
}
mc_area[CurrentX][CurrentY] = ' ';
CurrentY++;
}
HasChanges = true;
}
void RedrawText() {
string TextData = this->Text;
console_size MyHeight = Height, MyWidth = Width;
console_pos CurrentX = 0, CurrentY = 0; // X: linear pos, Y: wide pos.
for (auto &i : TextData) {
if (i == '\n') {
CurrentX++;
CurrentY = 0;
continue;
}
if (i == '\t') {
CurrentY += tab_size;
continue;
}
if (CurrentY == MyWidth) {
CurrentX++;
CurrentY = 0;
}
if (CurrentX >= MyHeight) {
break; // Break the rest
}
mc_area[CurrentX][CurrentY] = i;
CurrentY++;
}
HasChanges = true;
}
client_area mc_area;
};
// Button control
class button : public control {
public:
button(string Text, console_size Height, console_size Width) : mc_area(client_area(Height, Width)) {
//this->Enabled = false; // Can't be selected!
mc_area.Fillup(spixel(' ', inactived_button));
this->Height = Height;
this->Width = Width;
//this->Style = text_label;
//this->HaveGotFocus = false;
//this->IsActived = true;
this->Text = Text;
PreRender += [this](event_args e) {
this->Enabled = true;
this->DrawBar(inactived_button);
};
LostFocus += [this](event_args e) {
// Set window title bar to inactive state
HaveGotFocus = false;
this->CurrentStyle = inactived_button;
this->DrawBar(inactived_button);
};
// Default GotFocus Drawer
GotFocus += [this](event_args e) {
// Set window title bar to active state
HaveGotFocus = true;
this->CurrentStyle = actived_button;
this->DrawBar(actived_button);
};
OnResize += [this](resize_event_args e) {
mc_area.Resize(e.NewHeight, e.NewWidth);
this->Width.fdata = e.NewWidth;
this->Height.fdata = e.NewHeight;
mc_area.Fillup(client_area::pixel(' ', my_background.color_info));
InvalidateText();
RedrawText();
DrawBar(CurrentStyle);
};
}
virtual client_area& GetClientArea() {
// Do nothing for child
return mc_area;
}
virtual void ProcessKey(keyboard_state KeyInfo) {
if (KeyInfo == active_button && Enabled) {
OnClick.RunEvent(event_args());
}
}
virtual control_symbol operator += (control_place others) {
throw scg_exception("Cannot add sub control for a button");
}
virtual bool operator -= (control_symbol others) {
throw scg_exception("Cannot remove sub control from a button");
}
virtual void ActiveNext() {
throw scg_exception("Cannot active sub control of a button");
}
virtual void ActivePrevious() {
throw scg_exception("Cannot active sub control of a button");
}
property<console_size> Height = property<console_size>([this](console_size &buf) -> console_size {
return buf;
}, [this](console_size sets, console_size &buf) {
InvalidateText();
buf = sets;
RedrawText();
}
);
property<console_size> Width = property<console_size>([this](console_size &buf) -> console_size {
return buf;
}, [this](console_size sets, console_size &buf) {
InvalidateText();
buf = sets;
RedrawText();
}
);
property<string> Text = property<string>([this](string &buf) -> string {
return buf;
}, [this](string sets, string &buf) {
InvalidateText();
buf = sets;
RedrawText();
});
property<pixel_color> Style = property<pixel_color>([this](pixel_color &buf) -> pixel_color {
return buf;
}, [this](pixel_color sets, pixel_color &buf) {
buf = sets;
console_size MyHeight = Height, MyWidth = Width;
for (console_pos i = 0; i < MyHeight; i++) {
for (console_pos j = 0; j < MyWidth; j++) {
mc_area[i][j].color_info = sets;
}
}
this->CurrentStyle = sets;
HasChanges = true;
});
property<bool> IsActived = property<bool>([this](bool &buf) -> bool {
return Enabled;
}, [this](bool sets, bool &buf) {
Enabled = sets;
if (sets) {
if (HaveGotFocus) {
this->DrawBar(actived_button);
this->CurrentStyle = actived_button;
}
else {
this->DrawBar(inactived_button);
this->CurrentStyle = inactived_button;
}
}
else {
this->DrawBar(deactived_button);
this->CurrentStyle = deactived_button;
}
});
event<event_args> OnClick;
protected:
bool HaveGotFocus = false;
pixel_color CurrentStyle = inactived_button;
void DrawBar(pixel_color BarColor) {
for (console_size i = 0; i < mc_area.SizeH; i++) {
for (console_size j = 0; j < mc_area.SizeW; j++) {
auto &m = mc_area[i][j];
m.color_info = BarColor;
}
}
HasChanges = true;
}
void InvalidateText() {
string TextData = this->Text;
console_size MyHeight = Height, MyWidth = Width;
console_pos CurrentX = 0, CurrentY = 0; // X: linear pos, Y: wide pos.
for (auto &i : TextData) {
if (i == '\n') {
CurrentX++;
continue;
}
if (i == '\t') {
CurrentY += tab_size;
continue;
}
if (CurrentY == MyWidth) {
CurrentX++;
CurrentY = 0;
}
if (CurrentX >= MyHeight) {
break; // Break the rest
}
mc_area[CurrentX][CurrentY] = ' ';
CurrentY++;
}
HasChanges = true;
}
void RedrawText() {
string TextData = this->Text;
console_size MyHeight = Height, MyWidth = Width;
console_pos CurrentX = 0, CurrentY = 0; // X: linear pos, Y: wide pos.
for (auto &i : TextData) {
if (i == '\n') {
CurrentX++;
CurrentY = 0;
continue;
}
if (i == '\t') {
CurrentY += tab_size;
continue;
}
if (CurrentY == MyWidth) {
CurrentX++;
CurrentY = 0;
}
if (CurrentX >= MyHeight) {
break; // Break the rest
}
mc_area[CurrentX][CurrentY] = i;
CurrentY++;
}
HasChanges = true;
}
client_area mc_area;
};
class input : public control {
public:
input(console_size Height, console_size Width, string Text = "") : mc_area(client_area(Height, Width)) {
mc_area.Fillup(spixel(' ', background_input));
this->Text = Text;
this->Height = Height;
this->Width = Width;
this->GotFocus += [this](event_args e) {
SetCursorDisplay(display_show, display_enable);
MoveAbsoluteCursor(GetBaseCoords());
has_focus = true;
};
this->LostFocus += [this](event_args e) {
SetCursorDisplay(display_show, display_disable);
has_focus = false;
};
this->AfterDraw += [this](event_args e) {
if (has_focus) MoveAbsoluteCursor(GetBaseCoords() + coords(MyCurrentX, MyCurrentY));
};
this->OnResize += [this](resize_event_args e) {
mc_area.Resize(e.NewHeight, e.NewWidth);
this->Width.fdata = e.NewWidth;
this->Height.fdata = e.NewHeight;
mc_area.Fillup(client_area::pixel(' ', my_background.color_info));
this->DrawBar();
this->RedrawText();
};
}
virtual client_area& GetClientArea() {
// Do nothing for child
return mc_area;
}
virtual void ProcessKey(keyboard_state KeyInfo) {
// Process input
if (KeyInfo.PrimaryKey >= 32 && KeyInfo.PrimaryKey <= 126 && KeyInfo.ExtendedKey == 0) {
// Acceptable input ...
this->Text = this->Text.GetValue() + char(KeyInfo.PrimaryKey);
}
else if (KeyInfo.PrimaryKey == delete_key) {
// Delete ...
string s = this->Text.GetValue();
if (s.length())
this->Text = s.substr(0, s.length() - 1);
}
else if (KeyInfo.PrimaryKey == 9) {
// Tab ...
this->Text = this->Text.GetValue() + '\t';
}
else if (KeyInfo.PrimaryKey == 13) {
// Enter ...
this->Text = this->Text.GetValue() + '\n';
}
}
virtual control_symbol operator += (control_place others) {
throw scg_exception("Cannot add sub control for a textbox");
}
virtual bool operator -= (control_symbol others) {
throw scg_exception("Cannot remove sub control from a textbox");
}
virtual void ActiveNext() {
throw scg_exception("Cannot active sub control of a textbox");