1
0
Fork 0
mirror of https://github.com/Reuh/ubiquitousse.git synced 2025-10-27 17:19:31 +00:00

uqt.signal

This commit is contained in:
Étienne Fildadut 2019-12-27 18:54:30 +01:00
parent 82bc7268e6
commit f6fb8ad649
11 changed files with 331 additions and 80 deletions

View file

@ -1,5 +1,8 @@
local input = require((...):match("^(.-%.)backend").."input")
local loaded, signal = pcall(require, (...):match("^(.-)input").."signal")
if not loaded then signal = nil end
local gfx = require("ctr.gfx")
local hid = require("ctr.hid")
@ -266,4 +269,9 @@ input.default.pointer:bind(
input.default.confirm:bind("key.a")
input.default.cancel:bind("key.b")
--- Register signals
if signal then
signal.event:replace("update", oUpdate, input.update)
end
return input

View file

@ -1,5 +1,8 @@
local input = require((...):match("^(.-%.)backend").."input")
local loaded, signal = pcall(require, (...):match("^(.-)input").."signal")
if not loaded then signal = nil end
-- Config --
-- Use ScanCodes (layout independant input) instead of KeyConstants (layout dependant) for keyboard input
@ -13,24 +16,23 @@ local displayKeyConstant = true
love.mouse.setVisible(false)
-- Button detection
-- FIXME love callbacks do something cleaner
local buttonsInUse = {}
local axesInUse = {}
function love.keypressed(key, scancode, isrepeat)
function input.keypressed(key, scancode, isrepeat)
if useScancodes then key = scancode end
buttonsInUse["keyboard."..key] = true
end
function love.keyreleased(key, scancode)
function input.keyreleased(key, scancode)
if useScancodes then key = scancode end
buttonsInUse["keyboard."..key] = nil
end
function love.mousepressed(x, y, button, istouch)
function input.mousepressed(x, y, button, istouch)
buttonsInUse["mouse."..button] = true
end
function love.mousereleased(x, y, button, istouch)
function input.mousereleased(x, y, button, istouch)
buttonsInUse["mouse."..button] = nil
end
function love.wheelmoved(x, y)
function input.wheelmoved(x, y)
if y > 0 then
buttonsInUse["mouse.wheel.up"] = true
elseif y < 0 then
@ -42,17 +44,17 @@ function love.wheelmoved(x, y)
buttonsInUse["mouse.wheel.left"] = true
end
end
function love.mousemoved(x, y, dx, dy)
function input.mousemoved(x, y, dx, dy)
if dx ~= 0 then axesInUse["mouse.move.x"] = dx/love.graphics.getWidth() end
if dy ~= 0 then axesInUse["mouse.move.y"] = dy/love.graphics.getHeight() end
end
function love.gamepadpressed(joystick, button)
function input.gamepadpressed(joystick, button)
buttonsInUse["gamepad.button."..joystick:getID().."."..button] = true
end
function love.gamepadreleased(joystick, button)
function input.gamepadreleased(joystick, button)
buttonsInUse["gamepad.button."..joystick:getID().."."..button] = nil
end
function love.gamepadaxis(joystick, axis, value)
function input.gamepadaxis(joystick, axis, value)
if value ~= 0 then
axesInUse["gamepad.axis."..joystick:getID().."."..axis] = value
else
@ -61,11 +63,7 @@ function love.gamepadaxis(joystick, axis, value)
end
-- Windows size
input.drawWidth, input.drawHeight = love.graphics.getWidth(), love.graphics.getHeight()
function love.resize(width, height)
input.drawWidth = width
input.drawHeight = height
end
input.getDrawWidth, input.getDrawHeight = love.graphics.getWidth, love.graphics.getHeight
-- Update
local oUpdate = input.update
@ -190,7 +188,7 @@ input.basicAxisDetector = function(id)
end
end
input.buttonsInUse = function(threshold)
input.buttonUsed = function(threshold)
local r = {}
threshold = threshold or 0.5
for b in pairs(buttonsInUse) do
@ -201,10 +199,10 @@ input.buttonsInUse = function(threshold)
table.insert(r, b.."%"..(v < 0 and -threshold or threshold))
end
end
return r
return unpack(r)
end
input.axesInUse = function(threshold)
input.axisUsed = function(threshold)
local r = {}
threshold = threshold or 0.5
for b,v in pairs(axesInUse) do
@ -212,7 +210,7 @@ input.axesInUse = function(threshold)
table.insert(r, b.."%"..threshold)
end
end
return r
return unpack(r)
end
input.buttonName = function(...)
@ -316,4 +314,18 @@ input.default.cancel:bind(
"gamepad.button.1.b"
)
--- Register signals
if signal then
signal.event:bind("keypressed", input.keypressed)
signal.event:bind("keyreleased", input.keyreleased)
signal.event:bind("mousepressed", input.mousepressed)
signal.event:bind("mousereleased", input.mousereleased)
signal.event:bind("wheelmoved", input.wheelmoved)
signal.event:bind("mousemoved", input.mousemoved)
signal.event:bind("gamepadpressed", input.gamepadpressed)
signal.event:bind("gamepadreleased", input.gamepadreleased)
signal.event:bind("gamepadaxis", input.gamepadaxis)
signal.event:replace("update", oUpdate, input.update)
end
return input

View file

@ -1,5 +1,8 @@
--- ubiquitousse.input
-- Depends on a backend.
-- Optional dependencies: ubiquitousse.signal (to bind to update signal in signal.event)
local loaded, signal = pcall(require, (...):match("^(.-)input").."signal")
if not loaded then signal = nil end
-- TODO: some key selection helper? Will be backend-implemented, to account for all the possible input methods.
-- TODO: some way to list all possible input / outputs, or make the *inUse make some separation between inputs indiscutitably in use and those who are incertain.
@ -40,8 +43,8 @@ local button_mt = {
-- @treturn ButtonInput this ButtonInput object
unbind = function(self, ...)
for _, d in ipairs({...}) do
for i, detector in ipairs(self.detectors) do
if detector == d then
for i=#self.detectors, 1, -1 do
if self.detectors[i] == d then
table.remove(self.detectors, i)
break
end
@ -270,7 +273,9 @@ local axis_mt = {
self.val, self.raw, self.max = val, raw, max
updated[self] = true
end
end
end,
--- LÖVE note: other callbacks that are defined in backend/love.lua and need to be called in the associated LÖVE callbacks.
}
axis_mt.__index = axis_mt
@ -382,9 +387,9 @@ local pointer_mt = {
x = function(self)
if self.grabbing == self then
self:update()
return self.valX + (self.offsetX or self.width or input.drawWidth/2)
return self.valX + (self.offsetX or self.width or input.getDrawWidth()/2)
else
return self.offsetX or self.width or input.drawWidth/2
return self.offsetX or self.width or input.getDrawWidth()/2
end
end,
--- Returns the current Y value of the pointer.
@ -392,9 +397,9 @@ local pointer_mt = {
y = function(self)
if self.grabbing == self then
self:update()
return self.valY + (self.offsetY or self.height or input.drawHeight/2)
return self.valY + (self.offsetY or self.height or input.getDrawHeight()/2)
else
return self.offsetY or self.height or input.drawHeight/2
return self.offsetY or self.height or input.getDrawHeight()/2
end
end,
@ -418,9 +423,9 @@ local pointer_mt = {
local magnitude = sqrt(x*x + y*y)
cx, cy = cx / magnitude * width, cy / magnitude * height
end
return cx + (self.offsetX or width or input.drawWidth/2), cy + (self.offsetY or height or input.drawHeight/2)
return cx + (self.offsetX or width or input.getDrawWidth()/2), cy + (self.offsetY or height or input.getDrawHeight()/2)
else
return self.offsetX or width or input.drawWidth/2, self.offsetY or height or input.drawHeight/2
return self.offsetX or width or input.getDrawWidth()/2, self.offsetY or height or input.getDrawHeight()/2
end
end,
@ -445,7 +450,7 @@ local pointer_mt = {
if not updated[self] then
local x, y = self.valX, self.valY
local xSpeed, ySpeed = self.xSpeed, self.ySpeed
local width, height = self.width or input.drawWidth/2, self.height or input.drawHeight/2
local width, height = self.width or input.getDrawWidth()/2, self.height or input.getDrawHeight()/2
local newX, newY = x, y
local maxMovX, maxMovY = 0, 0 -- the maxium axis movement in a direction (used to determine which axes have the priority) (absolute value)
for _, pointer in ipairs(self.detectors) do
@ -642,16 +647,16 @@ input = {
--- Returns a list of the buttons currently in use, identified by their string button identifier.
-- This may also returns "axis threshold" buttons if an axis passes the threshold.
-- @treturn table<string> buttons identifiers list
-- @treturn[opt=0.5] number threshold the threshold to detect axes as button
-- @tparam[opt=0.5] number threshold the threshold to detect axes as button
-- @treturn string,... buttons identifiers list
-- @impl backend
buttonsInUse = function(threshold) end,
buttonUsed = function(threshold) end,
--- Returns a list of the axes currently in use, identified by their string axis identifier
-- @treturn table<string> axes identifiers list
-- @treturn[opt=0.5] number threshold the threshold to detect axes
-- @tparam[opt=0.5] number threshold the threshold to detect axes
-- @treturn string,... axes identifiers list
-- @impl backend
axesInUse = function(threshold) end,
axisUsed = function(threshold) end,
--- Returns a nice name for the button identifier.
-- Can be locale-depedant and stuff, it's only for display.
@ -686,14 +691,14 @@ input = {
cancel = nil -- Button: used to cancel something. Example binds: Escape, B button.
},
--- Draw area dimensions.
--- Get draw area dimensions.
-- Used for pointers.
-- @impl backend
drawWidth = 1,
drawHeight = 1,
getDrawWidth = function() return 1 end,
getDrawHeight = function() return 1 end,
--- Update all the Inputs.
-- Should be called at every game update; called by ubiquitousse.update.
-- Should be called at every game update. If ubiquitousse.signal is available, will be bound to the "update" signal in signal.event.
-- The backend can hook into this function to to its input-related updates.
-- @tparam numder dt the delta-time
-- @impl ubiquitousse
@ -701,6 +706,11 @@ input = {
dt = newDt
updated = {}
end
--- If you use LÖVE, note that in order to provide every feature (especially key detection), several callbacks functions will
-- need to be called on LÖVE events. See backend/love.lua.
-- If ubiquitousse.signal is available, these callbacks will be bound to signals in signal.event (with the same name as the LÖVE
-- callbacks, minux the "love.").
}
-- Create default inputs
@ -708,4 +718,9 @@ input.default.pointer = input.pointer()
input.default.confirm = input.button()
input.default.cancel = input.button()
-- Bind signals
if signal then
signal.event:bind("update", input.update)
end
return input