2008-08-11 23:08:21 +00:00
|
|
|
/* 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.
|
2014-02-18 02:34:21 +01:00
|
|
|
*
|
2008-08-11 23:08:21 +00:00
|
|
|
*/
|
|
|
|
|
2008-08-08 14:23:59 +00:00
|
|
|
#include "backends/keymapper/remap-dialog.h"
|
2008-09-30 13:51:01 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_KEYMAPPER
|
|
|
|
|
2017-08-11 13:57:28 +02:00
|
|
|
#include "backends/keymapper/action.h"
|
2017-08-13 17:04:45 +02:00
|
|
|
#include "backends/keymapper/hardware-input.h"
|
2017-08-13 13:59:00 +02:00
|
|
|
#include "backends/keymapper/input-watcher.h"
|
2017-08-13 17:04:45 +02:00
|
|
|
#include "backends/keymapper/keymap.h"
|
|
|
|
#include "backends/keymapper/keymapper.h"
|
2017-08-11 13:57:28 +02:00
|
|
|
|
2011-04-29 13:15:54 +02:00
|
|
|
#include "common/system.h"
|
2010-11-16 10:19:01 +00:00
|
|
|
#include "gui/gui-manager.h"
|
2010-11-16 10:11:57 +00:00
|
|
|
#include "gui/widgets/popup.h"
|
2017-08-13 19:04:34 +02:00
|
|
|
#include "gui/widgets/scrollcontainer.h"
|
2009-01-18 14:46:53 +00:00
|
|
|
#include "gui/ThemeEval.h"
|
2010-06-15 11:02:42 +00:00
|
|
|
#include "common/translation.h"
|
|
|
|
|
2008-08-08 14:23:59 +00:00
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
enum {
|
2017-08-13 19:04:34 +02:00
|
|
|
kRemapCmd = 'REMP',
|
|
|
|
kClearCmd = 'CLER',
|
|
|
|
kCloseCmd = 'CLOS',
|
|
|
|
kReflowCmd = 'REFL'
|
2008-08-08 14:23:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
RemapDialog::RemapDialog()
|
2017-08-13 19:04:34 +02:00
|
|
|
: Dialog("KeyMapper"), _remapTimeout(0), _remapAction(nullptr) {
|
2008-08-08 14:23:59 +00:00
|
|
|
|
|
|
|
_keymapper = g_system->getEventManager()->getKeymapper();
|
|
|
|
assert(_keymapper);
|
|
|
|
|
2017-08-13 13:59:00 +02:00
|
|
|
EventDispatcher *eventDispatcher = g_system->getEventManager()->getEventDispatcher();
|
|
|
|
_remapInputWatcher = new InputWatcher(eventDispatcher, _keymapper);
|
|
|
|
|
2010-06-15 11:02:42 +00:00
|
|
|
_kmPopUpDesc = new GUI::StaticTextWidget(this, "KeyMapper.PopupDesc", _("Keymap:"));
|
2009-06-12 08:00:26 +00:00
|
|
|
_kmPopUp = new GUI::PopUpWidget(this, "KeyMapper.Popup");
|
2008-08-13 19:24:52 +00:00
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
_scrollContainer = new GUI::ScrollContainerWidget(this, "KeyMapper.KeymapArea", "", kReflowCmd);
|
|
|
|
_scrollContainer->setTarget(this);
|
2019-12-28 10:43:58 +01:00
|
|
|
|
2010-06-15 11:02:42 +00:00
|
|
|
new GUI::ButtonWidget(this, "KeyMapper.Close", _("Close"), 0, kCloseCmd);
|
2008-08-13 19:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RemapDialog::~RemapDialog() {
|
2017-08-13 13:59:00 +02:00
|
|
|
delete _remapInputWatcher;
|
2008-08-13 19:24:52 +00:00
|
|
|
}
|
2008-08-13 11:46:08 +00:00
|
|
|
|
2008-08-13 19:24:52 +00:00
|
|
|
void RemapDialog::open() {
|
2017-08-13 16:35:58 +02:00
|
|
|
_keymapTable = _keymapper->getKeymaps();
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-13 16:35:58 +02:00
|
|
|
debug(3, "RemapDialog::open keymaps: %d", _keymapTable.size());
|
2012-02-09 02:10:19 -06:00
|
|
|
|
2017-08-13 16:35:58 +02:00
|
|
|
// Show the keymaps by order of priority (game keymaps first)
|
|
|
|
for (int i = _keymapTable.size() - 1; i >= 0; i--) {
|
|
|
|
_kmPopUp->appendEntry(_keymapTable[i]->getName(), i);
|
2008-08-08 14:23:59 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 22:20:18 +00:00
|
|
|
_changes = false;
|
|
|
|
|
2008-08-14 01:42:02 +00:00
|
|
|
_kmPopUp->setSelected(0);
|
2017-08-13 19:04:34 +02:00
|
|
|
|
2008-08-14 01:42:02 +00:00
|
|
|
loadKeymap();
|
2017-08-13 19:04:34 +02:00
|
|
|
refreshKeymap();
|
|
|
|
reflowActionWidgets();
|
|
|
|
|
|
|
|
Dialog::open();
|
2008-08-08 14:23:59 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 19:24:52 +00:00
|
|
|
void RemapDialog::close() {
|
|
|
|
_kmPopUp->clearEntries();
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2009-05-10 18:04:47 +00:00
|
|
|
if (_changes)
|
2008-08-14 01:42:02 +00:00
|
|
|
ConfMan.flushToDisk();
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2008-08-13 19:24:52 +00:00
|
|
|
Dialog::close();
|
|
|
|
}
|
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
void RemapDialog::reflowActionWidgets() {
|
2009-01-18 14:46:53 +00:00
|
|
|
int buttonHeight = g_gui.xmlEval()->getVar("Globals.Button.Height", 0);
|
2011-12-30 15:28:46 -06:00
|
|
|
|
2009-06-12 08:00:26 +00:00
|
|
|
int spacing = g_gui.xmlEval()->getVar("Globals.KeyMapper.Spacing");
|
2011-10-15 15:30:32 -05:00
|
|
|
int keyButtonWidth = g_gui.xmlEval()->getVar("Globals.KeyMapper.ButtonWidth");
|
|
|
|
int clearButtonWidth = g_gui.xmlEval()->getVar("Globals.Line.Height");
|
|
|
|
int clearButtonHeight = g_gui.xmlEval()->getVar("Globals.Line.Height");
|
2017-08-13 19:04:34 +02:00
|
|
|
int labelWidth = getWidth() - (spacing + keyButtonWidth + spacing + clearButtonWidth + spacing);
|
2008-08-13 19:24:52 +00:00
|
|
|
|
|
|
|
uint textYOff = (buttonHeight - kLineHeight) / 2;
|
2011-10-15 15:30:32 -05:00
|
|
|
uint clearButtonYOff = (buttonHeight - clearButtonHeight) / 2;
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
for (uint i = 0; i < _actions.size(); i++) {
|
|
|
|
uint x = spacing;
|
|
|
|
uint y = spacing + (i) * (buttonHeight + spacing);
|
2011-10-15 15:30:32 -05:00
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
ActionRow &row = _actions[i];
|
|
|
|
row.keyButton->resize(x, y, keyButtonWidth, buttonHeight);
|
|
|
|
row.clearButton->resize(x + keyButtonWidth + spacing, y + clearButtonYOff, clearButtonWidth, clearButtonHeight);
|
|
|
|
row.actionText->resize(x + keyButtonWidth + spacing + clearButtonWidth + spacing, y + textYOff, labelWidth, kLineHeight);
|
2008-08-13 19:24:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-08 14:23:59 +00:00
|
|
|
void RemapDialog::handleCommand(GUI::CommandSender *sender, uint32 cmd, uint32 data) {
|
2011-10-15 15:30:32 -05:00
|
|
|
debug(3, "RemapDialog::handleCommand %u %u", cmd, data);
|
2009-05-12 20:40:15 +00:00
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
if (cmd >= kRemapCmd && cmd < kRemapCmd + _actions.size()) {
|
2008-08-13 11:46:08 +00:00
|
|
|
startRemapping(cmd - kRemapCmd);
|
2017-08-13 19:04:34 +02:00
|
|
|
} else if (cmd >= kClearCmd && cmd < kClearCmd + _actions.size()) {
|
2011-10-15 15:30:32 -05:00
|
|
|
clearMapping(cmd - kClearCmd);
|
2017-08-13 19:04:34 +02:00
|
|
|
} else if (cmd == kCloseCmd) {
|
|
|
|
close();
|
|
|
|
} else if (cmd == kReflowCmd) {
|
|
|
|
reflowActionWidgets();
|
2008-08-08 14:23:59 +00:00
|
|
|
} else if (cmd == GUI::kPopUpItemSelectedCmd) {
|
2017-08-13 19:04:34 +02:00
|
|
|
clearKeymap();
|
2008-08-11 23:08:21 +00:00
|
|
|
loadKeymap();
|
2008-08-13 11:46:08 +00:00
|
|
|
refreshKeymap();
|
2017-08-13 19:04:34 +02:00
|
|
|
_scrollContainer->reflowLayout();
|
|
|
|
g_gui.scheduleTopDialogRedraw();
|
2008-08-08 14:23:59 +00:00
|
|
|
} else {
|
|
|
|
GUI::Dialog::handleCommand(sender, cmd, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-15 15:30:32 -05:00
|
|
|
void RemapDialog::clearMapping(uint i) {
|
|
|
|
debug(3, "clear the mapping %u", i);
|
2017-08-13 19:04:34 +02:00
|
|
|
Action *activeRemapAction = _actions[i].action;
|
2017-08-11 13:57:28 +02:00
|
|
|
_keymapper->clearMapping(activeRemapAction);
|
2011-10-15 15:30:32 -05:00
|
|
|
_changes = true;
|
|
|
|
|
2017-08-13 13:59:00 +02:00
|
|
|
stopRemapping();
|
2011-10-15 15:30:32 -05:00
|
|
|
refreshKeymap();
|
|
|
|
}
|
|
|
|
|
2008-08-13 11:46:08 +00:00
|
|
|
void RemapDialog::startRemapping(uint i) {
|
2017-08-13 13:59:00 +02:00
|
|
|
if (_remapInputWatcher->isWatching()) {
|
2014-02-20 01:06:50 -05:00
|
|
|
// Handle a second click on the button as a stop to remapping
|
2017-08-13 13:59:00 +02:00
|
|
|
stopRemapping();
|
2014-02-20 01:06:50 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
_remapAction = _actions[i].action;
|
2010-03-18 15:05:14 +00:00
|
|
|
_remapTimeout = g_system->getMillis() + kRemapTimeoutDelay;
|
2017-08-13 13:59:00 +02:00
|
|
|
_remapInputWatcher->startWatching();
|
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
_actions[i].keyButton->setLabel("...");
|
|
|
|
_actions[i].keyButton->markAsDirty();
|
2008-08-08 14:23:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 13:59:00 +02:00
|
|
|
void RemapDialog::stopRemapping() {
|
|
|
|
_remapAction = nullptr;
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2008-08-08 14:23:59 +00:00
|
|
|
refreshKeymap();
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-13 13:59:00 +02:00
|
|
|
_remapInputWatcher->stopWatching();
|
2008-08-08 14:23:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 13:59:00 +02:00
|
|
|
void RemapDialog::handleMouseDown(int x, int y, int button, int clickCount) {
|
|
|
|
if (_remapInputWatcher->isWatching())
|
|
|
|
stopRemapping();
|
|
|
|
else
|
|
|
|
Dialog::handleMouseDown(x, y, button, clickCount);
|
2009-05-12 20:40:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 13:59:00 +02:00
|
|
|
void RemapDialog::handleTickle() {
|
|
|
|
const HardwareInput *hardwareInput = _remapInputWatcher->checkForCapturedInput();
|
|
|
|
if (hardwareInput) {
|
|
|
|
_keymapper->registerMapping(_remapAction, hardwareInput);
|
2009-05-11 09:01:54 +00:00
|
|
|
|
2012-02-22 18:30:47 -06:00
|
|
|
_changes = true;
|
|
|
|
stopRemapping();
|
2008-08-08 14:23:59 +00:00
|
|
|
}
|
2008-08-13 14:33:17 +00:00
|
|
|
|
2017-08-13 13:59:00 +02:00
|
|
|
if (_remapInputWatcher->isWatching() && g_system->getMillis() > _remapTimeout)
|
2008-08-13 14:33:17 +00:00
|
|
|
stopRemapping();
|
|
|
|
Dialog::handleTickle();
|
2008-08-08 14:23:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
void RemapDialog::clearKeymap() {
|
|
|
|
for (uint i = 0; i < _actions.size(); i++) {
|
|
|
|
if (_actions[i].keyButton) _scrollContainer->removeWidget(_actions[i].keyButton);
|
|
|
|
if (_actions[i].actionText) _scrollContainer->removeWidget(_actions[i].actionText);
|
|
|
|
if (_actions[i].clearButton) _scrollContainer->removeWidget(_actions[i].clearButton);
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
delete _actions[i].keyButton;
|
|
|
|
delete _actions[i].actionText;
|
|
|
|
delete _actions[i].clearButton;
|
2008-08-08 14:23:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
_actions.clear();
|
2008-08-08 14:23:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
void RemapDialog::loadKeymap() {
|
|
|
|
assert(_actions.empty());
|
|
|
|
assert(_kmPopUp->getSelected() != -1);
|
2008-08-13 11:46:08 +00:00
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
Keymap *km = _keymapTable[_kmPopUp->getSelectedTag()];
|
2017-08-14 12:59:37 +02:00
|
|
|
for (Keymap::ActionArray::const_iterator it = km->getActions().begin(); it != km->getActions().end(); ++it) {
|
2017-08-13 19:04:34 +02:00
|
|
|
ActionRow row;
|
|
|
|
row.action = *it;
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
_actions.push_back(row);
|
|
|
|
}
|
|
|
|
}
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
void RemapDialog::refreshKeymap() {
|
|
|
|
int clearButtonWidth = g_gui.xmlEval()->getVar("Globals.Line.Height");
|
|
|
|
int clearButtonHeight = g_gui.xmlEval()->getVar("Globals.Line.Height");
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
for (uint i = 0; i < _actions.size(); i++) {
|
|
|
|
ActionRow &row = _actions[i];
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
if (!row.actionText) {
|
|
|
|
row.actionText = new GUI::StaticTextWidget(_scrollContainer, 0, 0, 0, 0, "", Graphics::kTextAlignLeft);
|
|
|
|
row.keyButton = new GUI::ButtonWidget(_scrollContainer, 0, 0, 0, 0, "", 0, kRemapCmd + i);
|
|
|
|
row.clearButton = addClearButton(_scrollContainer, "", kClearCmd + i, 0, 0, clearButtonWidth, clearButtonHeight);
|
|
|
|
}
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
row.actionText->setLabel(row.action->description);
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-13 19:04:34 +02:00
|
|
|
Keymap *keymap = row.action->getParent();
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2017-08-14 13:58:43 +02:00
|
|
|
Array<const HardwareInput *> mappedInputs = keymap->getActionMapping(row.action);
|
|
|
|
|
|
|
|
String keysLabel;
|
|
|
|
for (uint j = 0; j < mappedInputs.size(); j++) {
|
|
|
|
if (!keysLabel.empty()) {
|
|
|
|
keysLabel += ", ";
|
|
|
|
}
|
|
|
|
|
|
|
|
keysLabel += mappedInputs[j]->description;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!keysLabel.empty())
|
|
|
|
row.keyButton->setLabel(keysLabel);
|
2017-08-13 19:04:34 +02:00
|
|
|
else
|
|
|
|
row.keyButton->setLabel("-");
|
2008-08-11 23:08:21 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-08 14:23:59 +00:00
|
|
|
|
2009-10-04 21:26:33 +00:00
|
|
|
} // End of namespace Common
|
2008-09-30 13:51:01 +00:00
|
|
|
|
|
|
|
#endif // #ifdef ENABLE_KEYMAPPER
|