implement auto repeat configuration for key press events, to give more flexibility

This commit is contained in:
Eduardo Bart 2012-02-06 10:53:28 -02:00
parent f97104f12e
commit add8505a5b
18 changed files with 68 additions and 198 deletions

View File

@ -6,14 +6,13 @@ local loadBox
local characterList
-- private functions
local function onCharactersWindowKeyPress(self, keyCode, keyboardModifiers, wouldFilter)
if wouldFilter then return end
local function onCharactersWindowKeyPress(self, keyCode, keyboardModifiers)
if keyboardModifiers == KeyboardNoModifier then
if keyCode == KeyUp then
characterList:focusPreviousChild(ActiveFocusReason)
characterList:focusPreviousChild(KeyboardFocusReason)
return true
elseif keyCode == KeyDown or keyCode == KeyTab then
characterList:focusNextChild(ActiveFocusReason)
characterList:focusNextChild(KeyboardFocusReason)
return true
end
end

View File

@ -13,6 +13,9 @@ function ModuleManager.init()
ModuleManager.updateModuleInfo(focusedChild:getText())
end })
Keyboard.bindKeyPress('Up', function() moduleList:focusPreviousChild(KeyboardFocusReason) end, moduleManagerWindow)
Keyboard.bindKeyPress('Down', function() moduleList:focusNextChild(KeyboardFocusReason) end, moduleManagerWindow)
moduleManagerButton = TopMenu.addButton('moduleManagerButton', 'Module manager', 'modulemanager.png', ModuleManager.toggle)
addEvent(ModuleManager.listModules)

View File

@ -1,134 +0,0 @@
Hotkeys = {}
-- private functions
local function translateKeyCombo(keyCombo)
if not keyCombo or #keyCombo == 0 then return nil end
table.sort(keyCombo)
local keyComboDesc = ''
for k,v in pairs(keyCombo) do
local keyDesc = KeyCodeDescs[v]
if keyDesc == nil then return nil end
keyComboDesc = keyComboDesc .. '+' .. keyDesc
end
keyComboDesc = keyComboDesc:sub(2)
return keyComboDesc
end
local function retranslateKeyComboDesc(keyComboDesc)
if keyComboDesc == nil then return nil end
local keyCombo = {}
for i,currentKeyDesc in ipairs(keyComboDesc:split('+')) do
for keyCode, keyDesc in pairs(KeyCodeDescs) do
if keyDesc:lower() == currentKeyDesc:trim():lower() then
table.insert(keyCombo, keyCode)
end
end
end
return translateKeyCombo(keyCombo)
end
local function determineKeyComboDesc(keyCode, keyboardModifiers)
local keyCombo = {}
if keyCode == KeyCtrl or keyCode == KeyShift or keyCode == KeyAlt then
table.insert(keyCombo, keyCode)
elseif KeyCodeDescs[keyCode] ~= nil then
if keyboardModifiers == KeyboardCtrlModifier then
table.insert(keyCombo, KeyCtrl)
elseif keyboardModifiers == KeyboardAltModifier then
table.insert(keyCombo, KeyAlt)
elseif keyboardModifiers == KeyboardCtrlAltModifier then
table.insert(keyCombo, KeyCtrl)
table.insert(keyCombo, KeyAlt)
elseif keyboardModifiers == KeyboardShiftModifier then
table.insert(keyCombo, KeyShift)
elseif keyboardModifiers == KeyboardCtrlShiftModifier then
table.insert(keyCombo, KeyCtrl)
table.insert(keyCombo, KeyShift)
elseif keyboardModifiers == KeyboardAltShiftModifier then
table.insert(keyCombo, KeyAlt)
table.insert(keyCombo, KeyShift)
elseif keyboardModifiers == KeyboardCtrlAltShiftModifier then
table.insert(keyCombo, KeyCtrl)
table.insert(keyCombo, KeyAlt)
table.insert(keyCombo, KeyShift)
end
table.insert(keyCombo, keyCode)
end
table.sort(keyCombo)
return translateKeyCombo(keyCombo)
end
local function onWidgetKeyDown(widget, keyCode, keyboardModifiers)
if keyCode == KeyUnknown then return end
local keyComboDesc = determineKeyComboDesc(keyCode, keyboardModifiers)
local callback = widget.boundKeyDownCombos[keyComboDesc]
if callback then
callback()
return true
end
return false
end
local function onWidgetKeyPress(widget, keyCode, keyboardModifiers, wouldFilter)
local keyComboDesc = determineKeyComboDesc(keyCode, keyboardModifiers)
if keyCode == KeyUnknown then return end
local callback = widget.boundKeyPressCombos[keyComboDesc]
if callback then
callback()
return true
end
return false
end
local function connectKeyDownEvent(widget)
if widget.boundKeyDownCombos then return end
connect(widget, { onKeyDown = onWidgetKeyDown })
widget.boundKeyDownCombos = {}
end
local function connectKeyPressEvent(widget)
if widget.boundKeyPressCombos then return end
connect(widget, { onKeyPress = onWidgetKeyPress })
widget.boundKeyPressCombos = {}
end
-- public functions
function Hotkeys.bindKeyDown(keyComboDesc, callback, widget)
widget = widget or rootWidget
connectKeyDownEvent(widget)
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
if keyComboDesc then
widget.boundKeyDownCombos[keyComboDesc] = callback
else
error('key combo \'' .. keyComboDesc .. '\' is failed')
end
end
function Hotkeys.bindKeyPress(keyComboDesc, callback, widget)
widget = widget or rootWidget
connectKeyPressEvent(widget)
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
if keyComboDesc then
widget.boundKeyPressCombos[keyComboDesc] = callback
else
error('key combo \'' .. keyComboDesc .. '\' is failed')
end
end
function Hotkeys.unbindKeyDown(keyComboDesc, widget)
widget = widget or rootWidget
if widget.boundKeyDownCombos == nil then return end
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
if keyComboDesc then
widget.boundKeyDownCombos[keyComboDesc] = nil
end
end
function Hotkeys.unbindKeyPress(keyComboDesc, widget)
widget = widget or rootWidget
if widget.boundKeyPressCombos == nil then return end
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
if keyComboDesc then
widget.boundKeyPressCombos[keyComboDesc] = nil
end
end

