KEYMAPPER: Change the remap dialog to use a scrollable container

This commit is contained in:
Bastien Bouclet 2017-08-13 19:04:34 +02:00
parent 9c0bc2b633
commit 3b5016a62d
11 changed files with 124 additions and 180 deletions

View file

@ -33,20 +33,21 @@
#include "common/system.h" #include "common/system.h"
#include "gui/gui-manager.h" #include "gui/gui-manager.h"
#include "gui/widgets/popup.h" #include "gui/widgets/popup.h"
#include "gui/widgets/scrollbar.h" #include "gui/widgets/scrollcontainer.h"
#include "gui/ThemeEval.h" #include "gui/ThemeEval.h"
#include "common/translation.h" #include "common/translation.h"
namespace Common { namespace Common {
enum { enum {
kRemapCmd = 'REMP', kRemapCmd = 'REMP',
kClearCmd = 'CLER', kClearCmd = 'CLER',
kCloseCmd = 'CLOS' kCloseCmd = 'CLOS',
kReflowCmd = 'REFL'
}; };
RemapDialog::RemapDialog() RemapDialog::RemapDialog()
: Dialog("KeyMapper"), _topAction(0), _remapTimeout(0), _topKeymapIsGui(false), _remapAction(nullptr) { : Dialog("KeyMapper"), _remapTimeout(0), _remapAction(nullptr) {
_keymapper = g_system->getEventManager()->getKeymapper(); _keymapper = g_system->getEventManager()->getKeymapper();
assert(_keymapper); assert(_keymapper);
@ -57,10 +58,8 @@ RemapDialog::RemapDialog()
_kmPopUpDesc = new GUI::StaticTextWidget(this, "KeyMapper.PopupDesc", _("Keymap:")); _kmPopUpDesc = new GUI::StaticTextWidget(this, "KeyMapper.PopupDesc", _("Keymap:"));
_kmPopUp = new GUI::PopUpWidget(this, "KeyMapper.Popup"); _kmPopUp = new GUI::PopUpWidget(this, "KeyMapper.Popup");
_scrollBar = new GUI::ScrollBarWidget(this, 0, 0, 0, 0); _scrollContainer = new GUI::ScrollContainerWidget(this, "KeyMapper.KeymapArea", "", kReflowCmd);
_scrollContainer->setTarget(this);
GUI::ContainerWidget *keymapArea = new GUI::ContainerWidget(this, "KeyMapper.KeymapArea");
keymapArea->setBackgroundType(GUI::ThemeEngine::kWidgetBackgroundNo);
new GUI::ButtonWidget(this, "KeyMapper.Close", _("Close"), 0, kCloseCmd); new GUI::ButtonWidget(this, "KeyMapper.Close", _("Close"), 0, kCloseCmd);
} }
@ -81,10 +80,13 @@ void RemapDialog::open() {
_changes = false; _changes = false;
Dialog::open();
_kmPopUp->setSelected(0); _kmPopUp->setSelected(0);
loadKeymap(); loadKeymap();
refreshKeymap();
reflowActionWidgets();
Dialog::open();
} }
void RemapDialog::close() { void RemapDialog::close() {
@ -96,101 +98,54 @@ void RemapDialog::close() {
Dialog::close(); Dialog::close();
} }
void RemapDialog::reflowLayout() { void RemapDialog::reflowActionWidgets() {
Dialog::reflowLayout();
int buttonHeight = g_gui.xmlEval()->getVar("Globals.Button.Height", 0); int buttonHeight = g_gui.xmlEval()->getVar("Globals.Button.Height", 0);
int scrollbarWidth = g_gui.xmlEval()->getVar("Globals.Scrollbar.Width", 0);
int16 areaX, areaY;
uint16 areaW, areaH;
g_gui.xmlEval()->getWidgetData((const String&)String("KeyMapper.KeymapArea"), areaX, areaY, areaW, areaH);
int spacing = g_gui.xmlEval()->getVar("Globals.KeyMapper.Spacing"); int spacing = g_gui.xmlEval()->getVar("Globals.KeyMapper.Spacing");
int keyButtonWidth = g_gui.xmlEval()->getVar("Globals.KeyMapper.ButtonWidth"); int keyButtonWidth = g_gui.xmlEval()->getVar("Globals.KeyMapper.ButtonWidth");
int clearButtonWidth = g_gui.xmlEval()->getVar("Globals.Line.Height"); int clearButtonWidth = g_gui.xmlEval()->getVar("Globals.Line.Height");
int clearButtonHeight = g_gui.xmlEval()->getVar("Globals.Line.Height"); int clearButtonHeight = g_gui.xmlEval()->getVar("Globals.Line.Height");
int labelWidth = getWidth() - (spacing + keyButtonWidth + spacing + clearButtonWidth + spacing);
int colWidth = areaW - scrollbarWidth;
int labelWidth = colWidth - (keyButtonWidth + spacing + clearButtonWidth + spacing);
_rowCount = (areaH + spacing) / (buttonHeight + spacing);
debug(7, "rowCount = %d" , _rowCount);
if (colWidth <= 0 || _rowCount <= 0)
error("Remap dialog too small to display any keymaps");
_scrollBar->resize(areaX + areaW - scrollbarWidth, areaY, scrollbarWidth, areaH);
_scrollBar->_entriesPerPage = _rowCount;
_scrollBar->_numEntries = 1;
_scrollBar->recalc();
uint textYOff = (buttonHeight - kLineHeight) / 2; uint textYOff = (buttonHeight - kLineHeight) / 2;
uint clearButtonYOff = (buttonHeight - clearButtonHeight) / 2; uint clearButtonYOff = (buttonHeight - clearButtonHeight) / 2;
uint oldSize = _keymapWidgets.size();
uint newSize = _rowCount;
_keymapWidgets.reserve(newSize); for (uint i = 0; i < _actions.size(); i++) {
uint x = spacing;
uint y = spacing + (i) * (buttonHeight + spacing);
for (uint i = 0; i < newSize; i++) { ActionRow &row = _actions[i];
ActionWidgets widg; row.keyButton->resize(x, y, keyButtonWidth, buttonHeight);
row.clearButton->resize(x + keyButtonWidth + spacing, y + clearButtonYOff, clearButtonWidth, clearButtonHeight);
if (i >= _keymapWidgets.size()) { row.actionText->resize(x + keyButtonWidth + spacing + clearButtonWidth + spacing, y + textYOff, labelWidth, kLineHeight);
widg.actionText =
new GUI::StaticTextWidget(this, 0, 0, 0, 0, "", Graphics::kTextAlignLeft);
widg.keyButton =
new GUI::ButtonWidget(this, 0, 0, 0, 0, "", 0, kRemapCmd + i);
widg.clearButton = addClearButton(this, "", kClearCmd + i, 0, 0, clearButtonWidth, clearButtonHeight);
_keymapWidgets.push_back(widg);
} else {
widg = _keymapWidgets[i];
}
uint x = areaX;
uint y = areaY + (i) * (buttonHeight + spacing);
widg.keyButton->resize(x, y, keyButtonWidth, buttonHeight);
widg.clearButton->resize(x + keyButtonWidth + spacing, y + clearButtonYOff, clearButtonWidth, clearButtonHeight);
widg.actionText->resize(x + keyButtonWidth + spacing + clearButtonWidth + spacing, y + textYOff, labelWidth, kLineHeight);
}
while (oldSize > newSize) {
ActionWidgets widg = _keymapWidgets.remove_at(--oldSize);
removeWidget(widg.actionText);
delete widg.actionText;
removeWidget(widg.keyButton);
delete widg.keyButton;
removeWidget(widg.clearButton);
delete widg.clearButton;
} }
} }
void RemapDialog::handleCommand(GUI::CommandSender *sender, uint32 cmd, uint32 data) { void RemapDialog::handleCommand(GUI::CommandSender *sender, uint32 cmd, uint32 data) {
debug(3, "RemapDialog::handleCommand %u %u", cmd, data); debug(3, "RemapDialog::handleCommand %u %u", cmd, data);
if (cmd >= kRemapCmd && cmd < kRemapCmd + _keymapWidgets.size()) { if (cmd >= kRemapCmd && cmd < kRemapCmd + _actions.size()) {
startRemapping(cmd - kRemapCmd); startRemapping(cmd - kRemapCmd);
} else if (cmd >= kClearCmd && cmd < kClearCmd + _keymapWidgets.size()) { } else if (cmd >= kClearCmd && cmd < kClearCmd + _actions.size()) {
clearMapping(cmd - kClearCmd); clearMapping(cmd - kClearCmd);
} else if (cmd == GUI::kPopUpItemSelectedCmd) {
loadKeymap();
} else if (cmd == GUI::kSetPositionCmd) {
refreshKeymap();
} else if (cmd == kCloseCmd) { } else if (cmd == kCloseCmd) {
close(); close();
} else if (cmd == kReflowCmd) {
reflowActionWidgets();
} else if (cmd == GUI::kPopUpItemSelectedCmd) {
clearKeymap();
loadKeymap();
refreshKeymap();
_scrollContainer->reflowLayout();
g_gui.scheduleTopDialogRedraw();
} else { } else {
GUI::Dialog::handleCommand(sender, cmd, data); GUI::Dialog::handleCommand(sender, cmd, data);
} }
} }
void RemapDialog::clearMapping(uint i) { void RemapDialog::clearMapping(uint i) {
if (_topAction + i >= _currentActions.size())
return;
debug(3, "clear the mapping %u", i); debug(3, "clear the mapping %u", i);
Action *activeRemapAction = _currentActions[_topAction + i]; Action *activeRemapAction = _actions[i].action;
_keymapper->clearMapping(activeRemapAction); _keymapper->clearMapping(activeRemapAction);
_changes = true; _changes = true;
@ -199,25 +154,21 @@ void RemapDialog::clearMapping(uint i) {
} }
void RemapDialog::startRemapping(uint i) { void RemapDialog::startRemapping(uint i) {
if (_topAction + i >= _currentActions.size())
return;
if (_remapInputWatcher->isWatching()) { if (_remapInputWatcher->isWatching()) {
// Handle a second click on the button as a stop to remapping // Handle a second click on the button as a stop to remapping
stopRemapping(); stopRemapping();
return; return;
} }
_remapAction = _currentActions[_topAction + i]; _remapAction = _actions[i].action;
_remapTimeout = g_system->getMillis() + kRemapTimeoutDelay; _remapTimeout = g_system->getMillis() + kRemapTimeoutDelay;
_remapInputWatcher->startWatching(); _remapInputWatcher->startWatching();
_keymapWidgets[i].keyButton->setLabel("..."); _actions[i].keyButton->setLabel("...");
_keymapWidgets[i].keyButton->markAsDirty(); _actions[i].keyButton->markAsDirty();
} }
void RemapDialog::stopRemapping() { void RemapDialog::stopRemapping() {
_topAction = -1;
_remapAction = nullptr; _remapAction = nullptr;
refreshKeymap(); refreshKeymap();
@ -246,79 +197,58 @@ void RemapDialog::handleTickle() {
Dialog::handleTickle(); Dialog::handleTickle();
} }
void RemapDialog::loadKeymap() { void RemapDialog::clearKeymap() {
_currentActions.clear(); 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);
if (_kmPopUp->getSelected() != -1) { delete _actions[i].keyButton;
// This is the regular view of a keymap that isn't the topmost one. delete _actions[i].actionText;
// It shows all of that keymap's actions delete _actions[i].clearButton;
Keymap *km = _keymapTable[_kmPopUp->getSelectedTag()];
List<Action *>::iterator it;
for (it = km->getActions().begin(); it != km->getActions().end(); ++it) {
_currentActions.push_back(*it);
}
} }
// refresh scroll bar _actions.clear();
_scrollBar->_currentPos = 0; }
_scrollBar->_numEntries = _currentActions.size();
_scrollBar->recalc();
// force refresh void RemapDialog::loadKeymap() {
_topAction = -1; assert(_actions.empty());
refreshKeymap(); assert(_kmPopUp->getSelected() != -1);
Keymap *km = _keymapTable[_kmPopUp->getSelectedTag()];
for (List<Action *>::iterator it = km->getActions().begin(); it != km->getActions().end(); ++it) {
ActionRow row;
row.action = *it;
_actions.push_back(row);
}
} }
void RemapDialog::refreshKeymap() { void RemapDialog::refreshKeymap() {
int newTopAction = _scrollBar->_currentPos; int clearButtonWidth = g_gui.xmlEval()->getVar("Globals.Line.Height");
int clearButtonHeight = g_gui.xmlEval()->getVar("Globals.Line.Height");
if (newTopAction == _topAction) for (uint i = 0; i < _actions.size(); i++) {
return; ActionRow &row = _actions[i];
_topAction = newTopAction; if (!row.actionText) {
row.actionText = new GUI::StaticTextWidget(_scrollContainer, 0, 0, 0, 0, "", Graphics::kTextAlignLeft);
//_container->markAsDirty(); row.keyButton = new GUI::ButtonWidget(_scrollContainer, 0, 0, 0, 0, "", 0, kRemapCmd + i);
_scrollBar->markAsDirty(); row.clearButton = addClearButton(_scrollContainer, "", kClearCmd + i, 0, 0, clearButtonWidth, clearButtonHeight);
uint actionI = _topAction;
for (uint widgetI = 0; widgetI < _keymapWidgets.size(); widgetI++) {
ActionWidgets &widg = _keymapWidgets[widgetI];
if (actionI < _currentActions.size()) {
debug(8, "RemapDialog::refreshKeymap actionI=%u", actionI);
Action *action = _currentActions[actionI];
widg.actionText->setLabel(action->description);
Keymap *keymap = action->getParent();
const HardwareInput *mappedInput = keymap->getActionMapping(action);
if (mappedInput)
widg.keyButton->setLabel(mappedInput->description);
else
widg.keyButton->setLabel("-");
widg.actionText->setVisible(true);
widg.keyButton->setVisible(true);
widg.clearButton->setVisible(true);
actionI++;
} else {
widg.actionText->setVisible(false);
widg.keyButton->setVisible(false);
widg.clearButton->setVisible(false);
} }
row.actionText->setLabel(row.action->description);
Keymap *keymap = row.action->getParent();
const HardwareInput *mappedInput = keymap->getActionMapping(row.action);
if (mappedInput)
row.keyButton->setLabel(mappedInput->description);
else
row.keyButton->setLabel("-");
} }
// need to redraw entire Dialog so that invisible widgets disappear
g_gui.scheduleTopDialogRedraw();
} }
} // End of namespace Common } // End of namespace Common
#endif // #ifdef ENABLE_KEYMAPPER #endif // #ifdef ENABLE_KEYMAPPER

