diff options
author | Matthias Richter <vrld@vrld.org> | 2012-02-07 23:10:29 +0100 |
---|---|---|
committer | Matthias Richter <vrld@vrld.org> | 2012-02-07 23:10:29 +0100 |
commit | 40dbc7134bae5cff48c2262d92721a3dd5394f84 (patch) | |
tree | de6097abf421b873aa6052928c56fbd578bafa01 /input.lua | |
download | Quickie-40dbc7134bae5cff48c2262d92721a3dd5394f84.tar.gz Quickie-40dbc7134bae5cff48c2262d92721a3dd5394f84.tar.bz2 Quickie-40dbc7134bae5cff48c2262d92721a3dd5394f84.tar.xz Quickie-40dbc7134bae5cff48c2262d92721a3dd5394f84.zip |
Initial commit
Diffstat (limited to 'input.lua')
-rw-r--r-- | input.lua | 43 |
1 files changed, 43 insertions, 0 deletions
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 |