tibia-client/modules/game_hotkeys/hotkeys_manager.lua

458 lines
14 KiB
Lua
Raw Normal View History

2012-02-07 04:32:15 +01:00
HotkeysManager = {}
2012-02-07 07:57:04 +01:00
local hotkeysManagerLoaded = false
2012-02-07 04:32:15 +01:00
local hotkeysWindow
local hotkeysButton
local currentHotkeysList
local hotkeyLabelSelectedOnList
local currentItemPreview
local itemWidget
2012-02-07 06:52:48 +01:00
local addHotkey
local removeHotkey
local hotkeyText
local hotKeyTextLabel
local sendAutomatically
local selectObjectButton
local clearObjectButton
local useOnSelf
local useOnTarget
local useWith
local hotkeyList = {}
2012-02-07 04:32:15 +01:00
HOTKEY_MANAGER_USEONSELF = 1
HOTKEY_MANAGER_USEONTARGET = 2
HOTKEY_MANAGER_USEWITH = 3
local hotkeyColors = {
text = '#888888',
textAutoSend = '#FFFFFF',
itemUse = '#8888FF',
itemUseSelf = '#00FF00',
itemUseTarget = '#FF0000',
itemUseWith = '#CC0000',
}
-- public functions
function HotkeysManager.init()
2012-02-07 06:52:48 +01:00
hotkeysWindow = displayUI('hotkeys_manager.otui')
2012-02-08 22:23:15 +01:00
2012-02-07 06:52:48 +01:00
hotkeysWindow:setVisible(false)
hotkeysButton = TopMenu.addGameButton('hotkeysButton', 'Hotkeys (Ctrl+K)', '/game_hotkeys/icon.png', HotkeysManager.toggle)
2012-02-07 06:52:48 +01:00
Keyboard.bindKeyDown('Ctrl+K', HotkeysManager.toggle)
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-02-07 06:52:48 +01:00
addHotkey = hotkeysWindow:getChildById('addHotkey')
removeHotkey = hotkeysWindow:getChildById('removeHotkey')
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-02-07 04:32:15 +01:00
itemWidget = createWidget('UIItem')
itemWidget:setVirtual(true)
itemWidget:setVisible(false)
2012-02-08 22:23:15 +01:00
itemWidget:setFocusable(false)
2012-02-07 06:52:48 +01:00
connect(currentHotkeysList, { onChildFocusChange = function (self, focusedChild) HotkeysManager.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
2012-02-07 07:57:04 +01:00
HotkeysManager.load()
2012-02-07 04:32:15 +01:00
end
function HotkeysManager.load()
local hotkeySettings = Settings.getNode('HotkeysManager')
2012-02-08 22:23:15 +01:00
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
2012-02-07 07:57:04 +01:00
HotkeysManager.addKeyCombo(nil, v.keyCombo, v)
2012-02-08 22:23:15 +01:00
end
2012-02-07 04:32:15 +01:00
end
end
function HotkeysManager.save()
2012-02-08 22:23:15 +01:00
local hotkeySettings = {}
2012-02-07 04:32:15 +01:00
for i = 1, currentHotkeysList:getChildCount() do
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
Settings.setNode('HotkeysManager', hotkeySettings)
end
2012-02-08 22:23:15 +01:00
function HotkeysManager.terminate()
2012-02-07 07:57:04 +01:00
hotkeysManagerLoaded = false
2012-02-08 22:23:15 +01:00
2012-02-07 06:52:48 +01:00
Keyboard.unbindKeyDown('Ctrl+K')
2012-02-07 04:32:15 +01:00
HotkeysManager.save()
2012-02-08 22:23:15 +01:00
2012-02-07 06:52:48 +01:00
currentHotkeysList = nil
hotkeyLabelSelectedOnList = nil
currentItemPreview = nil
2012-02-08 22:23:15 +01:00
hotkeyList = {}
2012-02-07 06:52:48 +01:00
addHotkey = nil
removeHotkey = nil
hotkeyText = nil
hotKeyTextLabel = nil
sendAutomatically = nil
selectObjectButton = nil
clearObjectButton = nil
useOnSelf = nil
useOnTarget = nil
useWith = nil
2012-02-08 22:23:15 +01:00
2012-02-07 07:57:04 +01:00
itemWidget:destroy()
itemWidget = nil
2012-02-07 06:52:48 +01:00
hotkeysWindow:destroy()
hotkeysWindow = nil
hotkeysButton:destroy()
hotkeysButton = nil
2012-02-07 04:32:15 +01:00
end
function HotkeysManager.toggle()
2012-02-07 06:52:48 +01:00
if hotkeysWindow:isVisible() then
HotkeysManager.hide()
else
HotkeysManager.show()
end
2012-02-07 04:32:15 +01:00
end
function HotkeysManager.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
2012-02-08 22:23:15 +01:00
function HotkeysManager.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 HotkeysManager.onChooseItemMouseRelease(self, mousePosition, mouseButton)
local item = nil
if mouseButton == MouseLeftButton then
local clickedWidget = GameInterface.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
2012-02-07 06:52:48 +01:00
item = thing:asItem()
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()
HotkeysManager.changeUseType(HOTKEY_MANAGER_USEONSELF)
2012-02-07 06:52:48 +01:00
HotkeysManager.checkSelectedHotkey(hotkeyLabelSelectedOnList)
2012-02-08 22:23:15 +01:00
HotkeysManager:show()
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
Mouse.restoreCursor()
self:ungrabMouse()
self:destroy()
end
function HotkeysManager.startChooseItem()
2012-02-07 07:57:04 +01:00
local mouseGrabberWidget = createWidget('UIWidget')
2012-02-07 04:32:15 +01:00
mouseGrabberWidget:setVisible(false)
mouseGrabberWidget:setFocusable(false)
connect(mouseGrabberWidget, { onMouseRelease = HotkeysManager.onChooseItemMouseRelease })
mouseGrabberWidget:grabMouse()
Mouse.setTargetCursor()
2012-02-08 22:23:15 +01:00
2012-02-07 04:32:15 +01:00
HotkeysManager:hide()
end
function HotkeysManager.clearObject()
2012-02-08 22:23:15 +01:00
hotkeyLabelSelectedOnList.itemId = nil
2012-02-07 07:57:04 +01:00
currentItemPreview:clearItem()
2012-02-07 04:32:15 +01:00
HotkeysManager.changeUseType(HOTKEY_MANAGER_USEONSELF)
HotkeysManager.sendAutomatically(false)
hotkeyLabelSelectedOnList:setText(hotkeyLabelSelectedOnList.keyCombo .. ': ')
2012-02-08 22:23:15 +01:00
2012-02-07 06:52:48 +01:00
HotkeysManager.checkSelectedHotkey(hotkeyLabelSelectedOnList)
2012-02-07 04:32:15 +01:00
end
function HotkeysManager.addHotkey()
2012-02-07 06:52:48 +01:00
local widget
2012-02-08 22:23:15 +01:00
2012-03-29 04:03:01 +02:00
messageBox = createWidget('MainWindow', rootWidget)
messageBox:grabKeyboard()
2012-02-07 04:32:15 +01:00
messageBox:setId('assignWindow')
messageBox:setText('Button Assign')
messageBox:setWidth(420)
messageBox:setHeight(140)
messageBox:setDragable(false)
2012-02-08 22:23:15 +01:00
2012-02-07 06:52:48 +01:00
widget = createWidget('Label', messageBox)
widget:setText('Please, press the key you wish to add onto your hotkeys manager')
widget:resizeToText()
widget:addAnchor(AnchorHorizontalCenter, 'parent', AnchorHorizontalCenter)
widget:addAnchor(AnchorTop, 'parent', AnchorTop)
2012-02-08 22:23:15 +01:00
2012-02-07 06:52:48 +01:00
widget = createWidget('Label', messageBox)
widget:setId('comboPreview')
widget:setText('Current hotkey to add: None')
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-02-07 06:52:48 +01:00
widget = createWidget('Button', messageBox)
widget:setId('cancelButton')
widget:setText('Cancel')
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-02-07 06:52:48 +01:00
widget = createWidget('Button', messageBox)
widget:setId('addButton')
widget:setText('Add')
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
HotkeysManager.addKeyCombo(self:getParent(), self:getParent():getChildById('comboPreview').keyCombo)
end
2012-02-08 22:23:15 +01:00
2012-02-07 04:32:15 +01:00
connect(messageBox, { onKeyDown = HotkeysManager.hotkeyCapture }, true)
end
function HotkeysManager.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
local label = createWidget('HotkeyListLabel', currentHotkeysList)
label:setId(keyCombo)
label:setColor(hotkeyColors.text)
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
HotkeysManager.sendAutomatically(keySettings.autoSend)
label.itemId = keySettings.itemId
currentItemPreview:setItemId(keySettings.itemId)
HotkeysManager.changeUseType(tonumber(keySettings.useType))
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
2012-02-07 06:52:48 +01:00
HotkeysManager.checkSelectedHotkey(label)
2012-02-08 22:23:15 +01:00
2012-02-07 04:32:15 +01:00
hotkeyList[keyCombo] = label
2012-02-07 06:52:48 +01:00
Keyboard.bindKeyPress(keyCombo, function () HotkeysManager.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 HotkeysManager.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.itemId == nil and hotKey.value ~= '' then
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
2012-02-07 06:52:48 +01:00
Console.setLineEditText(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)
GameInterface.startUseWith(itemWidget:getItem())
2012-02-08 22:23:15 +01:00
end
2012-02-07 04:32:15 +01:00
end
end
end
2012-02-07 06:52:48 +01:00
function HotkeysManager.checkSelectedHotkey(focused)
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-02-07 07:57:04 +01:00
removeHotkey:enable()
2012-02-07 06:52:48 +01:00
2012-02-07 07:57:04 +01:00
if hotkeyLabelSelectedOnList.itemId == nil then
hotkeyText:enable()
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
2012-02-08 22:23:15 +01:00
HotkeysManager.changeUseType(hotkeyLabelSelectedOnList.useType)
2012-02-07 07:57:04 +01:00
else
2012-02-07 06:52:48 +01:00
hotkeyText:clearText()
2012-02-07 07:57:04 +01:00
removeHotkey: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 HotkeysManager.changeUseType(useType, checked)
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(hotkeyLabelSelectedOnList.keyCombo .. ': (use object on yourself)')
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
2012-02-07 04:32:15 +01:00
hotkeyLabelSelectedOnList:setText(hotkeyLabelSelectedOnList.keyCombo .. ': (use object on target)')
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(hotkeyLabelSelectedOnList.keyCombo .. ': (use object with crosshair)')
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
2012-02-07 04:32:15 +01:00
hotkeyLabelSelectedOnList:setText(hotkeyLabelSelectedOnList.keyCombo .. ': (use object)')
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 HotkeysManager.removeHotkey()
if hotkeyLabelSelectedOnList ~= nil then
hotkeyList[hotkeyLabelSelectedOnList.keyCombo] = nil
2012-02-07 06:52:48 +01:00
Keyboard.unbindKeyPress(hotkeyLabelSelectedOnList.keyCombo)
2012-02-07 04:32:15 +01:00
hotkeyLabelSelectedOnList:destroy()
end
end
2012-02-08 22:23:15 +01:00
function HotkeysManager.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)
end
2012-02-07 04:32:15 +01:00
end
end
function HotkeysManager.sendAutomatically(value)
hotkeyLabelSelectedOnList.autoSend = value
if value then
hotkeyLabelSelectedOnList:setColor(hotkeyColors.autoSend)
else
hotkeyLabelSelectedOnList:setColor(hotkeyColors.text)
end
end
function HotkeysManager.hotkeyCapture(widget, keyCode, keyboardModifiers)
local keyCombo = determineKeyComboDesc(keyCode, keyboardModifiers)
2012-03-29 04:03:01 +02:00
local comboPreview = rootWidget:getChildById('assignWindow'):getChildById('comboPreview')
2012-02-07 04:32:15 +01:00
comboPreview:setText('Current hotkey to add: '.. keyCombo)
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