TextBox in LOVE2d?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Tiago059
Prole
Posts: 5
Joined: Fri Nov 03, 2017 9:34 pm

TextBox in LOVE2d?

Post by Tiago059 »

Good evening :awesome:

I try to create a simple text box what the user digita using the keyboard in love2d. I dit it, but i wanna insert a way to establish a limit of the caracthers what can be inserted.

Anybody can help me?
The code what i use to create the text box is equal to example in Wikia of love2d, in function love.textinput.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: TextBox in LOVE2d?

Post by vrld »

Shameless plug: Here is how to do it with SUIT. The important part is in the validate function, which uses string.gsub() to remove unwanted characters. You can do anything you like here.

Code: Select all

local suit = require 'suit'
local utf8 = require 'utf8'

local function validate(input)
  local len = utf8.len(input.text)

  -- allow only lowercase a-z (also no spaces, etc)
  input.text = input.text:gsub("[^a-z]", "")

  -- reset cursor to where it was before charater removal
  input.cursor = input.cursor - (len - utf8.len(input.text))
end

local input = {text = ""}
function love.update(dt)
  -- place textbox at (100,100) with with 200px and height 30px
  suit.Input(input, 100,100,200,30)
  validate(input)
end

function love.draw(dt)
  suit.draw()
end

function love.textinput(t)
  suit.textinput(t)
end

function love.keypressed(key)
  suit.keypressed(key)
end

-- if you want IME input, requires a font that can display the characters
function love.textedited(text, start, length)
  suit.textedited(text, start, length)
end
More info about suit in general here and about the input widget here.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Tiago059
Prole
Posts: 5
Joined: Fri Nov 03, 2017 9:34 pm

Re: TextBox in LOVE2d?

Post by Tiago059 »

Wow, i like veryyyyyy your code. I'm not home now, but i'll test your code in my program. Thanks very much and forgive my poor english :)
User avatar
Tiago059
Prole
Posts: 5
Joined: Fri Nov 03, 2017 9:34 pm

Re: TextBox in LOVE2d?

Post by Tiago059 »

To use suit in my code, i need to install the 'SUIT' how? How i install or a don't need to install? I like very much the SUIT.
derekbaker90
Prole
Posts: 2
Joined: Fri Nov 06, 2020 10:41 pm

Re: TextBox in LOVE2d?

Post by derekbaker90 »

Tiago059 wrote: Mon Nov 06, 2017 6:25 pm To use suit in my code, i need to install the 'SUIT' how? How i install or a don't need to install? I like very much the SUIT.
Just download the SUIT project (you can download the .zip file here https://suit.readthedocs.io/en/latest/i ... ml#suit-up
Extract it and then add all the .lua files to a folder in your proyect. For instance: vendor/suit and it contains button.lua, checkbox.lua, core.lua, etc.

Then, import it in your .lua file as

Code: Select all

suit = require 'vendor/suit'
and that's all
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 55 guests