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

Last change on this file since 929b4e0 was 3f5616f, checked in by Dmitry Portnoy <dmp1488@…>, 12 years ago

Changed the client makefile to use static linking for the allegro library and fixed a bug where button and textbox borders would display incorrectly on Linux

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[87b3ee2]1#include "Textbox.h"
2
3#include <iostream>
4
5using namespace std;
6
7Textbox::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;
[e607c0f]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[' '] = ' ';
[87b3ee2]41}
42
43Textbox::~Textbox(void)
44{
45}
46
47const string& Textbox::getStr() const
48{
49 return str;
50}
51
52void Textbox::clear(void)
53{
54 str.clear();
55}
56
57void 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());
[3f5616f]70
71 #ifdef WINDOWS
72 al_draw_rectangle(1, 1, this->width, this->height, al_map_rgb(0, 255, 0), 1);
73 #else
74 al_draw_rectangle(1, 0, this->width, this->height-1, al_map_rgb(0, 255, 0), 1);
75 #endif
[87b3ee2]76
77 al_set_target_bitmap(al_get_backbuffer(display));
78 al_draw_bitmap(bitmap, x, y, 0);
79}
80
81bool Textbox::handleEvent(ALLEGRO_EVENT& e)
82{
83 ALLEGRO_KEYBOARD_STATE keys;
84 al_get_keyboard_state(&keys);
85
86 if (e.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
87 if (e.mouse.button == 1) {
88 if (this->x < e.mouse.x && e.mouse.x < (this->x+this->width)
89 && this->y < e.mouse.y && e.mouse.y < (this->y+this->height))
90 {
91 selected = true;
92 return true;
93 }else
94 selected = false;
95 }
96
97 return false;
98 }
99
100 if (!selected)
101 return false;
102
103 if (e.type == ALLEGRO_EVENT_KEY_DOWN) {
104 char newChar = 0;
105
[e607c0f]106 if (ALLEGRO_KEY_A <= e.keyboard.keycode && e.keyboard.keycode <= ALLEGRO_KEY_Z)
[87b3ee2]107 newChar = 'a'+e.keyboard.keycode-ALLEGRO_KEY_A;
108 else if (ALLEGRO_KEY_0 <= e.keyboard.keycode && e.keyboard.keycode <= ALLEGRO_KEY_9)
109 newChar = '0'+e.keyboard.keycode-ALLEGRO_KEY_0;
[e607c0f]110 else {
111 switch(e.keyboard.keycode)
112 {
113 case ALLEGRO_KEY_TILDE:
114 newChar = '`';
115 break;
116 case ALLEGRO_KEY_MINUS:
117 newChar = '-';
118 break;
119 case ALLEGRO_KEY_EQUALS:
120 newChar = '=';
121 break;
122 case ALLEGRO_KEY_OPENBRACE:
123 newChar = '[';
124 break;
125 case ALLEGRO_KEY_CLOSEBRACE:
126 newChar = ']';
127 break;
128 case ALLEGRO_KEY_SEMICOLON:
129 newChar = ';';
130 break;
131 case ALLEGRO_KEY_QUOTE:
132 newChar = '\'';
133 break;
134 case ALLEGRO_KEY_BACKSLASH:
135 newChar = '\\';
136 break;
137 case ALLEGRO_KEY_COMMA:
138 newChar = ',';
139 break;
140 case ALLEGRO_KEY_FULLSTOP:
141 newChar = '.';
142 break;
143 case ALLEGRO_KEY_SLASH:
144 newChar = '/';
145 break;
146 case ALLEGRO_KEY_SPACE:
147 newChar = ' ';
148 break;
149 case ALLEGRO_KEY_BACKSPACE:
150 if (str.size() > 0)
151 {
152 str = str.substr(0, str.size()-1);
153 return true;
154 }
155 else
156 return false;
157 break;
158 case ALLEGRO_KEY_LSHIFT:
159 case ALLEGRO_KEY_RSHIFT:
160 shiftPressed = true;
161 break;
162 default:
163 cout << "unknown keycode: " << e.keyboard.keycode << endl;
164 break;
165 }
166 }
[87b3ee2]167
168 if (newChar != 0) {
[e607c0f]169 if (al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT))
170 newChar = shiftMap[newChar];
171
[87b3ee2]172 str.append(1, newChar);
173 return true;
174 }
175 }
[e607c0f]176 else if (e.type == ALLEGRO_EVENT_KEY_UP) {
177 switch(e.keyboard.keycode)
178 {
179 case ALLEGRO_KEY_LSHIFT:
180 case ALLEGRO_KEY_RSHIFT:
181 shiftPressed = false;
182 break;
183 }
184 }
[87b3ee2]185
186 return false;
187}
Note: See TracBrowser for help on using the repository browser.