1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "ultima/ultima4/controllers/read_choice_controller.h"
24
25 namespace Ultima {
26 namespace Ultima4 {
27
28
ReadChoiceController(const Common::String & choices)29 ReadChoiceController::ReadChoiceController(const Common::String &choices) :
30 WaitableController<int>(-1) {
31 _choices = choices;
32 }
33
keyPressed(int key)34 bool ReadChoiceController::keyPressed(int key) {
35 // Common::isUpper() accepts 1-byte characters, yet the modifier keys
36 // (ALT, SHIFT, ETC) produce values beyond 255
37 if ((key <= 0x7F) && (Common::isUpper(key)))
38 key = tolower(key);
39
40 _value = key;
41
42 if (_choices.empty() || _choices.findFirstOf(_value) < _choices.size()) {
43 // If the value is printable, display it
44 if (!Common::isSpace(key))
45 g_screen->screenMessage("%c", toupper(key));
46 doneWaiting();
47 return true;
48 }
49
50 return false;
51 }
52
keybinder(KeybindingAction action)53 void ReadChoiceController::keybinder(KeybindingAction action) {
54 if (action == KEYBIND_ESCAPE && _choices.contains('\x1B')) {
55 _value = 27;
56 doneWaiting();
57 } else {
58 WaitableController<int>::keybinder(action);
59 }
60 }
61
62
get(const Common::String & choices,EventHandler * eh)63 char ReadChoiceController::get(const Common::String &choices, EventHandler *eh) {
64 if (!eh)
65 eh = eventHandler;
66
67 ReadChoiceController ctrl(choices);
68 eh->pushController(&ctrl);
69 return ctrl.waitFor();
70 }
71
72 } // End of namespace Ultima4
73 } // End of namespace Ultima
74