1 | #include "Textbox.h"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 |
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 | Textbox::Textbox(int x, int y, int width, int height, ALLEGRO_FONT *font) :
|
---|
8 | GuiComponent(x, y, width, height, font)
|
---|
9 | {
|
---|
10 | str = "";
|
---|
11 | selected = false;
|
---|
12 | shiftPressed = false;
|
---|
13 |
|
---|
14 | // populate the shift map
|
---|
15 | for(int i=0; i<26; i++)
|
---|
16 | shiftMap['a'+i] = 'A'+i;
|
---|
17 |
|
---|
18 | shiftMap['1'] = '!';
|
---|
19 | shiftMap['2'] = '@';
|
---|
20 | shiftMap['3'] = '#';
|
---|
21 | shiftMap['4'] = '$';
|
---|
22 | shiftMap['5'] = '%';
|
---|
23 | shiftMap['6'] = '^';
|
---|
24 | shiftMap['7'] = '&';
|
---|
25 | shiftMap['8'] = '*';
|
---|
26 | shiftMap['9'] = '(';
|
---|
27 | shiftMap['0'] = ')';
|
---|
28 |
|
---|
29 | shiftMap['`'] = '~';
|
---|
30 | shiftMap['-'] = '_';
|
---|
31 | shiftMap['='] = '+';
|
---|
32 | shiftMap['['] = '{';
|
---|
33 | shiftMap[']'] = '}';
|
---|
34 | shiftMap['\\'] = '|';
|
---|
35 | shiftMap[';'] = ':';
|
---|
36 | shiftMap['\''] = '\"';
|
---|
37 | shiftMap[','] = '<';
|
---|
38 | shiftMap['.'] = '>';
|
---|
39 | shiftMap['/'] = '?';
|
---|
40 | shiftMap[' '] = ' ';
|
---|
41 | }
|
---|
42 |
|
---|
43 | Textbox::~Textbox(void)
|
---|
44 | {
|
---|
45 | }
|
---|
46 |
|
---|
47 | const string& Textbox::getStr() const
|
---|
48 | {
|
---|
49 | return str;
|
---|
50 | }
|
---|
51 |
|
---|
52 | void Textbox::clear(void)
|
---|
53 | {
|
---|
54 | str.clear();
|
---|
55 | }
|
---|
56 |
|
---|
57 | void Textbox::draw(ALLEGRO_DISPLAY *display)
|
---|
58 | {
|
---|
59 | al_set_target_bitmap(bitmap);
|
---|
60 | al_clear_to_color(al_map_rgb(0, 0, 0));
|
---|
61 |
|
---|
62 | int textWidth = al_get_text_width(font, str.c_str());
|
---|
63 | int fontHeight = al_get_font_line_height(font);
|
---|
64 |
|
---|
65 | int textPos = 1;
|
---|
66 | if(textWidth > this->width)
|
---|
67 | textPos = this->width-textWidth-3;
|
---|
68 |
|
---|
69 | al_draw_text(font, al_map_rgb(0, 255, 0), textPos, (this->height-fontHeight)/2, ALLEGRO_ALIGN_LEFT, str.c_str());
|
---|
70 | al_draw_rectangle(1, 1, this->width, this->height, al_map_rgb(0, 255, 0), 1);
|
---|
71 |
|
---|
72 | al_set_target_bitmap(al_get_backbuffer(display));
|
---|
73 | al_draw_bitmap(bitmap, x, y, 0);
|
---|
74 | }
|
---|
75 |
|
---|
76 | bool Textbox::handleEvent(ALLEGRO_EVENT& e)
|
---|
77 | {
|
---|
78 | ALLEGRO_KEYBOARD_STATE keys;
|
---|
79 | al_get_keyboard_state(&keys);
|
---|
80 |
|
---|
81 | if (e.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
|
---|
82 | if (e.mouse.button == 1) {
|
---|
83 | if (this->x < e.mouse.x && e.mouse.x < (this->x+this->width)
|
---|
84 | && this->y < e.mouse.y && e.mouse.y < (this->y+this->height))
|
---|
85 | {
|
---|
86 | selected = true;
|
---|
87 | return true;
|
---|
88 | }else
|
---|
89 | selected = false;
|
---|
90 | }
|
---|
91 |
|
---|
92 | return false;
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (!selected)
|
---|
96 | return false;
|
---|
97 |
|
---|
98 | if (e.type == ALLEGRO_EVENT_KEY_DOWN) {
|
---|
99 | char newChar = 0;
|
---|
100 |
|
---|
101 | if (ALLEGRO_KEY_A <= e.keyboard.keycode && e.keyboard.keycode <= ALLEGRO_KEY_Z)
|
---|
102 | newChar = 'a'+e.keyboard.keycode-ALLEGRO_KEY_A;
|
---|
103 | else if (ALLEGRO_KEY_0 <= e.keyboard.keycode && e.keyboard.keycode <= ALLEGRO_KEY_9)
|
---|
104 | newChar = '0'+e.keyboard.keycode-ALLEGRO_KEY_0;
|
---|
105 | else {
|
---|
106 | switch(e.keyboard.keycode)
|
---|
107 | {
|
---|
108 | case ALLEGRO_KEY_TILDE:
|
---|
109 | newChar = '`';
|
---|
110 | break;
|
---|
111 | case ALLEGRO_KEY_MINUS:
|
---|
112 | newChar = '-';
|
---|
113 | break;
|
---|
114 | case ALLEGRO_KEY_EQUALS:
|
---|
115 | newChar = '=';
|
---|
116 | break;
|
---|
117 | case ALLEGRO_KEY_OPENBRACE:
|
---|
118 | newChar = '[';
|
---|
119 | break;
|
---|
120 | case ALLEGRO_KEY_CLOSEBRACE:
|
---|
121 | newChar = ']';
|
---|
122 | break;
|
---|
123 | case ALLEGRO_KEY_SEMICOLON:
|
---|
124 | newChar = ';';
|
---|
125 | break;
|
---|
126 | case ALLEGRO_KEY_QUOTE:
|
---|
127 | newChar = '\'';
|
---|
128 | break;
|
---|
129 | case ALLEGRO_KEY_BACKSLASH:
|
---|
130 | newChar = '\\';
|
---|
131 | break;
|
---|
132 | case ALLEGRO_KEY_COMMA:
|
---|
133 | newChar = ',';
|
---|
134 | break;
|
---|
135 | case ALLEGRO_KEY_FULLSTOP:
|
---|
136 | newChar = '.';
|
---|
137 | break;
|
---|
138 | case ALLEGRO_KEY_SLASH:
|
---|
139 | newChar = '/';
|
---|
140 | break;
|
---|
141 | case ALLEGRO_KEY_SPACE:
|
---|
142 | newChar = ' ';
|
---|
143 | break;
|
---|
144 | case ALLEGRO_KEY_BACKSPACE:
|
---|
145 | if (str.size() > 0)
|
---|
146 | {
|
---|
147 | str = str.substr(0, str.size()-1);
|
---|
148 | return true;
|
---|
149 | }
|
---|
150 | else
|
---|
151 | return false;
|
---|
152 | break;
|
---|
153 | case ALLEGRO_KEY_LSHIFT:
|
---|
154 | case ALLEGRO_KEY_RSHIFT:
|
---|
155 | shiftPressed = true;
|
---|
156 | break;
|
---|
157 | default:
|
---|
158 | cout << "unknown keycode: " << e.keyboard.keycode << endl;
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | if (newChar != 0) {
|
---|
164 | if (al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT))
|
---|
165 | newChar = shiftMap[newChar];
|
---|
166 |
|
---|
167 | str.append(1, newChar);
|
---|
168 | return true;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | else if (e.type == ALLEGRO_EVENT_KEY_UP) {
|
---|
172 | switch(e.keyboard.keycode)
|
---|
173 | {
|
---|
174 | case ALLEGRO_KEY_LSHIFT:
|
---|
175 | case ALLEGRO_KEY_RSHIFT:
|
---|
176 | shiftPressed = false;
|
---|
177 | break;
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | return false;
|
---|
182 | }
|
---|