From 25ebf2c8c73105018ece879c2976ff36c0afa029 Mon Sep 17 00:00:00 2001 From: Mikayla Date: Wed, 5 Jun 2024 00:31:06 +0000 Subject: [PATCH] graphics automatic constraints --- graphics/core.lua | 2 +- graphics/element.lua | 10 +++++++++- graphics/elements/rectangle.lua | 2 +- graphics/elements/textbox.lua | 14 ++++++++++++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/graphics/core.lua b/graphics/core.lua index 830ca69..f6a647a 100644 --- a/graphics/core.lua +++ b/graphics/core.lua @@ -7,7 +7,7 @@ local flasher = require("graphics.flasher") local core = {} -core.version = "2.2.4" +core.version = "2.3.0" core.flasher = flasher core.events = events diff --git a/graphics/element.lua b/graphics/element.lua index 731a22c..6f859ab 100644 --- a/graphics/element.lua +++ b/graphics/element.lua @@ -82,9 +82,10 @@ end -- a base graphics element, should not be created on its own ---@nodiscard ---@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_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 = { id = nil, ---@type element_id|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() f.w = math.min(f.w, w - (f.x - 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 -- check frame diff --git a/graphics/elements/rectangle.lua b/graphics/elements/rectangle.lua index 252790b..eceb9bd 100644 --- a/graphics/elements/rectangle.lua +++ b/graphics/elements/rectangle.lua @@ -45,7 +45,7 @@ local function rectangle(args) end -- 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 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)) diff --git a/graphics/elements/textbox.lua b/graphics/elements/textbox.lua index 07a8736..5a02579 100644 --- a/graphics/elements/textbox.lua +++ b/graphics/elements/textbox.lua @@ -10,12 +10,13 @@ local ALIGN = core.ALIGN ---@class textbox_args ---@field text string text to show ---@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 id? string element id ---@field x? integer 1 if omitted ---@field y? integer auto incremented 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 fg_bg? cpair foreground/background colors ---@field hidden? boolean true to hide on initial draw @@ -26,8 +27,17 @@ local ALIGN = core.ALIGN local function textbox(args) 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 - local e = element.new(args) + local e = element.new(args, constrain) e.value = args.text