-
Notifications
You must be signed in to change notification settings - Fork 6
/
GUITextViews.h
201 lines (135 loc) · 4.15 KB
/
GUITextViews.h
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
#ifndef GUI_TEXT_VIEWS_H
#define GUI_TEXT_VIEWS_H
#include "GUIView.h"
#include "GUIImageView.h"
#include "GUIUtility.h"
#include "Ptr_config.h"
#include "GUITimer.h"
#include "GUIColors.h"
#include <list>
#include <vector>
#include <map>
class GUILetter;
namespace GUI {
class NewLetter_Disp_Obj;
// A View that displays text atop a clear background.
class TextView : public GUI::View {
public:
TextView(int w_ = 200, int h_ = 200,
bool resizeable_down = false, bool resizable_right = false);
// Text functions
virtual void clear_text();
void set_text(const std::string& text);
void append_text(const std::string& text);
std::string get_text() const;
// Display features
void set_text_size(int size);
void set_text_color(SDL_Color color);
int get_text_size() const;
SDL_Color get_text_color() const;
/// @todo
// Justification
// enum Justification_e { LEFT, MIDDLE, RIGHT };
//
// void set_justification(Justification_e just);
protected:
typedef std::vector<NewLetter_Disp_Obj> letters_ctr_t;
const letters_ctr_t& get_letters() const
{ return letters; }
virtual void add_letter(char ltr, int index);
virtual void remove_letter(int index);
void add_letter_no_redraw(char ltr, int index);
void remove_letter_no_redraw(int index);
DispPoint pos_at_index(size_t i);
int index_at_pos(DispPoint pos_);
virtual void update();
private:
bool resizeable_down, resizeable_right;
int w_init, h_init;
letters_ctr_t letters;
int text_size;
SDL_Color color;
// Justification_e justification;
};
TextView* createTextView(std::string text, int size = 14,
SDL_Color color = black_color_c);
// A TextView that allows for text entry (including mouse motion, deletion, etc.)
class TextField : public TextView {
public:
TextField(int w_ = 200, int h_ = 200,
bool resizeable_down = false, bool resizable_right = false);
protected:
// remove cursor when lose focus.
virtual void lost_focus();
private:
bool resizeable_down, resizeable_right;
virtual bool handle_mouse_down(DispPoint pos_);
virtual bool handle_key_down(SDL_keysym key);
virtual bool handle_key_up(SDL_keysym key);
bool key_is_held;
SDLKey key_held;
GUITimer time_key_held;
SDLMod modifiers_held;
void handle_key_held();
void handle_key();
void handle_alpha_num(char ltr);
void handle_modifier(SDLMod key);
class Cursor : public GUI::View {
public:
Cursor(TextField* tb_ptr);
void display(int text_size);
void move_right();
void move_left();
void move_up();
void move_down();
void move_to(int index_);
int get_index() const
{ return index; }
DispPoint get_pos() const
{ return position; }
private:
DispPoint position;
int index;
TextField* text_box_ptr;
bool flicker;
};
Cursor *cursor;
bool flicker;
void blink_cursor();
};
// Wraps a TextField with a white background and bevelled corners.
class TextBox : public GUI::View {
public:
TextBox(int w_, int h_);
void set_text(const std::string& text) { field->set_text(text); }
std::string get_text() const { return field->get_text(); }
void set_text_size(int size) { return field->set_text_size(size); }
void set_text_color(SDL_Color color) { return field->set_text_color(color);}
int get_text_size() { return field->get_text_size(); }
SDL_Color get_text_color() { return field->get_text_color(); }
private:
TextField *field;
};
// Implements the Flyweight pattern.
class NewLetter_Disp_Obj{
public:
NewLetter_Disp_Obj(char ltr, int size, DispPoint pos, SDL_Color color);
void drawself(GUI::View *dest) const;
int get_width() const;
DispPoint get_pos() const
{ return position; }
void set_pos(DispPoint point)
{ position = point; }
static int get_line_height() {
return line_height;
}
char get_ltr() const { return ltr; }
private:
DispPoint position;
GUILetter* letter;
char ltr;
int height;
static int line_height;
};
} // namespace GUI
#endif /* NEW_GUI_TEXT_BOX_H */