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

Add triggerThresold (0.5 by default) for buttons associated with axes, grabbing terminology change

courtesy of Lenade Lamidedi
This commit is contained in:
Étienne Fildadut 2020-04-02 21:02:09 +02:00
parent f22e0bef26
commit d85424d866
3 changed files with 95 additions and 80 deletions

View file

@ -59,6 +59,7 @@ let system_mt = {
systems = nil, systems = nil,
--- Returns true if the entity should be added to this system (and therefore its subsystems). --- Returns true if the entity should be added to this system (and therefore its subsystems).
-- If this is a string, it will be converted to a filter function on instanciation using ecs.all.
-- By default, rejects everything. -- By default, rejects everything.
filter = :(e) return false end, filter = :(e) return false end,
--- Returns true if e1 <= e2. --- Returns true if e1 <= e2.
@ -87,8 +88,8 @@ let system_mt = {
--- Called when drawing the system, for every entity the system contains. Called after :onDraw was called on the system. --- Called when drawing the system, for every entity the system contains. Called after :onDraw was called on the system.
render = :(e) end, render = :(e) end,
--- If set, the system will only update every interval seconds. --- If not false, the system will only update every interval seconds.
interval = nil, interval = false,
--- The system and its susbsystems will only update if this is true. --- The system and its susbsystems will only update if this is true.
active = true, active = true,
--- The system and its subsystems will only draw if this is true. --- The system and its subsystems will only draw if this is true.
@ -263,6 +264,9 @@ let recInstanciateSystems = (world, systems)
end end
})) }))
let system = t[#t] let system = t[#t]
if type(s.filter) == "string" then
system.filter = (_, e) return e[s.filter] ~= nil end
end
if s.name then if s.name then
world.s[s.name] = system world.s[s.name] = system
end end

View file

@ -113,9 +113,9 @@ input.basicButtonDetector = function(id)
-- Gamepad axis -- Gamepad axis
elseif id:match("^gamepad%.axis%.") then elseif id:match("^gamepad%.axis%.") then
local gid, axis, threshold = id:match("^gamepad%.axis%.(.+)%.(.+)%%(.+)$") local gid, axis, threshold = id:match("^gamepad%.axis%.(.+)%.(.+)%%(.+)$")
if not gid then gid, axis = id:match("^gamepad%.axis%.(.+)%.(.+)$") end -- no threshold (=0) if not gid then gid, axis = id:match("^gamepad%.axis%.(.+)%.(.+)$") end -- no threshold (=0.5)
gid = tonumber(gid) gid = tonumber(gid)
threshold = tonumber(threshold) or 0.1 threshold = tonumber(threshold) or 0.5
return function() return function()
local gamepad local gamepad
for _,j in ipairs(love.joystick.getJoysticks()) do for _,j in ipairs(love.joystick.getJoysticks()) do
@ -168,7 +168,7 @@ input.basicAxisDetector = function(id)
-- Gamepad axis -- Gamepad axis
elseif id:match("^gamepad%.axis%.") then elseif id:match("^gamepad%.axis%.") then
local gid, axis, threshold = id:match("^gamepad%.axis%.(.+)%.(.+)%%(.+)$") local gid, axis, threshold = id:match("^gamepad%.axis%.(.+)%.(.+)%%(.+)$")
if not gid then gid, axis = id:match("^gamepad%.axis%.(.+)%.(.+)$") end -- no threshold (=0) if not gid then gid, axis = id:match("^gamepad%.axis%.(.+)%.(.+)$") end -- no threshold (=0.1)
gid = tonumber(gid) gid = tonumber(gid)
threshold = tonumber(threshold) or 0.1 threshold = tonumber(threshold) or 0.1
return function() return function()

View file

@ -59,38 +59,38 @@ local button_mt = {
return self return self
end, end,
--- Grabs the input. --- Hijacks the input.
-- This function returns a new input object which mirrors the current object, except it will grab every new input. -- This function returns a new input object which mirrors the current object, except it will hijack every new input.
-- This means any new button press/down/release will only be visible to the new object; the button will always appear unpressed for the initial object. -- This means any new button press/down/release will only be visible to the new object; the button will always appear unpressed for the initial object.
-- This is useful for contextual input, for example if you want to display a menu without pausing the game: the menu -- This is useful for contextual input, for example if you want to display a menu without pausing the game: the menu
-- can grab relevant inputs while it is open, so they don't trigger any action in the rest of the game. -- can hijack relevant inputs while it is open, so they don't trigger any action in the rest of the game.
-- An input can be grabbed several times; the one which grabbed it last will be the active one. -- An input can be hijacked several times; the one which hijacked it last will be the active one.
-- @treturn ButtonInput the new input object which is grabbing the input -- @treturn ButtonInput the new input object which is hijacking the input
grab = function(self) hijack = function(self)
local grabbed = setmetatable({}, { __index = self, __newindex = self }) local hijacked = setmetatable({}, { __index = self, __newindex = self })
table.insert(self.grabStack, grabbed) table.insert(self.hijackStack, hijacked)
self.grabbing = grabbed self.hijacking = hijacked
return grabbed return hijacked
end, end,
--- Release the input that was grabbed by this object. --- Release the input that was hijacked by this object.
-- Input will be given back to the previous object. -- Input will be given back to the previous object.
-- @treturn ButtonInput this ButtonInput object -- @treturn ButtonInput this ButtonInput object
release = function(self) free = function(self)
local grabStack = self.grabStack local hijackStack = self.hijackStack
for i, v in ipairs(grabStack) do for i, v in ipairs(hijackStack) do
if v == self then if v == self then
table.remove(grabStack, i) table.remove(hijackStack, i)
self.grabbing = grabStack[#grabStack] self.hijacking = hijackStack[#hijackStack]
return self return self
end end
end end
error("This object is currently not grabbing this input") error("This object is currently not hijacking this input")
end, end,
--- Returns true if the input was just pressed. --- Returns true if the input was just pressed.
-- @treturn boolean true if the input was pressed, false otherwise -- @treturn boolean true if the input was pressed, false otherwise
pressed = function(self) pressed = function(self)
if self.grabbing == self then if self.hijacking == self then
self:update() self:update()
return self.state == "pressed" return self.state == "pressed"
else else
@ -100,7 +100,7 @@ local button_mt = {
--- Returns true if the input was just released. --- Returns true if the input was just released.
-- @treturn boolean true if the input was released, false otherwise -- @treturn boolean true if the input was released, false otherwise
released = function(self) released = function(self)
if self.grabbing == self then if self.hijacking == self then
self:update() self:update()
return self.state == "released" return self.state == "released"
else else
@ -110,7 +110,7 @@ local button_mt = {
--- Returns true if the input is down. --- Returns true if the input is down.
-- @treturn boolean true if the input is currently down, false otherwise -- @treturn boolean true if the input is currently down, false otherwise
down = function(self) down = function(self)
if self.grabbing == self then if self.hijacking == self then
self:update() self:update()
local state = self.state local state = self.state
return state == "down" or state == "pressed" return state == "down" or state == "pressed"
@ -183,27 +183,28 @@ local axis_mt = {
-- @treturn AxisInput this AxisInput object -- @treturn AxisInput this AxisInput object
clear = button_mt.clear, clear = button_mt.clear,
--- Grabs the input. --- Hijacks the input.
-- This function returns a new input object which mirrors the current object, except it will grab every new input. -- This function returns a new input object which mirrors the current object, except it will hijack every new input.
-- This means any value change will only be visible to the new object; the axis will always appear to be at 0 for the initial object. -- This means any value change will only be visible to the new object; the axis will always appear to be at 0 for the initial object.
-- An input can be grabbed several times; the one which grabbed it last will be the active one. -- An input can be hijacked several times; the one which hijacked it last will be the active one.
-- @treturn AxisInput the new input object which is grabbing the input -- @treturn AxisInput the new input object which is hijacking the input
grab = function(self) hijack = function(self)
local grabbed local hijacked
grabbed = setmetatable({ hijacked = setmetatable({
positive = input.button(function() return grabbed:value() > 0 end), positive = input.button(function() return hijacked:value() > self.triggeringThreshold end),
negative = input.button(function() return grabbed:value() < 0 end) negative = input.button(function() return hijacked:value() < self.triggeringThreshold end)
}, { __index = self, __newindex = self }) }, { __index = self, __newindex = self })
table.insert(self.grabStack, grabbed) table.insert(self.hijackStack, hijacked)
self.grabbing = grabbed self.hijacking = hijacked
return grabbed return hijacked
end, end,
--- Release the input that was grabbed by this object. --- Release the input that was hijacked by this object.
-- Input will be given back to the previous object. -- Input will be given back to the previous object.
-- @treturn AxisInput this AxisInput object -- @treturn AxisInput this AxisInput object
release = button_mt.release, free = button_mt.free,
--- Sets the default detection threshold (deadzone). --- Sets the default detection threshold (deadzone).
-- 0 by default.
-- @tparam number new the new detection threshold -- @tparam number new the new detection threshold
-- @treturn AxisInput this AxisInput object -- @treturn AxisInput this AxisInput object
threshold = function(self, new) threshold = function(self, new)
@ -215,7 +216,7 @@ local axis_mt = {
-- @tparam[opt=default threshold] number threshold value to use -- @tparam[opt=default threshold] number threshold value to use
-- @treturn number the input value -- @treturn number the input value
value = function(self, curThreshold) value = function(self, curThreshold)
if self.grabbing == self then if self.hijacking == self then
self:update() self:update()
local val = self.val local val = self.val
return math.abs(val) > math.abs(curThreshold or self.threshold) and val or 0 return math.abs(val) > math.abs(curThreshold or self.threshold) and val or 0
@ -226,7 +227,7 @@ local axis_mt = {
--- Returns the change in value of the input since last update (between -2 and 2). --- Returns the change in value of the input since last update (between -2 and 2).
-- @treturn number the value delta -- @treturn number the value delta
delta = function(self) delta = function(self)
if self.grabbing == self then if self.hijacking == self then
self:update() self:update()
return self.dval return self.dval
else else
@ -237,7 +238,7 @@ local axis_mt = {
-- @tparam[opt=default threshold*max] number raw threshold value to use -- @tparam[opt=default threshold*max] number raw threshold value to use
-- @treturn number the input raw value -- @treturn number the input raw value
raw = function(self, rawThreshold) raw = function(self, rawThreshold)
if self.grabbing == self then if self.hijacking == self then
self:update() self:update()
local raw = self.raw local raw = self.raw
return math.abs(raw) > math.abs(rawThreshold or self.threshold*self.max) and raw or 0 return math.abs(raw) > math.abs(rawThreshold or self.threshold*self.max) and raw or 0
@ -252,6 +253,15 @@ local axis_mt = {
return self.max return self.max
end, end,
--- Sets the default triggering threshold, i.e. how the minimal axis value for which the associated buttons will be considered down.
-- 0.5 by default.
-- @tparam number new the new triggering threshold
-- @treturn AxisInput this AxisInput object
triggeringThreshold = function(self, new)
self.triggeringThreshold = tonumber(new)
return self
end,
--- The associated button pressed when the axis reaches a positive value. --- The associated button pressed when the axis reaches a positive value.
positive = nil, positive = nil,
--- The associated button pressed when the axis reaches a negative value. --- The associated button pressed when the axis reaches a negative value.
@ -319,36 +329,36 @@ local pointer_mt = {
-- @treturn PointerInput this PointerInput object -- @treturn PointerInput this PointerInput object
clear = button_mt.clear, clear = button_mt.clear,
--- Grabs the input. --- Hijacks the input.
-- This function returns a new input object which mirrors the current object, except it will grab every new input. -- This function returns a new input object which mirrors the current object, except it will hijack every new input.
-- This means any value change will only be visible to the new object; the pointer will always appear to be at offsetX,offsetY for the initial object. -- This means any value change will only be visible to the new object; the pointer will always appear to be at offsetX,offsetY for the initial object.
-- An input can be grabbed several times; the one which grabbed it last will be the active one. -- An input can be hijacked several times; the one which hijacked it last will be the active one.
-- @treturn PointerInput the new input object which is grabbing the input -- @treturn PointerInput the new input object which is hijacking the input
grab = function(self) hijack = function(self)
local grabbed local hijacked
grabbed = { hijacked = {
horizontal = input.axis(function() horizontal = input.axis(function()
local h = grabbed:x() local h = hijacked:x()
local width = grabbed.width local width = hijacked.width
return h/width, h, width return h/width, h, width
end), end),
vertical = input.axis(function() vertical = input.axis(function()
local v = grabbed:y() local v = hijacked:y()
local height = grabbed.height local height = hijacked.height
return v/height, v, height return v/height, v, height
end) end)
} }
grabbed.right, grabbed.left = grabbed.horizontal.positive, grabbed.horizontal.negative hijacked.right, hijacked.left = hijacked.horizontal.positive, hijacked.horizontal.negative
grabbed.up, grabbed.down = grabbed.vertical.negative, grabbed.vertical.positive hijacked.up, hijacked.down = hijacked.vertical.negative, hijacked.vertical.positive
setmetatable(grabbed, { __index = self, __newindex = self }) setmetatable(hijacked, { __index = self, __newindex = self })
table.insert(self.grabStack, grabbed) table.insert(self.hijackStack, hijacked)
self.grabbing = grabbed self.hijacking = hijacked
return grabbed return hijacked
end, end,
--- Release the input that was grabbed by this object. --- Free the input that was hijacked by this object.
-- Input will be given back to the previous object. -- Input will be given back to the previous object.
-- @treturn PointerInput this PointerInput object -- @treturn PointerInput this PointerInput object
release = button_mt.release, free = button_mt.free,
--- Set the moving area half-dimensions. --- Set the moving area half-dimensions.
-- Call without argument to use half the window dimensions. -- Call without argument to use half the window dimensions.
@ -385,7 +395,7 @@ local pointer_mt = {
--- Returns the current X value of the pointer. --- Returns the current X value of the pointer.
-- @treturn number X value -- @treturn number X value
x = function(self) x = function(self)
if self.grabbing == self then if self.hijacking == self then
self:update() self:update()
return self.valX + (self.offsetX or self.width or input.getDrawWidth()/2) return self.valX + (self.offsetX or self.width or input.getDrawWidth()/2)
else else
@ -395,7 +405,7 @@ local pointer_mt = {
--- Returns the current Y value of the pointer. --- Returns the current Y value of the pointer.
-- @treturn number Y value -- @treturn number Y value
y = function(self) y = function(self)
if self.grabbing == self then if self.hijacking == self then
self:update() self:update()
return self.valY + (self.offsetY or self.height or input.getDrawHeight()/2) return self.valY + (self.offsetY or self.height or input.getDrawHeight()/2)
else else
@ -414,7 +424,7 @@ local pointer_mt = {
-- @treturn number Y value -- @treturn number Y value
clamped = function(self) clamped = function(self)
local width, height = self.width, self.height local width, height = self.width, self.height
if self.grabbing == self then if self.hijacking == self then
self:update() self:update()
local x, y = self.valX, self.valY local x, y = self.valX, self.valY
local cx, cy = x, y local cx, cy = x, y
@ -582,39 +592,40 @@ input = {
-- @impl ubiquitousse -- @impl ubiquitousse
button = function(...) button = function(...)
local r = setmetatable({ local r = setmetatable({
grabStack = {}, -- grabbers stack, last element is the object currently grabbing this input hijackStack = {}, -- hijackers stack, last element is the object currently hijacking this input
grabbing = nil, -- object currently grabbing this input hijacking = nil, -- object currently hijacking this input
detectors = {}, -- detectors list detectors = {}, -- detectors list
state = "none" -- current state (none, pressed, down, released) state = "none" -- current state (none, pressed, down, released)
}, button_mt) }, button_mt)
table.insert(r.grabStack, r) table.insert(r.hijackStack, r)
r.grabbing = r r.hijacking = r
r:bind(...) r:bind(...)
return r return r
end, end,
-- Axis inputs -- -- Axis inputs --
-- Axis input is a container for axes detector. An axis input will return the value of the axis detector the most far away from their center (0). -- Axis input is a container for axes detector. An axis input will return the value of the axis detector the most far away from their center (0).
-- Axis input provide a threshold setting ; every axis which has a distance to the center below the threshold (none by default) will be ignored. -- Axis input provide a threshold setting; every axis which has a distance to the center below the threshold (none by default) will be ignored.
-- @tparam AxisDetectors ... all the axis detectors or axis identifiers -- @tparam AxisDetectors ... all the axis detectors or axis identifiers
-- @tretrun AxisInput the object -- @tretrun AxisInput the object
-- @impl ubiquitousse -- @impl ubiquitousse
axis = function(...) axis = function(...)
local r = setmetatable({ local r = setmetatable({
grabStack = {}, -- grabbers stack, last element is the object currently grabbing this input hijackStack = {}, -- hijackers stack, last element is the object currently hijacking this input
grabbing = nil, -- object currently grabbing this input hijacking = nil, -- object currently hijacking this input
detectors = {}, -- detectors list detectors = {}, -- detectors list
val = 0, -- current value between -1 and 1 val = 0, -- current value between -1 and 1
dval = 0, -- change between -2 and 2 dval = 0, -- change between -2 and 2
raw = 0, -- raw value between -max and +max raw = 0, -- raw value between -max and +max
max = 1, -- maximum for raw values max = 1, -- maximum for raw values
threshold = 0 -- ie., the deadzone threshold = 0, -- ie., the deadzone
triggeringThreshold = 0.5 -- digital button threshold
}, axis_mt) }, axis_mt)
table.insert(r.grabStack, r) table.insert(r.hijackStack, r)
r.grabbing = r r.hijacking = r
r:bind(...) r:bind(...)
r.positive = input.button(function() return r:value() > 0 end) r.positive = input.button(function() return r:value() > r.triggeringThreshold end)
r.negative = input.button(function() return r:value() < 0 end) r.negative = input.button(function() return r:value() < r.triggeringThreshold end)
return r return r
end, end,
@ -628,16 +639,16 @@ input = {
-- @impl ubiquitousse -- @impl ubiquitousse
pointer = function(...) pointer = function(...)
local r = setmetatable({ local r = setmetatable({
grabStack = {}, -- grabbers stack, first element is the object currently grabbing this input hijackStack = {}, -- hijackers stack, first element is the object currently hijacking this input
grabbing = nil, -- object currently grabbing this input hijacking = nil, -- object currently hijacking this input
detectors = {}, -- pointers list (composite detectors) detectors = {}, -- pointers list (composite detectors)
valX = 0, valY = 0, -- pointer position valX = 0, valY = 0, -- pointer position
width = 1, height = 1, -- half-dimensions of the movement area width = 1, height = 1, -- half-dimensions of the movement area
offsetX = 0, offsetY = 0, -- offsets offsetX = 0, offsetY = 0, -- offsets
xSpeed = 1, ySpeed = 1, -- speed (pixels/milisecond); for relative mode xSpeed = 1, ySpeed = 1, -- speed (pixels/milisecond); for relative mode
}, pointer_mt) }, pointer_mt)
table.insert(r.grabStack, r) table.insert(r.hijackStack, r)
r.grabbing = r r.hijacking = r
r:bind(...) r:bind(...)
r.horizontal = input.axis(function() r.horizontal = input.axis(function()
local h = r:x() local h = r:x()