EZControls - Control handling made easy.

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Firelight
Prole
Posts: 2
Joined: Tue Dec 08, 2015 10:11 pm

EZControls - Control handling made easy.

Post by Firelight »

EZControls allows you to bind keys to binding objects and add onPress and onRelease callbacks to said objects. It sports support for separate bindings for different gamestates as well as built in utilities to help load and save keybinds to files in a human readable format.

GitHub

By default, it checks if the love key events are defined (love.keypressed(), etc) and defines them if they aren't. Functions for firing control events are provided in case they're defined.

Small Example:

Code: Select all

local controls = require('EZControls') -- Load the library.
controls.currentState = 'game' -- Set gamestate to "game". This will only allow bindings in the gamestate "game" to work. There's a special gamestate called "all" that can contain bindings that work regardless of current gamestate.

-- Bind some keys.
controls.state('game').binding('shoot'):bind(' ') -- Bind space to binding "shoot".
controls.state('game').binding('jetpack_fire'):bind({'up', 'w'}) -- Bind up and w to binding "jetpack_fire"

-- Traditional isDown check.
function love.update(deltaTime)
  if controls.state('game').binding('shoot'):isDown() then
    -- shoot gun logic
  end
end

-- Callback style.
controls.state('game').binding('jetpack_fire'):onPress(function() -- Pass anonymous function as an argument to onPress function.
  enableJetpack()
end)

controls.state('game').binding('jetpack_fire'):onRelease(function()
  disableJetpack()
end)

local humanReadableKeybinds = controls.serialize() -- Dump keybinds to table.
controls.parse(humanReadableKeybinds) -- Load keybinds from table.

Full example on GitHub.
User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

Re: EZControls - Control handling made easy.

Post by appleide »

Looks good code. Will keep this in mind.
Firelight
Prole
Posts: 2
Joined: Tue Dec 08, 2015 10:11 pm

Re: EZControls - Control handling made easy.

Post by Firelight »

New Release: v0.4.0
Added a new way to bind keys. You can now call a binding's :bindnext() method to bind the next key pressed to the binding. The method accepts a function callback as the argument. The function callback is called with the key pressed passed as an argument.

Example:

Code: Select all

-- Load the library.
local controls = require('EZControls')

-- Set the controls library state.
controls.currentState = 'game'

 -- Binds the next key pressed to the binding "jetpack_fire".
controls.state('game').binding('jetpack_fire'):bindNext(function(key)
  print('binded ' .. key .. ' to binding "jetpack_fire"')
end)

-- Create callbacks for the bindings.
controls.state('game').binding('jetpack_fire'):onPress(function()
  enableJetpack()
end)

controls.state('game').binding('jetpack_fire'):onRelease(function()
  disableJetpack()
end)
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests