From 66a089a07f4d24557cb8e06f78eefb07a344ea32 Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Wed, 11 Dec 2013 15:19:04 +0100 Subject: Fix bug in input.lua, make 0.9-ready, add utf8 editing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1) [input.lua] Pressing backspace while the cursor was at the beggining of the text removed the first character. Fixed thanks to riidom. 2) [LÖVE 0.9] Use setLine(Width|Style) instead of setLine. Split keyboard.pressed() into keyboard.pressed() and keyboard.textinput(). (see readme) 3) [utf8.lua] Add support for UTF-8 text editing. May still fail spectacurlarly with invalid UTF-8 strings. --- input.lua | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'input.lua') diff --git a/input.lua b/input.lua index ad0683f..b161993 100644 --- a/input.lua +++ b/input.lua @@ -29,6 +29,7 @@ local core = require(BASE .. 'core') local group = require(BASE .. 'group') local mouse = require(BASE .. 'mouse') local keyboard = require(BASE .. 'keyboard') +local utf8 = require(BASE .. 'utf8') -- {info = {text = "", cursor = text:len()}, pos = {x, y}, size={w, h}, widgetHit=widgetHit, draw=draw} return function(w) @@ -45,11 +46,13 @@ return function(w) if not keyboard.hasFocus(id) then --[[nothing]] -- editing - elseif keyboard.key == 'backspace' then - w.info.text = w.info.text:sub(1,w.info.cursor-1) .. w.info.text:sub(w.info.cursor+1) + elseif keyboard.key == 'backspace' and w.info.cursor > 0 then w.info.cursor = math.max(0, w.info.cursor-1) + local left, right = utf8.split(w.info.text, w.info.cursor) + w.info.text = left .. utf8.sub(right, 2) elseif keyboard.key == 'delete' then - w.info.text = w.info.text:sub(1,w.info.cursor) .. w.info.text:sub(w.info.cursor+2) + local left, right = utf8.split(w.info.text, w.info.cursor) + w.info.text = left .. utf8.sub(right, 2) w.info.cursor = math.min(w.info.text:len(), w.info.cursor) -- movement elseif keyboard.key == 'left' then @@ -64,10 +67,9 @@ return function(w) elseif keyboard.key == 'return' then keyboard.clearFocus() keyboard.pressed('', -1) - elseif keyboard.code >= 32 and keyboard.code < 127 then - local left = w.info.text:sub(1,w.info.cursor) - local right = w.info.text:sub(w.info.cursor+1) - w.info.text = table.concat{left, string.char(keyboard.code), right} + elseif keyboard.str then + local left, right = utf8.split(w.info.text, w.info.cursor) + w.info.text = left .. keyboard.str .. right w.info.cursor = w.info.cursor + 1 end -- cgit v1.2.3-54-g00ecf