114 lines
2.8 KiB
Plaintext
114 lines
2.8 KiB
Plaintext
-- @name Joystick Data
|
|
-- @author Opti1337
|
|
JOYSTICK_INPUT_TYPE = {Axis = 1, Button = 2}
|
|
|
|
local CONFIG = {
|
|
Steer = {
|
|
DeviceId = 0,
|
|
InputId = 0,
|
|
Type = JOYSTICK_INPUT_TYPE.Axis,
|
|
Handler = function(value)
|
|
return math.remap(value, 0, 65535, -1, 1)
|
|
end
|
|
},
|
|
Clutch = {
|
|
DeviceId = 0,
|
|
InputId = 4,
|
|
Type = JOYSTICK_INPUT_TYPE.Button,
|
|
Handler = function(value)
|
|
return math.remap(value, 0, 128, 0, 1)
|
|
end
|
|
},
|
|
Throttle = {
|
|
DeviceId = 0,
|
|
InputId = 5,
|
|
Type = JOYSTICK_INPUT_TYPE.Button,
|
|
Handler = function(value)
|
|
return math.remap(value, 0, 128, 0, 1)
|
|
end
|
|
},
|
|
Brake = {
|
|
DeviceId = 0,
|
|
InputId = 2,
|
|
Type = JOYSTICK_INPUT_TYPE.Button,
|
|
Handler = function(value)
|
|
return math.remap(value, 0, 128, 0, 1)
|
|
end
|
|
},
|
|
Handbrake = {
|
|
DeviceId = 0,
|
|
InputId = 0,
|
|
Type = JOYSTICK_INPUT_TYPE.Button,
|
|
Handler = function(value)
|
|
return math.remap(value, 0, 128, 0, 1)
|
|
end
|
|
},
|
|
GearUp = {
|
|
DeviceId = 0,
|
|
InputId = 1,
|
|
Type = JOYSTICK_INPUT_TYPE.Button,
|
|
Handler = function(value)
|
|
return math.remap(value, 0, 128, 0, 1)
|
|
end
|
|
},
|
|
GearDown = {
|
|
DeviceId = 0,
|
|
InputId = 3,
|
|
Type = JOYSTICK_INPUT_TYPE.Button,
|
|
Handler = function(value)
|
|
return math.remap(value, 0, 128, 0, 1)
|
|
end
|
|
}
|
|
}
|
|
|
|
if CLIENT and player() == owner() then
|
|
local data = {}
|
|
local isDirty = false
|
|
|
|
hook.add("think", "_think", function()
|
|
for k, v in pairs(CONFIG) do
|
|
local value = 0
|
|
|
|
if v.Type == JOYSTICK_INPUT_TYPE.Axis then
|
|
value = joystick.getAxis(v.DeviceId, v.InputId)
|
|
elseif v.Type == JOYSTICK_INPUT_TYPE.Button then
|
|
value = joystick.getButton(v.DeviceId, v.InputId)
|
|
end
|
|
|
|
if v.Handler ~= nil and type(v.Handler) == "function" then
|
|
value = v.Handler(value) or value
|
|
end
|
|
|
|
if not isDirty and data[k] ~= value then
|
|
isDirty = true
|
|
end
|
|
|
|
data[k] = value
|
|
end
|
|
|
|
if isDirty then
|
|
net.start("data")
|
|
for k in pairs(CONFIG) do
|
|
net.writeFloat(data[k])
|
|
end
|
|
net.send({}, true)
|
|
|
|
isDirty = false
|
|
end
|
|
end)
|
|
elseif SERVER then
|
|
local outputs = {}
|
|
|
|
for _, v in pairs(table.getKeys(CONFIG)) do
|
|
outputs[v] = "NORMAL"
|
|
end
|
|
|
|
wire.adjustPorts({}, outputs)
|
|
|
|
net.receive("data", function()
|
|
for k in pairs(CONFIG) do
|
|
wire.ports[k] = net.readFloat()
|
|
end
|
|
end)
|
|
end
|