Merge 155eb193b5
into 61a4172edd
This commit is contained in:
commit
9d07ed2168
3 changed files with 15 additions and 2 deletions
|
@ -25,6 +25,9 @@ local suit = require 'suit'
|
||||||
-- storage for text input
|
-- storage for text input
|
||||||
local input = {text = ""}
|
local input = {text = ""}
|
||||||
|
|
||||||
|
-- By default, backspace only registers once, if you want classic GUI backspace that
|
||||||
|
-- Keeps removing chars when pushed down, add this line: love.keyboard.setKeyRepeat(true)
|
||||||
|
|
||||||
-- all the UI is defined in love.update or functions that are called from here
|
-- all the UI is defined in love.update or functions that are called from here
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
-- put the layout origin at position (100,100)
|
-- put the layout origin at position (100,100)
|
||||||
|
|
|
@ -17,7 +17,10 @@ return function(core, input, ...)
|
||||||
w = w or text_width + 6
|
w = w or text_width + 6
|
||||||
h = h or opt.font:getHeight() + 4
|
h = h or opt.font:getHeight() + 4
|
||||||
|
|
||||||
|
input.label = input.label or ""
|
||||||
|
input.password = input.password or false
|
||||||
input.text = input.text or ""
|
input.text = input.text or ""
|
||||||
|
if input.label ~= "" then input.text = input.label end
|
||||||
input.cursor = math.max(1, math.min(utf8.len(input.text)+1, input.cursor or utf8.len(input.text)+1))
|
input.cursor = math.max(1, math.min(utf8.len(input.text)+1, input.cursor or utf8.len(input.text)+1))
|
||||||
-- cursor is position *before* the character (including EOS) i.e. in "hello":
|
-- cursor is position *before* the character (including EOS) i.e. in "hello":
|
||||||
-- position 1: |hello
|
-- position 1: |hello
|
||||||
|
@ -29,7 +32,8 @@ return function(core, input, ...)
|
||||||
opt.cursor_pos = 0
|
opt.cursor_pos = 0
|
||||||
if input.cursor > 1 then
|
if input.cursor > 1 then
|
||||||
local s = input.text:sub(1, utf8.offset(input.text, input.cursor)-1)
|
local s = input.text:sub(1, utf8.offset(input.text, input.cursor)-1)
|
||||||
opt.cursor_pos = opt.font:getWidth(s)
|
if password then opt.cursor_pos = opt.font:getWidth(string.rep("*", string.len(input.text)))
|
||||||
|
else opt.cursor_pos = opt.font:getWidth(s) end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- compute drawing offset
|
-- compute drawing offset
|
||||||
|
@ -58,6 +62,7 @@ return function(core, input, ...)
|
||||||
opt.hasKeyboardFocus = core:grabKeyboardFocus(opt.id)
|
opt.hasKeyboardFocus = core:grabKeyboardFocus(opt.id)
|
||||||
|
|
||||||
if opt.hasKeyboardFocus then
|
if opt.hasKeyboardFocus then
|
||||||
|
if input.label ~= "" then input.text = "" input.label = "" input.cursor = 1 end
|
||||||
local keycode,char = core:getPressedKey()
|
local keycode,char = core:getPressedKey()
|
||||||
-- text input
|
-- text input
|
||||||
if char and char ~= "" then
|
if char and char ~= "" then
|
||||||
|
|
|
@ -121,7 +121,12 @@ function theme.Input(input, opt, x,y,w,h)
|
||||||
-- text
|
-- text
|
||||||
love.graphics.setColor((opt.color and opt.color.normal and opt.color.normal.fg) or theme.color.normal.fg)
|
love.graphics.setColor((opt.color and opt.color.normal and opt.color.normal.fg) or theme.color.normal.fg)
|
||||||
love.graphics.setFont(opt.font)
|
love.graphics.setFont(opt.font)
|
||||||
|
if not input.password then love.graphics.print(input.text, x, y+(h-th)/2) end
|
||||||
|
if input.password and input.text ~= input.label then
|
||||||
|
love.graphics.print(string.rep("*", string.len(input.text)), x, y+(h-th)/2)
|
||||||
|
else
|
||||||
love.graphics.print(input.text, x, y+(h-th)/2)
|
love.graphics.print(input.text, x, y+(h-th)/2)
|
||||||
|
end
|
||||||
|
|
||||||
-- cursor
|
-- cursor
|
||||||
if opt.hasKeyboardFocus and (love.timer.getTime() % 1) > .5 then
|
if opt.hasKeyboardFocus and (love.timer.getTime() % 1) > .5 then
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue