More improvements in walk

master
Eduardo Bart 11 years ago
parent 4b1db2bcd6
commit 4351f3c63f

@ -71,8 +71,11 @@ end
local function onWidgetKeyDown(widget, keyCode, keyboardModifiers) local function onWidgetKeyDown(widget, keyCode, keyboardModifiers)
if keyCode == KeyUnknown then return false end if keyCode == KeyUnknown then return false end
local keyComboDesc = determineKeyComboDesc(keyCode, keyboardModifiers) local callback = widget.boundAloneKeyDownCombos[determineKeyComboDesc(keyCode, KeyboardNoModifier)]
local callback = widget.boundKeyDownCombos[keyComboDesc] if callback then
callback(widget, keyCode)
end
callback = widget.boundKeyDownCombos[determineKeyComboDesc(keyCode, keyboardModifiers)]
if callback then if callback then
callback(widget, keyCode) callback(widget, keyCode)
return true return true
@ -82,8 +85,11 @@ end
local function onWidgetKeyUp(widget, keyCode, keyboardModifiers) local function onWidgetKeyUp(widget, keyCode, keyboardModifiers)
if keyCode == KeyUnknown then return false end if keyCode == KeyUnknown then return false end
local keyComboDesc = determineKeyComboDesc(keyCode, keyboardModifiers) local callback = widget.boundAloneKeyUpCombos[determineKeyComboDesc(keyCode, KeyboardNoModifier)]
local callback = widget.boundKeyUpCombos[keyComboDesc] if callback then
callback(widget, keyCode)
end
callback = widget.boundKeyUpCombos[determineKeyComboDesc(keyCode, keyboardModifiers)]
if callback then if callback then
callback(widget, keyCode) callback(widget, keyCode)
return true return true
@ -106,12 +112,14 @@ local function connectKeyDownEvent(widget)
if widget.boundKeyDownCombos then return end if widget.boundKeyDownCombos then return end
connect(widget, { onKeyDown = onWidgetKeyDown }) connect(widget, { onKeyDown = onWidgetKeyDown })
widget.boundKeyDownCombos = {} widget.boundKeyDownCombos = {}
widget.boundAloneKeyDownCombos = {}
end end
local function connectKeyUpEvent(widget) local function connectKeyUpEvent(widget)
if widget.boundKeyUpCombos then return end if widget.boundKeyUpCombos then return end
connect(widget, { onKeyUp = onWidgetKeyUp }) connect(widget, { onKeyUp = onWidgetKeyUp })
widget.boundKeyUpCombos = {} widget.boundKeyUpCombos = {}
widget.boundAloneKeyUpCombos = {}
end end
local function connectKeyPressEvent(widget) local function connectKeyPressEvent(widget)
@ -121,24 +129,32 @@ local function connectKeyPressEvent(widget)
end end
-- public functions -- public functions
function g_keyboard.bindKeyDown(keyComboDesc, callback, widget) function g_keyboard.bindKeyDown(keyComboDesc, callback, widget, alone)
widget = widget or rootWidget widget = widget or rootWidget
connectKeyDownEvent(widget) connectKeyDownEvent(widget)
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc) local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
if widget.boundKeyDownCombos[keyComboDesc] then if widget.boundKeyDownCombos[keyComboDesc] then
pwarning('KeyDown event \'' .. keyComboDesc .. '\' redefined on widget ' .. widget:getId()) pwarning('KeyDown event \'' .. keyComboDesc .. '\' redefined on widget ' .. widget:getId())
end end
widget.boundKeyDownCombos[keyComboDesc] = callback if alone then
widget.boundAloneKeyDownCombos[keyComboDesc] = callback
else
widget.boundKeyDownCombos[keyComboDesc] = callback
end
end end
function g_keyboard.bindKeyUp(keyComboDesc, callback, widget) function g_keyboard.bindKeyUp(keyComboDesc, callback, widget, alone)
widget = widget or rootWidget widget = widget or rootWidget
connectKeyUpEvent(widget) connectKeyUpEvent(widget)
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc) local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
if widget.boundKeyUpCombos[keyComboDesc] then if widget.boundKeyUpCombos[keyComboDesc] then
pwarning('KeyUp event \'' .. keyComboDesc .. '\' redefined on widget ' .. widget:getId()) pwarning('KeyUp event \'' .. keyComboDesc .. '\' redefined on widget ' .. widget:getId())
end end
widget.boundKeyUpCombos[keyComboDesc] = callback if alone then
widget.boundAloneKeyUpCombos[keyComboDesc] = callback
else
widget.boundKeyUpCombos[keyComboDesc] = callback
end
end end
function g_keyboard.bindKeyPress(keyComboDesc, callback, widget, autoRepeatDelay) function g_keyboard.bindKeyPress(keyComboDesc, callback, widget, autoRepeatDelay)

