Files
ljkiwi/tmp/ex.lua
2024-02-22 20:58:47 -06:00

91 lines
2.3 KiB
Lua

local kiwi = require("kiwi")
local Var = kiwi.Var
local Button = setmetatable({}, {
__call = function(_, identifier)
return setmetatable({
left = Var(identifier .. " left"),
width = Var(identifier .. " width"),
}, {
__tostring = function(self)
return "Button(" .. self.left:value() .. ", " .. self.width:value() .. ")"
end,
})
end,
})
local b1 = Button("b1")
local b2 = Button("b2")
local left_edge = Var("left")
local right_edge = Var("width")
local STRONG = kiwi.Strength.STRONG
local constraints
for i = 1, 200 do
q = kiwi.Term(left_edge, 2.0)
end
for i = 1, 50, 1 do
-- stylua: ignore start
constraints = {
left_edge :eq(0.0),
-- two buttons are the same width
b1.width :eq(b2.width),
-- button1 starts 50 for the left margin
(1.00 * b1.left) :eq(left_edge + 50),
-- button2 ends 50 from the right margin
right_edge :eq(b2.left + b2.width + 50),
-- button2 starts at least 100 from the end of button1. This is the "elastic" constraint
b2.left :ge(b1.left + b1.width + 100),
-- button1 has a minimum width of 87
b1.width :ge(87),
-- button1 has a preferred width of 87
b1.width :eq(87, STRONG),
-- button2 has minimum width of 113
b2.width :ge(113),
-- button2 has a preferred width of 113
b2.width :eq(113, STRONG),
}
-- stylua: ignore end
--
k = kiwi.Strength.create(0.0, 1.0, 0.0, 1.25)
end
local solver = kiwi.Solver()
for _, c in ipairs(constraints) do
print(c)
solver:add_constraint(c)
end
solver:update_vars()
print(b1) -- Button(50, 113)
print(b2) -- Button(263, 113)
print(left_edge:value()) -- 0
print(right_edge:value()) -- 426
solver:add_edit_var(right_edge, STRONG)
solver:suggest_value(right_edge, 500)
solver:update_vars()
-- solver:dump()
-- print(b1) -- Button(50, 113)
-- print(b2) -- Button(337, 113)
-- print(right_edge:value()) -- 500
-- print(solver:dumps())
-- solver = kiwi.Solver()
-- local trailing = Var("trailing")
-- local leading = Var("leading")
-- solver:add_constraint(kiwi.new_single_constraint(trailing, 86))
-- solver:add_constraint(kiwi.new_pair_constraint(trailing, leading, 8.0))
-- print(solver:dumps())
-- solver:update_vars()
-- print(trailing:value()) -- 86
-- print(leading:value()) -- 76