aboutsummaryrefslogtreecommitdiff
path: root/gui/KeysDialog.cpp
blob: 666f03bbc944d8a34cf524481ab49143ae7dcfd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/stdafx.h"
#include "gui/KeysDialog.h"
#include "gui/Actions.h"
#include <SDL_keyboard.h>

#ifdef _WIN32_WCE
#include "CEDevice.h"
#endif

namespace GUI {

enum {
	kMapCmd	= 'map ',
	kOKCmd	= 'ok  '
};

KeysDialog::KeysDialog(const Common::String &title)
	: GUI::Dialog("keysdialog") {

	new ButtonWidget(this, "keysdialog_map", "Map", kMapCmd, 0);
	new ButtonWidget(this, "keysdialog_ok", "OK", kOKCmd, 0);
	new ButtonWidget(this, "keysdialog_cancel", "Cancel", kCloseCmd, 0);

	_actionsList = new ListWidget(this, "keysdialog_list");
	_actionsList->setNumberingMode(kListNumberingZero);

	_actionTitle = new StaticTextWidget(this, "keysdialog_action", title);
	_keyMapping = new StaticTextWidget(this, "keysdialog_mapping", "Select an action and click 'Map'");

	_actionTitle->setFlags(WIDGET_CLEARBG);
	_keyMapping->setFlags(WIDGET_CLEARBG);

	// Get actions names
	Common::StringList l;

	for (int i = 0; i < Actions::Instance()->size(); i++)
		l.push_back(Actions::Instance()->actionName((ActionType)i));

	_actionsList->setList(l);

	_actionSelected = -1;
	Actions::Instance()->beginMapping(false);
}

void KeysDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
	switch (cmd) {

	case kListSelectionChangedCmd:
		if (_actionsList->getSelected() >= 0) {
			char selection[100];

			uint16 key = Actions::Instance()->getMapping(_actionsList->getSelected());
#ifdef __SYMBIAN32__
			// ScummVM mappings for F1-F9 are different from SDL so remap back to sdl
			if (key >= Common::ASCII_F1 && key <= Common::ASCII_F9)
				key = key - Common::ASCII_F1 + SDLK_F1;
#endif
			if (key != 0)
				sprintf(selection, "Associated key : %s", SDL_GetKeyName((SDLKey)key));
			else
				sprintf(selection, "Associated key : none");

			_keyMapping->setLabel(selection);
			_keyMapping->draw();
		}
		break;
	case kMapCmd:
		if (_actionsList->getSelected() < 0) {
				_actionTitle->setLabel("Please select an action");
		} else {
			char selection[100];

			_actionSelected = _actionsList->getSelected();
			uint16 key = Actions::Instance()->getMapping(_actionSelected);
#ifdef __SYMBIAN32__
			// ScummVM mappings for F1-F9 are different from SDL so remap back to sdl
			if (key >= Common::ASCII_F1 && key <= Common::ASCII_F9)
				key = key - Common::ASCII_F1 + SDLK_F1;
#endif
			if (key != 0) 
				sprintf(selection, "Associated key : %s", SDL_GetKeyName((SDLKey)key));
			else
				sprintf(selection, "Associated key : none");

			_actionTitle->setLabel("Press the key to associate");
			_keyMapping->setLabel(selection);
			_keyMapping->draw();
			Actions::Instance()->beginMapping(true);
			_actionsList->setEnabled(false);
		}
		_actionTitle->draw();
		break;
	case kOKCmd:
		Actions::Instance()->saveMapping();
		close();
		break;
	case kCloseCmd:
		Actions::Instance()->loadMapping();
		close();
		break;
	}
}

void KeysDialog::handleKeyDown(Common::KeyState state){
	if (!Actions::Instance()->mappingActive())
		Dialog::handleKeyDown(state);
}

void KeysDialog::handleKeyUp(Common::KeyState state) {
#ifdef __SYMBIAN32__
	if (Actions::Instance()->mappingActive()) {
#else
	if (state.flags == 0xff  && Actions::Instance()->mappingActive()) {	// GAPI key was selected
#endif
		char selection[100];

		Actions::Instance()->setMapping((ActionType)_actionSelected, state.ascii);

		if (state.ascii != 0)
			sprintf(selection, "Associated key : %s", SDL_GetKeyName((SDLKey) state.keycode));
		else
			sprintf(selection, "Associated key : none");

		_actionTitle->setLabel("Choose an action to map");
		_keyMapping->setLabel(selection);
		_keyMapping->draw();
		_actionTitle->draw();
		_actionSelected = -1;
		_actionsList->setEnabled(true);
		Actions::Instance()->beginMapping(false);
	} else 
		Dialog::handleKeyUp(state);
}

} // namespace GUI