source: network-game/client/Client/Textbox.cpp@ 0065962

Last change on this file since 0065962 was 6319311, checked in by Dmitry Portnoy <dportnoy@…>, 11 years ago

Some redfinition issues related to winsock2 are fixed and a few allegro rendering functions are now in GameRender

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