@ -76,9 +76,10 @@ function table.removevalue(t, value)
for k,v in pairs(t) do for k,v in pairs(t) do
if v == value then if v == value then
table.remove(t, k) table.remove(t, k)
break return true
end end
end end
return false
end end
function table.popvalue(value) function table.popvalue(value)

@ -1,4 +1,4 @@
WALK_REPEAT_DELAY = 60 WALK_REPEAT_DELAY = 90
WALK_STEPS_RETRY = 10 WALK_STEPS_RETRY = 10
gameRootPanel = nil gameRootPanel = nil
@ -31,7 +31,7 @@ function init()
gameRootPanel:hide() gameRootPanel:hide()
gameRootPanel:lower() gameRootPanel:lower()
gameRootPanel.onGeometryChange = updateStretchShrink gameRootPanel.onGeometryChange = updateStretchShrink
gameRootPanel.onFocusChange = cancelSmartWalk gameRootPanel.onFocusChange = stopSmartWalk
mouseGrabberWidget = gameRootPanel:getChildById('mouseGrabber') mouseGrabberWidget = gameRootPanel:getChildById('mouseGrabber')
mouseGrabberWidget.onMouseRelease = onMouseGrabberRelease mouseGrabberWidget.onMouseRelease = onMouseGrabberRelease
@ -55,35 +55,35 @@ function init()
end end
function bindKeys() function bindKeys()
g_keyboard.bindKeyDown('Up', function() changeWalkDir(North) end, gameRootPanel) g_keyboard.bindKeyDown('Up', function() changeWalkDir(North) end, gameRootPanel, true)
g_keyboard.bindKeyDown('Right', function() changeWalkDir(East) end, gameRootPanel) g_keyboard.bindKeyDown('Right', function() changeWalkDir(East) end, gameRootPanel, true)
g_keyboard.bindKeyDown('Down', function() changeWalkDir(South) end, gameRootPanel) g_keyboard.bindKeyDown('Down', function() changeWalkDir(South) end, gameRootPanel, true)
g_keyboard.bindKeyDown('Left', function() changeWalkDir(West) end, gameRootPanel) g_keyboard.bindKeyDown('Left', function() changeWalkDir(West) end, gameRootPanel, true)
g_keyboard.bindKeyDown('Numpad8', function() changeWalkDir(North) end, gameRootPanel) g_keyboard.bindKeyDown('Numpad8', function() changeWalkDir(North) end, gameRootPanel, true)
g_keyboard.bindKeyDown('Numpad9', function() changeWalkDir(NorthEast) end, gameRootPanel) g_keyboard.bindKeyDown('Numpad9', function() changeWalkDir(NorthEast) end, gameRootPanel, true)
g_keyboard.bindKeyDown('Numpad6', function() changeWalkDir(East) end, gameRootPanel) g_keyboard.bindKeyDown('Numpad6', function() changeWalkDir(East) end, gameRootPanel, true)
g_keyboard.bindKeyDown('Numpad3', function() changeWalkDir(SouthEast) end, gameRootPanel) g_keyboard.bindKeyDown('Numpad3', function() changeWalkDir(SouthEast) end, gameRootPanel, true)
g_keyboard.bindKeyDown('Numpad2', function() changeWalkDir(South) end, gameRootPanel) g_keyboard.bindKeyDown('Numpad2', function() changeWalkDir(South) end, gameRootPanel, true)
g_keyboard.bindKeyDown('Numpad1', function() changeWalkDir(SouthWest) end, gameRootPanel) g_keyboard.bindKeyDown('Numpad1', function() changeWalkDir(SouthWest) end, gameRootPanel, true)
g_keyboard.bindKeyDown('Numpad4', function() changeWalkDir(West) end, gameRootPanel) g_keyboard.bindKeyDown('Numpad4', function() changeWalkDir(West) end, gameRootPanel, true)
g_keyboard.bindKeyDown('Numpad7', function() changeWalkDir(NorthWest) end, gameRootPanel) g_keyboard.bindKeyDown('Numpad7', function() changeWalkDir(NorthWest) end, gameRootPanel, true)
g_keyboard.bindKeyUp('Up', function() changeWalkDir(North, true) end, gameRootPanel) g_keyboard.bindKeyUp('Up', function() changeWalkDir(North, true) end, gameRootPanel, true)
g_keyboard.bindKeyUp('Right', function() changeWalkDir(East, true) end, gameRootPanel) g_keyboard.bindKeyUp('Right', function() changeWalkDir(East, true) end, gameRootPanel, true)
g_keyboard.bindKeyUp('Down', function() changeWalkDir(South, true) end, gameRootPanel) g_keyboard.bindKeyUp('Down', function() changeWalkDir(South, true) end, gameRootPanel, true)
g_keyboard.bindKeyUp('Left', function() changeWalkDir(West, true) end, gameRootPanel) g_keyboard.bindKeyUp('Left', function() changeWalkDir(West, true) end, gameRootPanel, true)
g_keyboard.bindKeyUp('Numpad8', function() changeWalkDir(North, true) end, gameRootPanel) g_keyboard.bindKeyUp('Numpad8', function() changeWalkDir(North, true) end, gameRootPanel, true)
g_keyboard.bindKeyUp('Numpad9', function() changeWalkDir(NorthEast, true) end, gameRootPanel) g_keyboard.bindKeyUp('Numpad9', function() changeWalkDir(NorthEast, true) end, gameRootPanel, true)
g_keyboard.bindKeyUp('Numpad6', function() changeWalkDir(East, true) end, gameRootPanel) g_keyboard.bindKeyUp('Numpad6', function() changeWalkDir(East, true) end, gameRootPanel, true)
g_keyboard.bindKeyUp('Numpad3', function() changeWalkDir(SouthEast, true) end, gameRootPanel) g_keyboard.bindKeyUp('Numpad3', function() changeWalkDir(SouthEast, true) end, gameRootPanel, true)
g_keyboard.bindKeyUp('Numpad2', function() changeWalkDir(South, true) end, gameRootPanel) g_keyboard.bindKeyUp('Numpad2', function() changeWalkDir(South, true) end, gameRootPanel, true)
g_keyboard.bindKeyUp('Numpad1', function() changeWalkDir(SouthWest, true) end, gameRootPanel) g_keyboard.bindKeyUp('Numpad1', function() changeWalkDir(SouthWest, true) end, gameRootPanel, true)
g_keyboard.bindKeyUp('Numpad4', function() changeWalkDir(West, true) end, gameRootPanel) g_keyboard.bindKeyUp('Numpad4', function() changeWalkDir(West, true) end, gameRootPanel, true)
g_keyboard.bindKeyUp('Numpad7', function() changeWalkDir(NorthWest, true) end, gameRootPanel) g_keyboard.bindKeyUp('Numpad7', function() changeWalkDir(NorthWest, true) end, gameRootPanel, true)
g_keyboard.bindKeyPress('Ctrl+Up', function() g_game.turn(North) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) g_keyboard.bindKeyPress('Ctrl+Up', function() g_game.turn(North) changeWalkDir(North) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY)
g_keyboard.bindKeyPress('Ctrl+Right', function() g_game.turn(East) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) g_keyboard.bindKeyPress('Ctrl+Right', function() g_game.turn(East) changeWalkDir(East) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY)
g_keyboard.bindKeyPress('Ctrl+Down', function() g_game.turn(South) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) g_keyboard.bindKeyPress('Ctrl+Down', function() g_game.turn(South) changeWalkDir(South) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY)
g_keyboard.bindKeyPress('Ctrl+Left', function() g_game.turn(West) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) g_keyboard.bindKeyPress('Ctrl+Left', function() g_game.turn(West) changeWalkDir(West) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY)
g_keyboard.bindKeyPress('Ctrl+Numpad8', function() g_game.turn(North) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) g_keyboard.bindKeyPress('Ctrl+Numpad8', function() g_game.turn(North) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY)
g_keyboard.bindKeyPress('Ctrl+Numpad6', function() g_game.turn(East) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) g_keyboard.bindKeyPress('Ctrl+Numpad6', function() g_game.turn(East) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY)
g_keyboard.bindKeyPress('Ctrl+Numpad2', function() g_game.turn(South) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) g_keyboard.bindKeyPress('Ctrl+Numpad2', function() g_game.turn(South) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY)
@ -101,7 +101,7 @@ end
function terminate() function terminate()
hide() hide()
cancelSmartWalk() stopSmartWalk()
disconnect(g_game, { disconnect(g_game, {
onGameStart = onGameStart, onGameStart = onGameStart,
@ -209,7 +209,7 @@ function tryLogout()
anchor=AnchorHorizontalCenter}, yesCallback, noCallback) anchor=AnchorHorizontalCenter}, yesCallback, noCallback)
end end
function cancelSmartWalk() function stopSmartWalk()
if smartWalkEvent then if smartWalkEvent then
smartWalkEvent:cancel() smartWalkEvent:cancel()
smartWalkEvent = nil smartWalkEvent = nil
@ -218,11 +218,10 @@ function cancelSmartWalk()
end end
function changeWalkDir(dir, pop) function changeWalkDir(dir, pop)
while table.removevalue(smartWalkDirs, dir) do end
if pop then if pop then
table.removevalue(smartWalkDirs, dir) if #smartWalkDirs == 0 then
if #smartWalkDirs == 0 and smartWalkEvent then stopSmartWalk()
smartWalkEvent:cancel()
smartWalkEvent = nil
return return
end end
else else
@ -253,20 +252,9 @@ function changeWalkDir(dir, pop)
end end
function smartWalk() function smartWalk()
local dir = smartWalkDir if g_keyboard.getModifiers() == KeyboardNoModifier and gameRootPanel:isFocused() then
if modules.client_options.getOption('smartWalk') then g_game.walk(smartWalkDir)
if g_keyboard.isKeyPressed('Up') and g_keyboard.isKeyPressed('Left') then
dir = NorthWest
elseif g_keyboard.isKeyPressed('Up') and g_keyboard.isKeyPressed('Right') then
dir = NorthEast
elseif g_keyboard.isKeyPressed('Down') and g_keyboard.isKeyPressed('Left') then
dir = SouthWest
elseif g_keyboard.isKeyPressed('Down') and g_keyboard.isKeyPressed('Right') then
dir = SouthEast
end
end end
g_game.walk(dir)
end end
function updateStretchShrink() function updateStretchShrink()

@ -81,11 +81,6 @@ std::string format(const std::string& format, const Args&... args) {
} }
} }
template<>
inline std::string format(const std::string& format) {
return format;
}
} }
#endif #endif

Loading…
Cancel
Save