[go: up one dir, main page]

Menu

[r103]: / lpi_gui_text.cpp  Maximize  Restore  History

Download this file

1225 lines (1004 with data), 31.9 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
/*
Copyright (c) 2005-2008 Lode Vandevenne
All rights reserved.
This file is part of Lode's Programming Interface.
Lode's Programming Interface is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Lode's Programming Interface is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Lode's Programming Interface. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lpi_gui_text.h"
#include "lodepng.h"
#include "lpi_gl.h"
#include "lpi_draw2dgl.h"
#include "lpi_draw2d.h"
#include "lpi_file.h"
#include "lpi_base64.h"
#include "lpi_xml.h"
#include <SDL/SDL.h> //this is for the key codes like SDLK_ENTER
#include <iostream>
#include <cstdlib>
namespace lpi
{
namespace gui
{
////////////////////////////////////////////////////////////////////////////////
//GUIINPUTLINE//////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
The InputLine Class Functions
InputLine allows the user to enter a single line of text.
How to use it:
You can give text color styles with color1, color2, style
When the draw() function is called, it is drawn and handled, i.e. user unicode key presses are detected, ...
It is only handled if "active" is true, otherwise it's only drawn and it ignores user input
Fetch the text with getText or by simply using inputLine.text
Check if the user pressed enter, clicked on it with mouse, .... with the check() function
*/
//the default constructor
InputLine::InputLine()
{
this->x0 = 0;
this->y0 = 0;
this->l = 0;
this->markup = TS_White;
this->type = 0;
this->allowedChars = 0;
this->title = "";
this->control_active = 0;
this->text = "";
cursor = 0;
entered = 0;
}
void InputLine::make(int x, int y, unsigned long l, const Markup& markup, int type, int allowedChars, const std::string& title, const Markup& titleMarkup, const ColorRGB& cursorColor)
{
this->x0 = x;
this->y0 = y;
this->l = l;
this->markup = markup;
this->titleMarkup = titleMarkup;
this->cursorColor = cursorColor;
this->type = type;
this->allowedChars = allowedChars;
this->title = title;
this->text = "";
cursor = 0;
entered = 0;
if(type == 2) //if the type is "integer"
{
allowedChars |= 3; //set "disable text" and "disable special symbols" to true
}
this->setSizeY( markup.getHeight()); //font height
this->setSizeX((title.length() + l) * markup.getWidth());
this->active = 1;
this->visible = 1;
this->present = 1;
}
//this function draws the line, and makes sure it gets handled too
void InputLine::drawWidget(IGUIDrawer& drawer) const
{
print(title, x0, y0, titleMarkup);
int inputX = x0 + title.length() * markup.getWidth()/*(inputPos / h) % w*/;
int inputY = y0/*inputPos % h*/;
if(type == 0) print(text, inputX, inputY, markup);
else if(type == 1) //password
{
unsigned long p = 0;
while(p < text.length() && p < l)
{
drawLetter('*', inputX + p * 8, inputY, markup);
p++;
}
}
else if(type == 2) print(getInteger(), inputX, inputY, markup);
//draw the cursor if active
if(control_active && (int(draw_time * 2.0) % 2 == 0 || mouseDown(drawer.getInput())))
{
int cursorXDraw = inputX + cursor * markup.getWidth();
drawLine(cursorXDraw, inputY, cursorXDraw, inputY + markup.getHeight(), cursorColor);
}
}
//return values of check():
/*
0: the text string is empty, and the rest of the things below aren't true either
&1: the text string is not empty
&2: you pressed ENTER (to enter the text string!)
normally you'll want to check if the return value is 3: then you entered a non-empty string
*/
int InputLine::check() //both check if you pressed enter, and also check letter keys pressed, backspace, etc...
{
int returnValue = 0;
if(text.length() > 0) returnValue |= 1;
if(entered) returnValue |= 2;
entered = 0;
return returnValue;
}
//returns true if the user entered text, i.e. if there's text in it, and the user presses enter, and it's active
int InputLine::enter()
{
if((check() & 2) && (check() & 1) && active) return 1;
else return 0;
}
void InputLine::handleWidget(const IGUIInput& input) //both check if you pressed enter, and also check letter keys pressed, backspace, etc...
{
autoActivate(input, auto_activate_mouse_state, control_active);
if(!control_active) return;
draw_time = input.getSeconds();
if(cursor >= text.length()) cursor = text.length();
int ascii = input.unicodeKey(allowedChars, 0.5, 0.025);
if(ascii)
{
switch(ascii)
{
case 8: //backspace
if(cursor > 0)
{
cursor--;
text.erase(cursor, 1);
}
break;
case 13: //enter
entered = 1;
break;
case 127: //delete
if(cursor <= text.size())
{
text.erase(cursor, 1);
}
break;
//a few certainly not meant to be printed ones
case 0:
break;
case 10: break;
default:
if(text.length() < l) text.insert(cursor, 1, ascii);
if(text.length() < l || cursor == l - 1) cursor++;
break;
}
}
if(input.keyPressed(SDLK_HOME)) cursor = 0;
if(input.keyPressed(SDLK_END))
{
unsigned long pos = 0;
while(text[pos] != 0 && pos < text.length()) pos++;
cursor = pos;
}
if(input.keyPressedTime(SDLK_LEFT, 0.5, 0.025)) if(cursor > 0) cursor--;
if(input.keyPressedTime(SDLK_RIGHT, 0.5, 0.025)) if(cursor < text.length()) cursor++;
if(mouseDown(input))
{
int relMouse = input.mouseX() - x0;
relMouse -= title.length() * markup.getWidth();
if(relMouse / markup.getWidth() < int(text.length())) cursor = relMouse / markup.getWidth();
else cursor = text.length();
}
}
void InputLine::clear()
{
setText("");
cursor = 0;
}
void InputLine::setText(const std::string& i_text)
{
this->text = i_text;
}
const std::string& InputLine::getText() const
{
return text;
}
void InputLine::setTitle(const std::string& i_title)
{
int size_diff = i_title.size() - title.size();
this->title = i_title;
growX1(size_diff * titleMarkup.getWidth());
}
const std::string& InputLine::getTitle() const
{
return title;
}
int InputLine::getInteger() const
{
return std::atoi(text.c_str());
}
////////////////////////////////////////////////////////////////////////////////
//MultiLineText class
////////////////////////////////////////////////////////////////////////////////
MultiLineText::MultiLineText()
{
this->text = "";
resetLines();
char10 = 1;
useFormatting = 1;
markup = TS_W;
}
void MultiLineText::resetLines()
{
line.clear(); //reset lines
line.push_back(0); //character 0 is always a new line since it's the beginning of the text
}
void MultiLineText::generateLineMarkup()
{
lineMarkup.clear();
lineMarkup.push_back(markup);
Markup markup_ = markup;
for(unsigned long i = 0; i < getNumLines(); i++)
{
std::string s = getLine(i);
getFormattedMarkup(s, markup_, markup);
lineMarkup.push_back(markup_);
}
}
//from now on, this will also split words that are too long into multiple parts
void MultiLineText::splitWords(int width)
{
resetLines();
unsigned long charsPerLine = width / markup.getWidth();
unsigned long pos = 0; //current position in the string
unsigned long cline = 1; //current line
unsigned long linepos = 0; //*printable* character position on current line, becomes 0 at every "newline"
while(pos < text.length())
{
//get length of next word
unsigned long wordpos = pos;
unsigned long wordlength = 0;
unsigned long steplength = 0; //word length + length of possible format symbols
bool formattedEnd = false;
while(!formattedEnd && !(text[wordpos] == ' ' || text[wordpos] == 0 || (char10 && (text[wordpos] == 10 || text[wordpos] == 13)) || wordlength >= charsPerLine))
{
if(!useFormatting || text[wordpos] != '#')
{
wordpos++;
wordlength++;
steplength++;
}
else
{
wordpos++;
steplength++;
if(wordpos < text.size())
{
char c = text[wordpos];
switch(c)
{
case 'n':
formattedEnd = true; //it breaks out of this loop, and the next part of the function will then detect the 'n' and treat it as newline
break;
default:
wordpos += getFormattedTextAttrSize(c) + 1;
wordlength += getFormattedTextSymbolPrintSize(c);
steplength += getFormattedTextAttrSize(c) + 1;
break;
}
}
}
}
//for now, words longer than width aren't split in two parts
//see if word can be drawn on current line
if(linepos + wordlength > charsPerLine)
{
line.push_back(pos); //the word is split by inserting newline in front of the word
cline++;
linepos = 0; //you're at a new line, so you're at the first char of the new line again
}
//pos pointer now goes to behind the following word
pos += steplength;
linepos += wordlength; //since you go to behind the word drawn on the current line
//now check what is there behind the word: is it a newline? a space? or a null-termination char?
switch(text[pos])
{
case ' ': //space
pos++;
linepos++;
break;
case 10: //newline
if(char10)
{
line.push_back(pos);
cline++;
linepos = 0;
pos++;
}
break;
case 13: //ms windows newline, ignore
if(char10)
{
pos++;
//linepos++;
}
break;
case 'n': //it's the newline, there's a # in front of it for sure because that was already checked higher
if(useFormatting)
{
line.push_back(pos - 1);
cline++;
linepos = 0;
pos++;
}
break;
/*case 0: //null termination char
break;*/
default: //not supposed to happen
//dprint("WTF? Error in MultiLineText::splitWords");
break;
}
}
line.push_back(text.length());
if(useFormatting) generateLineMarkup();
}
void MultiLineText::splitLetters(int width)
{
resetLines();
unsigned long charsPerLine = width / markup.getWidth();
unsigned long pos = 0; //current position in the string
unsigned long cline = 1; //current line
unsigned long linepos = 0; //character position on current line, becomes 0 at every "newline"
while(/*text[pos] != 0*/pos < text.length())
{
switch(text[pos])
{
/*case 0: //termination char
break;*/
case 10: //newline char
if(char10)
{
line[cline] = pos;
cline++;
linepos = 0;
pos++;
}
break;
default:
if(linepos >= charsPerLine)
{
line.push_back(pos);
cline++;
linepos = 0;
}
pos++;
linepos++;
break;
}
}
line.push_back(text.length());
if(useFormatting) generateLineMarkup();
}
void MultiLineText::splitChar10()
{
resetLines();
unsigned long pos = 0; //current position in the string
unsigned long cline = 1; //current line
while(/*text[pos] != 0*/pos < text.length())
{
switch(text[pos])
{
/*case 0: //termination char
break;*/
case 10: //newline char
line.push_back(pos);
cline++;
pos++;
break;
default:
pos++;
break;
}
}
line.push_back(text.length());
if(useFormatting) generateLineMarkup();
}
void MultiLineText::setText(const std::string& text)
{
this->text = text;
}
void MultiLineText::addText(const std::string& text)
{
this->text += text;
}
void MultiLineText::draw(int x, int y, unsigned long startLine, unsigned long endLine) const
{
Markup markup_ = markup;
if(startLine == endLine) return;
unsigned long cline = startLine;
while(cline < endLine)
{
int printy = y + markup.getHeight() * (cline - startLine); //y coordinate to draw the text
std::string sub = getLine(cline); //string containing the current line
if(useFormatting)
{
if(cline < lineMarkup.size()) markup_ = lineMarkup[cline];
printFormattedM(sub, x, printy, markup_, markup);
}
else print(sub, x, printy, markup);
cline++;
}
}
void MultiLineText::draw(int x, int y) const
{
draw(x, y, 0, getNumLines());
}
unsigned long MultiLineText::getNumLines() const
{
if(text.length() == 0) return 0; //no text!
return line.size() - 1; //the size of the std::vector is also the number of lines, minus one because the last value denotes the position of the final character of the text, which is no new line
}
std::string MultiLineText::getLine(unsigned long i) const
{
if(i >= getNumLines()/* || i < 0*/) return "";
int length = line[i + 1] - line[i];
int start = line[i]; //the first character of the substring
if(char10 && text[line[i]] == 10) //do NOT draw newline chars if it's used as newline (i.e. when char10 is true)
{
start++;
length--;
}
if(length < 0) length = 0;
//std::string sub = text.substr(start, length);
std::string sub;
for(int j = start; j < start + length; j++)
{
if(!(char10 && text[j] == 13)) sub += text[j];
}
return sub;
}
//returns line and column of character at pos (handy for cursors)
void MultiLineText::cursorAtCharPos(unsigned long pos, unsigned long& cline, unsigned long& column) const
{
cline = 0;
column = 0;
int extraPos = 0;
if(text[pos] == 10) {pos--; extraPos++;}
if(text.length() == 0)
{
cline = 0;
column = 0;
}
else if(getNumLines() == 1)
{
cline = 0;
column = pos;
}
else if(pos < line[1])
{
cline = 0;
column = pos;
}
else
{
if(pos >= text.length())
{
extraPos = pos - text.length() + 1;
pos = text.length() - 1;
}
while(line[cline] <= pos)
{
cline++;
}
cline--;
column = pos - line[cline];
if(text[line[cline]] == 10) {column--; } //lines start with a 10 char if you pressed enter but this char isn't drawn. If the line does not start with a 10 char, column must be done + 1
}
column += extraPos;
}
//returns position of letter at given line and column
int MultiLineText::charAtCursorPos(unsigned long cline, unsigned long column) const
{
if(getNumLines() <= cline) return text.length();
unsigned long lineLength;
if(cline == getNumLines() - 1) lineLength = text.length() - line[cline];
else if(cline == 0)
{
lineLength = line[1];
if(text[line[1]] != 10) lineLength--;
}
else lineLength = line[cline + 1] - line[cline] - 1;
unsigned long pos = line[cline];
if(column > lineLength) column = lineLength;
if(text[line[cline]] == 10) pos += 1;
pos += column;
if(pos > text.length()) pos = text.length();
/*if(cline == 0)
{
pos = column;
}*/
return pos;
}
////////////////////////////////////////////////////////////////////////////////
//STRINGSTACK///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
The String Stack is a rotating stack of strings, that is very helpful for the
Console.
It's actually not a real stack: you don't have to pop() to get values out of it,
you only have to push values into it, it rotates, and you can access any value.
You can access values relative to the newest, or relative to the oldest value.
The StringStack has a finite number of positions, when it's full, it'll overwrite
the oldest value. It can however use indexes as if the newely added text has always
the highest index when the stack is full.
First it must allocate memory for the wanted number of strings, and it must
destroy it again when it's no longer used.
num = max number of strings
*/
StringStack::StringStack()
{
this->num = 0;
this->oldest = 0;
this->next = 0;
this->numFilled = 0;
}
void StringStack::make(int num)
{
if(num < 1) num = 1;
this->num = num;
this->oldest = 0;
this->next = 0;
this->numFilled = 0;
for(int i = 0; i < num; i++) text.push_back("");
}
void StringStack::push(const std::string& text)
{
this->text[next] = text;
if(next == oldest && numFilled > 0) oldest = increase(oldest);
next = increase(next);
if(numFilled < num) numFilled++;
}
//returns the text that is position positions away from the oldest text
const std::string& StringStack::getOldest(int offset)
{
int pointer = increase(oldest, offset);
return text[pointer];
}
const std::string& StringStack::getNewest(int offset)
{
int pointer = decrease(next);
pointer = decrease(pointer, offset);
return text[pointer];
}
//returns how full the memory is
int StringStack::getNumText() const
{
return numFilled;
}
//increase the position of a pointer (like oldest or next), if it reaches the edge it goes back to 0
int StringStack::increase(int pointer, int amount)
{
pointer += amount;
if(pointer >= num) pointer %= num;
return pointer;
}
int StringStack::decrease(int pointer, int amount)
{
pointer -= amount;
while(pointer < 0) pointer += num;
if(pointer >= num) pointer %= num;
return pointer;
}
////////////////////////////////////////////////////////////////////////////////
//GUICONSOLE////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
Console is a text window that contains different texts and can display
some of them like a console.
It's supposed to be used together with a scrollbar to be able to scroll it.
*/
Console::Console()
{
this->num = 0;
this->active = 0;
this->visible = 0;
this->scroll = 0;
}
void Console::make(int x, int y, int sizex, int sizey, int num, const Markup& markup)
{
this->x0 = x;
this->y0 = y;
this->setSizeX(sizex);
this->setSizeY(sizey);
this->num = num;
this->markup = markup;
this->totallyEnable();
this->scroll = 0;
this->stack.make(num);
calcNumLines();
}
void Console::push(const std::string& text/*, int length*/)
{
stack.push(text);
calcNumLines();
}
void Console::calcNumLines()
{
int line = 0;
MultiLineText t;
t.markup = markup;
for(int i = 0; i < num; i++)
{
t.setText(stack.getOldest(i));
t.splitWords(getSizeX());
line += t.getNumLines();
}
numLines = line;
}
int Console::getNumLines() const
{
return numLines;
}
int Console::getNumMessages() const
{
return stack.getNumText();
}
int Console::getVisibleLines(const Markup& markup)
{
return getSizeY() / markup.getHeight();
}
void Console::drawWidget(IGUIDrawer& /*drawer*/) const
{
int line = 0;
int message = 0;
int maxScreenLines = getSizeY() / markup.getHeight();
int messageLines = 0;
int messageStartLine = 0;
int messageEndLine = 0;
int messageStartLineScreen = 0;
int messageEndLineScreen = 0;
int drawMessageStartLine = -1;
int drawMessageEndLine = -1;
int yDraw = 0;
MultiLineText t;
t.markup = markup;
while(line - scroll < maxScreenLines && message < stack.getNumText())
{
messageStartLine = line; //first line of the message in all messages
t.setText(stack.getOldest(message));
t.splitWords(getSizeX());
messageLines = t.getNumLines();
//messageLines = calcTextLines(text[message], sizex, markup); //number of lines of the message
messageEndLine = messageStartLine + messageLines - 1; //last line of the message
messageStartLineScreen = messageStartLine - scroll;
messageEndLineScreen = messageEndLine - scroll;
line += messageLines;
if(line >= scroll)
{
yDraw = y0 + (messageStartLine - scroll) * markup.getHeight();
if(messageStartLineScreen < 0)
{
drawMessageStartLine = -messageStartLineScreen;
yDraw = y0;
}
else drawMessageStartLine = -1;
if(messageEndLineScreen >= maxScreenLines) drawMessageEndLine = messageLines - (messageEndLineScreen - maxScreenLines + 1);
else drawMessageEndLine = -1;
t.draw(x0, yDraw, drawMessageStartLine, drawMessageEndLine);
}
message++;
}
}
////////////////////////////////////////////////////////////////////////////////
//GUITEXTAREA///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
hmm I forgot what a guitextarea is for...
*/
TextArea::TextArea()
{
this->active = 0;
this->visible = 0;
this->scroll = 0;
this->scrollEnabled = 0;
}
void TextArea::make(int x, int y, int sizex, int sizey, const std::string& text, const Markup& markup)
{
this->x0 = x;
this->y0 = y;
this->setSizeX(sizex);
this->setSizeY(sizey);
this->markup = markup;
this->text.markup = markup;
this->text.setText(text);
this->text.splitWords(getSizeX());
this->totallyEnable();
this->scroll = 0;
}
void TextArea::setText(const std::string& text, const Markup& markup)
{
this->markup = markup;
this->text.markup = markup;
this->text.setText(text);
int width = getSizeX();
if(scrollEnabled) width -= scrollbar.getSizeX();
this->text.splitWords(width);
this->scroll = 0;
if(scrollEnabled) setScrollbarSize();
}
int TextArea::getNumLines() const
{
return text.getNumLines();
}
void TextArea::resizeWidget()
{
this->text.splitWords(getSizeX());
}
Element* TextArea::getAutoSubElement(unsigned long i)
{
if(i == 0) return &scrollbar;
else return 0;
}
void TextArea::handleWidget(const IGUIInput& input)
{
if(scrollEnabled)
{
scrollbar.handle(input);
scroll = int(scrollbar.scrollPos);
}
}
int TextArea::getVisibleLines(const Markup& markup) const
{
return getSizeY() / markup.getHeight();
}
void TextArea::drawWidget(IGUIDrawer& drawer) const
{
int visible = getVisibleLines(markup);
//scroll is the startline, scroll + visible is the end line
int startLine = scroll;
int endLine = scroll + visible;
text.draw(x0, y0, startLine, endLine);
if(scrollEnabled) scrollbar.draw(drawer);
}
void TextArea::setScrollbarSize()
{
int size = text.getNumLines() - (getSizeY() / markup.getHeight());
if(size < 0) size = 0;
scrollbar.scrollSize = size;
}
void TextArea::addScrollbar()
{
scrollEnabled = true;
scrollbar.makeVertical(x1 - 16, 0, getSizeY());
this->text.splitWords(getSizeX() - scrollbar.getSizeX());
setScrollbarSize();
}
////////////////////////////////////////////////////////////////////////////////
//GUIINPUTBOX///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
The InputBox Class Functions
InputBox allows the user to enter a multi line text.
*/
InputBox::InputBox()
{
this->visible = 0;
this->active = 0;
}
void InputBox::make(int x, int y, int sizex, int sizey, int maxLines, int border
, const Markup& markup
//, BackPanel panel
, const ColorRGB& cursorColor)
{
this->x0 = x;
this->y0 = y;
this->setSizeX(sizex);
this->setSizeY(sizey);
this->maxLines = maxLines;
this->markup = markup;
this->multiText.markup = markup;
this->panel = panel;
this->cursorColor = cursorColor;
this->active = 1;
this->visible = 1;
this->present = 1;
this->cursor = 0;
this->control_active = 0;
this->border = border;
init();
}
void InputBox::makeScrollbar(const GuiSet* set)
{
bar.makeVertical(x1 - set->arrowN->getU(), y0, getSizeY(),
100, 0, 0, 1,
set, 1);
}
void InputBox::init()
{
bar.makeVertical(x1 - builtInGuiSet.arrowN->getU(), y0, getSizeY(), 100, 0, 0, 1);
bar.scrollSize = 1;
bar.scrollPos = 0;
}
int InputBox::getLeftText() const
{
return border;
}
int InputBox::getRightText() const
{
return getSizeX() - border - bar.getSizeX();
}
int InputBox::getTopText() const
{
return border;
}
int InputBox::getBottomText() const
{
return getSizeY() - border;
}
int InputBox::getTextAreaHeight() const
{
return getBottomText() - getTopText();
}
int InputBox::getTextAreaWidth() const
{
return getRightText() - getLeftText();
}
int InputBox::getLinesVisible() const
{
return getTextAreaHeight() / markup.getHeight();
}
void InputBox::drawWidget(IGUIDrawer& drawer) const
{
panel.draw(x0, y0, getSizeX(), getSizeY());
bar.draw(drawer);
//draw the cursor if active
if(control_active && int(draw_time * 2.0) % 2 == 0)
{
unsigned long cursorLine;
unsigned long cursorColumn;
multiText.cursorAtCharPos(cursor, cursorLine, cursorColumn);
int cursorXDraw = x0 + getLeftText() + cursorColumn * markup.getWidth();
int cursorYDraw = y0 + getTopText() + (cursorLine - firstVisibleLine) * markup.getHeight();
if(cursorYDraw >= y0 + getTopText() && cursorYDraw < y0 + getBottomText() - markup.getHeight()) drawLine(cursorXDraw, cursorYDraw, cursorXDraw, cursorYDraw + markup.getHeight(), cursorColor);
}
multiText.draw(x0 + getLeftText(), y0 + getTopText(), firstVisibleLine, firstVisibleLine + getLinesVisible());
}
void InputBox::handleWidget(const IGUIInput& input)
{
autoActivate(input, auto_activate_mouse_state, control_active);
if(!control_active) return;
draw_time = input.getSeconds();
bool scrollerMayJump = 0;
if(input.keyPressed(SDLK_RIGHT)) if(cursor < text.length()) cursor++, scrollerMayJump = 1;
if(input.keyPressed(SDLK_LEFT)) if(cursor > 0) cursor--, scrollerMayJump = 1;
if(input.keyPressed(SDLK_UP))
{
unsigned long cursorLine;
unsigned long cursorColumn;
multiText.cursorAtCharPos(cursor, cursorLine, cursorColumn);
if(cursorLine > 0) cursor = multiText.charAtCursorPos(cursorLine - 1, cursorColumn);
scrollerMayJump = 1;
}
if(input.keyPressed(SDLK_DOWN))
{
unsigned long cursorLine;
unsigned long cursorColumn;
multiText.cursorAtCharPos(cursor, cursorLine, cursorColumn);
cursor = multiText.charAtCursorPos(cursorLine + 1, cursorColumn);
scrollerMayJump = 1;
}
if(input.keyPressed(SDLK_HOME))
{
if(input.keyDown(SDLK_LCTRL)) cursor = 0;
else
{
unsigned long cursorLine;
unsigned long cursorColumn;
multiText.cursorAtCharPos(cursor, cursorLine, cursorColumn);
cursor = multiText.charAtCursorPos(cursorLine, 0);
}
scrollerMayJump = 1;
}
if(input.keyPressed(SDLK_END))
{
if(input.keyDown(SDLK_LCTRL)) cursor = text.length();
else
{
unsigned long cursorLine;
unsigned long cursorColumn;
multiText.cursorAtCharPos(cursor, cursorLine, cursorColumn);
cursor = multiText.charAtCursorPos(cursorLine, int(getSizeX() / markup.getWidth()));
}
scrollerMayJump = 1;
}
int ascii = input.unicodeKey(0, 0.5, 0.025);
if(ascii)
{
switch(ascii)
{
case 8: //backspace
if(cursor > 0)
{
cursor--;
text.erase(cursor, 1);
}
break;
//a few certainly not meant to be printed ones
case 0:
break;
case 10:
break;
case 13:
text.insert(cursor, 1, 10);
cursor++;
break;
default:
text.insert(cursor, 1, ascii);
cursor++;
break;
}
multiText.text = text;
multiText.splitWords(getTextAreaWidth());
scrollerMayJump = 1;
}
if(mouseDownHere(input) && !bar.mouseOver(input))
{
unsigned long mouseColumn = (mouseGetRelPosX(input) - getLeftText()) / markup.getWidth();
unsigned long mouseLine = (mouseGetRelPosY(input) - getTopText()) / markup.getHeight() + firstVisibleLine;
cursor = multiText.charAtCursorPos(mouseLine, mouseColumn);
}
int scrollSize = multiText.getNumLines() - getLinesVisible();
if(scrollSize < 1) scrollSize = 1;
bar.scrollSize = scrollSize;
if(mouseScrollUp(input) && !bar.mouseOver(input))
{
bar.scroll(input, -2);
}
if(mouseScrollDown(input) && !bar.mouseOver(input))
{
bar.scroll(input, 2);
}
bar.handle(input);
firstVisibleLine = int(bar.scrollPos);
if(scrollerMayJump)
{
unsigned long cursorLine;
unsigned long cursorColumn;
multiText.cursorAtCharPos(cursor, cursorLine, cursorColumn);
//int lineDifference = multiText.getNumLines() - getLinesVisible();
//if(lineDifference < 1) lineDifference = 1;
int cursorRelative = cursorLine - firstVisibleLine; //relative to the first visible line
if(cursorRelative >= getLinesVisible()) bar.scrollPos = cursorLine - getLinesVisible() + 1;
if(cursorRelative < 0) bar.scrollPos = cursorLine;
//if(cursorRelative < 0) bar.scrollPos = cursorRelative + 1;
//if((cursorRelative + 1) * markup.getHeight()
//if(-getTopText() + (cursorRelative + 1) * markup.getHeight() >= 0) bar.scrollPos = cursorRelative + 1;
//if(getBottomText() + cursorRelative * markup.getHeight() < (getBottomText() - getTopText())) bar.scrollPos = cursorLine;
}
}
Element* InputBox::getAutoSubElement(unsigned long i)
{
if(i == 0) return &bar;
else return 0;
}
////////////////////////////////////////////////////////////////////////////////
//GUIFORMATTEDTEXT//////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
FormattedText is just some formatted text as child of Element
*/
FormattedText::FormattedText()
{
this->visible = 0;
this->active = 0;
}
void FormattedText::make(int x, int y, const std::string& text, const Markup& markup)
{
this->x0 = x;
this->y0 = y;
this->setSizeX(text.length() * markup.getWidth());
this->setSizeY(markup.getHeight());
this->text = text;
this->markup = markup;
this->totallyEnable();
}
void FormattedText::drawWidget(IGUIDrawer& /*drawer*/) const
{
printFormatted(text, x0, y0, markup);
}
void FormattedText::setText(const std::string& text)
{
this->text = text;
}
} //namespace gui
} //namespace lpi
////////////////////////////////////////////////////////////////////////////////