View file

@ -32,7 +32,7 @@
namespace GUI { namespace GUI {
class ButtonWidget; class ButtonWidget;
class PopUpWidget; class PopUpWidget;
class ScrollBarWidget; class ScrollContainerWidget;
class StaticTextWidget; class StaticTextWidget;
} }
@ -49,20 +49,25 @@ public:
virtual ~RemapDialog(); virtual ~RemapDialog();
virtual void open(); virtual void open();
virtual void close(); virtual void close();
virtual void reflowLayout();
virtual void handleCommand(GUI::CommandSender *sender, uint32 cmd, uint32 data); virtual void handleCommand(GUI::CommandSender *sender, uint32 cmd, uint32 data);
virtual void handleMouseDown(int x, int y, int button, int clickCount); virtual void handleMouseDown(int x, int y, int button, int clickCount);
virtual void handleTickle(); virtual void handleTickle();
protected: protected:
struct ActionWidgets { struct ActionRow {
Common::Action *action;
GUI::StaticTextWidget *actionText; GUI::StaticTextWidget *actionText;
GUI::ButtonWidget *keyButton; GUI::ButtonWidget *keyButton;
GUI::ButtonWidget *clearButton; GUI::ButtonWidget *clearButton;
ActionRow() : action(nullptr), actionText(nullptr), keyButton(nullptr), clearButton(nullptr) { }
}; };
void loadKeymap(); void loadKeymap();
void refreshKeymap(); void refreshKeymap();
void clearKeymap();
void reflowActionWidgets();
void clearMapping(uint i); void clearMapping(uint i);
void startRemapping(uint i); void startRemapping(uint i);
void stopRemapping(); void stopRemapping();
@ -74,22 +79,15 @@ protected:
Action *_remapAction; Action *_remapAction;
uint32 _remapTimeout; uint32 _remapTimeout;
Array<Action *> _currentActions;
int _topAction;
GUI::StaticTextWidget *_kmPopUpDesc; GUI::StaticTextWidget *_kmPopUpDesc;
GUI::PopUpWidget *_kmPopUp; GUI::PopUpWidget *_kmPopUp;
GUI::ScrollBarWidget *_scrollBar; GUI::ScrollContainerWidget *_scrollContainer;
uint _rowCount;
Array<ActionWidgets> _keymapWidgets;
static const uint32 kRemapTimeoutDelay = 3000; static const uint32 kRemapTimeoutDelay = 3000;
bool _changes; bool _changes;
bool _topKeymapIsGui; Array<ActionRow> _actions;
}; };
} // End of namespace Common } // End of namespace Common

