#344 support hiding characters in text fields

This commit is contained in:
Mikayla Fischler 2023-09-23 16:45:33 -04:00
parent 18bcfb4014
commit 689d474796
2 changed files with 35 additions and 6 deletions

View File

@ -149,6 +149,19 @@ function core.new_ifield(e, max_len, fg_bg, dis_fg_bg)
---@class ifield ---@class ifield
local public = {} local public = {}
-- censor the display (for private info, for example) with the provided character<br>
-- disable by passing no argument
---@param censor string? character to hide data with
function public.censor(censor)
if type(censor) == "string" and string.len(censor) == 1 then
self.censor = censor
public.show()
else
self.censor = nil
public.show()
end
end
-- show the field -- show the field
function public.show() function public.show()
_update_visible() _update_visible()
@ -166,15 +179,23 @@ function core.new_ifield(e, max_len, fg_bg, dis_fg_bg)
e.w_write(string.rep(" ", e.frame.w)) e.w_write(string.rep(" ", e.frame.w))
e.w_set_cur(1, 1) e.w_set_cur(1, 1)
local function _write()
if self.censor then
e.w_write(string.rep(self.censor, string.len(self.visible_text)))
else
e.w_write(self.visible_text)
end
end
if e.is_focused() and e.enabled then if e.is_focused() and e.enabled then
-- write text with cursor -- write text with cursor
if self.selected_all then if self.selected_all then
e.w_set_bkg(fg_bg.fgd) e.w_set_bkg(fg_bg.fgd)
e.w_set_fgd(fg_bg.bkg) e.w_set_fgd(fg_bg.bkg)
e.w_write(self.visible_text) _write()
elseif self.cursor_pos >= (string.len(self.visible_text) + 1) then elseif self.cursor_pos >= (string.len(self.visible_text) + 1) then
-- write text with cursor at the end, no need to blit -- write text with cursor at the end, no need to blit
e.w_write(self.visible_text) _write()
e.w_set_fgd(colors.lightGray) e.w_set_fgd(colors.lightGray)
e.w_write("_") e.w_write("_")
else else
@ -188,13 +209,17 @@ function core.new_ifield(e, max_len, fg_bg, dis_fg_bg)
local b_fgd = string.rep(fg_bg.blit_fgd, self.cursor_pos - 1) .. a .. string.rep(fg_bg.blit_fgd, string.len(self.visible_text) - self.cursor_pos) local b_fgd = string.rep(fg_bg.blit_fgd, self.cursor_pos - 1) .. a .. string.rep(fg_bg.blit_fgd, string.len(self.visible_text) - self.cursor_pos)
local b_bkg = string.rep(fg_bg.blit_bkg, self.cursor_pos - 1) .. b .. string.rep(fg_bg.blit_bkg, string.len(self.visible_text) - self.cursor_pos) local b_bkg = string.rep(fg_bg.blit_bkg, self.cursor_pos - 1) .. b .. string.rep(fg_bg.blit_bkg, string.len(self.visible_text) - self.cursor_pos)
e.w_blit(self.visible_text, b_fgd, b_bkg) if self.censor then
e.w_blit(string.rep(self.censor, string.len(self.visible_text)), b_fgd, b_bkg)
else
e.w_blit(self.visible_text, b_fgd, b_bkg)
end
end end
else else
self.selected_all = false self.selected_all = false
-- write text without cursor -- write text without cursor
e.w_write(self.visible_text) _write()
end end
end end

View File

@ -9,6 +9,7 @@ local MOUSE_CLICK = core.events.MOUSE_CLICK
---@class text_field_args ---@class text_field_args
---@field value? string initial value ---@field value? string initial value
---@field max_len? integer maximum string length ---@field max_len? integer maximum string length
---@field censor? string character to replace text with when printing to screen
---@field dis_fg_bg? cpair foreground/background colors when disabled ---@field dis_fg_bg? cpair foreground/background colors when disabled
---@field parent graphics_element ---@field parent graphics_element
---@field id? string element id ---@field id? string element id
@ -20,7 +21,7 @@ local MOUSE_CLICK = core.events.MOUSE_CLICK
-- new text entry field -- new text entry field
---@param args text_field_args ---@param args text_field_args
---@return graphics_element element, element_id id ---@return graphics_element element, element_id id, function censor_ctl
local function text_field(args) local function text_field(args)
args.height = 1 args.height = 1
args.can_focus = true args.can_focus = true
@ -34,6 +35,8 @@ local function text_field(args)
-- make an interactive field manager -- make an interactive field manager
local ifield = core.new_ifield(e, args.max_len or e.frame.w, args.fg_bg, args.dis_fg_bg) local ifield = core.new_ifield(e, args.max_len or e.frame.w, args.fg_bg, args.dis_fg_bg)
ifield.censor(args.censor)
-- handle mouse interaction -- handle mouse interaction
---@param event mouse_interaction mouse event ---@param event mouse_interaction mouse event
function e.handle_mouse(event) function e.handle_mouse(event)
@ -96,7 +99,8 @@ local function text_field(args)
-- initial draw -- initial draw
ifield.show() ifield.show()
return e.complete() local elem, id = e.complete()
return elem, id, ifield.censor
end end
return text_field return text_field