View File

@ -59,7 +59,7 @@ local function determineKeyComboDesc(keyCode, keyboardModifiers)
end
local function onWidgetKeyDown(widget, keyCode, keyboardModifiers)
if keyCode == KeyUnknown then return end
if keyCode == KeyUnknown then return false end
local keyComboDesc = determineKeyComboDesc(keyCode, keyboardModifiers)
local callback = widget.boundKeyDownCombos[keyComboDesc]
if callback then
@ -69,12 +69,12 @@ local function onWidgetKeyDown(widget, keyCode, keyboardModifiers)
return false
end
local function onWidgetKeyPress(widget, keyCode, keyboardModifiers, wouldFilter)
local function onWidgetKeyPress(widget, keyCode, keyboardModifiers, autoRepeatTicks)
local keyComboDesc = determineKeyComboDesc(keyCode, keyboardModifiers)
if keyCode == KeyUnknown then return end
local callback = widget.boundKeyPressCombos[keyComboDesc]
if callback then
callback()
if keyCode == KeyUnknown then return false end
local comboConf = widget.boundKeyPressCombos[keyComboDesc]
if comboConf and (autoRepeatTicks >= comboConf.autoRepeatDelay or autoRepeatTicks == 0) and comboConf.callback then
comboConf.callback()
return true
end
return false
@ -104,12 +104,14 @@ function Keyboard.bindKeyDown(keyComboDesc, callback, widget)
end
end
function Keyboard.bindKeyPress(keyComboDesc, callback, widget)
function Keyboard.bindKeyPress(keyComboDesc, callback, widget, autoRepeatDelay)
autoRepeatDelay = autoRepeatDelay or 500
widget = widget or rootWidget
connectKeyPressEvent(widget)
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
if keyComboDesc then
widget.boundKeyPressCombos[keyComboDesc] = callback
widget.boundKeyPressCombos[keyComboDesc] = { callback = callback, autoRepeatDelay = autoRepeatDelay }
widget:setAutoRepeatDelay(math.min(autoRepeatDelay, widget:getAutoRepeatDelay()))
else
error('key combo \'' .. keyComboDesc .. '\' is failed')
end

View File

@ -56,8 +56,7 @@ function UIPopupMenu:onMousePress(mousePos, mouseButton)
return false
end
function UIPopupMenu:onKeyPress(keyCode, keyboardModifiers, wouldFilter)
if wouldFilter then return end
function UIPopupMenu:onKeyPress(keyCode, keyboardModifiers)
if keyCode == KeyEscape then
self:destroy()
return true

View File