View file

@ -2536,8 +2536,8 @@ const char *defaultXML1 = "<?xml version = '1.0'?>"
"</layout>" "</layout>"
"</dialog>" "</dialog>"
"<dialog name='KeyMapper' overlays='screen_center' shading='dim'>" "<dialog name='KeyMapper' overlays='screen_center' shading='dim'>"
"<layout type='vertical' padding='8,8,32,8' spacing='10' align='center'>" "<layout type='vertical' padding='8,8,8,8' spacing='10' align='center'>"
"<layout type='horizontal' padding='0,0,0,0' spacing='10' align='center'>" "<layout type='horizontal' padding='8,8,8,8' spacing='10' align='center'>"
"<widget name='PopupDesc' " "<widget name='PopupDesc' "
"type='OptionsLabel' " "type='OptionsLabel' "
"/>" "/>"
@ -2551,10 +2551,12 @@ const char *defaultXML1 = "<?xml version = '1.0'?>"
"width='600' " "width='600' "
"height='280' " "height='280' "
"/>" "/>"
"<layout type='horizontal' padding='8,8,8,8' align='center'>"
"<widget name='Close' " "<widget name='Close' "
"type='Button' " "type='Button' "
"/>" "/>"
"</layout>" "</layout>"
"</layout>"
"</dialog>" "</dialog>"
"<dialog name='Predictive' overlays='screen_center'>" "<dialog name='Predictive' overlays='screen_center'>"
"<layout type='vertical' padding='5,5,5,5' align='center'>" "<layout type='vertical' padding='5,5,5,5' align='center'>"
@ -4281,10 +4283,12 @@ const char *defaultXML1 = "<?xml version = '1.0'?>"
"width='300' " "width='300' "
"height='120' " "height='120' "
"/>" "/>"
"<layout type='horizontal' padding='8,8,8,8' align='center'>"
"<widget name='Close' " "<widget name='Close' "
"type='Button' " "type='Button' "
"/>" "/>"
"</layout>" "</layout>"
"</layout>"
"</dialog>" "</dialog>"
"<dialog name='Predictive' overlays='screen_center'>" "<dialog name='Predictive' overlays='screen_center'>"
"<layout type='vertical' padding='1,1,1,1' align='center'>" "<layout type='vertical' padding='1,1,1,1' align='center'>"

