-
Notifications
You must be signed in to change notification settings - Fork 6
/
GUITextViews.cpp
581 lines (438 loc) · 13.6 KB
/
GUITextViews.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
#include "GUITextViews.h"
#include "GUIImageView.h"
#include "GUIApp.h"
#include "GameDisplay.h"
#include "GUIView.h"
#include "GUIImage.h"
#include "GUILetter.h"
#include "Compatibility.h"
#include <iostream>
#include <ctime>
#include <vector>
#include <cctype>
#include <algorithm>
#include TR1_FUNCTIONAL_H
using std::cout; using std::endl;
using std::string;
using std::clock;
using std::vector;
using std::for_each;
using std::min;
using std::tr1::bind;
using namespace std::tr1::placeholders;
const SDL_Color bg_color_c = {255,0,255,0};
const SDL_Color default_color_c = {0,0,0,0};
namespace GUI {
static char SDL_to_a(SDLKey key);
static char symbol_to_upper(char ltr);
TextView::TextView(int w_, int h_,
bool resizeable_down_, bool resizeable_right_)
:View(w_,h_), resizeable_down(resizeable_down_), resizeable_right(resizeable_right_),
w_init(w_), h_init(h_), text_size(min(30, h_-2)), color(default_color_c)
{
set_clear_color(bg_color_c);
update();
}
void TextView::update(){
// resize(200, get_h());
if (!letters.empty()) {
// resize(200, get_h());
// const NewLetter_Disp_Obj &last_letter = letters[letters.size()-1];
// if (resizeable_right && last_letter.get_pos().x + last_letter.get_width() > get_w()) {
// resize(200, get_h());
// }
}
for (size_t i = 0; i < letters.size(); i++){
letters[i].set_pos(pos_at_index(i));
}
if (!letters.empty()) {
const NewLetter_Disp_Obj &last_letter = letters[letters.size()-1];
if (resizeable_right && last_letter.get_pos().x + last_letter.get_width() > get_w()) {
resize(300, get_h());
// resize(last_letter.get_pos().x + last_letter.get_width(), get_h());
// return update(); // start over!
}
if (resizeable_down && last_letter.get_pos().y + last_letter.get_line_height() > get_h()) {
const NewLetter_Disp_Obj &last_letter = letters[letters.size()-1];
resize(get_w(), last_letter.get_pos().y + last_letter.get_line_height());
return update(); // start over!
}
}
fill_with_color(bg_color_c);
for (size_t i = 0; i < letters.size(); i++){
letters[i].drawself(this);
}
mark_changed();
}
void TextView::clear_text(){
letters.clear();
update();
}
void TextView::set_text(const string& text) {
clear_text();
append_text(text);
}
void TextView::append_text(const string& text) {
for (int i = 0; i < (int)text.length(); ++i) {
add_letter_no_redraw(text[i], i);
};
update();
}
string TextView::get_text() const {
string text;
for (letters_ctr_t::const_iterator it = letters.begin(); it != letters.end(); ++it){
text += it->get_ltr();
}
return text;
}
void TextView::set_text_size(int size) {
text_size = size;
set_text(get_text());
}
void TextView::set_text_color(SDL_Color color_) {
color = color_;
set_text(get_text());
}
int TextView::get_text_size() const {
return text_size;
}
SDL_Color TextView::get_text_color() const {
return color;
}
//void TextView::did_resize(int w, int h)
//{
//// if (TextField* tb = dynamic_cast<TextField*>(get_parent())) {
//// tb->resize(w,h);
//// }
//}
void TextView::add_letter(char ltr, int index){
add_letter_no_redraw(ltr, index);
update();
}
void TextView::remove_letter(int index){
remove_letter_no_redraw(index);
update();
}
void TextView::add_letter_no_redraw(char ltr, int index){
try{
letters.insert(letters.begin()+index, NewLetter_Disp_Obj(ltr, text_size, pos_at_index(index), color));
// cout << "letter: '" << letters[index].get_ltr() << "'\n";
// cout << "width: " << letters[index].get_width() << "\n";
}
catch(const Error& e){
cout << e.msg << endl;
}
}
void TextView::remove_letter_no_redraw(int index){
if (index < 0) return;
if (index >= (int)letters.size()) return;
letters.erase(letters.begin()+index);
}
DispPoint TextView::pos_at_index(size_t i){
DispPoint position;
if (i == 0){
position = DispPoint(3,0);
}
else {
// if (i >= letters.size()) throw Error("TextView: Couldn't get position -- index out of range");
const NewLetter_Disp_Obj& letter = letters[i-1];
int width = letter.get_width();
position = DispPoint(letter.get_pos().x + width + 1,
letter.get_pos().y);
if (!resizeable_right && position.x + width >= get_w()){
position.x = 0;
// position.y += NewLetter_Disp_Obj::get_line_height();
position.y += text_size;
}
}
return position;
}
int TextView::index_at_pos(DispPoint pos_){
if (letters.empty()){
return 0;
}
if (pos_.x < letters[0].get_pos().x || pos_.y < letters[0].get_pos().y){
return 0;
}
int i;
for (i = 0; i < (int)letters.size(); i++) {
if ((pos_.x >= letters[i].get_pos().x
&& pos_.x < letters[i].get_pos().x + letters[i].get_width()/2)
&& (pos_.y >= letters[i].get_pos().y
&& pos_.y < letters[i].get_pos().y + NewLetter_Disp_Obj::get_line_height())) {
break;
}
else if (i > 0 && ((pos_.x >= letters[i-1].get_pos().x + letters[i-1].get_width()/2
&& pos_.x <= letters[i-1].get_pos().x + letters[i-1].get_width())
&& (pos_.y >= letters[i-1].get_pos().y
&& pos_.y <= letters[i-1].get_pos().y + NewLetter_Disp_Obj::get_line_height())))
{
break;
}
}
return i;
}
TextView* createTextView(string text, int size, SDL_Color color) {
TextView *result = new TextView(1,1, true, true);
result->set_text(text);
result->set_text_size(size);
result->set_text_color(color);
return result;
}
// ----------- TextField
TextField::TextField(int w_, int h_,
bool resizeable_down_, bool resizeable_right_)
:TextView(w_,h_, resizeable_down_, resizeable_right_),
key_is_held(false), modifiers_held(KMOD_NONE), cursor(new Cursor(this)), flicker(false)
{
attach_subview(cursor, DispPoint(-1,0)); // start it out off screen.
App::get()->repeat_on_timer(bind(&TextField::handle_key_held, this), 0.05);
// App::get()->repeat_on_timer(bind(&TextField::blink_cursor, this), 0.5);
}
void TextField::blink_cursor() {
cursor->display(get_text_size());
if (flicker) {
attach_subview(cursor, cursor->get_rel_pos());
}
else {
remove_subview(cursor);
}
flicker = !flicker;
}
#include "GUIApp.h"
bool TextField::handle_key_down(SDL_keysym key_in) {
// cout << "text box key down: '" << key_in.sym << "'" << endl;
key_is_held = true;
key_held = key_in.sym;
time_key_held.reset();
handle_modifier(key_in.mod);
handle_key();
return true;
}
bool TextField::handle_key_up(SDL_keysym key_in) {
// cout << "text box key up" << endl;
key_is_held = false;
time_key_held.stop();
modifiers_held = KMOD_NONE;
return false;
}
void TextField::handle_key_held() {
if (!key_is_held) return;
if (!(time_key_held.get_time() > 500)) return;
handle_key();
}
void TextField::handle_key() {
SDLKey key = key_held;
switch (key) {
case SDLK_LEFT:
cursor->move_left();
break;
case SDLK_RIGHT:
cursor->move_right();
break;
case SDLK_UP:
cursor->move_up();
break;
case SDLK_DOWN:
cursor->move_down();
break;
case SDLK_BACKSPACE:
remove_letter(cursor->get_index()-1);
cursor->move_left();
break;
case SDLK_DELETE:
remove_letter(cursor->get_index());
break;
default:
handle_alpha_num(SDL_to_a(key));
break;
}
}
void TextField::handle_alpha_num(char ltr){
if (!isprint(ltr)) return;
int index = cursor->get_index();
if (modifiers_held & KMOD_SHIFT) {
if (isalpha(ltr)) ltr = toupper(ltr);
else ltr = symbol_to_upper(ltr);
}
else if (modifiers_held & KMOD_CAPS) {
if (isalpha(ltr)) ltr = toupper(ltr);
}
add_letter(ltr, index);
cursor->move_right();
}
void TextField::handle_modifier(SDLMod mod){
modifiers_held = mod;
}
bool TextField::handle_mouse_down(DispPoint pos_){
if (!rel_point_is_on_me(pos_)) {
lose_focus();
return false;
}
cursor->move_to(index_at_pos(pos_));
capture_focus();
return true;
}
#include <cctype>
static char SDL_to_a(SDLKey key){
if (key >= 0xFF) {
return 0;
}
return static_cast<char>(key);
}
void TextField::lost_focus() {
cout << "LOSTTTTT FOCUSSSSS" << endl;
move_subview(cursor, DispPoint(-100,-100));
update();
}
const SDL_Color cursor_clear_c = {0, 0xff, 0};
TextField::Cursor::Cursor(TextField* tb_ptr)
: View(1,1), position(0,0), index(0),
text_box_ptr(tb_ptr), flicker(true)
{
const SDL_Color black = {0,0,0};
fill_with_color(black);
set_clear_color(cursor_clear_c);
App::get()->repeat_on_timer(bind(&Cursor::display, this,
bind(&TextField::get_text_size, text_box_ptr)), 0.5);
}
void TextField::Cursor::move_right(){
if (index == (int)text_box_ptr->get_letters().size()) return;
move_to(index + 1);
}
void TextField::Cursor::move_left(){
if (index == 0) return;
move_to(index - 1);
}
void TextField::Cursor::move_up(){
DispPoint new_pos = position;
new_pos.y -= NewLetter_Disp_Obj::get_line_height();
int new_index = text_box_ptr->index_at_pos(new_pos);
move_to(new_index);
}
void TextField::Cursor::move_down(){
DispPoint new_pos = position;
new_pos.y += NewLetter_Disp_Obj::get_line_height();
int new_index = text_box_ptr->index_at_pos(new_pos);
move_to(new_index);
}
void TextField::Cursor::move_to(int index_) {
if (index_ < 0 || index_ > (int)text_box_ptr->get_letters().size()) return;
index = index_;
position = text_box_ptr->pos_at_index(index);
text_box_ptr->move_subview(this, position);}
void TextField::Cursor::display(int text_size) {
flicker = !flicker;
const SDL_Color colors[2] = {{0,0,0}, cursor_clear_c};
resize(1, text_size+2);
SDL_Color color;
// if (flicker) {
color = colors[0];
// }
// else {
// color = colors[1];
// }
fill_with_color(color); // bool will be 0 or 1 index
// if (!flicker) {
// text_box_ptr->remove_subview(this);
// }
// if (!flicker) {
// text_box_ptr->attach_subview(this, get_rel_pos());
// }
}
TextBox::TextBox(int w_, int h_)
:View(w_,h_), field(new TextField(w_-4,h_-4))
{
const SDL_Color clear = {0xff, 0, 0xff};
fill_with_color(clear);
set_clear_color(clear);
const SDL_Color field_bg_color = {0xff,0xff,0xff};
fill_with_color(field_bg_color);
// attach_subview(field, DispPoint(2,2));
// draw_onto_self(GUIImage("GUIImages/corner0.bmp"), DispPoint(0, 0));
// draw_onto_self(GUIImage("GUIImages/corner1.bmp"), DispPoint(w_-5, 0));
// draw_onto_self(GUIImage("GUIImages/corner2.bmp"), DispPoint(w_-5, h_-5));
// draw_onto_self(GUIImage("GUIImages/corner3.bmp"), DispPoint(0, h_-5));
// *** OUT OF DATE: Mask works, just need to save it and expose color changing! :)
// Should ideally use a mask layer instead so that bg color could be changed,
// but right now the problem is it receives the mouse clicks instead of field.
// *** Literally, I just had to switch the order so the field comes after..
const SDL_Color mask_clear = {0, 0xff, 0xff};
View *mask = new View(w_,h_);
mask->fill_with_color(mask_clear);
mask->set_clear_color(mask_clear);
mask->draw_onto_self(GUIImage("GUIImages/corner0.bmp"), DispPoint(0, 0));
mask->draw_onto_self(GUIImage("GUIImages/corner1.bmp"), DispPoint(w_-5, 0));
mask->draw_onto_self(GUIImage("GUIImages/corner2.bmp"), DispPoint(w_-5, h_-5));
mask->draw_onto_self(GUIImage("GUIImages/corner3.bmp"), DispPoint(0, h_-5));
attach_subview(mask, DispPoint(0,0));
// .. jokes.
attach_subview(field, DispPoint(2,2));
}
int NewLetter_Disp_Obj::line_height = 0;
NewLetter_Disp_Obj::NewLetter_Disp_Obj(char ltr, int size, DispPoint pos, SDL_Color color)
:position(pos), ltr(ltr), height(size)
{
letter = GUILetter::get_letter(ltr, size, color);
if (!letter) {
string error_msg = string("Couldn't load image for letter")
+ ltr + string(": ")+ SDL_GetError() +"\n";
throw Error(error_msg.c_str());
}
line_height = letter->get_height()+2; // for a line spacing
}
int NewLetter_Disp_Obj::get_width() const{
return letter->get_width();
}
void NewLetter_Disp_Obj::drawself(View *dest) const{
letter->drawself(dest, position.x, position.y); //+ height-letter->get_height());
}
static char symbol_to_upper(char ltr) {
switch (ltr) {
case '`':
return '~';
case '1':
return '!';
case '2':
return '@';
case '3':
return '#';
case '4':
return '$';
case '5':
return '%';
case '6':
return '^';
case '7':
return '&';
case '8':
return '*';
case '9':
return '(';
case '0':
return ')';
case '-':
return '_';
case '=':
return '+';
case '[':
return '{';
case ']':
return '}';
case '\\':
return '|';
case ';':
return ':';
case '\'':
return '"';
case ',':
return '<';
case '.':
return '>';
case '/':
return '?';
default:
return ltr;
}
}
} // namespace GUI