tibia-client/modules/game_hotkeys/hotkeys_manager.lua

453 lines
13 KiB
Lua
Raw Normal View History

2012-02-07 04:32:15 +01:00
HOTKEY_MANAGER_USEONSELF = 1
HOTKEY_MANAGER_USEONTARGET = 2
HOTKEY_MANAGER_USEWITH = 3
HotkeyColors = {
2012-02-07 04:32:15 +01:00
text = '#888888',
textAutoSend = '#FFFFFF',
itemUse = '#8888FF',
itemUseSelf = '#00FF00',
itemUseTarget = '#FF0000',
itemUseWith = '#F5B325',
2012-02-07 04:32:15 +01:00
}
hotkeysManagerLoaded = false
hotkeysWindow = nil
hotkeysButton = nil
currentHotkeysList = nil
hotkeyLabelSelectedOnList = nil
currentItemPreview = nil
itemWidget = nil
2012-07-27 00:13:47 +02:00
addHotkeyButton = nil
removeHotkeyButton = nil
hotkeyText = nil
hotKeyTextLabel = nil
sendAutomatically = nil
selectObjectButton = nil
clearObjectButton = nil
useOnSelf = nil
useOnTarget = nil
useWith = nil
hotkeyList = {}
2012-02-07 04:32:15 +01:00
-- public functions
function init()
hotkeysWindow = g_ui.displayUI('hotkeys_manager')
local hotkeyListPanel = hotkeysWindow:getChildById('currentHotkeys')
2012-02-08 22:23:15 +01:00
2012-02-07 06:52:48 +01:00
hotkeysWindow:setVisible(false)
hotkeysButton = TopMenu.addLeftGameButton('hotkeysButton', tr('Hotkeys') .. ' (Ctrl+K)', 'icon.png', toggle)
g_keyboard.bindKeyDown('Ctrl+K', toggle)
g_keyboard.bindKeyPress('Down', function() hotkeyListPanel:focusNextChild(KeyboardFocusReason) end, hotkeysWindow)
g_keyboard.bindKeyPress('Up', function() hotkeyListPanel:focusPreviousChild(KeyboardFocusReason) end, hotkeysWindow)
2012-02-08 22:23:15 +01:00
2012-02-07 04:32:15 +01:00
currentHotkeysList = hotkeysWindow:getChildById('currentHotkeys')
currentItemPreview = hotkeysWindow:getChildById('itemPreview')
2012-07-27 00:13:47 +02:00
addHotkeyButton = hotkeysWindow:getChildById('addHotkeyButton')
removeHotkeyButton = hotkeysWindow:getChildById('removeHotkeyButton')
2012-02-07 06:52:48 +01:00
hotkeyText = hotkeysWindow:getChildById('hotkeyText')
hotKeyTextLabel = hotkeysWindow:getChildById('hotKeyTextLabel')
sendAutomatically = hotkeysWindow:getChildById('sendAutomatically')
selectObjectButton = hotkeysWindow:getChildById('selectObjectButton')
clearObjectButton = hotkeysWindow:getChildById('clearObjectButton')
useOnSelf = hotkeysWindow:getChildById('useOnSelf')
useOnTarget = hotkeysWindow:getChildById('useOnTarget')
useWith = hotkeysWindow:getChildById('useWith')
2012-02-08 22:23:15 +01:00
2012-06-26 00:13:30 +02:00
itemWidget = g_ui.createWidget('UIItem')
2012-02-07 04:32:15 +01:00
itemWidget:setVirtual(true)
itemWidget:setVisible(false)
2012-02-08 22:23:15 +01:00
itemWidget:setFocusable(false)
connect(g_game, {
onGameStart = onGameStart,
onGameEnd = hide
})
connect(currentHotkeysList, { onChildFocusChange = function (self, focusedChild) checkSelectedHotkey(focusedChild) end } )
2012-02-08 22:23:15 +01:00
2012-02-07 07:57:04 +01:00
hotkeysManagerLoaded = true
end
function terminate()
hotkeysManagerLoaded = false
disconnect(g_game, {
onGameStart = onGameStart,
onGameEnd = hide
})
g_keyboard.unbindKeyDown('Ctrl+K')
for keyCombo,v in pairs(hotkeyList) do
g_keyboard.unbindKeyPress(keyCombo)
end
hotkeyList = {}
itemWidget:destroy()
hotkeysWindow:destroy()
hotkeysButton:destroy()
2012-02-07 04:32:15 +01:00
end
function load()
local hotkeySettings = g_settings.getNode('HotkeysManager')
local hasCombos = false
if not table.empty(hotkeySettings) then
local playerHotkeySettings = hotkeySettings[g_game.getLocalPlayer():getName()]
if not table.empty(playerHotkeySettings) then
for k, setting in pairs(playerHotkeySettings) do
addKeyCombo(nil, setting.keyCombo, setting)
hasCombos = true
end
end
end
-- add default F keys combos
if not hasCombos then
loadDefautComboKeys()
2012-02-07 04:32:15 +01:00
end
end
function save()
local char = g_game.getLocalPlayer():getName()
local hotkeySettings = g_settings.getNode('HotkeysManager') or {}
hotkeySettings[char] = {}
2012-06-04 02:28:19 +02:00
for i=1, currentHotkeysList:getChildCount() do
2012-02-07 04:32:15 +01:00
local child = currentHotkeysList:getChildByIndex(i)
table.insert(hotkeySettings[char], {
keyCombo = child.keyCombo,
autoSend = child.autoSend,
itemId = child.itemId,
useType = child.useType,
value = child.value
})
2012-02-07 04:32:15 +01:00
end
2012-06-26 00:13:30 +02:00
g_settings.setNode('HotkeysManager', hotkeySettings)
2012-02-07 04:32:15 +01:00
end
function toggle()
if not hotkeysWindow:isVisible() then
show()
2012-02-07 06:52:48 +01:00
end
2012-02-07 04:32:15 +01:00
end
function show()
2012-02-08 22:23:15 +01:00
if g_game.isOnline() then
2012-02-07 04:32:15 +01:00
hotkeysWindow:grabKeyboard()
2012-02-07 06:52:48 +01:00
hotkeysWindow:show()
hotkeysWindow:raise()
2012-02-07 06:52:48 +01:00
end
2012-02-07 04:32:15 +01:00
end
function hide()
2012-02-07 04:32:15 +01:00
hotkeysWindow:ungrabKeyboard()
2012-02-07 06:52:48 +01:00
hotkeysWindow:hide()
2012-02-07 04:32:15 +01:00
end
function ok()
save()
hide()
end
function reload()
local children = currentHotkeysList:getChildren()
for i=1,#children do
hotkeyList[children[i].keyCombo] = nil
g_keyboard.unbindKeyPress(children[i].keyCombo)
end
currentHotkeysList:destroyChildren()
hotkeyLabelSelectedOnList = nil
load()
hide()
end
function loadDefautComboKeys()
for i=1,12 do
addKeyCombo(nil, 'F' .. i)
end
for i=1,4 do
addKeyCombo(nil, 'Shift+F' .. i)
end
end
2012-02-07 04:32:15 +01:00
-- private functions
function onGameStart()
reload()
end
function onChooseItemMouseRelease(self, mousePosition, mouseButton)
2012-02-07 04:32:15 +01:00
local item = nil
if mouseButton == MouseLeftButton then
local clickedWidget = modules.game_interface.getRootPanel():recursiveGetChildByPos(mousePosition, false)
2012-02-07 04:32:15 +01:00
if clickedWidget then
if clickedWidget:getClassName() == 'UIMap' then
local tile = clickedWidget:getTile(mousePosition)
if tile then
2012-02-07 06:52:48 +01:00
local thing = tile:getTopMoveThing()
if thing and thing:isItem() then
item = thing
2012-02-07 04:32:15 +01:00
end
end
elseif clickedWidget:getClassName() == 'UIItem' and not clickedWidget:isVirtual() then
item = clickedWidget:getItem()
end
end
end
if item then
currentItemPreview:setItemId(item:getId())
hotkeyLabelSelectedOnList.itemId = item:getId()
changeUseType(HOTKEY_MANAGER_USEONSELF)
checkSelectedHotkey(hotkeyLabelSelectedOnList)
2012-02-07 04:32:15 +01:00
end
2012-02-08 22:23:15 +01:00
show()
2012-06-26 00:13:30 +02:00
g_mouse.restoreCursor()
2012-02-07 04:32:15 +01:00
self:ungrabMouse()
self:destroy()
end
function startChooseItem()
2012-06-26 00:13:30 +02:00
local mouseGrabberWidget = g_ui.createWidget('UIWidget')
2012-02-07 04:32:15 +01:00
mouseGrabberWidget:setVisible(false)
mouseGrabberWidget:setFocusable(false)
connect(mouseGrabberWidget, { onMouseRelease = onChooseItemMouseRelease })
2012-02-07 04:32:15 +01:00
mouseGrabberWidget:grabMouse()
2012-06-26 00:13:30 +02:00
g_mouse.setTargetCursor()
2012-02-08 22:23:15 +01:00
2012-07-27 00:13:47 +02:00
hide()
2012-02-07 04:32:15 +01:00
end
function clearObject()
2012-02-08 22:23:15 +01:00
hotkeyLabelSelectedOnList.itemId = nil
2012-02-07 07:57:04 +01:00
currentItemPreview:clearItem()
changeUseType(HOTKEY_MANAGER_USEONSELF)
setSendAutomatically(false)
2012-02-07 04:32:15 +01:00
hotkeyLabelSelectedOnList:setText(hotkeyLabelSelectedOnList.keyCombo .. ': ')
2012-02-08 22:23:15 +01:00
checkSelectedHotkey(hotkeyLabelSelectedOnList)
2012-02-07 04:32:15 +01:00
end
function addHotkey()
local assignWindow = g_ui.createWidget('HotkeyAssignWindow', rootWidget)
assignWindow:grabKeyboard()
local comboLabel = assignWindow:getChildById('comboPreview')
comboLabel.keyCombo = ''
connect(assignWindow, { onKeyDown = hotkeyCapture }, true)
2012-02-07 04:32:15 +01:00
end
function addKeyCombo(messageBox, keyCombo, keySettings)
2012-02-07 06:52:48 +01:00
local label = nil
2012-02-07 04:32:15 +01:00
if currentHotkeysList:getChildById(keyCombo) == nil then
2012-06-26 00:13:30 +02:00
local label = g_ui.createWidget('HotkeyListLabel', currentHotkeysList)
2012-02-07 04:32:15 +01:00
label:setId(keyCombo)
label:setColor(HotkeyColors.text)
2012-02-07 04:32:15 +01:00
label:setText(keyCombo..': ')
if keySettings then
2012-02-08 22:23:15 +01:00
hotkeyLabelSelectedOnList = label
2012-02-07 04:32:15 +01:00
label.keyCombo = keyCombo
setSendAutomatically(keySettings.autoSend)
2012-02-07 04:32:15 +01:00
label.itemId = keySettings.itemId
currentItemPreview:setItemId(keySettings.itemId)
changeUseType(tonumber(keySettings.useType))
2012-02-07 04:32:15 +01:00
label.value = keySettings.value
else
label.keyCombo = keyCombo
label.autoSend = false
label.itemId = nil
label.useType = HOTKEY_MANAGER_USEONSELF
label.value = ''
end
2012-02-08 22:23:15 +01:00
checkSelectedHotkey(label)
2012-02-08 22:23:15 +01:00
2012-02-07 04:32:15 +01:00
hotkeyList[keyCombo] = label
g_keyboard.bindKeyPress(keyCombo, function () call(keyCombo) end, nil, 350)
2012-02-07 04:32:15 +01:00
end
2012-02-08 22:23:15 +01:00
2012-02-07 04:32:15 +01:00
if messageBox then
messageBox:destroy()
2012-02-07 06:52:48 +01:00
messageBox = nil
2012-02-08 22:23:15 +01:00
end
2012-02-07 04:32:15 +01:00
end
function call(keyCombo)
2012-02-08 22:23:15 +01:00
if g_game.isOnline() then
2012-02-07 04:32:15 +01:00
local hotKey = hotkeyList[keyCombo]
if hotKey ~= nil and hotKey.itemId == nil and hotKey.value ~= '' then
2012-02-07 04:32:15 +01:00
if hotKey.autoSend then
2012-02-08 22:23:15 +01:00
g_game.talk(hotKey.value)
2012-02-07 04:32:15 +01:00
else
modules.game_console.setTextEditText(hotKey.value)
2012-02-07 04:32:15 +01:00
end
elseif hotKey.itemId ~= nil then
if hotKey.useType == HOTKEY_MANAGER_USEONSELF then
2012-02-08 22:23:15 +01:00
g_game.useInventoryItemWith(hotKey.itemId, g_game.getLocalPlayer())
2012-02-07 04:32:15 +01:00
elseif hotKey.useType == HOTKEY_MANAGER_USEONTARGET then
2012-02-08 22:23:15 +01:00
local attackingCreature = g_game.getAttackingCreature()
2012-02-07 04:32:15 +01:00
if attackingCreature then
2012-02-08 22:23:15 +01:00
g_game.useInventoryItemWith(hotKey.itemId, attackingCreature)
2012-02-07 04:32:15 +01:00
end
2012-02-08 22:23:15 +01:00
elseif hotKey.useType == HOTKEY_MANAGER_USEWITH then
2012-02-07 04:32:15 +01:00
itemWidget:setItemId(hotKey.itemId)
modules.game_interface.startUseWith(itemWidget:getItem())
2012-02-08 22:23:15 +01:00
end
2012-02-07 04:32:15 +01:00
end
end
end
function checkSelectedHotkey(focused)
2012-06-04 02:28:19 +02:00
if not focused then return end
2012-02-07 07:57:04 +01:00
if hotkeysManagerLoaded then
hotkeyLabelSelectedOnList = focused
2012-02-08 22:23:15 +01:00
if hotkeyLabelSelectedOnList ~= nil then
2012-07-27 00:13:47 +02:00
removeHotkeyButton:enable()
2012-02-07 06:52:48 +01:00
2012-02-07 07:57:04 +01:00
if hotkeyLabelSelectedOnList.itemId == nil then
hotkeyText:enable()
hotkeyText:focus()
2012-02-07 07:57:04 +01:00
hotKeyTextLabel:enable()
hotkeyText:setText(hotkeyLabelSelectedOnList.value)
2012-02-08 22:23:15 +01:00
if hotkeyLabelSelectedOnList.value ~= '' then
sendAutomatically:setChecked(hotkeyLabelSelectedOnList.autoSend)
2012-02-07 07:57:04 +01:00
sendAutomatically:enable()
else
sendAutomatically:disable()
end
2012-02-08 22:23:15 +01:00
2012-02-07 07:57:04 +01:00
selectObjectButton:enable()
2012-02-08 22:23:15 +01:00
clearObjectButton:disable()
2012-02-07 07:57:04 +01:00
currentItemPreview:setItemId(0)
2012-02-07 04:32:15 +01:00
else
2012-02-07 07:57:04 +01:00
hotkeyText:clearText()
hotkeyText:disable()
hotKeyTextLabel:disable()
2012-02-07 06:52:48 +01:00
sendAutomatically:disable()
2012-02-08 22:23:15 +01:00
2012-02-07 07:57:04 +01:00
selectObjectButton:disable()
clearObjectButton:enable()
2012-02-08 22:23:15 +01:00
currentItemPreview:setItemId(hotkeyLabelSelectedOnList.itemId)
2012-02-07 04:32:15 +01:00
end
changeUseType(hotkeyLabelSelectedOnList.useType)
2012-02-07 07:57:04 +01:00
else
2012-02-07 06:52:48 +01:00
hotkeyText:clearText()
2012-07-27 00:13:47 +02:00
removeHotkeyButton:disable()
2012-02-07 06:52:48 +01:00
hotkeyText:disable()
sendAutomatically:disable()
2012-02-07 07:57:04 +01:00
sendAutomatically:setChecked(false)
2012-02-08 22:23:15 +01:00
2012-02-07 07:57:04 +01:00
currentItemPreview:setItemId(0)
2012-02-07 06:52:48 +01:00
selectObjectButton:disable()
2012-02-07 07:57:04 +01:00
clearObjectButton:disable()
useOnSelf:disable()
useOnTarget:disable()
useWith:disable()
useOnSelf:setChecked(false)
useOnTarget:setChecked(false)
useWith:setChecked(false)
2012-02-08 22:23:15 +01:00
end
end
2012-02-07 04:32:15 +01:00
end
function changeUseType(useType, checked)
2012-02-07 04:32:15 +01:00
if checked == nil or checked then
2012-02-08 22:23:15 +01:00
hotkeyLabelSelectedOnList.useType = useType
2012-02-07 04:32:15 +01:00
if hotkeyLabelSelectedOnList.itemId ~= nil and currentItemPreview:getItem():isMultiUse() then
2012-02-07 06:52:48 +01:00
useOnSelf:enable()
useOnTarget:enable()
useWith:enable()
2012-02-08 22:23:15 +01:00
2012-02-07 04:32:15 +01:00
if useType == HOTKEY_MANAGER_USEONSELF then
hotkeyLabelSelectedOnList:setText(tr('%s: (use object on yourself)', hotkeyLabelSelectedOnList.keyCombo))
hotkeyLabelSelectedOnList:setColor(HotkeyColors.itemUseSelf)
2012-02-07 06:52:48 +01:00
useOnSelf:setChecked(true)
useOnTarget:setChecked(false)
useWith:setChecked(false)
2012-02-08 22:23:15 +01:00
elseif useType == HOTKEY_MANAGER_USEONTARGET then
hotkeyLabelSelectedOnList:setText(tr('%s: (use object on target)', hotkeyLabelSelectedOnList.keyCombo))
hotkeyLabelSelectedOnList:setColor(HotkeyColors.itemUseTarget)
2012-02-07 06:52:48 +01:00
useOnSelf:setChecked(false)
useOnTarget:setChecked(true)
useWith:setChecked(false)
2012-02-07 04:32:15 +01:00
elseif useType == HOTKEY_MANAGER_USEWITH then
hotkeyLabelSelectedOnList:setText(tr('%s: (use object with crosshair)', hotkeyLabelSelectedOnList.keyCombo))
hotkeyLabelSelectedOnList:setColor(HotkeyColors.itemUseWith)
2012-02-08 22:23:15 +01:00
2012-02-07 06:52:48 +01:00
useOnSelf:setChecked(false)
useOnTarget:setChecked(false)
useWith:setChecked(true)
2012-02-07 04:32:15 +01:00
end
elseif hotkeyLabelSelectedOnList.itemId ~= nil and not currentItemPreview:getItem():isMultiUse() then
2012-02-07 06:52:48 +01:00
useOnSelf:disable()
useOnTarget:disable()
useWith:disable()
2012-02-08 22:23:15 +01:00
hotkeyLabelSelectedOnList:setText(tr('%s: (use object)', hotkeyLabelSelectedOnList.keyCombo))
hotkeyLabelSelectedOnList:setColor(HotkeyColors.itemUse)
2012-02-08 22:23:15 +01:00
2012-02-07 06:52:48 +01:00
useOnSelf:setChecked(false)
useOnTarget:setChecked(false)
useWith:setChecked(false)
2012-02-08 22:23:15 +01:00
else
2012-02-07 06:52:48 +01:00
useOnSelf:disable()
useOnTarget:disable()
useWith:disable()
2012-02-08 22:23:15 +01:00
2012-02-07 06:52:48 +01:00
useOnSelf:setChecked(false)
useOnTarget:setChecked(false)
useWith:setChecked(false)
2012-02-07 04:32:15 +01:00
end
end
end
function removeHotkey()
2012-02-07 04:32:15 +01:00
if hotkeyLabelSelectedOnList ~= nil then
hotkeyList[hotkeyLabelSelectedOnList.keyCombo] = nil
2012-06-26 00:13:30 +02:00
g_keyboard.unbindKeyPress(hotkeyLabelSelectedOnList.keyCombo)
2012-02-07 04:32:15 +01:00
hotkeyLabelSelectedOnList:destroy()
end
end
function onHotkeyTextChange(id, value)
2012-02-07 06:52:48 +01:00
if hotkeyLabelSelectedOnList ~= nil and hotkeyLabelSelectedOnList.keyCombo ~= nil then
2012-02-07 07:57:04 +01:00
hotkeyLabelSelectedOnList:setText(hotkeyLabelSelectedOnList.keyCombo .. ': ' .. value)
2012-02-07 06:52:48 +01:00
hotkeyLabelSelectedOnList.value = value
2012-02-08 22:23:15 +01:00
2012-02-07 06:52:48 +01:00
if value ~= '' then
sendAutomatically:enable()
else
sendAutomatically:disable()
sendAutomatically:setChecked(false)
2012-06-26 00:13:30 +02:00
end
2012-02-07 04:32:15 +01:00
end
end
function setSendAutomatically(value)
2012-02-07 04:32:15 +01:00
hotkeyLabelSelectedOnList.autoSend = value
if value then
hotkeyLabelSelectedOnList:setColor(HotkeyColors.autoSend)
2012-02-07 04:32:15 +01:00
else
hotkeyLabelSelectedOnList:setColor(HotkeyColors.text)
2012-02-07 04:32:15 +01:00
end
end
function hotkeyCapture(widget, keyCode, keyboardModifiers)
2012-02-07 04:32:15 +01:00
local keyCombo = determineKeyComboDesc(keyCode, keyboardModifiers)
2012-03-29 04:03:01 +02:00
local comboPreview = rootWidget:getChildById('assignWindow'):getChildById('comboPreview')
comboPreview:setText(tr('Current hotkey to add: %s', keyCombo))
2012-02-07 04:32:15 +01:00
comboPreview.keyCombo = keyCombo
comboPreview:resizeToText()
2012-03-29 04:03:01 +02:00
rootWidget:getChildById('assignWindow'):getChildById('addButton'):enable()
2012-02-08 22:23:15 +01:00
2012-02-07 04:32:15 +01:00
return true
end