Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 109 additions & 5 deletions modules/game_hotkeys/hotkeys_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ HotkeyColors = {
itemUseSelf = '#00FF00',
itemUseTarget = '#FF0000',
itemUseWith = '#F5B325',
action = '#FF00FF',
}

hotkeysManagerLoaded = false
Expand All @@ -28,6 +29,7 @@ clearObjectButton = nil
useOnSelf = nil
useOnTarget = nil
useWith = nil
hotkeyAction = nil
defaultComboKeys = nil
perServer = true
perCharacter = true
Expand All @@ -37,12 +39,14 @@ currentHotkeys = nil
boundCombosCallback = {}
hotkeysList = {}
lastHotkeyTime = g_clock.millis()
actionList = {}

-- public functions
function init()
hotkeysButton = modules.client_topmenu.addLeftGameButton('hotkeysButton', tr('Hotkeys') .. ' (Ctrl+K)', '/images/topbuttons/hotkeys', toggle)
g_keyboard.bindKeyDown('Ctrl+K', toggle)
hotkeysWindow = g_ui.displayUI('hotkeys_manager')
addAction('Hotkeys manager', 'Toggle window', toggle)
hotkeysWindow:setVisible(false)

currentHotkeys = hotkeysWindow:getChildById('currentHotkeys')
Expand All @@ -54,6 +58,8 @@ function init()
sendAutomatically = hotkeysWindow:getChildById('sendAutomatically')
selectObjectButton = hotkeysWindow:getChildById('selectObjectButton')
clearObjectButton = hotkeysWindow:getChildById('clearObjectButton')
hotkeyAction = hotkeysWindow:getChildById('hotkeyAction')
clearAction = hotkeysWindow:getChildById('clearAction')
useOnSelf = hotkeysWindow:getChildById('useOnSelf')
useOnTarget = hotkeysWindow:getChildById('useOnTarget')
useWith = hotkeysWindow:getChildById('useWith')
Expand Down Expand Up @@ -219,7 +225,9 @@ function save()
itemId = child.itemId,
subType = child.subType,
useType = child.useType,
value = child.value
value = child.value,
module = child.module,
actionName = child.actionName
}
end

Expand Down Expand Up @@ -302,10 +310,55 @@ function clearObject()
currentHotkeyLabel.useType = nil
currentHotkeyLabel.autoSend = nil
currentHotkeyLabel.value = nil
currentHotkeyLabel.module = nil
currentHotkeyLabel.actionName = nil
updateHotkeyLabel(currentHotkeyLabel)
updateHotkeyForm(true)
end

function startChooseAction()
local actionWindow = g_ui.createWidget('HotkeyActionWindow', rootWidget)

local modComboBox = actionWindow:recursiveGetChildById('modComboBox')
local actionComboBox = actionWindow:recursiveGetChildById('actionComboBox')

for k,v in pairs(actionList) do
modComboBox:addOption(k)
end

if modComboBox:getCurrentOption() then
for k,v in pairs(actionList[modComboBox:getCurrentOption().text]) do
actionComboBox:addOption(k)
end

actionComboBox:setEnabled(true)
end

modComboBox.onOptionChange = function(obj, text, data)
if not actionList[text] then
return
end

actionComboBox:clearOptions()
actionComboBox:setEnabled(false)

for k,v in pairs(actionList[text]) do
actionComboBox:addOption(k)
actionComboBox:setEnabled(true)
end

if actionComboBox:getCurrentOption() then
actionComboBox:setEnabled(true)
end
end

local addButton = actionWindow:recursiveGetChildById('addButton')
addButton.onClick = function()
hotkeySetAction(modComboBox:getText(), actionComboBox:getText())
actionWindow:destroy()
end
end

function addHotkey()
local assignWindow = g_ui.createWidget('HotkeyAssignWindow', rootWidget)
assignWindow:grabKeyboard()
Expand Down Expand Up @@ -348,13 +401,17 @@ function addKeyCombo(keyCombo, keySettings, focus)
hotkeyLabel.itemId = tonumber(keySettings.itemId)
hotkeyLabel.subType = tonumber(keySettings.subType)
hotkeyLabel.useType = tonumber(keySettings.useType)
hotkeyLabel.module = keySettings.module
hotkeyLabel.actionName = keySettings.actionName
if keySettings.value then hotkeyLabel.value = tostring(keySettings.value) end
else
hotkeyLabel.keyCombo = keyCombo
hotkeyLabel.autoSend = false
hotkeyLabel.itemId = nil
hotkeyLabel.subType = nil
hotkeyLabel.useType = nil
hotkeyLabel.module = nil
hotkeyLabel.actionName = nil
hotkeyLabel.value = ''
end

Expand Down Expand Up @@ -382,11 +439,19 @@ function doKeyCombo(keyCombo)
lastHotkeyTime = g_clock.millis()

