2022-06-06 19:42:39 +00:00
|
|
|
-- Text Box Graphics Element
|
|
|
|
|
2022-06-08 22:53:24 +00:00
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
|
|
|
local core = require("graphics.core")
|
2022-06-06 19:42:39 +00:00
|
|
|
local element = require("graphics.element")
|
|
|
|
|
2023-10-04 03:16:46 +00:00
|
|
|
local ALIGN = core.ALIGN
|
2022-06-08 22:53:24 +00:00
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
---@class textbox_args
|
|
|
|
---@field text string text to show
|
2023-10-04 03:16:46 +00:00
|
|
|
---@field alignment? ALIGN text alignment, left by default
|
2024-06-05 00:31:06 +00:00
|
|
|
---@field anchor? boolean true to use this as an anchor, making it focusable
|
2022-07-28 14:09:34 +00:00
|
|
|
---@field parent graphics_element
|
|
|
|
---@field id? string element id
|
2022-06-06 19:42:39 +00:00
|
|
|
---@field x? integer 1 if omitted
|
2023-07-10 03:42:44 +00:00
|
|
|
---@field y? integer auto incremented if omitted
|
2022-06-06 19:42:39 +00:00
|
|
|
---@field width? integer parent width if omitted
|
2024-06-05 00:31:06 +00:00
|
|
|
---@field height? integer minimum necessary height for wrapped text if omitted
|
2022-06-06 19:42:39 +00:00
|
|
|
---@field gframe? graphics_frame frame instead of x/y/width/height
|
|
|
|
---@field fg_bg? cpair foreground/background colors
|
2023-05-25 21:40:16 +00:00
|
|
|
---@field hidden? boolean true to hide on initial draw
|
2022-06-06 19:42:39 +00:00
|
|
|
|
|
|
|
-- new text box
|
|
|
|
---@param args textbox_args
|
2022-07-28 14:09:34 +00:00
|
|
|
---@return graphics_element element, element_id id
|
2022-06-06 19:42:39 +00:00
|
|
|
local function textbox(args)
|
2023-09-30 15:46:47 +00:00
|
|
|
element.assert(type(args.text) == "string", "text is a required field")
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2024-06-05 00:31:06 +00:00
|
|
|
if args.anchor == true then args.can_focus = true end
|
|
|
|
|
|
|
|
-- provide a constraint condition to element creation to prevent an pointlessly tall text box
|
|
|
|
---@param frame graphics_frame
|
|
|
|
local function constrain(frame)
|
2024-06-05 23:38:22 +00:00
|
|
|
local new_height = math.max(1, #util.strwrap(args.text, frame.w))
|
|
|
|
|
|
|
|
if args.height then
|
|
|
|
new_height = math.max(frame.h, new_height)
|
|
|
|
end
|
|
|
|
|
|
|
|
return frame.w, new_height
|
2024-06-05 00:31:06 +00:00
|
|
|
end
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
-- create new graphics element base object
|
2024-06-05 00:31:06 +00:00
|
|
|
local e = element.new(args, constrain)
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
e.value = args.text
|
|
|
|
|
2023-10-04 03:16:46 +00:00
|
|
|
local alignment = args.alignment or ALIGN.LEFT
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2022-06-08 22:53:24 +00:00
|
|
|
-- draw textbox
|
2023-09-29 23:34:10 +00:00
|
|
|
function e.redraw()
|
|
|
|
e.window.clear()
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
local lines = util.strwrap(e.value, e.frame.w)
|
2022-06-08 22:53:24 +00:00
|
|
|
|
2022-09-12 16:59:28 +00:00
|
|
|
for i = 1, #lines do
|
|
|
|
if i > e.frame.h then break end
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2024-08-26 00:29:52 +00:00
|
|
|
-- trim leading/trailing whitespace
|
|
|
|
lines[i] = util.trim(lines[i])
|
|
|
|
|
2022-09-12 16:59:28 +00:00
|
|
|
local len = string.len(lines[i])
|
|
|
|
|
|
|
|
-- use cursor position to align this line
|
2023-10-04 03:16:46 +00:00
|
|
|
if alignment == ALIGN.CENTER then
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_cur(math.floor((e.frame.w - len) / 2) + 1, i)
|
2023-10-04 03:16:46 +00:00
|
|
|
elseif alignment == ALIGN.RIGHT then
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_cur((e.frame.w - len) + 1, i)
|
2022-09-12 16:59:28 +00:00
|
|
|
else
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_cur(1, i)
|
2022-09-12 16:59:28 +00:00
|
|
|
end
|
|
|
|
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_write(lines[i])
|
2022-06-06 19:42:39 +00:00
|
|
|
end
|
2022-09-12 16:59:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- set the string value and re-draw the text
|
|
|
|
---@param val string value
|
|
|
|
function e.set_value(val)
|
2023-09-29 23:34:10 +00:00
|
|
|
e.value = val
|
|
|
|
e.redraw()
|
2022-06-06 19:42:39 +00:00
|
|
|
end
|
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
-- initial draw
|
|
|
|
e.redraw()
|
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
return e.complete()
|
2022-06-06 19:42:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return textbox
|