Binary file not shown.

View file

@ -1705,8 +1705,8 @@
</dialog> </dialog>
<dialog name = 'KeyMapper' overlays = 'screen_center' shading = 'dim'> <dialog name = 'KeyMapper' overlays = 'screen_center' shading = 'dim'>
<layout type = 'vertical' padding = '8, 8, 32, 8' spacing = '10' align = 'center'> <layout type = 'vertical' padding = '8, 8, 8, 8' spacing = '10' align = 'center'>
<layout type = 'horizontal' padding = '0, 0, 0, 0' spacing = '10' align = 'center'> <layout type = 'horizontal' padding = '8, 8, 8, 8' spacing = '10' align = 'center'>
<widget name = 'PopupDesc' <widget name = 'PopupDesc'
type = 'OptionsLabel' type = 'OptionsLabel'
/> />
@ -1720,9 +1720,11 @@
width = '600' width = '600'
height = '280' height = '280'
/> />
<widget name = 'Close' <layout type = 'horizontal' padding = '8, 8, 8, 8' align = 'center'>
type = 'Button' <widget name = 'Close'
/> type = 'Button'
/>
</layout>
</layout> </layout>
</dialog> </dialog>

View file

@ -1689,9 +1689,11 @@
width = '300' width = '300'
height = '120' height = '120'
/> />
<widget name = 'Close' <layout type = 'horizontal' padding = '8, 8, 8, 8' align = 'center'>
type = 'Button' <widget name = 'Close'
/> type = 'Button'
/>
</layout>
</layout> </layout>
</dialog> </dialog>

