TextInput - A simple textbox class with proper cursor

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Franc[e]sco
Prole
Posts: 19
Joined: Sun May 08, 2011 9:09 am

TextInput - A simple textbox class with proper cursor

Post by Franc[e]sco »

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:

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.
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:

Code: Select all

require 'lib.middleclass'
require 'TextInput'
- Here's a basic example on how you would use this class:

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
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: TextInput - A simple textbox class with proper cursor

Post by Petunien »

Hi,

may you reupload your file? It isn't available. Thank you in advance. :)
"Docendo discimus" - Lucius Annaeus Seneca
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: TextInput - A simple textbox class with proper cursor

Post by trubblegum »

Look here for how to capture keystrokes : https://github.com/vrld/Quickie
Also might find something useful below.
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: TextInput - A simple textbox class with proper cursor

Post by Petunien »

Thank you! :)
"Docendo discimus" - Lucius Annaeus Seneca
trevorstarick
Prole
Posts: 1
Joined: Sun Jun 03, 2012 11:15 am

Re: TextInput - A simple textbox class with proper cursor

Post by trevorstarick »

Is shift working? It doesn't seem like it on my computer.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests