Added Label and Password options

This commit is contained in:
Whitecl4ws 2017-01-28 12:00:07 -05:00
parent 67642622e4
commit 6044a1f901
2 changed files with 12 additions and 1 deletions

View file

@ -17,7 +17,10 @@ return function(core, input, ...)
w = w or text_width + 6
h = h or opt.font:getHeight() + 4
input.label = input.label or ""
input.password = input.password or false
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))
-- cursor is position *before* the character (including EOS) i.e. in "hello":
-- position 1: |hello
@ -26,9 +29,11 @@ return function(core, input, ...)
-- position 6: hello|
-- get size of text and cursor position
opt.cursor_pos = 0
if input.cursor > 1 then
local s = input.text:sub(1, utf8.offset(input.text, input.cursor)-1)
if input.password then s = string.rep("*", string.len(input.text)) end
opt.cursor_pos = opt.font:getWidth(s)
end
@ -53,6 +58,7 @@ return function(core, input, ...)
opt.hasKeyboardFocus = core:grabKeyboardFocus(opt.id)
if opt.hasKeyboardFocus then
if input.label ~= "" then input.text = "" input.label = "" input.cursor = 1 end
local keycode,char = core:getPressedKey()
-- text input
if char and char ~= "" then

View file

@ -121,7 +121,12 @@ function theme.Input(input, opt, x,y,w,h)
-- text
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.print(input.text, x, y+(h-th)/2)
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)
end
-- cursor
if opt.hasKeyboardFocus and (love.timer.getTime() % 1) > .5 then