TextInput - A simple textbox class with proper cursor
Posted: Sat May 14, 2011 11:08 pm
TextInput 1.0 - for love2d 0.7.2
This class creates a simple input textbox with a proper cursor that blinks and can be moved around. It supports moving the cursor, deleting characters at cursor with del, deleting characters before cursor with backspace, uppercase characters and a FEW symbols. You can add more symbols if you want but keep in mind that it won't work for people with a different keyboard layout.
This is based on kikito's middleclass library which I have included. All credits to kikito for the middleclass library.
Download --> HERE.
Constructor Arguments:
- x: horizontal position
- y: vertical position
- size: maximum length of the string
- w: width of the textbox
- callback: the function that is called when you hit enter
Methods:
Useful Member Variables:
- TextInput.text: Contains the inputted text
- TextInput.cursor_pos: the cursor position
If you want more just look at the source, it's pretty simple.
How To Use It:
- Put TextInput.lua and BSD-LICENSE.txt in your project folder
- Copy lib folder to your project folder
- Include it in your sources like this:
- Here's a basic example on how you would use this class:
This class creates a simple input textbox with a proper cursor that blinks and can be moved around. It supports moving the cursor, deleting characters at cursor with del, deleting characters before cursor with backspace, uppercase characters and a FEW symbols. You can add more symbols if you want but keep in mind that it won't work for people with a different keyboard layout.
This is based on kikito's middleclass library which I have included. All credits to kikito for the middleclass library.
Download --> HERE.
Constructor Arguments:
- x: horizontal position
- y: vertical position
- size: maximum length of the string
- w: width of the textbox
- callback: the function that is called when you hit enter
Methods:
Code: Select all
TextInput:reset()
Clears the text in the textbox and resets the cursor.
Arguments:
- none
Code: Select all
TextInput:step()
Updates the textbox status. It MUST be called on love.update (or any function that is called in love.update) while the textbox is visibile.
Arguments:
- k: you might know this as dt (Delta Time). It's love.update's argument and you must pass it over to TextInput:step()
Code: Select all
TextInput:draw()
Draws the textbox on screen. It MUST be called on love.draw (or any function that is called in love.draw)
Arguments:
- none
Code: Select all
TextInput:keypressed()
Handles key input. It MUST be called on love.keypressed (or any function that is called in love.keypressed)
Arguments:
- key: The key that was pressed. This is a love.keypressed argument and you must pass it over to this function.
- unicode: The key code of the key that was pressed. This is a love.keypressed argument and you must pass it over to this function.
- TextInput.text: Contains the inputted text
- TextInput.cursor_pos: the cursor position
If you want more just look at the source, it's pretty simple.
How To Use It:
- Put TextInput.lua and BSD-LICENSE.txt in your project folder
- Copy lib folder to your project folder
- Include it in your sources like this:
Code: Select all
require 'lib.middleclass'
require 'TextInput'
Code: Select all
require 'lib.middleclass'
require 'TextInput'
function love.load()
state = "input"
textbox = TextInput(
love.graphics.getWidth()/2,
love.graphics.getHeight()/2,
11,
300,
function ()
state = "done"
end
)
love.keyboard.setKeyRepeat(500, 50) -- This is required if you want to hold down keys to spam them
end
function love.update(k)
if state == "input" then
textbox:step(k)
end
end
function love.draw()
if state == "input" then
textbox:draw()
elseif state == "done" then
love.graphics.print(
"You typed: " .. textbox.text,
love.graphics.getWidth()/2,
love.graphics.getHeight()/2
)
end
end
function love.keypressed(key, unicode)
if state == "input" then
textbox:keypressed(key, unicode)
end
end