@ -6,8 +6,7 @@ function UIWindow.create()
return window
end
function UIWindow:onKeyPress(keyCode, keyboardModifiers, wouldFilter)
if wouldFilter then return end
function UIWindow:onKeyPress(keyCode, keyboardModifiers)
if keyboardModifiers == KeyboardNoModifier then
if keyCode == KeyEnter then
signalcall(self.onEnter, self)

View File

@ -2,8 +2,7 @@
local m_mouseGrabberWidget
-- private functions
local function onGameKeyPress(self, keyCode, keyboardModifiers, wouldFilter)
if wouldFilter then return end
local function onGameKeyPress(self, keyCode, keyboardModifiers)
if keyboardModifiers == KeyboardCtrlModifier then
if keyCode == KeyG then
CharacterList.show()

View File

@ -37,8 +37,8 @@ struct InputEvent {
mouseButton = Fw::MouseNoButton;
keyCode = Fw::KeyUnknown;
keyText = "";
autoRepeatTicks = 0;
mouseMoved = Point();
wouldFilter = false;
};
Fw::InputEventType type;
@ -49,7 +49,7 @@ struct InputEvent {
int keyboardModifiers;
Point mousePos;
Point mouseMoved;
bool wouldFilter;
int autoRepeatTicks;
};
#endif

View File

@ -101,6 +101,7 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UIWidget>("setPhantom", &UIWidget::setPhantom);
g_lua.bindClassMemberFunction<UIWidget>("setFixedSize", &UIWidget::setFixedSize);
g_lua.bindClassMemberFunction<UIWidget>("setLastFocusReason", &UIWidget::setLastFocusReason);
g_lua.bindClassMemberFunction<UIWidget>("setAutoRepeatDelay", &UIWidget::setAutoRepeatDelay);
g_lua.bindClassMemberFunction<UIWidget>("isVisible", &UIWidget::isVisible);
g_lua.bindClassMemberFunction<UIWidget>("isChildLocked", &UIWidget::isChildLocked);
g_lua.bindClassMemberFunction<UIWidget>("hasChild", &UIWidget::hasChild);
@ -154,6 +155,7 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UIWidget>("getStyle", &UIWidget::getStyle);
g_lua.bindClassMemberFunction<UIWidget>("getChildCount", &UIWidget::getChildCount);
g_lua.bindClassMemberFunction<UIWidget>("getLastFocusReason", &UIWidget::getLastFocusReason);
g_lua.bindClassMemberFunction<UIWidget>("getAutoRepeatDelay", &UIWidget::getAutoRepeatDelay);
g_lua.bindClassMemberFunction<UIWidget>("getStyleName", &UIWidget::getStyleName);
g_lua.bindClassMemberFunction<UIWidget>("setX", &UIWidget::setX);
g_lua.bindClassMemberFunction<UIWidget>("setY", &UIWidget::setY);

View File

@ -58,7 +58,7 @@ void PlatformWindow::processKeyDown(Fw::Key keyCode)
else if(keyCode == Fw::KeyShift)
m_inputEvent.keyboardModifiers |= Fw::KeyboardShiftModifier;
m_inputEvent.reset();
m_inputEvent.reset(Fw::KeyDownInputEvent);
m_inputEvent.type = Fw::KeyDownInputEvent;
m_inputEvent.keyCode = keyCode;
@ -126,7 +126,7 @@ void PlatformWindow::fireKeysPress()
if(m_onInputEvent) {
m_inputEvent.reset(Fw::KeyPressInputEvent);
m_inputEvent.keyCode = keyCode;
m_inputEvent.wouldFilter = g_clock.ticksElapsed(firstKeyPress) < KEY_PRESS_REPEAT_DELAY;
m_inputEvent.autoRepeatTicks = g_clock.ticksElapsed(firstKeyPress);
m_onInputEvent(m_inputEvent);
}
m_lastKeysPress[keyCode] = g_clock.ticks();

View File