if hotKey.itemId == nil then
if not hotKey.value or #hotKey.value == 0 then return end
if hotKey.autoSend then
modules.game_console.sendMessage(hotKey.value)
if hotKey.module and hotKey.actionName then
if actionList[hotKey.module] and actionList[hotKey.module][hotKey.actionName] then
actionList[hotKey.module][hotKey.actionName]()
else
pwarning('Choosen hotkey [' .. keyCombo .. '] action [' .. hotKey.module .. ' | ' .. hotKey.actionName .. '] doesn\'t exist.')
end
else
modules.game_console.setTextEditText(hotKey.value)
if not hotKey.value or #hotKey.value == 0 then return end
if hotKey.autoSend then
modules.game_console.sendMessage(hotKey.value)
else
modules.game_console.setTextEditText(hotKey.value)
end
end
elseif hotKey.useType == HOTKEY_MANAGER_USE then
if g_game.getClientVersion() < 780 or hotKey.subType then
Expand Down Expand Up @@ -454,6 +519,9 @@ function updateHotkeyLabel(hotkeyLabel)
elseif hotkeyLabel.itemId ~= nil then
hotkeyLabel:setText(tr('%s: (use object)', hotkeyLabel.keyCombo))
hotkeyLabel:setColor(HotkeyColors.itemUse)
elseif hotkeyLabel.module and hotkeyLabel.actionName then
hotkeyLabel:setText(hotkeyLabel.keyCombo .. ': ' .. hotkeyLabel.module .. ' | ' .. hotkeyLabel.actionName)
hotkeyLabel:setColor(HotkeyColors.action)
else
local text = hotkeyLabel.keyCombo .. ': '
if hotkeyLabel.value then
Expand All @@ -479,6 +547,8 @@ function updateHotkeyForm(reset)
sendAutomatically:disable()
selectObjectButton:disable()
clearObjectButton:enable()
hotkeyAction:disable()
clearAction:disable()
currentItemPreview:setItemId(currentHotkeyLabel.itemId)
if currentHotkeyLabel.subType then
currentItemPreview:setItemSubType(currentHotkeyLabel.subType)
Expand Down Expand Up @@ -508,6 +578,12 @@ function updateHotkeyForm(reset)
hotkeyText:enable()
hotkeyText:focus()
hotKeyTextLabel:enable()
hotkeyAction:enable()
if currentHotkeyLabel.module or currentHotkeyLabel.actionName then
clearAction:enable()
else
clearAction:disable()
end
if reset then
hotkeyText:setCursorPos(-1)
end
Expand All @@ -531,6 +607,8 @@ function updateHotkeyForm(reset)
useRadioGroup:clearSelected()
sendAutomatically:setChecked(false)
currentItemPreview:clearItem()
hotkeyAction:disable()
clearAction:disable()
end
end

Expand Down Expand Up @@ -597,3 +675,29 @@ function hotkeyCaptureOk(assignWindow, keyCombo)
addKeyCombo(keyCombo, nil, true)
assignWindow:destroy()
end

function hotkeySetAction(module, actionName)
currentHotkeyLabel.module = module
currentHotkeyLabel.actionName = actionName
updateHotkeyLabel(currentHotkeyLabel)
end

function addAction(module, actionName, func)
if not actionList[module] then
actionList[module] = {}
end

actionList[module][actionName] = func
end

function removeAction(module, actionName)
actionName = actionName or false

if not actionName then
actionList[module] = nil
else
if actionList[module] and actionList[module][actionName] then
actionList[module][actionName] = nil
end
end
end
68 changes: 67 additions & 1 deletion modules/game_hotkeys/hotkeys_manager.otui
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ HotkeyListLabel < UILabel
MainWindow
id: hotkeysWindow
!text: tr('Hotkeys')
size: 340 410
size: 340 445

@onEnter: modules.game_hotkeys.ok()
@onEscape: modules.game_hotkeys.cancel()
Expand Down Expand Up @@ -157,6 +157,25 @@ MainWindow
checked: false
margin-top: 2

Button
id: hotkeyAction
!text: tr('Add action')
width: 128
enabled: false
anchors.left: parent.left
anchors.top: prev.bottom
margin-top: 8
@onClick: modules.game_hotkeys.startChooseAction()

Button
id: clearAction
!text: tr('Clear action')
width: 128
enabled: false
anchors.right: parent.right
anchors.top: prev.top
@onClick: modules.game_hotkeys.clearObject()

HorizontalSeparator
id: separator
anchors.left: parent.left
Expand Down Expand Up @@ -226,3 +245,50 @@ HotkeyAssignWindow < MainWindow
anchors.right: parent.right
anchors.bottom: parent.bottom
@onClick: self:getParent():destroy()

HotkeyActionWindow < MainWindow
id: actionWindow
!text: tr('Button Action')
size: 230 150
@onEscape: self:destroy()

ComboBox
id: modComboBox
width: 200
anchors.left: parent.left
anchors.top: parent.top
margin-top: 5
mouse-scroll: false

ComboBox
id: actionComboBox
width: 200
enabled: false
anchors.left: parent.left
anchors.top: prev.bottom
margin-top: 5
mouse-scroll: false

HorizontalSeparator
id: separator
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: next.top
margin-bottom: 10

Button
id: addButton
!text: tr('Add')
width: 64
anchors.right: next.left
anchors.bottom: parent.bottom
margin-right: 10
@onClick: modules.game_hotkeys.hotkeyCaptureOk(self:getParent(), self:getParent():getChildById('comboPreview').keyCombo)

Button
id: cancelButton
!text: tr('Cancel')
width: 64
anchors.right: parent.right
anchors.bottom: parent.bottom
@onClick: self:getParent():destroy()