Page 1 of 1
TextBox in LOVE2d?
Posted: Fri Nov 03, 2017 9:42 pm
by Tiago059
Good evening
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.
Re: TextBox in LOVE2d?
Posted: Sat Nov 04, 2017 10:42 am
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.
Re: TextBox in LOVE2d?
Posted: Sat Nov 04, 2017 3:04 pm
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
Re: TextBox in LOVE2d?
Posted: Mon Nov 06, 2017 6:25 pm
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.
Re: TextBox in LOVE2d?
Posted: Sat Nov 07, 2020 12:41 am
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
and that's all