From 689d4747962af7302f2e30979aadeb09bd62fbb1 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Sat, 23 Sep 2023 16:45:33 -0400 Subject: [PATCH] #344 support hiding characters in text fields --- graphics/core.lua | 33 +++++++++++++++++++++++---- graphics/elements/form/text_field.lua | 8 +++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/graphics/core.lua b/graphics/core.lua index 46110c2..ee7d829 100644 --- a/graphics/core.lua +++ b/graphics/core.lua @@ -149,6 +149,19 @@ function core.new_ifield(e, max_len, fg_bg, dis_fg_bg) ---@class ifield local public = {} + -- censor the display (for private info, for example) with the provided character
+ -- 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 function public.show() _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_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 -- write text with cursor if self.selected_all then e.w_set_bkg(fg_bg.fgd) 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 -- 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_write("_") 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_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 else self.selected_all = false -- write text without cursor - e.w_write(self.visible_text) + _write() end end diff --git a/graphics/elements/form/text_field.lua b/graphics/elements/form/text_field.lua index 7ac786a..3211676 100644 --- a/graphics/elements/form/text_field.lua +++ b/graphics/elements/form/text_field.lua @@ -9,6 +9,7 @@ local MOUSE_CLICK = core.events.MOUSE_CLICK ---@class text_field_args ---@field value? string initial value ---@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 parent graphics_element ---@field id? string element id @@ -20,7 +21,7 @@ local MOUSE_CLICK = core.events.MOUSE_CLICK -- new text entry field ---@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) args.height = 1 args.can_focus = true @@ -34,6 +35,8 @@ local function text_field(args) -- 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) + ifield.censor(args.censor) + -- handle mouse interaction ---@param event mouse_interaction mouse event function e.handle_mouse(event) @@ -96,7 +99,8 @@ local function text_field(args) -- initial draw ifield.show() - return e.complete() + local elem, id = e.complete() + return elem, id, ifield.censor end return text_field