graphics automatic constraints

This commit is contained in:
Mikayla 2024-06-05 00:31:06 +00:00
parent 4d87887709
commit 25ebf2c8c7
4 changed files with 23 additions and 5 deletions

View File

@ -7,7 +7,7 @@ local flasher = require("graphics.flasher")
local core = {} local core = {}
core.version = "2.2.4" core.version = "2.3.0"
core.flasher = flasher core.flasher = flasher
core.events = events core.events = events

View File

@ -82,9 +82,10 @@ end
-- a base graphics element, should not be created on its own -- a base graphics element, should not be created on its own
---@nodiscard ---@nodiscard
---@param args graphics_args arguments ---@param args graphics_args arguments
---@param constraint? function apply a dimensional constraint based on proposed dimensions function(frame) -> width, height
---@param child_offset_x? integer mouse event offset x ---@param child_offset_x? integer mouse event offset x
---@param child_offset_y? integer mouse event offset y ---@param child_offset_y? integer mouse event offset y
function element.new(args, child_offset_x, child_offset_y) function element.new(args, constraint, child_offset_x, child_offset_y)
local self = { local self = {
id = nil, ---@type element_id|nil id = nil, ---@type element_id|nil
is_root = args.parent == nil, is_root = args.parent == nil,
@ -226,6 +227,13 @@ function element.new(args, child_offset_x, child_offset_y)
local w, h = self.p_window.getSize() local w, h = self.p_window.getSize()
f.w = math.min(f.w, w - (f.x - 1)) f.w = math.min(f.w, w - (f.x - 1))
f.h = math.min(f.h, h - (f.y - 1)) f.h = math.min(f.h, h - (f.y - 1))
if type(constraint) == "function" then
-- constrain per provided constraint function (can only get smaller than available space)
w, h = constraint(w, h)
f.w = math.min(f.w, w)
f.h = math.min(f.h, h)
end
end end
-- check frame -- check frame

View File

@ -45,7 +45,7 @@ local function rectangle(args)
end end
-- create new graphics element base object -- create new graphics element base object
local e = element.new(args, offset_x, offset_y) local e = element.new(args, nil, offset_x, offset_y)
-- create content window for child elements -- create content window for child elements
e.content_window = window.create(e.window, 1 + offset_x, 1 + offset_y, e.frame.w - (2 * offset_x), e.frame.h - (2 * offset_y)) e.content_window = window.create(e.window, 1 + offset_x, 1 + offset_y, e.frame.w - (2 * offset_x), e.frame.h - (2 * offset_y))

View File

@ -10,12 +10,13 @@ local ALIGN = core.ALIGN
---@class textbox_args ---@class textbox_args
---@field text string text to show ---@field text string text to show
---@field alignment? ALIGN text alignment, left by default ---@field alignment? ALIGN text alignment, left by default
---@field anchor? boolean true to use this as an anchor, making it focusable
---@field parent graphics_element ---@field parent graphics_element
---@field id? string element id ---@field id? string element id
---@field x? integer 1 if omitted ---@field x? integer 1 if omitted
---@field y? integer auto incremented if omitted ---@field y? integer auto incremented if omitted
---@field width? integer parent width if omitted ---@field width? integer parent width if omitted
---@field height? integer parent height if omitted ---@field height? integer minimum necessary height for wrapped text if omitted
---@field gframe? graphics_frame frame instead of x/y/width/height ---@field gframe? graphics_frame frame instead of x/y/width/height
---@field fg_bg? cpair foreground/background colors ---@field fg_bg? cpair foreground/background colors
---@field hidden? boolean true to hide on initial draw ---@field hidden? boolean true to hide on initial draw
@ -26,8 +27,17 @@ local ALIGN = core.ALIGN
local function textbox(args) local function textbox(args)
element.assert(type(args.text) == "string", "text is a required field") element.assert(type(args.text) == "string", "text is a required field")
if args.anchor == true then args.can_focus = true end
-- regex to identify entries without a height currently: ^.*TextBox\{((?!height=).)*$
-- provide a constraint condition to element creation to prevent an pointlessly tall text box
---@param frame graphics_frame
local function constrain(frame)
return frame.w, math.max(args.height, math.max(1, #util.strwrap(args.text, frame.w)))
end
-- create new graphics element base object -- create new graphics element base object
local e = element.new(args) local e = element.new(args, constrain)
e.value = args.text e.value = args.text