Date: | 2011-10-23 11:56:32 (12 years 5 months ago) |
---|---|
Author: | Maarten ter Huurne |
Commit: | fa5a9816dbf8a2dd5e87d06b4db6548861757f47 |
Message: | InputDialog: re-layouted code. No functional changes. |
Files: |
src/inputdialog.cpp (10 diffs) |
Change Details
src/inputdialog.cpp | ||
---|---|---|
1 | 1 | /*************************************************************************** |
2 | * Copyright (C) 2006 by Massimiliano Torromeo * | |
3 | * massimiliano.torromeo@gmail.com * | |
2 | * Copyright (C) 2006 by Massimiliano Torromeo * | |
3 | * massimiliano.torromeo@gmail.com * | |
4 | 4 | * * |
5 | 5 | * This program is free software; you can redistribute it and/or modify * |
6 | 6 | * it under the terms of the GNU General Public License as published by * |
... | ... | |
33 | 33 | , inputMgr(inputMgr_) |
34 | 34 | , ts(ts_) |
35 | 35 | { |
36 | if (title=="") { | |
36 | if (title == "") { | |
37 | 37 | this->title = text; |
38 | 38 | this->text = ""; |
39 | 39 | } else { |
... | ... | |
41 | 41 | this->text = text; |
42 | 42 | } |
43 | 43 | this->icon = ""; |
44 | if (icon!="" && gmenu2x->sc[icon] != NULL) | |
44 | if (icon != "" && gmenu2x->sc[icon] != NULL) { | |
45 | 45 | this->icon = icon; |
46 | } | |
46 | 47 | |
47 | 48 | input = startvalue; |
48 | 49 | selCol = 0; |
... | ... | |
85 | 86 | setKeyboard(0); |
86 | 87 | |
87 | 88 | buttonbox = new ButtonBox(gmenu2x); |
88 | IconButton *btnBackspace = new IconButton(gmenu2x, "skin:imgs/buttons/l.png", gmenu2x->tr["Backspace"]); | |
89 | IconButton *btnBackspace = new IconButton(gmenu2x, | |
90 | "skin:imgs/buttons/l.png", gmenu2x->tr["Backspace"]); | |
89 | 91 | btnBackspace->setAction(MakeDelegate(this, &InputDialog::backspace)); |
90 | 92 | buttonbox->add(btnBackspace); |
91 | 93 | |
92 | IconButton *btnSpace = new IconButton(gmenu2x, "skin:imgs/buttons/r.png", gmenu2x->tr["Space"]); | |
94 | IconButton *btnSpace = new IconButton(gmenu2x, | |
95 | "skin:imgs/buttons/r.png", gmenu2x->tr["Space"]); | |
93 | 96 | btnSpace->setAction(MakeDelegate(this, &InputDialog::space)); |
94 | 97 | buttonbox->add(btnSpace); |
95 | 98 | |
96 | IconButton *btnConfirm = new IconButton(gmenu2x, "skin:imgs/buttons/accept.png", gmenu2x->tr["Confirm"]); | |
99 | IconButton *btnConfirm = new IconButton(gmenu2x, | |
100 | "skin:imgs/buttons/accept.png", gmenu2x->tr["Confirm"]); | |
97 | 101 | btnConfirm->setAction(MakeDelegate(this, &InputDialog::confirm)); |
98 | 102 | buttonbox->add(btnConfirm); |
99 | 103 | |
100 | IconButton *btnChangeKeys = new IconButton(gmenu2x, "skin:imgs/buttons/cancel.png", gmenu2x->tr["Change keys"]); | |
104 | IconButton *btnChangeKeys = new IconButton(gmenu2x, | |
105 | "skin:imgs/buttons/cancel.png", gmenu2x->tr["Change keys"]); | |
101 | 106 | btnChangeKeys->setAction(MakeDelegate(this, &InputDialog::changeKeys)); |
102 | 107 | buttonbox->add(btnChangeKeys); |
103 | 108 | } |
104 | 109 | |
105 | 110 | void InputDialog::setKeyboard(int kb) { |
106 | kb = constrain(kb,0,keyboard.size()-1); | |
111 | kb = constrain(kb, 0, keyboard.size() - 1); | |
107 | 112 | curKeyboard = kb; |
108 | 113 | this->kb = &(keyboard[kb]); |
109 | 114 | kbLength = this->kb->at(0).length(); |
110 | for (int x = 0, l = kbLength; x<l; x++) | |
115 | for (int x = 0, l = kbLength; x < l; x++) { | |
111 | 116 | if (gmenu2x->font->utf8Code(this->kb->at(0)[x])) { |
112 | 117 | kbLength--; |
113 | 118 | x++; |
114 | 119 | } |
120 | } | |
115 | 121 | |
116 | kbLeft = 160 - kbLength*KEY_WIDTH/2; | |
117 | kbWidth = kbLength*KEY_WIDTH+3; | |
118 | kbHeight = (this->kb->size()+1)*KEY_HEIGHT+3; | |
122 | kbLeft = 160 - kbLength * KEY_WIDTH / 2; | |
123 | kbWidth = kbLength * KEY_WIDTH + 3; | |
124 | kbHeight = (this->kb->size() + 1) * KEY_HEIGHT + 3; | |
119 | 125 | |
120 | kbRect.x = kbLeft-3; | |
121 | kbRect.y = KB_TOP-2; | |
126 | kbRect.x = kbLeft - 3; | |
127 | kbRect.y = KB_TOP - 2; | |
122 | 128 | kbRect.w = kbWidth; |
123 | 129 | kbRect.h = kbHeight; |
124 | 130 | } |
125 | 131 | |
126 | 132 | bool InputDialog::exec() { |
127 | SDL_Rect box = {0, 60, 0, gmenu2x->font->getHeight()+4}; | |
133 | SDL_Rect box = { 0, 60, 0, gmenu2x->font->getHeight() + 4 }; | |
128 | 134 | |
129 | 135 | Uint32 caretTick = 0, curTick; |
130 | 136 | bool caretOn = true; |
... | ... | |
140 | 146 | |
141 | 147 | buttonbox->paint(5); |
142 | 148 | |
143 | box.w = gmenu2x->font->getTextWidth(input)+18; | |
144 | box.x = 160-box.w/2; | |
149 | box.w = gmenu2x->font->getTextWidth(input) + 18; | |
150 | box.x = 160 - box.w / 2; | |
145 | 151 | gmenu2x->s->box(box.x, box.y, box.w, box.h, |
146 | 152 | gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
147 | gmenu2x->s->rectangle(box.x, box.y, box.w, box.h, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); | |
153 | gmenu2x->s->rectangle(box.x, box.y, box.w, box.h, | |
154 | gmenu2x->skinConfColors[COLOR_SELECTION_BG]); | |
148 | 155 | |
149 | gmenu2x->s->write(gmenu2x->font, input, box.x+5, box.y+box.h-2, ASFont::HAlignLeft, ASFont::VAlignBottom); | |
156 | gmenu2x->s->write(gmenu2x->font, input, box.x + 5, box.y + box.h - 2, | |
157 | ASFont::HAlignLeft, ASFont::VAlignBottom); | |
150 | 158 | |
151 | 159 | curTick = SDL_GetTicks(); |
152 | if (curTick-caretTick>=600) { | |
160 | if (curTick - caretTick >= 600) { | |
153 | 161 | caretOn = !caretOn; |
154 | 162 | caretTick = curTick; |
155 | 163 | } |
156 | 164 | |
157 | if (caretOn) gmenu2x->s->box(box.x+box.w-12, box.y+3, 8, box.h-6, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); | |
165 | if (caretOn) { | |
166 | gmenu2x->s->box(box.x + box.w - 12, box.y + 3, 8, box.h - 6, | |
167 | gmenu2x->skinConfColors[COLOR_SELECTION_BG]); | |
168 | } | |
158 | 169 | |
159 | 170 | if (ts.initialized()) ts.poll(); |
160 | 171 | action = drawVirtualKeyboard(); |
161 | 172 | gmenu2x->s->flip(); |
162 | 173 | |
163 | switch (inputMgr.waitForPressedButton()) { | |
164 | case InputManager::SETTINGS: | |
174 | switch (inputMgr.waitForPressedButton()) { | |
175 | case InputManager::SETTINGS: | |
165 | 176 | ok = false; |
166 | 177 | close = true; |
167 | break; | |
168 | case InputManager::UP: | |
178 | break; | |
179 | case InputManager::UP: | |
169 | 180 | selRow--; |
170 | break; | |
171 | case InputManager::DOWN: | |
181 | break; | |
182 | case InputManager::DOWN: | |
172 | 183 | selRow++; |
173 | if (selRow==(int)kb->size()) selCol = selCol<8 ? 0 : 1; | |
174 | break; | |
175 | case InputManager::LEFT: | |
184 | if (selRow == (int)kb->size()) selCol = selCol < 8 ? 0 : 1; | |
185 | break; | |
186 | case InputManager::LEFT: | |
176 | 187 | selCol--; |
177 | break; | |
178 | case InputManager::RIGHT: | |
188 | break; | |
189 | case InputManager::RIGHT: | |
179 | 190 | selCol++; |
180 | break; | |
181 | case InputManager::ACCEPT: | |
182 | confirm(); | |
183 | break; | |
184 | case InputManager::CANCEL: | |
185 | changeKeys(); | |
186 | break; | |
187 | case InputManager::ALTLEFT: | |
188 | backspace(); | |
189 | break; | |
190 | case InputManager::ALTRIGHT: | |
191 | space(); | |
192 | break; | |
193 | default: | |
194 | break; | |
195 | } | |
191 | break; | |
192 | case InputManager::ACCEPT: | |
193 | confirm(); | |
194 | break; | |
195 | case InputManager::CANCEL: | |
196 | changeKeys(); | |
197 | break; | |
198 | case InputManager::ALTLEFT: | |
199 | backspace(); | |
200 | break; | |
201 | case InputManager::ALTRIGHT: | |
202 | space(); | |
203 | break; | |
204 | default: | |
205 | break; | |
206 | } | |
196 | 207 | } |
197 | 208 | |
198 | 209 | return ok; |
199 | 210 | } |
200 | 211 | |
201 | 212 | void InputDialog::backspace() { |
202 | // check for utf8 characters | |
203 | input = input.substr(0,input.length()-( gmenu2x->font->utf8Code(input[input.length()-2]) ? 2 : 1 )); | |
213 | // Check for UTF8 characters. | |
214 | input = input.substr(0, input.length() | |
215 | - (gmenu2x->font->utf8Code(input[input.length() - 2]) ? 2 : 1)); | |
204 | 216 | } |
205 | 217 | |
206 | 218 | void InputDialog::space() { |
207 | // check for utf8 characters | |
208 | 219 | input += " "; |
209 | 220 | } |
210 | 221 | |
211 | 222 | void InputDialog::confirm() { |
212 | if (selRow==(int)kb->size()) { | |
213 | if (selCol==0) | |
223 | if (selRow == (int)kb->size()) { | |
224 | if (selCol == 0) { | |
214 | 225 | ok = false; |
226 | } | |
215 | 227 | close = true; |
216 | 228 | } 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); | |
229 | int xc = 0; | |
230 | for (uint x = 0; x < kb->at(selRow).length(); x++) { | |
231 | bool utf8 = gmenu2x->font->utf8Code(kb->at(selRow)[x]); | |
232 | if (xc == selCol) input += kb->at(selRow).substr(x, utf8 ? 2 : 1); | |
222 | 233 | if (utf8) x++; |
223 | 234 | xc++; |
224 | 235 | } |
... | ... | |
226 | 237 | } |
227 | 238 | |
228 | 239 | void InputDialog::changeKeys() { |
229 | if (curKeyboard==6) | |
240 | if (curKeyboard == 6) { | |
230 | 241 | setKeyboard(0); |
231 | else | |
232 | setKeyboard(curKeyboard+1); | |
242 | } else { | |
243 | setKeyboard(curKeyboard + 1); | |
244 | } | |
233 | 245 | } |
234 | 246 | |
235 | 247 | int InputDialog::drawVirtualKeyboard() { |
... | ... | |
245 | 257 | |
246 | 258 | //selection |
247 | 259 | 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]); | |
260 | gmenu2x->s->box(kbLeft + selCol * KEY_WIDTH - 1, | |
261 | KB_TOP + selRow * KEY_HEIGHT, KEY_WIDTH - 1, KEY_HEIGHT - 2, | |
262 | gmenu2x->skinConfColors[COLOR_SELECTION_BG]); | |
249 | 263 | 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]); | |
264 | if (selCol > 1) selCol = 0; | |
265 | if (selCol < 0) selCol = 1; | |
266 | gmenu2x->s->box(kbLeft + selCol * kbLength * KEY_WIDTH / 2 - 1, | |
267 | KB_TOP + kb->size() * KEY_HEIGHT, kbLength * KEY_WIDTH / 2 - 1, | |
268 | KEY_HEIGHT - 1, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); | |
254 | 269 | } |
255 | 270 | |
256 | 271 | //keys |
... | ... | |
265 | 280 | } else |
266 | 281 | charX = line[x]; |
267 | 282 | |
268 | SDL_Rect re = {kbLeft+xc*KEY_WIDTH-1, KB_TOP+l*KEY_HEIGHT, KEY_WIDTH-1, KEY_HEIGHT-2}; | |
283 | SDL_Rect re = { | |
284 | kbLeft + xc * KEY_WIDTH - 1, KB_TOP + l * KEY_HEIGHT, | |
285 | KEY_WIDTH - 1, KEY_HEIGHT - 2 | |
286 | }; | |
269 | 287 | |
270 | 288 | //if ts on rect, change selection |
271 | 289 | if (ts.initialized() && ts.pressed() && ts.inRect(re)) { |
... | ... | |
273 | 291 | selRow = l; |
274 | 292 | } |
275 | 293 | |
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, ASFont::HAlignCenter, ASFont::VAlignMiddle); | |
294 | gmenu2x->s->rectangle(re, | |
295 | gmenu2x->skinConfColors[COLOR_SELECTION_BG]); | |
296 | gmenu2x->s->write(gmenu2x->font, charX, | |
297 | kbLeft + xc * KEY_WIDTH + KEY_WIDTH / 2 - 1, | |
298 | KB_TOP + l * KEY_HEIGHT + KEY_HEIGHT / 2, | |
299 | ASFont::HAlignCenter, ASFont::VAlignMiddle); | |
278 | 300 | xc++; |
279 | 301 | } |
280 | 302 | } |
... | ... | |
286 | 308 | selCol = 0; |
287 | 309 | selRow = kb->size(); |
288 | 310 | } |
289 | gmenu2x->s->write(gmenu2x->font, gmenu2x->tr["Cancel"], (int)(160-kbLength*KEY_WIDTH/4), KB_TOP+kb->size()*KEY_HEIGHT+KEY_HEIGHT/2, ASFont::HAlignCenter, ASFont::VAlignMiddle); | |
311 | gmenu2x->s->write(gmenu2x->font, gmenu2x->tr["Cancel"], | |
312 | (int)(160 - kbLength * KEY_WIDTH / 4), | |
313 | KB_TOP + kb->size() * KEY_HEIGHT + KEY_HEIGHT / 2, | |
314 | ASFont::HAlignCenter, ASFont::VAlignMiddle); | |
290 | 315 | |
291 | re.x = kbLeft+kbLength*KEY_WIDTH/2-1; | |
316 | re.x = kbLeft + kbLength * KEY_WIDTH / 2 - 1; | |
292 | 317 | gmenu2x->s->rectangle(re, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
293 | 318 | if (ts.initialized() && ts.pressed() && ts.inRect(re)) { |
294 | 319 | selCol = 1; |
295 | 320 | selRow = kb->size(); |
296 | 321 | } |
297 | gmenu2x->s->write(gmenu2x->font, gmenu2x->tr["OK"], (int)(160+kbLength*KEY_WIDTH/4), KB_TOP+kb->size()*KEY_HEIGHT+KEY_HEIGHT/2, ASFont::HAlignCenter, ASFont::VAlignMiddle); | |
322 | gmenu2x->s->write(gmenu2x->font, gmenu2x->tr["OK"], | |
323 | (int)(160 + kbLength * KEY_WIDTH / 4), | |
324 | KB_TOP + kb->size() * KEY_HEIGHT + KEY_HEIGHT / 2, | |
325 | ASFont::HAlignCenter, ASFont::VAlignMiddle); | |
298 | 326 | |
299 | 327 | //if ts released |
300 | 328 | if (ts.initialized() && ts.released() && ts.inRect(kbRect)) { |
Branches:
install_locations
master
opkrun
packages