More improvements in walk

This commit is contained in:
Eduardo Bart 2013-01-26 20:12:00 -02:00
parent 4b1db2bcd6
commit 4351f3c63f
4 changed files with 63 additions and 63 deletions

View File

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

View File

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

View File

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

View File

@ -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