mirror of
https://github.com/edubart/otclient.git
synced 2025-10-14 11:34:54 +02:00
replace require with dofile, rename Hotkeys to Keyboard
This commit is contained in:
@@ -5,21 +5,21 @@ Module
|
||||
website: https://github.com/edubart/otclient
|
||||
|
||||
onLoad: |
|
||||
require 'ext/table'
|
||||
require 'ext/string'
|
||||
require 'ext/os'
|
||||
require 'math/point'
|
||||
require 'math/size'
|
||||
require 'math/color'
|
||||
require 'math/rect'
|
||||
require 'const'
|
||||
require 'util'
|
||||
require 'globals'
|
||||
require 'dispatcher'
|
||||
require 'effects'
|
||||
require 'settings'
|
||||
require 'hotkeys'
|
||||
require 'cursor'
|
||||
dofile 'ext/table'
|
||||
dofile 'ext/string'
|
||||
dofile 'ext/os'
|
||||
dofile 'math/point'
|
||||
dofile 'math/size'
|
||||
dofile 'math/color'
|
||||
dofile 'math/rect'
|
||||
dofile 'const'
|
||||
dofile 'util'
|
||||
dofile 'globals'
|
||||
dofile 'dispatcher'
|
||||
dofile 'effects'
|
||||
dofile 'settings'
|
||||
dofile 'keyboard'
|
||||
dofile 'mouse'
|
||||
|
||||
onUnload: |
|
||||
rootWidget = nil
|
||||
|
@@ -1,7 +0,0 @@
|
||||
function setTargetCursor()
|
||||
g_window.setMouseCursor('/core_styles/icons/targetcursor.png', {x=9,y=9})
|
||||
end
|
||||
|
||||
function restoreCursor()
|
||||
g_window.restoreMouseCursor()
|
||||
end
|
134
modules/core_lib/keyboard.lua
Normal file
134
modules/core_lib/keyboard.lua
Normal file
@@ -0,0 +1,134 @@
|
||||
Keyboard = {}
|
||||
|
||||
-- private functions
|
||||
local function translateKeyCombo(keyCombo)
|
||||
if not keyCombo or #keyCombo == 0 then return nil end
|
||||
table.sort(keyCombo)
|
||||
local keyComboDesc = ''
|
||||
for k,v in pairs(keyCombo) do
|
||||
local keyDesc = KeyCodeDescs[v]
|
||||
if keyDesc == nil then return nil end
|
||||
keyComboDesc = keyComboDesc .. '+' .. keyDesc
|
||||
end
|
||||
keyComboDesc = keyComboDesc:sub(2)
|
||||
return keyComboDesc
|
||||
end
|
||||
|
||||
local function retranslateKeyComboDesc(keyComboDesc)
|
||||
if keyComboDesc == nil then return nil end
|
||||
local keyCombo = {}
|
||||
for i,currentKeyDesc in ipairs(keyComboDesc:split('+')) do
|
||||
for keyCode, keyDesc in pairs(KeyCodeDescs) do
|
||||
if keyDesc:lower() == currentKeyDesc:trim():lower() then
|
||||
table.insert(keyCombo, keyCode)
|
||||
end
|
||||
end
|
||||
end
|
||||
return translateKeyCombo(keyCombo)
|
||||
end
|
||||
|
||||
local function determineKeyComboDesc(keyCode, keyboardModifiers)
|
||||
local keyCombo = {}
|
||||
if keyCode == KeyCtrl or keyCode == KeyShift or keyCode == KeyAlt then
|
||||
table.insert(keyCombo, keyCode)
|
||||
elseif KeyCodeDescs[keyCode] ~= nil then
|
||||
if keyboardModifiers == KeyboardCtrlModifier then
|
||||
table.insert(keyCombo, KeyCtrl)
|
||||
elseif keyboardModifiers == KeyboardAltModifier then
|
||||
table.insert(keyCombo, KeyAlt)
|
||||
elseif keyboardModifiers == KeyboardCtrlAltModifier then
|
||||
table.insert(keyCombo, KeyCtrl)
|
||||
table.insert(keyCombo, KeyAlt)
|
||||
elseif keyboardModifiers == KeyboardShiftModifier then
|
||||
table.insert(keyCombo, KeyShift)
|
||||
elseif keyboardModifiers == KeyboardCtrlShiftModifier then
|
||||
table.insert(keyCombo, KeyCtrl)
|
||||
table.insert(keyCombo, KeyShift)
|
||||
elseif keyboardModifiers == KeyboardAltShiftModifier then
|
||||
table.insert(keyCombo, KeyAlt)
|
||||
table.insert(keyCombo, KeyShift)
|
||||
elseif keyboardModifiers == KeyboardCtrlAltShiftModifier then
|
||||
table.insert(keyCombo, KeyCtrl)
|
||||
table.insert(keyCombo, KeyAlt)
|
||||
table.insert(keyCombo, KeyShift)
|
||||
end
|
||||
table.insert(keyCombo, keyCode)
|
||||
end
|
||||
table.sort(keyCombo)
|
||||
return translateKeyCombo(keyCombo)
|
||||
end
|
||||
|
||||
local function onWidgetKeyDown(widget, keyCode, keyboardModifiers)
|
||||
if keyCode == KeyUnknown then return end
|
||||
local keyComboDesc = determineKeyComboDesc(keyCode, keyboardModifiers)
|
||||
local callback = widget.boundKeyDownCombos[keyComboDesc]
|
||||
if callback then
|
||||
callback()
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function onWidgetKeyPress(widget, keyCode, keyboardModifiers, wouldFilter)
|
||||
local keyComboDesc = determineKeyComboDesc(keyCode, keyboardModifiers)
|
||||
if keyCode == KeyUnknown then return end
|
||||
local callback = widget.boundKeyPressCombos[keyComboDesc]
|
||||
if callback then
|
||||
callback()
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function connectKeyDownEvent(widget)
|
||||
if widget.boundKeyDownCombos then return end
|
||||
connect(widget, { onKeyDown = onWidgetKeyDown })
|
||||
widget.boundKeyDownCombos = {}
|
||||
end
|
||||
|
||||
local function connectKeyPressEvent(widget)
|
||||
if widget.boundKeyPressCombos then return end
|
||||
connect(widget, { onKeyPress = onWidgetKeyPress })
|
||||
widget.boundKeyPressCombos = {}
|
||||
end
|
||||
|
||||
-- public functions
|
||||
function Keyboard.bindKeyDown(keyComboDesc, callback, widget)
|
||||
widget = widget or rootWidget
|
||||
connectKeyDownEvent(widget)
|
||||
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
||||
if keyComboDesc then
|
||||
widget.boundKeyDownCombos[keyComboDesc] = callback
|
||||
else
|
||||
error('key combo \'' .. keyComboDesc .. '\' is failed')
|
||||
end
|
||||
end
|
||||
|
||||
function Keyboard.bindKeyPress(keyComboDesc, callback, widget)
|
||||
widget = widget or rootWidget
|
||||
connectKeyPressEvent(widget)
|
||||
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
||||
if keyComboDesc then
|
||||
widget.boundKeyPressCombos[keyComboDesc] = callback
|
||||
else
|
||||
error('key combo \'' .. keyComboDesc .. '\' is failed')
|
||||
end
|
||||
end
|
||||
|
||||
function Keyboard.unbindKeyDown(keyComboDesc, widget)
|
||||
widget = widget or rootWidget
|
||||
if widget.boundKeyDownCombos == nil then return end
|
||||
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
||||
if keyComboDesc then
|
||||
widget.boundKeyDownCombos[keyComboDesc] = nil
|
||||
end
|
||||
end
|
||||
|
||||
function Keyboard.unbindKeyPress(keyComboDesc, widget)
|
||||
widget = widget or rootWidget
|
||||
if widget.boundKeyPressCombos == nil then return end
|
||||
local keyComboDesc = retranslateKeyComboDesc(keyComboDesc)
|
||||
if keyComboDesc then
|
||||
widget.boundKeyPressCombos[keyComboDesc] = nil
|
||||
end
|
||||
end
|
@@ -1,2 +1,9 @@
|
||||
Mouse = {}
|
||||
|
||||
function Mouse.setTargetCursor()
|
||||
g_window.setMouseCursor('/core_styles/icons/targetcursor.png', {x=9,y=9})
|
||||
end
|
||||
|
||||
function Mouse.restoreCursor()
|
||||
g_window.restoreMouseCursor()
|
||||
end
|
||||
|
@@ -114,7 +114,3 @@ function signalcall(param, ...)
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function runscript(file)
|
||||
g_lua.runScript(resolvepath(file, 2))
|
||||
end
|
Reference in New Issue
Block a user