aboutsummaryrefslogtreecommitdiff
path: root/backends/keymapper/remap-dialog.cpp
blob: 8bd36fe952e4e7fd8dc5c5ed5b780de064d0e623 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "backends/keymapper/remap-dialog.h"
#include "gui/eval.h"
#include "gui/newgui.h"
#include "gui/PopUpWidget.h"

namespace Common {

enum {
	kRemapCmd = 'REMP'
};

RemapDialog::RemapDialog()
	: Dialog("remap"), _activeRemap(0) {

	const int screenW = g_system->getOverlayWidth();
	const int screenH = g_system->getOverlayHeight();

	_keymapper = g_system->getEventManager()->getKeymapper();
	assert(_keymapper);
	_activeKeymaps = &_keymapper->_activeMaps;

	int keymapCount = 0;
	_globalKeymaps = &_keymapper->_keymapMan->getGlobalDomain();
	if (_globalKeymaps->count() == 0)
		_globalKeymaps = 0;
	else
		keymapCount += _globalKeymaps->count();
	if (ConfMan.getActiveDomain() != 0) {
		_gameKeymaps = &_keymapper->_keymapMan->getGameDomain();
		if (_gameKeymaps->count() == 0)
			_gameKeymaps = 0;
		else
			keymapCount += _gameKeymaps->count();
	} else
		_gameKeymaps = 0;

	_keymapTable = (Keymap**)malloc(sizeof(Keymap*) * keymapCount);

	int labelWidth = g_gui.evaluator()->getVar("remap_popup_labelW");
	_kmPopUp = new GUI::PopUpWidget(this, "remap_popup", "Keymap:", labelWidth);

	if (_activeKeymaps->size() > 0) {
		_kmPopUp->appendEntry(_activeKeymaps->top().keymap->getName() + " (Active)");
	}
	KeymapManager::Domain::iterator it;
	uint32 idx = 0;

	if (_globalKeymaps) {
		_kmPopUp->appendEntry("");
		for (it = _globalKeymaps->begin(); it != _globalKeymaps->end(); it++) {
			_kmPopUp->appendEntry(it->_value->getName() + " (Global)", idx);
			_keymapTable[idx++] = it->_value;
		}
	}
	if (_gameKeymaps) {
		_kmPopUp->appendEntry("");
		for (it = _gameKeymaps->begin(); it != _gameKeymaps->end(); it++) {
			_kmPopUp->appendEntry(it->_value->getName() + " (Game)", idx);
			_keymapTable[idx++] = it->_value;
		}
	}

	_w = screenW - 2 * 20;
	_h = screenH - 2 * 20;
	// Center the dialog
	_x = (screenW - _w) / 2;
	_y = (screenH - _h) / 2;
	
	_colCount = 2;
	_spacing = 10;
	_colWidth = (_w - (_colCount - 1) * _spacing) / _colCount;
	_widgetsY = g_gui.evaluator()->getVar("remap_widgetsY");
	if (g_gui.getWidgetSize() == GUI::kBigWidgetSize)
		_buttonHeight = GUI::kBigButtonHeight;
	else
		_buttonHeight = GUI::kButtonHeight;
	
}

RemapDialog::~RemapDialog() {
	free(_keymapTable);
}

void RemapDialog::open() {
	Dialog::open();
	
	_kmPopUp->setSelected(0);
	refreshKeymap();
}

void RemapDialog::handleCommand(GUI::CommandSender *sender, uint32 cmd, uint32 data) {
	if (cmd >= kRemapCmd && cmd < kRemapCmd + _keymapMappings.size()) {
		startRemapping(&_keymapMappings[cmd - kRemapCmd]);
	} else if (cmd == GUI::kPopUpItemSelectedCmd) {
		refreshKeymap();
	} else {
		GUI::Dialog::handleCommand(sender, cmd, data);
	}
}

void RemapDialog::startRemapping(Mapping *remap) {
	_activeRemap = remap;
	_activeRemap->keyButton->setLabel("...");
	_keymapper->setEnabled(false);
}

void RemapDialog::stopRemapping() {
	refreshKeymap();
	_activeRemap = 0;
	_keymapper->setEnabled(true);
}

void RemapDialog::handleKeyDown(Common::KeyState state) {
	if (_activeRemap) {
		const HardwareKey *hwkey = _keymapper->getHardwareKey(state);
		if (hwkey) {
			_activeRemap->action->mapKey(hwkey);
			stopRemapping();
		}
	} else {
		GUI::Dialog::handleKeyDown(state);
	}

}

void RemapDialog::refreshKeymap() {
	if (_activeKeymaps->size() > 0 && _kmPopUp->getSelected() == 0) {
		// TODO: show active keymaps (with inherited mappings)
	} else {
		Keymap *km = _keymapTable[_kmPopUp->getSelectedTag()];
		List<Action*>& actions = km->getActions();
		setNumOfWidgets(actions.size());
		uint idx = 0;
		List<Action*>::iterator it;
		for (it = actions.begin(); it != actions.end(); it++, idx++) {
			Mapping& ma = _keymapMappings[idx];
			ma.action = *it;
			ma.actionText->setLabel(ma.action->description + ":");
			ma.keyButton->setLabel(ma.action->getMappedKey()->description);
		}
	}

}

void RemapDialog::setNumOfWidgets(uint newNum) {
	uint num = _keymapMappings.size();
	if (num < newNum) {
		uint textYOff = (_buttonHeight - kLineHeight) / 2;
		while (num < newNum) {
			uint x = (num % _colCount) * (_colWidth + _spacing);
			uint y = _widgetsY + (num / _colCount) * (_buttonHeight + _spacing);
			Mapping ma;
			ma.action = 0;
			ma.actionText = new GUI::StaticTextWidget(this, x, y + textYOff, 
				_colWidth / 2, kLineHeight, "", Graphics::kTextAlignRight);
			ma.keyButton = new GUI::ButtonWidget(this, x + _colWidth / 2,
				y, _colWidth / 2, _buttonHeight, "", kRemapCmd + num);
			_keymapMappings.push_back(ma);
			num++;
		}
	} else {
		while (num > newNum) {
			Mapping ma = _keymapMappings.remove_at(num - 1);
			removeWidget(ma.actionText);
			delete ma.actionText;
			removeWidget(ma.keyButton);
			delete ma.keyButton;
			num--;
		}
	}

}

} // end of namespace Common