tibia-client/modules/game_hotkeys/hotkeys_manager.lua

460 lines
14 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 = '#CC0000',
}
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()
2012-06-26 00:13:30 +02:00
hotkeysWindow = g_ui.displayUI('hotkeys_manager.otui')
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)', '/game_hotkeys/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, { 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
2012-02-08 22:23:15 +01:00
load()
end
function terminate()
hotkeysManagerLoaded = false
disconnect(g_game, { onGameEnd = hide })
g_keyboard.unbindKeyDown('Ctrl+K')
save()
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()
2012-06-26 00:13:30 +02:00
local hotkeySettings = g_settings.getNode('HotkeysManager')
2012-02-08 22:23:15 +01:00
local hasCombos = false
2012-02-07 06:52:48 +01:00
if hotkeySettings ~= nil then
2012-02-08 22:23:15 +01:00
for i, v in pairs(hotkeySettings) do
addKeyCombo(nil, v.keyCombo, v)
hasCombos = true
end
end
-- add default F keys combos
if not hasCombos then
for i=1,12 do
addKeyCombo(nil, 'F' .. i)
2012-02-08 22:23:15 +01:00
end
2012-02-07 04:32:15 +01:00
end
end
function save()
2012-02-08 22:23:15 +01:00
local hotkeySettings = {}
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)
2012-02-08 22:23:15 +01:00
table.insert(hotkeySettings, {keyCombo = child.keyCombo,
autoSend = child.autoSend,
itemId = child.itemId,
2012-02-07 04:32:15 +01:00
useType = child.useType,
2012-02-08 22:23:15 +01:00
value = child.value})
2012-02-07 04:32:15 +01:00
end
2012-06-04 02:28:19 +02:00
2012-06-26 00:13:30 +02:00
g_settings.setNode('HotkeysManager', hotkeySettings)
2012-02-07 04:32:15 +01:00
end
function toggle()
2012-02-07 06:52:48 +01:00
if hotkeysWindow:isVisible() then
hide()
2012-02-07 06:52:48 +01:00
else
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
-- private functions
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()
2012-02-08 22:23:15 +01:00
if thing then
item = thing:isItem()
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-07-27 00:13:47 +02:00
show()
2012-02-07 04:32:15 +01:00
end
2012-02-08 22:23:15 +01:00
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()
2012-02-07 06:52:48 +01:00
local widget
2012-02-08 22:23:15 +01:00
2012-06-26 00:13:30 +02:00
messageBox = g_ui.createWidget('MainWindow', rootWidget)
messageBox:grabKeyboard()
2012-02-07 04:32:15 +01:00
messageBox:setId('assignWindow')
messageBox:setText(tr('Button Assign'))
2012-02-07 04:32:15 +01:00
messageBox:setWidth(420)
messageBox:setHeight(140)
2012-08-07 01:43:14 +02:00
messageBox:setDraggable(false)
2012-02-08 22:23:15 +01:00
2012-06-26 00:13:30 +02:00
widget = g_ui.createWidget('Label', messageBox)
widget:setText(tr('Please, press the key you wish to add onto your hotkeys manager'))
2012-02-07 06:52:48 +01:00
widget:resizeToText()
widget:addAnchor(AnchorHorizontalCenter, 'parent', AnchorHorizontalCenter)
widget:addAnchor(AnchorTop, 'parent', AnchorTop)
2012-02-08 22:23:15 +01:00
2012-06-26 00:13:30 +02:00
widget = g_ui.createWidget('Label', messageBox)
2012-02-07 06:52:48 +01:00
widget:setId('comboPreview')
widget:setText(tr('Current hotkey to add: %s', 'none'))
2012-02-07 06:52:48 +01:00
widget.keyCombo = ''
widget:resizeToText()
widget:addAnchor(AnchorHorizontalCenter, 'parent', AnchorHorizontalCenter)
widget:addAnchor(AnchorTop, 'prev', AnchorBottom)
widget:setMarginTop(20)
2012-02-08 22:23:15 +01:00
2012-06-26 00:13:30 +02:00
widget = g_ui.createWidget('Button', messageBox)
2012-02-07 06:52:48 +01:00
widget:setId('cancelButton')
widget:setText(tr('Cancel'))
2012-02-07 06:52:48 +01:00
widget:setWidth(64)
widget:addAnchor(AnchorBottom, 'parent', AnchorBottom)
widget:addAnchor(AnchorRight, 'parent', AnchorRight)
2012-02-08 22:23:15 +01:00
widget.onClick = function (self)
messageBox = nil
self:getParent():destroy()
2012-02-07 07:57:04 +01:00
end
2012-02-08 22:23:15 +01:00
2012-06-26 00:13:30 +02:00
widget = g_ui.createWidget('Button', messageBox)
2012-02-07 06:52:48 +01:00
widget:setId('addButton')
widget:setText(tr('Add'))
2012-02-07 06:52:48 +01:00
widget:setWidth(64)
widget:disable()
widget:addAnchor(AnchorBottom, 'cancelButton', AnchorBottom)
widget:addAnchor(AnchorRight, 'cancelButton', AnchorLeft)
widget:setMarginRight(10)
2012-02-08 22:23:15 +01:00
widget.onClick = function (self)
2012-02-07 07:57:04 +01:00
messageBox = nil
addKeyCombo(self:getParent(), self:getParent():getChildById('comboPreview').keyCombo)
2012-02-07 07:57:04 +01:00
end
2012-02-08 22:23:15 +01:00
connect(messageBox, { 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