Binary file not shown.

View file

@ -1719,8 +1719,8 @@
</dialog> </dialog>
<dialog name = 'KeyMapper' overlays = 'screen_center' shading = 'dim'> <dialog name = 'KeyMapper' overlays = 'screen_center' shading = 'dim'>
<layout type = 'vertical' padding = '8, 8, 32, 8' spacing = '10' align = 'center'> <layout type = 'vertical' padding = '8, 8, 8, 8' spacing = '10' align = 'center'>
<layout type = 'horizontal' padding = '0, 0, 0, 0' spacing = '10' align = 'center'> <layout type = 'horizontal' padding = '8, 8, 8, 8' spacing = '10' align = 'center'>
<widget name = 'PopupDesc' <widget name = 'PopupDesc'
type = 'OptionsLabel' type = 'OptionsLabel'
/> />
@ -1734,11 +1734,14 @@
width = '600' width = '600'
height = '280' height = '280'
/> />
<widget name = 'Close' <layout type = 'horizontal' padding = '8, 8, 8, 8' align = 'center'>
type = 'Button' <widget name = 'Close'
/> type = 'Button'
/>
</layout>
</layout> </layout>
</dialog> </dialog>
<dialog name = 'Predictive' overlays = 'screen_center'> <dialog name = 'Predictive' overlays = 'screen_center'>
<layout type = 'vertical' padding = '5, 5, 5, 5' align = 'center'> <layout type = 'vertical' padding = '5, 5, 5, 5' align = 'center'>
<widget name = 'Headline' <widget name = 'Headline'

