1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 05:04:50 +00:00
opus/sys/apis/input.lua

192 lines
4.1 KiB
Lua
Raw Normal View History

2017-10-15 23:55:05 +00:00
local Util = require('util')
2018-01-24 23:55:21 +00:00
local keyboard = _G.device and _G.device.keyboard
local keys = _G.keys
local os = _G.os
2017-10-15 23:55:05 +00:00
local modifiers = Util.transpose {
2018-01-24 22:39:38 +00:00
keys.leftCtrl, keys.rightCtrl,
keys.leftShift, keys.rightShift,
keys.leftAlt, keys.rightAlt,
2017-10-15 23:55:05 +00:00
}
local input = {
2018-01-24 22:39:38 +00:00
state = { },
2017-10-15 23:55:05 +00:00
}
2018-01-24 23:55:21 +00:00
if not keyboard then
keyboard = { state = input.state }
end
2017-10-20 17:57:24 +00:00
function input:modifierPressed()
2018-01-24 22:39:38 +00:00
return keyboard.state[keys.leftCtrl] or
keyboard.state[keys.rightCtrl] or
keyboard.state[keys.leftAlt] or
keyboard.state[keys.rightAlt]
2017-10-20 17:57:24 +00:00
end
2017-10-15 23:55:05 +00:00
2017-10-20 17:57:24 +00:00
function input:toCode(ch, code)
2018-01-24 22:39:38 +00:00
local result = { }
if not ch and code == 1 then
ch = 'escape'
end
if keyboard.state[keys.leftCtrl] or keyboard.state[keys.rightCtrl] or
code == keys.leftCtrl or code == keys.rightCtrl then
table.insert(result, 'control')
end
-- the key-up event for alt keys is not generated if the minecraft
-- window loses focus
2019-03-24 21:00:42 +00:00
if keyboard.state[keys.leftAlt] or keyboard.state[keys.rightAlt] or
code == keys.leftAlt or code == keys.rightAlt then
table.insert(result, 'alt')
end
2018-01-24 22:39:38 +00:00
if keyboard.state[keys.leftShift] or keyboard.state[keys.rightShift] or
code == keys.leftShift or code == keys.rightShift then
if code and modifiers[code] then
table.insert(result, 'shift')
elseif #ch == 1 then
table.insert(result, ch:upper())
else
table.insert(result, 'shift')
table.insert(result, ch)
end
elseif not code or not modifiers[code] then
table.insert(result, ch)
end
return table.concat(result, '-')
2017-10-15 23:55:05 +00:00
end
function input:reset()
2018-01-24 22:39:38 +00:00
self.state = { }
self.fired = nil
2017-10-16 04:02:42 +00:00
2018-01-24 22:39:38 +00:00
self.timer = nil
self.mch = nil
self.mfired = nil
2017-10-15 23:55:05 +00:00
end
function input:translate(event, code, p1, p2)
2018-01-24 22:39:38 +00:00
if event == 'key' then
if p1 then -- key is held down
if not modifiers[code] then
self.fired = true
return { code = input:toCode(keys.getName(code), code) }
end
else
self.state[code] = true
if self:modifierPressed() and not modifiers[code] or code == 57 then
self.fired = true
return { code = input:toCode(keys.getName(code), code) }
else
self.fired = false
end
end
elseif event == 'char' then
if not keyboard.state[keys.leftAlt] and
not keyboard.state[keys.rightAlt] then
2018-01-24 22:39:38 +00:00
self.fired = true
return { code = event, ch = code }
-- return { code = event, ch = input:toCode(code) }
2018-01-24 22:39:38 +00:00
end
elseif event == 'key_up' then
if not self.fired then
if self.state[code] then
self.fired = true
local ch = input:toCode(keys.getName(code), code)
self.state[code] = nil
return { code = ch }
end
end
self.state[code] = nil
elseif event == 'paste' then
self.fired = true
if keyboard.state[keys.leftShift] or keyboard.state[keys.rightShift] then
return { code = 'shift-paste', text = code }
else
return { code = 'paste', text = code }
end
elseif event == 'mouse_click' then
local buttons = { 'mouse_click', 'mouse_rightclick' }
self.mch = buttons[code]
self.mfired = nil
2019-03-29 13:56:56 +00:00
return {
code = input:toCode('mouse_down', 255),
button = code,
x = p1,
y = p2,
}
2018-01-24 22:39:38 +00:00
elseif event == 'mouse_drag' then
self.mfired = true
self.fired = true
return {
code = input:toCode('mouse_drag', 255),
button = code,
x = p1,
y = p2,
}
elseif event == 'mouse_up' then
if not self.mfired then
local clock = os.clock()
if self.timer and
p1 == self.x and p2 == self.y and
(clock - self.timer < .5) then
self.mch = 'mouse_doubleclick'
self.timer = nil
else
self.timer = os.clock()
self.x = p1
self.y = p2
end
self.mfired = input:toCode(self.mch, 255)
else
self.mch = 'mouse_up'
self.mfired = input:toCode(self.mch, 255)
end
self.fired = true
return {
code = self.mfired,
button = code,
x = p1,
y = p2,
}
elseif event == "mouse_scroll" then
local directions = {
[ -1 ] = 'scroll_up',
[ 1 ] = 'scroll_down'
}
self.fired = true
return {
code = input:toCode(directions[code], 255),
x = p1,
y = p2,
}
elseif event == 'terminate' then
return { code = 'terminate' }
end
2017-10-16 04:02:42 +00:00
end
2017-10-15 23:55:05 +00:00
2017-10-16 04:02:42 +00:00
function input:test()
2018-01-24 22:39:38 +00:00
while true do
local ch = self:translate(os.pullEvent())
if ch then
Util.print(ch)
end
end
2017-10-15 23:55:05 +00:00
end
return input