Root/
Source at commit 7fd1ab0 created 13 years 10 months ago. By Lars-Peter Clausen, Use a simple array instead of a hashmap for the colors. | |
---|---|
1 | /*************************************************************************** |
2 | * Copyright (C) 2006 by Massimiliano Torromeo * |
3 | * massimiliano.torromeo@gmail.com * |
4 | * * |
5 | * This program is free software; you can redistribute it and/or modify * |
6 | * it under the terms of the GNU General Public License as published by * |
7 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * (at your option) any later version. * |
9 | * * |
10 | * This program is distributed in the hope that it will be useful, * |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * GNU General Public License for more details. * |
14 | * * |
15 | * You should have received a copy of the GNU General Public License * |
16 | * along with this program; if not, write to the * |
17 | * Free Software Foundation, Inc., * |
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | ***************************************************************************/ |
20 | |
21 | #include <SDL.h> |
22 | #include <SDL_gfxPrimitives.h> |
23 | |
24 | #include "inputdialog.h" |
25 | |
26 | using namespace std; |
27 | using namespace fastdelegate; |
28 | |
29 | InputDialog::InputDialog(GMenu2X *gmenu2x, const string &text, const string &startvalue, const string &title, const string &icon) { |
30 | this->gmenu2x = gmenu2x; |
31 | if (title=="") { |
32 | this->title = text; |
33 | this->text = ""; |
34 | } else { |
35 | this->title = title; |
36 | this->text = text; |
37 | } |
38 | this->icon = ""; |
39 | if (icon!="" && gmenu2x->sc[icon] != NULL) |
40 | this->icon = icon; |
41 | |
42 | input = startvalue; |
43 | selCol = 0; |
44 | selRow = 0; |
45 | keyboard.resize(7); |
46 | |
47 | keyboard[0].push_back("abcdefghijklm"); |
48 | keyboard[0].push_back("nopqrstuvwxyz"); |
49 | keyboard[0].push_back("0123456789. "); |
50 | |
51 | keyboard[1].push_back("ABCDEFGHIJKLM"); |
52 | keyboard[1].push_back("NOPQRSTUVWXYZ"); |
53 | keyboard[1].push_back("_\"'`.,:;!? "); |
54 | |
55 | |
56 | keyboard[2].push_back("¡¿*+-/\\&<=>|"); |
57 | keyboard[2].push_back("()[]{}@#$%^~"); |
58 | keyboard[2].push_back("_\"'`.,:;!? "); |
59 | |
60 | |
61 | keyboard[3].push_back("àáèéìíòóùúýäõ"); |
62 | keyboard[3].push_back("ëïöüÿâêîôûåãñ"); |
63 | keyboard[3].push_back("čďěľĺňôřŕšťůž"); |
64 | keyboard[3].push_back("ąćęłńśżź "); |
65 | |
66 | keyboard[4].push_back("ÀÁÈÉÌÍÒÓÙÚÝÄÕ"); |
67 | keyboard[4].push_back("ËÏÖÜŸÂÊÎÔÛÅÃÑ"); |
68 | keyboard[4].push_back("ČĎĚĽĹŇÔŘŔŠŤŮŽ"); |
69 | keyboard[4].push_back("ĄĆĘŁŃŚŻŹ "); |
70 | |
71 | |
72 | keyboard[5].push_back("æçабвгдеёжзий "); |
73 | keyboard[5].push_back("клмнопрстуфхцч"); |
74 | keyboard[5].push_back("шщъыьэюяøðßÐÞþ"); |
75 | |
76 | keyboard[6].push_back("ÆÇАБВГДЕЁЖЗИЙ "); |
77 | keyboard[6].push_back("КЛМНОПРСТУФХЦЧ"); |
78 | keyboard[6].push_back("ШЩЪЫЬЭЮЯØðßÐÞþ"); |
79 | |
80 | setKeyboard(0); |
81 | |
82 | ButtonAction actBackspace = MakeDelegate(this, &InputDialog::backspace); |
83 | |
84 | btnBackspaceX = new IconButton(gmenu2x, "skin:imgs/buttons/x.png"); |
85 | btnBackspaceX->setAction(actBackspace); |
86 | |
87 | btnBackspaceL = new IconButton(gmenu2x, "skin:imgs/buttons/l.png", gmenu2x->tr["Backspace"]); |
88 | btnBackspaceL->setAction(actBackspace); |
89 | |
90 | btnSpace = new IconButton(gmenu2x, "skin:imgs/buttons/r.png", gmenu2x->tr["Space"]); |
91 | btnSpace->setAction(MakeDelegate(this, &InputDialog::space)); |
92 | |
93 | btnConfirm = new IconButton(gmenu2x, "skin:imgs/buttons/b.png", gmenu2x->tr["Confirm"]); |
94 | btnConfirm->setAction(MakeDelegate(this, &InputDialog::confirm)); |
95 | |
96 | btnChangeKeys = new IconButton(gmenu2x, "skin:imgs/buttons/y.png", gmenu2x->tr["Change keys"]); |
97 | btnChangeKeys->setAction(MakeDelegate(this, &InputDialog::changeKeys)); |
98 | } |
99 | |
100 | void InputDialog::setKeyboard(int kb) { |
101 | kb = constrain(kb,0,keyboard.size()-1); |
102 | curKeyboard = kb; |
103 | this->kb = &(keyboard[kb]); |
104 | kbLength = this->kb->at(0).length(); |
105 | for (int x = 0, l = kbLength; x<l; x++) |
106 | if (gmenu2x->font->utf8Code(this->kb->at(0)[x])) { |
107 | kbLength--; |
108 | x++; |
109 | } |
110 | |
111 | kbLeft = 160 - kbLength*KEY_WIDTH/2; |
112 | kbWidth = kbLength*KEY_WIDTH+3; |
113 | kbHeight = (this->kb->size()+1)*KEY_HEIGHT+3; |
114 | |
115 | kbRect.x = kbLeft-3; |
116 | kbRect.y = KB_TOP-2; |
117 | kbRect.w = kbWidth; |
118 | kbRect.h = kbHeight; |
119 | } |
120 | |
121 | bool InputDialog::exec() { |
122 | SDL_Rect box = {0, 60, 0, gmenu2x->font->getHeight()+4}; |
123 | |
124 | Uint32 caretTick = 0, curTick; |
125 | bool caretOn = true; |
126 | |
127 | uint action; |
128 | close = false; |
129 | ok = true; |
130 | while (!close) { |
131 | gmenu2x->bg->blit(gmenu2x->s,0,0); |
132 | gmenu2x->writeTitle(title); |
133 | gmenu2x->writeSubTitle(text); |
134 | gmenu2x->drawTitleIcon(icon); |
135 | |
136 | gmenu2x->drawButton(gmenu2x->s, "y", gmenu2x->tr["Change keys"], |
137 | gmenu2x->drawButton(gmenu2x->s, "b", gmenu2x->tr["Confirm"], |
138 | gmenu2x->drawButton(gmenu2x->s, "r", gmenu2x->tr["Space"], |
139 | gmenu2x->drawButton(btnBackspaceL, |
140 | gmenu2x->drawButton(btnBackspaceX)-6)))); |
141 | |
142 | box.w = gmenu2x->font->getTextWidth(input)+18; |
143 | box.x = 160-box.w/2; |
144 | gmenu2x->s->box(box.x, box.y, box.w, box.h, |
145 | gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
146 | gmenu2x->s->rectangle(box.x, box.y, box.w, box.h, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
147 | |
148 | gmenu2x->s->write(gmenu2x->font, input, box.x+5, box.y+box.h-2, SFontHAlignLeft, SFontVAlignBottom); |
149 | |
150 | curTick = SDL_GetTicks(); |
151 | if (curTick-caretTick>=600) { |
152 | caretOn = !caretOn; |
153 | caretTick = curTick; |
154 | } |
155 | |
156 | if (caretOn) gmenu2x->s->box(box.x+box.w-12, box.y+3, 8, box.h-6, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
157 | |
158 | if (gmenu2x->f200) gmenu2x->ts.poll(); |
159 | action = drawVirtualKeyboard(); |
160 | gmenu2x->s->flip(); |
161 | |
162 | gmenu2x->input.update(); |
163 | if ( gmenu2x->input[ACTION_START] ) action = ID_ACTION_CLOSE; |
164 | if ( gmenu2x->input[ACTION_UP ] ) action = ID_ACTION_UP; |
165 | if ( gmenu2x->input[ACTION_DOWN ] ) action = ID_ACTION_DOWN; |
166 | if ( gmenu2x->input[ACTION_LEFT ] ) action = ID_ACTION_LEFT; |
167 | if ( gmenu2x->input[ACTION_RIGHT] ) action = ID_ACTION_RIGHT; |
168 | if ( gmenu2x->input[ACTION_B] ) action = ID_ACTION_SELECT; |
169 | if ( gmenu2x->input[ACTION_Y] ) action = ID_ACTION_KB_CHANGE; |
170 | if ( gmenu2x->input[ACTION_X] || gmenu2x->input[ACTION_L] ) action = ID_ACTION_BACKSPACE; |
171 | if ( gmenu2x->input[ACTION_R ] ) action = ID_ACTION_SPACE; |
172 | |
173 | switch (action) { |
174 | case ID_ACTION_CLOSE: { |
175 | ok = false; |
176 | close = true; |
177 | } break; |
178 | case ID_ACTION_UP: { |
179 | selRow--; |
180 | } break; |
181 | case ID_ACTION_DOWN: { |
182 | selRow++; |
183 | if (selRow==(int)kb->size()) selCol = selCol<8 ? 0 : 1; |
184 | } break; |
185 | case ID_ACTION_LEFT: { |
186 | selCol--; |
187 | } break; |
188 | case ID_ACTION_RIGHT: { |
189 | selCol++; |
190 | } break; |
191 | case ID_ACTION_BACKSPACE: backspace(); break; |
192 | case ID_ACTION_SPACE: space(); break; |
193 | case ID_ACTION_KB_CHANGE: changeKeys(); break; |
194 | case ID_ACTION_SELECT: confirm(); break; |
195 | } |
196 | } |
197 | |
198 | return ok; |
199 | } |
200 | |
201 | void InputDialog::backspace() { |
202 | // check for utf8 characters |
203 | input = input.substr(0,input.length()-( gmenu2x->font->utf8Code(input[input.length()-2]) ? 2 : 1 )); |
204 | } |
205 | |
206 | void InputDialog::space() { |
207 | // check for utf8 characters |
208 | input += " "; |
209 | } |
210 | |
211 | void InputDialog::confirm() { |
212 | if (selRow==(int)kb->size()) { |
213 | if (selCol==0) |
214 | ok = false; |
215 | close = true; |
216 | } else { |
217 | bool utf8; |
218 | int xc=0; |
219 | for (uint x=0; x<kb->at(selRow).length(); x++) { |
220 | utf8 = gmenu2x->font->utf8Code(kb->at(selRow)[x]); |
221 | if (xc==selCol) input += kb->at(selRow).substr(x, utf8 ? 2 : 1); |
222 | if (utf8) x++; |
223 | xc++; |
224 | } |
225 | } |
226 | } |
227 | |
228 | void InputDialog::changeKeys() { |
229 | if (curKeyboard==6) |
230 | setKeyboard(0); |
231 | else |
232 | setKeyboard(curKeyboard+1); |
233 | } |
234 | |
235 | int InputDialog::drawVirtualKeyboard() { |
236 | int action = ID_NO_ACTION; |
237 | |
238 | //keyboard border |
239 | gmenu2x->s->rectangle(kbRect, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
240 | |
241 | if (selCol<0) selCol = selRow==(int)kb->size() ? 1 : kbLength-1; |
242 | if (selCol>=(int)kbLength) selCol = 0; |
243 | if (selRow<0) selRow = kb->size()-1; |
244 | if (selRow>(int)kb->size()) selRow = 0; |
245 | |
246 | //selection |
247 | if (selRow<(int)kb->size()) |
248 | gmenu2x->s->box(kbLeft+selCol*KEY_WIDTH-1, KB_TOP+selRow*KEY_HEIGHT, KEY_WIDTH-1, KEY_HEIGHT-2, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
249 | else { |
250 | if (selCol>1) selCol = 0; |
251 | if (selCol<0) selCol = 1; |
252 | gmenu2x->s->box(kbLeft+selCol*kbLength*KEY_WIDTH/2-1, |
253 | KB_TOP+kb->size()*KEY_HEIGHT, kbLength*KEY_WIDTH/2-1, KEY_HEIGHT-1, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
254 | } |
255 | |
256 | //keys |
257 | for (uint l=0; l<kb->size(); l++) { |
258 | string line = kb->at(l); |
259 | for (uint x=0, xc=0; x<line.length(); x++) { |
260 | string charX; |
261 | //utf8 characters |
262 | if (gmenu2x->font->utf8Code(line[x])) { |
263 | charX = line.substr(x,2); |
264 | x++; |
265 | } else |
266 | charX = line[x]; |
267 | |
268 | SDL_Rect re = {kbLeft+xc*KEY_WIDTH-1, KB_TOP+l*KEY_HEIGHT, KEY_WIDTH-1, KEY_HEIGHT-2}; |
269 | |
270 | //if ts on rect, change selection |
271 | if (gmenu2x->f200 && gmenu2x->ts.pressed() && gmenu2x->ts.inRect(re)) { |
272 | selCol = xc; |
273 | selRow = l; |
274 | } |
275 | |
276 | gmenu2x->s->rectangle(re, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
277 | gmenu2x->s->write(gmenu2x->font, charX, kbLeft+xc*KEY_WIDTH+KEY_WIDTH/2-1, KB_TOP+l*KEY_HEIGHT+KEY_HEIGHT/2, SFontHAlignCenter, SFontVAlignMiddle); |
278 | xc++; |
279 | } |
280 | } |
281 | |
282 | //Ok/Cancel |
283 | SDL_Rect re = {kbLeft-1, KB_TOP+kb->size()*KEY_HEIGHT, kbLength*KEY_WIDTH/2-1, KEY_HEIGHT-1}; |
284 | gmenu2x->s->rectangle(re, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
285 | if (gmenu2x->f200 && gmenu2x->ts.pressed() && gmenu2x->ts.inRect(re)) { |
286 | selCol = 0; |
287 | selRow = kb->size(); |
288 | } |
289 | gmenu2x->s->write(gmenu2x->font, gmenu2x->tr["Cancel"], (int)(160-kbLength*KEY_WIDTH/4), KB_TOP+kb->size()*KEY_HEIGHT+KEY_HEIGHT/2, SFontHAlignCenter, SFontVAlignMiddle); |
290 | |
291 | re.x = kbLeft+kbLength*KEY_WIDTH/2-1; |
292 | gmenu2x->s->rectangle(re, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
293 | if (gmenu2x->f200 && gmenu2x->ts.pressed() && gmenu2x->ts.inRect(re)) { |
294 | selCol = 1; |
295 | selRow = kb->size(); |
296 | } |
297 | gmenu2x->s->write(gmenu2x->font, gmenu2x->tr["OK"], (int)(160+kbLength*KEY_WIDTH/4), KB_TOP+kb->size()*KEY_HEIGHT+KEY_HEIGHT/2, SFontHAlignCenter, SFontVAlignMiddle); |
298 | |
299 | //if ts released |
300 | if (gmenu2x->f200 && gmenu2x->ts.wasPressed && !gmenu2x->ts.pressed() && gmenu2x->ts.inRect(kbRect)) |
301 | action = ID_ACTION_SELECT; |
302 | |
303 | return action; |
304 | } |
305 |
Branches:
install_locations
master
opkrun
packages