-
Notifications
You must be signed in to change notification settings - Fork 30
/
cuss.cpp
1657 lines (1406 loc) · 35.4 KB
/
cuss.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 "cuss.h"
#include "stringfunc.h"
#include <sstream>
#include <fstream>
using namespace cuss;
void print_scrollbar(Window *win, int posx, int posy, int length, int offset,
int size, bool selected);
#define SELECTCOLOR c_blue
#define PREP_ELEMENT(ele) \
(ele)->name = name;\
(ele)->posx = posx;\
(ele)->posy = posy;\
(ele)->sizex = szx;\
(ele)->sizey = szy;\
(ele)->selected = false;\
(ele)->selectable = selectable;\
(ele)->align = ALIGN_LEFT
std::string cuss::element_type_name(cuss::element_type type)
{
switch (type) {
case ELE_NULL: return "NULL";
case ELE_DRAWING: return "Drawing";
case ELE_TEXTBOX: return "Text";
case ELE_LIST: return "List selection";
case ELE_TEXTENTRY: return "Text Entry";
case ELE_NUMBER: return "Number";
case ELE_MENU: return "Menu";
default: return "Unknown";
}
return "What the heck";
}
// Base save/load functions.
std::string element::save_data()
{
std::stringstream ret;
ret << name << " " << STD_DELIM << " " << posx << " " << posy << " " <<
sizex << " " << sizey << " " << selectable << " " << align << " " <<
v_align;
return ret.str();
}
void element::load_data(std::istream &datastream)
{
name = load_to_delim(datastream, STD_DELIM);
int tmpalign, tmpvalign;
datastream >> posx >> posy >> sizex >> sizey >> selectable >> tmpalign >>
tmpvalign;
align = alignment(tmpalign);
v_align = vertical_alignment(tmpvalign);
}
bool element::set_data(nc_color FG, nc_color BG)
{
if (fg != c_null)
fg = FG;
if (BG != c_null) // bg defaults to c_null
bg = BG;
return true;
}
// *** DRAWING ELEMENT ***
void ele_drawing::draw(Window *win)
{
std::map<Point, glyph>::iterator it;
for (it = drawing.begin(); it != drawing.end(); it++)
win->putglyph( posx + it->first.x, posy + it->first.y, it->second);
}
std::string ele_drawing::save_data()
{
std::stringstream ret;
ret << element::save_data() << " " << drawing.size() << " ";
std::map<Point, glyph>::iterator it;
for (it = drawing.begin(); it != drawing.end(); it++)
ret << it->first.x << " " << it->first.y << " " << it->second.save_data() <<
" ";
return ret.str();
}
void ele_drawing::load_data(std::istream &datastream)
{
element::load_data(datastream);
int tmpsize;
datastream >> tmpsize;
for (int i = 0; i < tmpsize; i++) {
Point tmpPoint;
glyph tmpglyph;
datastream >> tmpPoint.x >> tmpPoint.y;
tmpglyph.load_data(datastream);
drawing[tmpPoint] = tmpglyph;
}
}
bool ele_drawing::set_data(glyph gl, int posx, int posy)
{
if (posx < 0 || posx >= sizex || posy < 0 || posy >= sizey)
return false;
if (gl.symbol == -1 || gl.symbol == ' ')
drawing.erase( Point(posx, posy) );
else
drawing[ Point(posx, posy) ] = gl;
return true;
}
bool ele_drawing::set_data(nc_color FG, nc_color BG)
{
std::map<Point, glyph>::iterator it;
for (it = drawing.begin(); it != drawing.end(); it++) {
it->second.fg = FG;
if (BG != c_null) // bg defaults to c_null
it->second.bg = BG;
}
return true;
}
bool ele_drawing::translate(long from, long to)
{
std::map<Point, glyph>::iterator it;
bool found = false;
for (it = drawing.begin(); it != drawing.end(); it++) {
if (it->second.symbol == from) {
it->second.symbol = to;
found = true;
}
}
return found;
}
// *** TEXTBOX ELEMENT ***
void ele_textbox::draw(Window *win)
{
std::vector<std::string> broken = break_into_lines(*text, sizex);
win->clear_area(posx, posy, posx + sizex - 1, posy + sizey - 1);
for (int i = 0; i + offset <= broken.size() && i < sizey; i++) {
int ypos, index;
if (v_align == ALIGN_BOTTOM) {
ypos = posy + sizey - 1 - i;
index = broken.size() - 1 - i - offset;
} else { // Default to top-aligned
ypos = posy + i;
index = i + offset;
}
if (index >= 0 && index < broken.size()) {
if (align == ALIGN_RIGHT) {
win->putstr_r(posx, ypos, fg, bg, sizex, broken[index]);
} else if (align == ALIGN_CENTER) {
win->putstr_c(posx, ypos, fg, bg, sizex, broken[index]);
} else {
win->putstr_n(posx, ypos, fg, bg, sizex, broken[index]);
}
}
}
if (selectable)
print_scrollbar(win, posx + sizex - 1, posy, sizey, offset, broken.size(),
selected);
}
std::string ele_textbox::save_data()
{
std::stringstream ret;
ret << element::save_data() << " " << (*text) << " " << STD_DELIM;
return ret.str();
}
void ele_textbox::load_data(std::istream &datastream)
{
element::load_data(datastream);
(*text) = load_to_delim(datastream, STD_DELIM);
}
bool ele_textbox::self_reference()
{
if (owns_data)
return false;
text = new std::string;
owns_data = true;
return true;
}
bool ele_textbox::set_data(std::string data)
{
(*text) = data;
return true;
}
bool ele_textbox::add_data(std::string data)
{
(*text) += data;
return true;
}
bool ele_textbox::set_data(std::vector<std::string> data)
{
(*text) = "";
add_data(data);
return true;
}
bool ele_textbox::add_data(std::vector<std::string> data)
{
for (int i = 0; i < data.size(); i++)
(*text) += data[i] + '\n';
return true;
}
bool ele_textbox::ref_data(std::string *data)
{
if (owns_data)
delete text;
text = data;
owns_data = false;
return true;
}
bool ele_textbox::set_data(int data)
{
std::vector<std::string> broken = break_into_lines(*text, sizex);
if (data <= 0)
offset = 0;
else if (data > broken.size() - 1)
offset = broken.size() - 1;
/*
else if (data > sizey - broken.size())
offset = sizey - broken.size();
*/
else
offset = data;
return true;
}
bool ele_textbox::add_data(int data)
{
return set_data(offset + data);
}
std::vector<std::string> ele_textbox::get_str_list()
{
return break_into_lines(*text, sizex);
}
// *** LIST ELEMENT ***
void ele_list::draw(Window *win)
{
win->clear_area(posx, posy, posx + sizex - 1, posy + sizey - 1);
for (int i = 0; i + offset < list->size() && i < sizey; i++) {
nc_color hilite = (selection == i + offset ? SELECTCOLOR : bg);
int ypos, index;
if (v_align == ALIGN_BOTTOM) {
ypos = posy + sizey - 1 - i;
index = list->size() - 1 - i - offset;
} else { // Default to top-aligned
ypos = posy + i;
index = i + offset;
}
if (!selected)
hilite = bg;
if (align == ALIGN_RIGHT) {
/* If it's selectable, we need an extra space at the end to compensate for the
* scroll bar; otherwise the scroll bar will cover up the last name in the list.
* This is hacky but it's good enough for now.
*/
if (selectable) {
win->putstr_r(posx, ypos, fg, hilite, sizex, (*list)[index] + " ");
} else {
win->putstr_r(posx, ypos, fg, hilite, sizex, (*list)[index]);
}
} else if (align == ALIGN_CENTER) {
win->putstr_c(posx, ypos, fg, hilite, sizex, (*list)[index]);
} else {
win->putstr_n(posx, ypos, fg, hilite, sizex, (*list)[index]);
}
}
if (selectable)
print_scrollbar(win, posx + sizex - 1, posy, sizey, offset, list->size(),
selected);
}
std::string ele_list::save_data()
{
std::stringstream ret;
ret << element::save_data() << " " << list->size() << " ";
for (int i = 0; i < list->size(); i++)
ret << (*list)[i] << " " << STD_DELIM << " ";
return ret.str();
}
void ele_list::load_data(std::istream &datastream)
{
element::load_data(datastream);
int tmpsize;
datastream >> tmpsize;
for (int i = 0; i < tmpsize; i++) {
std::string tmp = load_to_delim(datastream, STD_DELIM);
list->push_back(tmp);
}
}
bool ele_list::self_reference()
{
if (owns_data)
return false;
list = new std::vector<std::string>;
owns_data = true;
return true;
}
bool ele_list::set_data(std::string data)
{
list->clear();
add_data(data);
return true;
}
bool ele_list::add_data(std::string data)
{
list->push_back(data);
return true;
}
bool ele_list::set_data(std::vector<std::string> data)
{
(*list) = data;
selection = 0;
offset = 0;
return true;
}
bool ele_list::add_data(std::vector<std::string> data)
{
for (int i = 0; i < data.size(); i++)
list->push_back(data[i]);
return true;
}
bool ele_list::ref_data(std::vector<std::string> *data)
{
if (owns_data)
delete list;
list = data;
owns_data = false;
return true;
}
bool ele_list::set_data(int data)
{
selection = data;
if (selection < 0)
selection = 0;
if (selection >= list->size())
selection = list->size() - 1;
if (selection < sizey)
offset = 0;
else if (selection >= list->size() - sizey)
offset = list->size() - sizey;
else
offset = selection;
return true;
}
bool ele_list::add_data(int data)
{
selection += data;
if (selection < 0)
selection = 0;
if (selection >= list->size())
selection = list->size() - 1;
while (selection < offset)
offset--;
if (offset + sizey <= selection)
offset = selection - sizey + 1;
return true;
}
int ele_list::get_int()
{
return selection;
}
std::string ele_list::get_str()
{
if (selection < 0 || selection >= list->size()) {
std::string ret;
return ret;
}
return (*list)[selection];
}
// *** TEXT ENTRY ELEMENT ***
void ele_textentry::draw(Window *win)
{
nc_color hilite = (selected ? SELECTCOLOR : bg);
// Ensure we see the end of the word--and a blank space
int start = (selected ? text->size() + 1 - sizex : 0);
if (start < 0)
start = 0;
int length = (selected ? sizex - 1 : sizex);
std::string print = text->substr(start, length);
win->putstr_raw(posx, posy, fg, hilite, print);
for (int x = posx + print.length(); x < posx + sizex; x++)
win->putch(x, posy, bg, hilite, '_');
}
std::string ele_textentry::save_data()
{
std::stringstream ret;
ret << element::save_data() << " " << (*text) << " " << STD_DELIM;
return ret.str();
}
void ele_textentry::load_data(std::istream &datastream)
{
element::load_data(datastream);
(*text) = load_to_delim(datastream, STD_DELIM);
}
bool ele_textentry::self_reference()
{
if (owns_data)
return false;
text = new std::string;
owns_data = true;
return true;
}
bool ele_textentry::set_data(std::string data)
{
(*text) = data;
return true;
}
bool ele_textentry::add_data(std::string data)
{
(*text) += data;
return true;
}
bool ele_textentry::ref_data(std::string *data)
{
if (owns_data)
delete text;
text = data;
owns_data = false;
return true;
}
bool ele_textentry::handle_keypress(long ch)
{
if (is_backspace(ch) && !text->empty()) {
(*text) = text->substr(0, text->length() - 1);
return true;
}
if (ch >= 32 && ch <= 126) { // Printable chars
(*text) += char(ch);
return true;
}
return false;
}
// *** NUMBER ELEMENT ***
void ele_number::draw(Window *win)
{
nc_color hilite = (selected ? SELECTCOLOR : bg);
if (align == ALIGN_RIGHT) {
win->putstr_r(posx, posy, fg, hilite, sizex, "%d", (*value));
} else if (align == ALIGN_CENTER) {
win->putstr_c(posx, posy, fg, hilite, sizex, "%d", (*value));
} else {
win->putstr_n(posx, posy, fg, hilite, sizex, "%d", (*value));
}
}
std::string ele_number::save_data()
{
std::stringstream ret;
ret << element::save_data() << " " << (*value);
return ret.str();
}
void ele_number::load_data(std::istream &datastream)
{
element::load_data(datastream);
datastream >> (*value);
}
bool ele_number::self_reference()
{
if (owns_data)
return false;
value = new int;
(*value) = 0;
owns_data = true;
return true;
}
bool ele_number::set_data(int data)
{
(*value) = data;
return true;
}
bool ele_number::add_data(int data)
{
(*value) += data;
return true;
}
bool ele_number::ref_data(int *data)
{
if (owns_data)
delete value;
value = data;
owns_data = false;
return true;
}
bool ele_number::handle_keypress(long ch)
{
if (ch >= '0' && ch <= '9') {
if (recently_selected) {
(*value) = (ch - '0');
} else {
(*value) *= 10;
(*value) += (ch - '0');
}
return true;
} else if (is_backspace(ch)) {
(*value) /= 10;
return true;
} else if (ch == '-') {
(*value)*= -1;
}
return false;
}
// *** MENU ELEMENT ***
void ele_menu::draw(Window *win)
{
std::string text = title;
if (selection >= 0) {
text = (*list)[selection];
}
if (!selected || !open) {
if (align == ALIGN_RIGHT) {
win->putstr_r(posx + 1, posy, fg, (selected ? SELECTCOLOR : bg),
sizex - 2, text);
} else if (align == ALIGN_CENTER) {
win->putstr_c(posx + 1, posy, fg, (selected ? SELECTCOLOR : bg),
sizex - 2, text);
} else {
win->putstr_n(posx + 1, posy, fg, (selected ? SELECTCOLOR : bg),
sizex - 2, text);
}
return;
}
// The rest is for when it's selected, i.e. open
// Draw outline first
int tmpsizey = list->size() + 2;
if (posy + tmpsizey > win->sizey()) {
tmpsizey = win->sizey() - posy;
}
// Vertical lines
for (int y = posy + 1; y < posy + tmpsizey - 1; y++) {
win->putch(posx, y, fg, bg, LINE_XOXO);
win->putch(posx + sizex - 2, y, fg, bg, LINE_XOXO);
}
// Horizontal lines
for (int x = posx + 1; x < posx + sizex - 1; x++) {
win->putch(x, posy, fg, bg, LINE_OXOX);
win->putch(x, posy + tmpsizey - 1, fg, bg, LINE_OXOX);
}
// Corners
win->putch(posx, posy, fg, bg, LINE_OXXO);
win->putch(posx + sizex - 2, posy, fg, bg, LINE_OOXX);
win->putch(posx, posy + tmpsizey - 1, fg, bg, LINE_XXOO);
win->putch(posx + sizex - 2, posy + tmpsizey - 1, fg, bg, LINE_XOOX);
// Then draw menu items
if (align == ALIGN_RIGHT) {
win->putstr_r(posx + 1, posy, fg, bg, sizex - 2, title);
} else if (align == ALIGN_CENTER) {
win->putstr_c(posx + 1, posy, fg, bg, sizex - 2, title);
} else {
win->putstr_n(posx + 1, posy, fg, bg, sizex - 2, title);
}
for (int i = 0; i < tmpsizey && i < list->size(); i++) {
int n = i, line = i + posy + 1;
if ((*list)[n] == "-") { // Single dash indicates a horizontal line
win->putch(posx, line, fg, bg, LINE_XXXO);
win->putch(posx + sizex - 1, line, fg, bg, LINE_XOXX);
for (int x = posx + 1; x < posx + sizex - 2; x++) {
win->putch(x, line, fg, bg, LINE_OXOX);
}
} else {
// Clear the line using black Xs
for (int x = posx + 1; x < posx + sizex - 2; x++) {
win->putch(x, line, c_black, c_black, 'x');
}
nc_color back = (n == selection ? SELECTCOLOR : bg);
if (align == ALIGN_RIGHT) {
win->putstr_r(posx + 1, line, fg, back, sizex - 2, (*list)[n]);
} else if (align == ALIGN_CENTER) {
win->putstr_c(posx + 1, line, fg, back, sizex - 2, (*list)[n]);
} else {
win->putstr_n(posx + 1, line, fg, back, sizex - 2, (*list)[n]);
}
}
}
}
std::string ele_menu::save_data()
{
std::stringstream ret;
ret << element::save_data() << " " << title << " " << STD_DELIM << " " <<
list->size() << " ";
for (int i = 0; i < list->size(); i++)
ret << (*list)[i] << " " << STD_DELIM << " ";
return ret.str();
}
void ele_menu::load_data(std::istream &datastream)
{
element::load_data(datastream);
title = load_to_delim(datastream, STD_DELIM);
int tmpsize;
datastream >> tmpsize;
for (int i = 0; i < tmpsize; i++) {
std::string tmp = load_to_delim(datastream, STD_DELIM);
list->push_back(tmp);
}
}
bool ele_menu::self_reference()
{
if (owns_data)
return false;
list = new std::vector<std::string>;
owns_data = true;
return true;
}
bool ele_menu::set_data(std::string data)
{
title = data;
return true;
}
bool ele_menu::add_data(std::string data)
{
list->push_back(data);
return true;
}
bool ele_menu::handle_keypress(long ch)
{
if (ch == '\n') {
open = false;
return true;
}
return false;
}
bool ele_menu::set_data(std::vector<std::string> data)
{
(*list) = data;
selection = 0;
open = false;
return true;
}
bool ele_menu::add_data(std::vector<std::string> data)
{
for (int i = 0; i < data.size(); i++)
list->push_back(data[i]);
return true;
}
bool ele_menu::ref_data(std::vector<std::string> *data)
{
if (owns_data)
delete list;
list = data;
owns_data = false;
return true;
}
bool ele_menu::set_data(int data)
{
selection = data;
if (selection < 0)
selection = 0;
if (selection >= list->size())
selection = list->size() - 1;
if (data != -1) {
open = true;
}
return true;
}
bool ele_menu::add_data(int data)
{
return set_data(selection + data);
}
std::string ele_menu::get_str()
{
if (selection < 0 || selection >= list->size()) {
std::string ret;
return ret;
}
return (*list)[selection];
}
int ele_menu::get_int()
{
return selection;
}
void print_scrollbar(Window *win, int posx, int posy, int length, int offset,
int size, bool selected)
{
nc_color barcol = (selected ? SELECTCOLOR : c_ltgray);
int barsize = (length >= size ? -1 : 1 + ((length * length) / size));
if (barsize == -1) {
// Don't print a scroll bar
for (int y = posy; y < posy + length; y++)
win->putch(posx, y, barcol, c_black, LINE_XOXO);
} else {
int barpos = (offset * length) / size;
if (barpos + barsize > length)
barpos = length - barsize;
for (int y = 0; y < length; y++) {
long ch = ((y >= barpos && y < barpos + barsize) ? '#' : LINE_XOXO);
win->putch(posx, posy + y, barcol, c_black, ch);
}
}
}
std::string binding::save_data()
{
std::stringstream ret;
ret << int(act) << " " << target << " " STD_DELIM << " " << a << " " << b;
return ret.str();
}
void binding::load_data(std::istream &datastream)
{
int tmpact;
datastream >> tmpact;
target = load_to_delim(datastream, STD_DELIM);
datastream >> a >> b;
act = action_id(tmpact);
}
interface::interface(std::string N, int X, int Y)
{
active_element = -1;
use_bindings = false;
name = N;
sizex = X;
sizey = Y;
}
interface::~interface()
{
for (int i = 0; i < elements.size(); i++)
delete elements[i];
}
void interface::add_element(element_type type, std::string name, int posx,
int posy, int szx, int szy, bool selectable)
{
if (posx < 0 || posx >= sizex || posy < 0 || posy >= sizey)
return;
if (posx + szx >= sizex)
szx = sizex - posx;
if (posy + szy >= sizey)
szy = sizey - posy;
if (name.find(' ') != std::string::npos)
return;
switch (type) {
case ELE_NULL:
return; // We don't have any reason to actually add these, right?
case ELE_DRAWING: {
ele_drawing *ele = new ele_drawing;
PREP_ELEMENT(ele);
elements.push_back(ele);
} break;
case ELE_TEXTBOX: {
ele_textbox *ele = new ele_textbox;
PREP_ELEMENT(ele);
elements.push_back(ele);
} break;
case ELE_LIST: {
ele_list *ele = new ele_list;
PREP_ELEMENT(ele);
elements.push_back(ele);
} break;
case ELE_TEXTENTRY: {
ele_textentry *ele = new ele_textentry;
PREP_ELEMENT(ele);
elements.push_back(ele);
} break;
case ELE_NUMBER: {
ele_number *ele = new ele_number;
PREP_ELEMENT(ele);
elements.push_back(ele);
} break;
case ELE_MENU: {
ele_menu *ele = new ele_menu;
PREP_ELEMENT(ele);
elements.push_back(ele);
} break;
default:
debugmsg("Unknown element type %d", type);
return;
}
if (active_element == -1)
active_element = 0;
}
bool interface::erase_element(std::string name)
{
for (int i = 0; i < elements.size(); i++) {
if (elements[i]->name == name) {
element *el = elements[i];
elements.erase(elements.begin() + i);
delete el;
if (active_element == i)
active_element = -1;
return true;
}
}
return false;
}
void interface::draw(Window *win)
{
win->clear();
std::vector<element*> draw_last; // Menus need to be layered at the top
for (int i = 0; i < elements.size(); i++) {
if (elements[i]->type() == ELE_MENU && elements[i]->selected)
draw_last.push_back(elements[i]);
else
elements[i]->draw(win);
}
for (int i = 0; i < draw_last.size(); i++)
draw_last[i]->draw(win);
win->refresh();
}
void interface::draw_prototype(Window *win)
{
win->clear();
for (int i = 0; i < elements.size(); i++) {
if (elements[i]->name != "BG") {
int x1 = elements[i]->posx, y1 = elements[i]->posy;
int x2 = x1 + elements[i]->sizex - 1, y2 = y1 + elements[i]->sizey - 1;
// Draw the background color
for (int x = x1; x <= x2; x++) {
for (int y = y1; y <= y2; y++) {
element_type type = elements[i]->type();
win->putch(x, y, c_black,
type == ELE_DRAWING ? c_dkgray : nc_color(2 + type), ' ');
}
}
// Draw the corner/side delineators
if (y1 == y2) {
win->putch(x1, y1, c_white, c_black, LINE_XXXO);
win->putch(x2, y2, c_white, c_black, LINE_XOXX);
} else if (x1 == x2) {
win->putch(x1, y1, c_white, c_black, LINE_OXXX);
win->putch(x2, y2, c_white, c_black, LINE_XXOX);
} else {
win->putch(x1, y1, c_white, c_black, LINE_OXXO);
win->putch(x2, y2, c_white, c_black, LINE_XOOX);
}
if (elements[i]->align == ALIGN_RIGHT) {
win->putstr_r(x1 + 1, y1, (elements[i]->selected ? c_magenta : c_yellow),
c_black, elements[i]->sizex - 2, elements[i]->name);
} else if (elements[i]->align == ALIGN_CENTER) {
win->putstr_c(x1 + 1, y1, (elements[i]->selected ? c_magenta : c_yellow),
c_black, elements[i]->sizex - 2, elements[i]->name);
} else {
win->putstr_n(x1 + 1, y1, (elements[i]->selected ? c_magenta : c_yellow),
c_black, elements[i]->sizex - 2, elements[i]->name);
}
}
if (elements[i]->type() == ELE_DRAWING)
elements[i]->draw(win);
}
}
std::string interface::save_data()
{
std::stringstream ret;
ret << name << " " << STD_DELIM << " " << sizex << " " << sizey << " " <<
elements.size() << " ";
for (int i = 0; i < elements.size(); i++)
ret << elements[i]->type() << " " << elements[i]->save_data() << std::endl;
ret << bindings.size() << " ";
std::map<long, binding>::iterator it;
for (it = bindings.begin(); it != bindings.end(); it++)
ret << it->first << " " << it->second.save_data() << std::endl;
return ret.str();
}
void interface::load_data(std::istream &datastream)
{
name = load_to_delim(datastream, STD_DELIM);
datastream >> sizex >> sizey;
elements.clear();
int tmpcount, tmpbind;
datastream >> tmpcount;
for (int i = 0; i < tmpcount; i++) {
int tmptype;
datastream >> tmptype;
switch ( element_type(tmptype) ) {
case ELE_NULL:
debugmsg("Loaded NULL element!");
break;
case ELE_DRAWING: {
ele_drawing *tmp = new ele_drawing;
tmp->load_data(datastream);
elements.push_back(tmp);
} break;
case ELE_TEXTBOX: {
ele_textbox *tmp = new ele_textbox;
tmp->load_data(datastream);
elements.push_back(tmp);
} break;
case ELE_LIST: {
ele_list *tmp = new ele_list;
tmp->load_data(datastream);
elements.push_back(tmp);