@ -31,7 +31,6 @@ class PlatformWindow
{
enum {
KEY_PRESS_REPEAT_INTERVAL = 30,
KEY_PRESS_REPEAT_DELAY = 500
};
typedef std::function<void(const Size&)> OnResizeCallback;

View File

@ -429,36 +429,33 @@ void UILineEdit::onFocusChange(bool focused, Fw::FocusReason reason)
UIWidget::onFocusChange(focused, reason);
}
bool UILineEdit::onKeyPress(uchar keyCode, int keyboardModifiers, bool wouldFilter)
bool UILineEdit::onKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeatTicks)
{
if(UIWidget::onKeyPress(keyCode, keyboardModifiers, wouldFilter))
if(UIWidget::onKeyPress(keyCode, keyboardModifiers, autoRepeatTicks))
return true;
if(!wouldFilter) {
if(keyCode == Fw::KeyDelete) // erase right character
removeCharacter(true);
else if(keyCode == Fw::KeyBackspace) // erase left character {
removeCharacter(false);
else if(keyCode == Fw::KeyRight) // move cursor right
moveCursor(true);
else if(keyCode == Fw::KeyLeft) // move cursor left
moveCursor(false);
else if(keyCode == Fw::KeyHome) // move cursor to first character
setCursorPos(0);
else if(keyCode == Fw::KeyEnd) // move cursor to last character
setCursorPos(m_text.length());
else if(keyCode == Fw::KeyV && keyboardModifiers == Fw::KeyboardCtrlModifier)
appendText(g_window.getClipboardText());
else if(keyCode == Fw::KeyTab) {
if(!m_alwaysActive) {
if(UIWidgetPtr parent = getParent())
parent->focusNextChild(Fw::KeyboardFocusReason);
}
} else
return false;
return true;
}
return false;
if(keyCode == Fw::KeyDelete) // erase right character
removeCharacter(true);
else if(keyCode == Fw::KeyBackspace) // erase left character {
removeCharacter(false);
else if(keyCode == Fw::KeyRight) // move cursor right
moveCursor(true);
else if(keyCode == Fw::KeyLeft) // move cursor left
moveCursor(false);
else if(keyCode == Fw::KeyHome) // move cursor to first character
setCursorPos(0);
else if(keyCode == Fw::KeyEnd) // move cursor to last character
setCursorPos(m_text.length());
else if(keyCode == Fw::KeyV && keyboardModifiers == Fw::KeyboardCtrlModifier)
appendText(g_window.getClipboardText());
else if(keyCode == Fw::KeyTab) {
if(!m_alwaysActive) {
if(UIWidgetPtr parent = getParent())
parent->focusNextChild(Fw::KeyboardFocusReason);
}
} else
return false;
return true;
}
bool UILineEdit::onKeyText(const std::string& keyText)

View File

@ -63,7 +63,7 @@ protected:
virtual void onGeometryChange(const Rect& oldRect, const Rect& newRect);
virtual void onFocusChange(bool focused, Fw::FocusReason reason);
virtual bool onKeyText(const std::string& keyText);
virtual bool onKeyPress(uchar keyCode, int keyboardModifiers, bool wouldFilter);
virtual bool onKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeatTicks);
virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
private:

View File

@ -69,7 +69,7 @@ void UIManager::inputEvent(const InputEvent& event)
m_keyboardReceiver->propagateOnKeyDown(event.keyCode, event.keyboardModifiers);
break;
case Fw::KeyPressInputEvent:
m_keyboardReceiver->propagateOnKeyPress(event.keyCode, event.keyboardModifiers, event.wouldFilter);
m_keyboardReceiver->propagateOnKeyPress(event.keyCode, event.keyboardModifiers, event.autoRepeatTicks);
break;
case Fw::KeyUpInputEvent:
m_keyboardReceiver->propagateOnKeyUp(event.keyCode, event.keyboardModifiers);

View File

@ -36,6 +36,7 @@ UIWidget::UIWidget()
m_lastFocusReason = Fw::ActiveFocusReason;
m_states = Fw::DefaultState;
m_clickTimer.stop();
m_autoRepeatDelay = 500;
initBaseStyle();
initText();
@ -1152,9 +1153,9 @@ bool UIWidget::onKeyDown(uchar keyCode, int keyboardModifiers)
return callLuaField<bool>("onKeyDown", keyCode, keyboardModifiers);
}
bool UIWidget::onKeyPress(uchar keyCode, int keyboardModifiers, bool wouldFilter)
bool UIWidget::onKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeatTicks)
{
return callLuaField<bool>("onKeyPress", keyCode, keyboardModifiers, wouldFilter);
return callLuaField<bool>("onKeyPress", keyCode, keyboardModifiers, autoRepeatTicks);
}
bool UIWidget::onKeyUp(uchar keyCode, int keyboardModifiers)
@ -1260,7 +1261,7 @@ bool UIWidget::propagateOnKeyDown(uchar keyCode, int keyboardModifiers)
return onKeyDown(keyCode, keyboardModifiers);
}
bool UIWidget::propagateOnKeyPress(uchar keyCode, int keyboardModifiers, bool wouldFilter)
bool UIWidget::propagateOnKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeatTicks)
{
// do a backup of children list, because it may change while looping it
UIWidgetList children;
@ -1275,11 +1276,14 @@ bool UIWidget::propagateOnKeyPress(uchar keyCode, int keyboardModifiers, bool wo
}
for(const UIWidgetPtr& child : children) {
if(child->propagateOnKeyPress(keyCode, keyboardModifiers, wouldFilter))
if(child->propagateOnKeyPress(keyCode, keyboardModifiers, autoRepeatTicks))
return true;
}
return onKeyPress(keyCode, keyboardModifiers, wouldFilter);
if(autoRepeatTicks == 0 || autoRepeatTicks >= m_autoRepeatDelay) {
return onKeyPress(keyCode, keyboardModifiers, autoRepeatTicks);
} else
return false;
}
bool UIWidget::propagateOnKeyUp(uchar keyCode, int keyboardModifiers)

