Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
tesselode committed Sep 10, 2015
2 parents d5aa249 + 297d082 commit c32cbcb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
8 changes: 5 additions & 3 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ function love.load()
axis = {}
axis.horizontal = tactile.newAxis(gamepadXAxis, keyboardXAxis)
axis.vertical = tactile.newAxis(gamepadYAxis, keyboardYAxis)
axis.horizontal.deadzone = .25
axis.vertical.deadzone = .25

upButtonDisplay = ButtonDisplay(50, 0, button.up)
leftButtonDisplay = ButtonDisplay(0, 50, button.left)
Expand All @@ -101,9 +103,9 @@ function love.draw()

love.graphics.setColor(255, 255, 255, 255)
love.graphics.rectangle('line', -100, -100, 200, 200)
love.graphics.rectangle('line', -tactile.deadzone * 100, -tactile.deadzone * 100, tactile.deadzone * 200, tactile.deadzone * 200)
love.graphics.rectangle('line', -.25 * 100, -.25 * 100, .25 * 200, .25 * 200)
love.graphics.circle('fill', axis.horizontal:getValue() * 100, axis.vertical:getValue() * 100, 5, 100)
love.graphics.printf('The square represents two axes. It can be operated by the left analog stick on joystick 1 or the arrow keys. The inner square is the deadzone.', -75, 120, 150, 'center')
love.graphics.printf('The square represents two axes. It can be operated by the left analog stick on joystick 1 or the arrow keys. The inner square is the deadzone (custom set to 25%).', -75, 120, 150, 'center')

love.graphics.pop()

Expand All @@ -114,7 +116,7 @@ function love.draw()
leftButtonDisplay:draw()
downButtonDisplay:draw()
rightButtonDisplay:draw()
love.graphics.printf('These are 4 directional buttons. They light up when held down, and flash when pressed or released. These can be operated by both the left analog stick on joystick 1 and the arrow keys.', 0, 220, 150, 'center')
love.graphics.printf('These are 4 directional buttons. They light up when held down, and flash when pressed or released. These can be operated by both the left analog stick on joystick 1 and the arrow keys. These particular ones are set with a threshold of 0.5.', 0, 220, 150, 'center')

love.graphics.pop()

Expand Down
16 changes: 2 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,6 @@ API

What do you know, it's just like every other library!

###Main module

####Functions

`tactile.rescan()`

Refreshes the internal list of joysticks.

####Properties

Tactile has the following options you can set:

- `tactile.deadzone` is the deadzone amount to use for axes.

###Button detectors

A button detector is simply a function that returns true or false. Each button detector represents a single source of binary input, like a keyboard key or gamepad button. For example, here is a completely valid button detector:
Expand Down Expand Up @@ -192,3 +178,5 @@ Removes an axis detector from the axis.
`axis:getValue()`

Returns the current value of the axis.

You can change the deadzone of the axis by setting `axis.deadzone`. Each axis has an individual deadzone setting. By default, it is 0.5.
50 changes: 34 additions & 16 deletions tactile.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local tactile = {
_VERSION = 'Tactile v1.0.0',
_DESCRIPTION = 'A simple and straightfoward input library for LOVE.',
_VERSION = 'Tactile v1.1.0',
_DESCRIPTION = 'A simple and straightfoward input library for LÖVE.',
_URL = 'https://github.com/tesselode/tactile',
_LICENSE = [[
The MIT License (MIT)
Expand Down Expand Up @@ -45,8 +45,8 @@ function Button:update()
self.down = false

--check whether any detectors are down
for _, detector in pairs(self.detectors) do
if detector() then
for i = 1, #self.detectors do
if self.detectors[i]() then
self.down = true
break
end
Expand All @@ -73,9 +73,9 @@ function Axis:getValue()
self.value = 0

--check whether any detectors have a value greater than the deadzone
for _, detector in pairs(self.detectors) do
local value = detector()
if math.abs(value) > tactile.deadzone then
for i = 1, #self.detectors do
local value = self.detectors[i]()
if math.abs(value) > self.deadzone then
self.value = value
end
end
Expand All @@ -93,34 +93,43 @@ end

--main module
tactile.__index = tactile
tactile.deadzone = .25
tactile.gamepads = love.joystick.getJoysticks()

function tactile.rescan()
tactile.gamepads = love.joystick.getJoysticks()
end

--button detectors
function tactile.key(key)
assert(type(key) == 'string',
'key should be a KeyConstant (string)')

return function()
return love.keyboard.isDown(key)
end
end

function tactile.gamepadButton(button, gamepadNum)
assert(type(button) == 'string',
'button should be a GamepadButton (string)')
assert(type(gamepadNum) == 'number',
'gamepadNum should be a number')

return function()
local gamepad = tactile.gamepads[gamepadNum]
local gamepad = love.joystick.getJoysticks()[gamepadNum]
return gamepad and gamepad:isGamepadDown(button)
end
end

function tactile.mouseButton(button)
assert(type(button) == 'string',
'button should be a GamepadButton (string)')

return function()
return love.mouse.isDown(button)
end
end

function tactile.thresholdButton(axisDetector, threshold)
assert(axisDetector, 'No axisDetector supplied')
assert(type(threshold) == 'number',
'threshold should be a number')

return function()
local value = axisDetector()
return value and math.abs(value) > math.abs(threshold) and (value < 0) == (threshold < 0)
Expand All @@ -129,6 +138,9 @@ end

--axis detectors
function tactile.binaryAxis(negative, positive)
assert(negative, 'No negative button detector supplied')
assert(positive, 'No positive button detector supplied')

return function()
local negativeValue, positiveValue = negative(), positive()
if negativeValue and not positiveValue then
Expand All @@ -142,8 +154,13 @@ function tactile.binaryAxis(negative, positive)
end

function tactile.analogStick(axis, gamepadNum)
assert(type(axis) == 'string',
'axis should be a GamepadAxis (string)')
assert(type(gamepadNum) == 'number',
'gamepadNum should be a number')

return function()
local gamepad = tactile.gamepads[gamepadNum]
local gamepad = love.joystick.getJoysticks()[gamepadNum]
return gamepad and gamepad:getGamepadAxis(axis) or 0
end
end
Expand All @@ -161,7 +178,8 @@ end
--axis constructor
function tactile.newAxis(...)
local axisInstance = {
detectors = {...}
detectors = {...},
deadzone = 0.5
}
return setmetatable(axisInstance, Axis)
end
Expand Down

0 comments on commit c32cbcb

Please # to comment.