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.
TextBox in LOVE2d?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: TextBox in LOVE2d?
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.
More info about suit in general here and about the input widget 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
Re: TextBox in LOVE2d?
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?
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.
-
- Prole
- Posts: 2
- Joined: Fri Nov 06, 2020 10:41 pm
Re: TextBox in LOVE2d?
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'
Who is online
Users browsing this forum: Google [Bot] and 4 guests