Skip to content

Conversation

@snibo
Copy link

@snibo snibo commented Jul 26, 2025

As the title sais now you can make it so any function that can output a number between 0 and 1 to be it's own input type and can be used along side the other ones. For more details on how to use it check the bottom of the updated readme.md

This feature is useful for UI and especially for mobile games since it allows you to easily bind the touch controls using baton.

snibo added 4 commits July 26, 2025 14:17
Now you can have custom input types which means that any function that returns a number between 0 and 1 can be used as an input. Which is especially useful for touchscreen support.
Removed an unused piece of code
Added a "adding custom input types" section that explains how to use the "batton.newInputType()" function.

It is still not very clear and still has some things that need explaining and is missing an example.
Fleshed out the "add custom input types" section with an example and explaining the parsing functions.

Also mentioned that custom inputs types are not tracked as it's own input device
@tesselode
Copy link
Owner

Can you show an example of how this would be used for UI?

@snibo
Copy link
Author

snibo commented Jul 27, 2025

Here is a working demo all you have to do is past it in main.lua with my branch of baton in the same file.

What it does is using either the on screen buttons or the arrow keys (left/right) you can " browse through the pages".

local baton = require('baton')

local page = 0

local x, y = love.mouse.getPosition()
local rightClick = love.mouse.isDown(1)

-- initializing the buttons
local buttonSize = 100
local buttons = {
  back = {x = 0           , y = 200},
  next = {x = buttonSize*2, y = 200},
}

-- input type constructor to detect button presses
baton.newInputType('ui', function(value)
  local b = buttons[value]
  local s = buttonSize
  return (x > b.x and x < b.x+s and y > b.y and y < b.y+s and rightClick) and 1 or 0
end)

-- controls are set so you can browse through the pages with either
-- the ui or the arrow keys
local input = baton.new {
  controls = {
    back = {'key:left', 'ui:back'},
    next = {'key:right', 'ui:next'},
  }
}

function love.update(dt)
  x, y = love.mouse.getPosition()
  rightClick = love.mouse.isDown(1)
  input:update()
  
  if input:pressed('next') then
    page = page + 1
  elseif input:pressed('back') then
    page = page - 1
  end
end

-- rendering stuff don't pay attention
local function drawBox(x, y, w, h, r, g, b, name)
  local lg = love.graphics
  lg.setColor(r, g, b, 0.4)
  lg.rectangle('fill', x, y, w, h)
  lg.setColor(r, g, b, 1.0)
  lg.rectangle('line', x, y, w, h)
  lg.print(name, x + w/3, y + h/3)
  lg.setColor(1,1,1,1)
end

function love.draw()
  love.graphics.print('page: '..page, 0, 0)
  
  for name, button in pairs (buttons) do
    drawBox(button.x, button.y, buttonSize, buttonSize, 0, 0.5, 1, name)
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants