1 /* ScummVM - Graphic Adventure Engine
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.
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.
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.
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.
26 #include "gui/KeysDialog.h"
27 #include "gui/Actions.h"
28 #include <SDL_keyboard.h>
41 KeysDialog::KeysDialog(const Common::String
&title
)
42 : GUI::Dialog("KeysDialog") {
44 new ButtonWidget(this, "KeysDialog.Map", "Map", kMapCmd
, 0);
45 new ButtonWidget(this, "KeysDialog.Ok", "OK", kOKCmd
, 0);
46 new ButtonWidget(this, "KeysDialog.Cancel", "Cancel", kCloseCmd
, 0);
48 _actionsList
= new ListWidget(this, "KeysDialog.List");
49 _actionsList
->setNumberingMode(kListNumberingZero
);
51 _actionTitle
= new StaticTextWidget(this, "KeysDialog.Action", title
);
52 _keyMapping
= new StaticTextWidget(this, "KeysDialog.Mapping", "Select an action and click 'Map'");
54 _actionTitle
->setFlags(WIDGET_CLEARBG
);
55 _keyMapping
->setFlags(WIDGET_CLEARBG
);
60 for (int i
= 0; i
< Actions::Instance()->size(); i
++)
61 l
.push_back(Actions::Instance()->actionName((ActionType
)i
));
63 _actionsList
->setList(l
);
66 Actions::Instance()->beginMapping(false);
69 void KeysDialog::handleCommand(CommandSender
*sender
, uint32 cmd
, uint32 data
) {
72 case kListSelectionChangedCmd
:
73 if (_actionsList
->getSelected() >= 0) {
76 uint16 key
= Actions::Instance()->getMapping(_actionsList
->getSelected());
78 // ScummVM mappings for F1-F9 are different from SDL so remap back to sdl
79 if (key
>= Common::ASCII_F1
&& key
<= Common::ASCII_F9
)
80 key
= key
- Common::ASCII_F1
+ SDLK_F1
;
83 sprintf(selection
, "Associated key : %s", SDL_GetKeyName((SDLKey
)key
));
85 sprintf(selection
, "Associated key : none");
87 _keyMapping
->setLabel(selection
);
92 if (_actionsList
->getSelected() < 0) {
93 _actionTitle
->setLabel("Please select an action");
97 _actionSelected
= _actionsList
->getSelected();
98 uint16 key
= Actions::Instance()->getMapping(_actionSelected
);
100 // ScummVM mappings for F1-F9 are different from SDL so remap back to sdl
101 if (key
>= Common::ASCII_F1
&& key
<= Common::ASCII_F9
)
102 key
= key
- Common::ASCII_F1
+ SDLK_F1
;
105 sprintf(selection
, "Associated key : %s", SDL_GetKeyName((SDLKey
)key
));
107 sprintf(selection
, "Associated key : none");
109 _actionTitle
->setLabel("Press the key to associate");
110 _keyMapping
->setLabel(selection
);
112 Actions::Instance()->beginMapping(true);
113 _actionsList
->setEnabled(false);
115 _actionTitle
->draw();
118 Actions::Instance()->saveMapping();
122 Actions::Instance()->loadMapping();
128 void KeysDialog::handleKeyDown(Common::KeyState state
){
129 if (!Actions::Instance()->mappingActive())
130 Dialog::handleKeyDown(state
);
133 void KeysDialog::handleKeyUp(Common::KeyState state
) {
135 if (Actions::Instance()->mappingActive()) {
137 if (state
.flags
== 0xff && Actions::Instance()->mappingActive()) { // GAPI key was selected
141 Actions::Instance()->setMapping((ActionType
)_actionSelected
, state
.ascii
);
143 if (state
.ascii
!= 0)
144 sprintf(selection
, "Associated key : %s", SDL_GetKeyName((SDLKey
) state
.keycode
));
146 sprintf(selection
, "Associated key : none");
148 _actionTitle
->setLabel("Choose an action to map");
149 _keyMapping
->setLabel(selection
);
151 _actionTitle
->draw();
152 _actionSelected
= -1;
153 _actionsList
->setEnabled(true);
154 Actions::Instance()->beginMapping(false);
156 Dialog::handleKeyUp(state
);