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
11/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
44 * *
55 * This program is free software; you can redistribute it and/or modify *
66 * it under the terms of the GNU General Public License as published by *
...... 
3333    , inputMgr(inputMgr_)
3434    , ts(ts_)
3535{
36    if (title=="") {
36    if (title == "") {
3737        this->title = text;
3838        this->text = "";
3939    } else {
...... 
4141        this->text = text;
4242    }
4343    this->icon = "";
44    if (icon!="" && gmenu2x->sc[icon] != NULL)
44    if (icon != "" && gmenu2x->sc[icon] != NULL) {
4545        this->icon = icon;
46    }
4647
4748    input = startvalue;
4849    selCol = 0;
...... 
8586    setKeyboard(0);
8687
8788    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"]);
8991    btnBackspace->setAction(MakeDelegate(this, &InputDialog::backspace));
9092    buttonbox->add(btnBackspace);
9193
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"]);
9396    btnSpace->setAction(MakeDelegate(this, &InputDialog::space));
9497    buttonbox->add(btnSpace);
9598
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"]);
97101    btnConfirm->setAction(MakeDelegate(this, &InputDialog::confirm));
98102    buttonbox->add(btnConfirm);
99103
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"]);
101106    btnChangeKeys->setAction(MakeDelegate(this, &InputDialog::changeKeys));
102107    buttonbox->add(btnChangeKeys);
103108}
104109
105110void InputDialog::setKeyboard(int kb) {
106    kb = constrain(kb,0,keyboard.size()-1);
111    kb = constrain(kb, 0, keyboard.size() - 1);
107112    curKeyboard = kb;
108113    this->kb = &(keyboard[kb]);
109114    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++) {
111116        if (gmenu2x->font->utf8Code(this->kb->at(0)[x])) {
112117            kbLength--;
113118            x++;
114119        }
120    }
115121
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;
119125
120    kbRect.x = kbLeft-3;
121    kbRect.y = KB_TOP-2;
126    kbRect.x = kbLeft - 3;
127    kbRect.y = KB_TOP - 2;
122128    kbRect.w = kbWidth;
123129    kbRect.h = kbHeight;
124130}
125131
126132bool InputDialog::exec() {
127    SDL_Rect box = {0, 60, 0, gmenu2x->font->getHeight()+4};
133    SDL_Rect box = { 0, 60, 0, gmenu2x->font->getHeight() + 4 };
128134
129135    Uint32 caretTick = 0, curTick;
130136    bool caretOn = true;
...... 
140146
141147        buttonbox->paint(5);
142148
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;
145151        gmenu2x->s->box(box.x, box.y, box.w, box.h,
146152        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]);
148155
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);
150158
151159        curTick = SDL_GetTicks();
152        if (curTick-caretTick>=600) {
160        if (curTick - caretTick >= 600) {
153161            caretOn = !caretOn;
154162            caretTick = curTick;
155163        }
156164
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        }
158169
159170        if (ts.initialized()) ts.poll();
160171        action = drawVirtualKeyboard();
161172        gmenu2x->s->flip();
162173
163        switch (inputMgr.waitForPressedButton()) {
164            case InputManager::SETTINGS:
174        switch (inputMgr.waitForPressedButton()) {
175            case InputManager::SETTINGS:
165176                ok = false;
166177                close = true;
167                break;
168            case InputManager::UP:
178                break;
179            case InputManager::UP:
169180                selRow--;
170                break;
171            case InputManager::DOWN:
181                break;
182            case InputManager::DOWN:
172183                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:
176187                selCol--;
177                break;
178            case InputManager::RIGHT:
188                break;
189            case InputManager::RIGHT:
179190                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        }
196207    }
197208
198209    return ok;
199210}
200211
201212void 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));
204216}
205217
206218void InputDialog::space() {
207    // check for utf8 characters
208219    input += " ";
209220}
210221
211222void InputDialog::confirm() {
212    if (selRow==(int)kb->size()) {
213        if (selCol==0)
223    if (selRow == (int)kb->size()) {
224        if (selCol == 0) {
214225            ok = false;
226        }
215227        close = true;
216228    } 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);
222233            if (utf8) x++;
223234            xc++;
224235        }
...... 
226237}
227238
228239void InputDialog::changeKeys() {
229    if (curKeyboard==6)
240    if (curKeyboard == 6) {
230241        setKeyboard(0);
231    else
232        setKeyboard(curKeyboard+1);
242    } else {
243        setKeyboard(curKeyboard + 1);
244    }
233245}
234246
235247int InputDialog::drawVirtualKeyboard() {
...... 
245257
246258    //selection
247259    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]);
249263    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]);
254269    }
255270
256271    //keys
...... 
265280            } else
266281                charX = line[x];
267282
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            };
269287
270288            //if ts on rect, change selection
271289            if (ts.initialized() && ts.pressed() && ts.inRect(re)) {
...... 
273291                selRow = l;
274292            }
275293
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);
278300            xc++;
279301        }
280302    }
...... 
286308        selCol = 0;
287309        selRow = kb->size();
288310    }
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);
290315
291    re.x = kbLeft+kbLength*KEY_WIDTH/2-1;
316    re.x = kbLeft + kbLength * KEY_WIDTH / 2 - 1;
292317    gmenu2x->s->rectangle(re, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
293318    if (ts.initialized() && ts.pressed() && ts.inRect(re)) {
294319        selCol = 1;
295320        selRow = kb->size();
296321    }
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);
298326
299327    //if ts released
300328    if (ts.initialized() && ts.released() && ts.inRect(kbRect)) {

Archive Download the corresponding diff file



interactive