View File

@ -124,6 +124,7 @@ public:
void setDragable(bool dragable);
void setFixedSize(bool fixed);
void setLastFocusReason(Fw::FocusReason reason);
void setAutoRepeatDelay(int delay) { m_autoRepeatDelay = delay; }
bool isVisible();
bool isChildLocked(const UIWidgetPtr& child);
@ -177,7 +178,7 @@ protected:
virtual void onDrop(UIWidgetPtr draggedWidget, const Point& mousePos);
virtual bool onKeyText(const std::string& keyText);
virtual bool onKeyDown(uchar keyCode, int keyboardModifiers);
virtual bool onKeyPress(uchar keyCode, int keyboardModifiers, bool wouldFilter);
virtual bool onKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeatTicks);
virtual bool onKeyUp(uchar keyCode, int keyboardModifiers);
virtual bool onMousePress(const Point& mousePos, Fw::MouseButton button);
virtual bool onMouseRelease(const Point& mousePos, Fw::MouseButton button);
@ -188,7 +189,7 @@ protected:
bool propagateOnKeyText(const std::string& keyText);
bool propagateOnKeyDown(uchar keyCode, int keyboardModifiers);
bool propagateOnKeyPress(uchar keyCode, int keyboardModifiers, bool wouldFilter);
bool propagateOnKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeatTicks);
bool propagateOnKeyUp(uchar keyCode, int keyboardModifiers);
bool propagateOnMousePress(const Point& mousePos, Fw::MouseButton button);
bool propagateOnMouseRelease(const Point& mousePos, Fw::MouseButton button);
@ -240,6 +241,7 @@ public:
OTMLNodePtr getStyle() { return m_style; }
int getChildCount() { return m_children.size(); }
Fw::FocusReason getLastFocusReason() { return m_lastFocusReason; }
int getAutoRepeatDelay() { return m_autoRepeatDelay; }
std::string getStyleName() { return m_style->tag(); }
@ -264,6 +266,7 @@ protected:
EdgeGroup<int> m_margin;
EdgeGroup<int> m_padding;
float m_opacity;
int m_autoRepeatDelay;
public:
void setX(int x) { move(x, getY()); }

View File

@ -25,9 +25,9 @@
#include <framework/ui/uilineedit.h>
#include <framework/platform/platformwindow.h>
bool UIGame::onKeyPress(uchar keyCode, int keyboardModifiers, bool wouldFilter)
bool UIGame::onKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeatTicks)
{
if(UIWidget::onKeyPress(keyCode, keyboardModifiers, wouldFilter))
if(UIWidget::onKeyPress(keyCode, keyboardModifiers, autoRepeatTicks))
return true;
UILineEditPtr chatLineEdit = std::dynamic_pointer_cast<UILineEdit>(getParent()->recursiveGetChildById("consoleLineEdit"));
@ -58,8 +58,6 @@ bool UIGame::onKeyPress(uchar keyCode, int keyboardModifiers, bool wouldFilter)
} else if(keyCode == Fw::KeyNumpad7) {
g_game.walk(Otc::NorthWest);
return true;
} else if(wouldFilter) {
return false;
} else if(keyCode == Fw::KeyDelete) {
chatLineEdit->removeCharacter(true);
return true;

View File

@ -29,7 +29,7 @@
class UIGame : public UIWidget
{
protected:
bool onKeyPress(uchar keyCode, int keyboardModifiers, bool wouldFilter);
bool onKeyPress(uchar keyCode, int keyboardModifiers, int autoRepeatTicks);
bool onKeyText(const std::string& keyText);
};