View file

@ -1708,9 +1708,11 @@
width = '300' width = '300'
height = '120' height = '120'
/> />
<widget name = 'Close' <layout type = 'horizontal' padding = '8, 8, 8, 8' align = 'center'>
type = 'Button' <widget name = 'Close'
/> type = 'Button'
/>
</layout>
</layout> </layout>
</dialog> </dialog>
<dialog name = 'Predictive' overlays = 'screen_center'> <dialog name = 'Predictive' overlays = 'screen_center'>

Binary file not shown.

View file

@ -1719,8 +1719,8 @@
</dialog> </dialog>
<dialog name = 'KeyMapper' overlays = 'screen_center' shading = 'dim'> <dialog name = 'KeyMapper' overlays = 'screen_center' shading = 'dim'>
<layout type = 'vertical' padding = '8, 8, 32, 8' spacing = '10' align = 'center'> <layout type = 'vertical' padding = '8, 8, 8, 8' spacing = '10' align = 'center'>
<layout type = 'horizontal' padding = '0, 0, 0, 0' spacing = '10' align = 'center'> <layout type = 'horizontal' padding = '8, 8, 8, 8' spacing = '10' align = 'center'>
<widget name = 'PopupDesc' <widget name = 'PopupDesc'
type = 'OptionsLabel' type = 'OptionsLabel'
/> />
@ -1734,11 +1734,14 @@
width = '600' width = '600'
height = '280' height = '280'
/> />
<widget name = 'Close' <layout type = 'horizontal' padding = '8, 8, 8, 8' align = 'center'>
type = 'Button' <widget name = 'Close'
/> type = 'Button'
/>
</layout>
</layout> </layout>
</dialog> </dialog>
<dialog name = 'Predictive' overlays = 'screen_center'> <dialog name = 'Predictive' overlays = 'screen_center'>
<layout type = 'vertical' padding = '5, 5, 5, 5' align = 'center'> <layout type = 'vertical' padding = '5, 5, 5, 5' align = 'center'>
<widget name = 'Headline' <widget name = 'Headline'