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

@@ -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