InputField - text input handling library

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
ReFreezed
Party member
Posts: 347
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

InputField - text input handling library

Post by ReFreezed »

InputField enables simple handling of user text input into your program. The library is a single file with no external dependencies.

Features:
  • Different field types: Single-line, multi-line (with or without wrapping), password (obscured characters).
  • Text cursor and navigation (by keyboard and mouse).
  • Text selection.
  • Scrolling, both vertical and horizontal.
  • Shortcuts for common operations, like copying selected text and deleting the next word.
  • Undo and redo (history).
  • Helper functions for rendering.
The library does not do any rendering itself, but provides helper functions for rendering text, cursors and selections.

Download latest release Image
Direct link to library file
Example program
Repository

Basic usage:

Code: Select all

local InputField = require("InputField")
local field      = InputField("Initial text.")

local fieldX = 80
local fieldY = 50

love.keyboard.setKeyRepeat(true)

function love.keypressed(key, scancode, isRepeat)
	field:keypressed(key, isRepeat)
end
function love.textinput(text)
	field:textinput(text)
end

function love.mousepressed(mx, my, mbutton, pressCount)
	field:mousepressed(mx-fieldX, my-fieldY, mbutton, pressCount)
end
function love.mousemoved(mx, my)
	field:mousemoved(mx-fieldX, my-fieldY)
end
function love.mousereleased(mx, my, mbutton)
	field:mousereleased(mx-fieldX, my-fieldY, mbutton)
end

function love.draw()
	love.graphics.setColor(0, 0, 1)
	for _, x, y, w, h in field:eachSelection() do
		love.graphics.rectangle("fill", fieldX+x, fieldY+y, w, h)
	end

	love.graphics.setColor(1, 1, 1)
	for _, text, x, y in field:eachVisibleLine() do
		love.graphics.print(text, fieldX+x, fieldY+y)
	end

	local x, y, h = field:getCursorLayout()
	love.graphics.rectangle("fill", fieldX+x, fieldY+y, 1, h)
end
See the library file for documentation. Also look at the example program.
Last edited by ReFreezed on Sat Jan 15, 2022 7:31 pm, edited 3 times in total.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 2938
Joined: Sun Oct 18, 2015 2:58 pm

Re: InputField - text input handling library

Post by pgimeno »

Not providing rendering is a quite radical approach. I like it :)

Nice work. It's not often that you see so much love placed in an edit control. Almost all fall short of standard features. Slab and Thranduil were the closest to full features editors I've seen before this one, and both missed on some.

I've found a problem. Double-click to select word does not work well in presence of multibyte characters, and it appears that it doesn't work well when using Ctrl+Arrow either, so maybe it's about the word splitter. As for features, it's pretty complete, congrats! In my tests I only missed the double-click-and-drag feature to select multiple words, and the Shift+Del key combination as equivalent to Ctrl+X.
User avatar
ReFreezed
Party member
Posts: 347
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: InputField - text input handling library

Post by ReFreezed »

Thanks for the feedback, pgimeno!

Because rendering tend to be different in every game/GUI system I thought having a lower-level library like this would be good for handling text input as it's more portable. (I've actually used a version of the library for several years in multiple different projects and it's been working nicely.)

Yeah, word navigation was broken (fixed in repo). I noticed utf8.codes() generates garbage after I did most testing so I hastily implemented my own version (very incorrectly). Also, the library doesn't support non-ASCII punctuation and whitespace yet - it sees all non-ASCII characters as letters. It'll be fixed soon.

I'll add double-click-and-drag and the missing shortcut to the todo list.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
ReFreezed
Party member
Posts: 347
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: InputField - text input handling library

Post by ReFreezed »

Update 3.1

Changes since 3.0:
  • Added double-click-and-drag to select words.
  • Added shortcut Shift+Delete to cut text.
  • Pressing Escape while dragging stops the dragging.
  • Fixed word navigation/selection not working.
  • Better Unicode support for word navigation/selection.
  • Fixed freezing issue when calling some methods on fields that never had any text.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests