From 40dbc7134bae5cff48c2262d92721a3dd5394f84 Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Tue, 7 Feb 2012 23:10:29 +0100 Subject: Initial commit --- input.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 input.lua (limited to 'input.lua') diff --git a/input.lua b/input.lua new file mode 100644 index 0000000..2ba29b5 --- /dev/null +++ b/input.lua @@ -0,0 +1,43 @@ +local core = require((...):match("^(.+)%.[^%.]+") .. '.core') + +return function(info, x,y,w,h, draw) + info.text = info.text or "" + info.cursor = math.min(info.cursor or info.text:len(), info.text:len()) + + local id = core.generateID() + core.mouse.updateState(id, x,y,w,h) + core.makeTabable(id) + if core.isActive(id) then core.setKeyFocus(id) end + + core.registerDraw(id, draw or core.style.Input, info.text, info.cursor, x,y,w,h) + + local changed = false + -- editing + if core.keyboard.key == 'backspace' then + info.text = info.text:sub(1,info.cursor-1) .. info.text:sub(info.cursor+1) + info.cursor = math.max(0, info.cursor-1) + changed = true + elseif core.keyboard.key == 'delete' then + info.text = info.text:sub(1,info.cursor) .. info.text:sub(info.cursor+2) + info.cursor = math.min(info.text:len(), info.cursor) + changed = true + -- movement + elseif core.keyboard.key == 'left' then + info.cursor = math.max(0, info.cursor-1) + elseif core.keyboard.key == 'right' then + info.cursor = math.min(info.text:len(), info.cursor+1) + elseif core.keyboard.key == 'home' then + info.cursor = 0 + elseif core.keyboard.key == 'end' then + info.cursor = info.text:len() + -- input + elseif core.keyboard.code >= 32 and core.keyboard.code < 127 then + local left = info.text:sub(1,info.cursor) + local right = info.text:sub(info.cursor+1) + info.text = table.concat{left, string.char(core.keyboard.code), right} + info.cursor = info.cursor + 1 + changed = true + end + + return changed +end -- cgit v1.2.3-54-g00ecf