summaryrefslogtreecommitdiffstats
path: root/lua-bindings/diaphragm.lua
blob: d4e3dc93b97b208de2a5933aea8184e7135a15d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
-- TODO: split file

local M = {}

M.core = require("libdiaphragm")

M.util = {}

function M.util.eprint(s, ...)
	io.stderr:write(string.format(s, ...))
	io.stderr:write("\n")
end

function M.util.list_concat(...)
	local result = {}
	for i = 1, select("#", ...) do
		local current = select(i, ...)
		if current ~= nil then
			for _, v in ipairs(current) do
				table.insert(result, v)
			end
		end
	end
	return result
end

function M.util.list_intersperse_with(func, list)
	local result = {}

	for i = 1, #list - 1 do
		table.insert(result, list[i])
		table.insert(result, func())
	end
	table.insert(result, list[#list])

	return result
end

function M.util.tbl_map(func, t)
	local result = {}
	for k, v in pairs(t) do
		result[k] = func(v)
	end
	return result
end

function M.util.tbl_print(t)
	io.stderr:write("{ ")
	for k, v in pairs(t) do
		io.stderr:write(string.format("%s = %s, ", k, v))
	end
	io.stderr:write("}\n")
end

function M.util.tbl_extend(...)
	local result = {}
	for i = 1, select("#", ...) do
		local current = select(i, ...)
		if current ~= nil then
			for k, v in pairs(current) do
				result[k] = v
			end
		end
	end
	return result
end

function M.util.tbl_contains(t, value)
	for _, v in pairs(t) do
		if v == value then
			return true
		end
	end
	return false
end

function M.util.tbl_filter_by_key(func, t)
	local result = {}
	for k, v in pairs(t) do
		if func(k) then
			result[k] = v
		end
	end
	return result
end

function M.util.tbl_filter_reserved(t)
	return M.util.tbl_filter_by_key(function(k)
		return not M.util.is_reserved(k)
	end, t)
end

local bounds_reserved = {
	"top",
	"bottom",
	"left",
	"right",
	"width",
	"height",
	"vert_center",
	"horiz_center",

	"top_left",
	"top_right",
	"bottom_left",
	"bottom_right",
	"middle_left",
	"middle_right",
	"top_middle",
	"bottom_middle",
	"center",
}

local shape_reserved = {
	"stroke_color",
	"stroke_width",
	"fill_color",
}

local rectangle_reserved = M.util.tbl_extend(bounds_reserved, shape_reserved)
local text_reserved = M.util.tbl_extend(bounds_reserved, { "size" })

function M.util.tbl_assign_reserved(t, rhs, whitelist)
	for k, v in pairs(t) do
		if M.util.tbl_contains(whitelist, k) then
			rhs[k] = v
		end
	end
end

-- Return lhs, but with keys that are not in rhs
function M.util.tbl_diff(lhs, rhs)
	local result = {}
	for k, v in pairs(lhs) do
		if rhs[k] == nil then
			result[k] = v
		end
	end
	return result
end

-- Assign keys from lhs to rhs, useful if rhs is userdata
function M.util.tbl_assign(lhs, rhs)
	for k, v in pairs(lhs) do
		rhs[k] = v
	end
end

M.float = {}

M.float.new = M.core.float
M.float.max = M.core.float_max
M.float.min = M.core.float_min

M.text = {}
M.text.font = M.core.font

function M.text.new(params)
	params = params or {}
	local result = M.core.text(params)
	M.util.tbl_assign_reserved(params, result, text_reserved)
	return result
end

M.rectangle = {}

function M.rectangle.new(params)
	params = params or {}
	local result = M.core.rectangle()
	M.util.tbl_assign_reserved(params, result, rectangle_reserved)
	return result
end

function M.rectangle.surrounding(content, params)
	local result = M.rectangle.new(params)
	M.constraint.inset(content, result, params)
	return result
end

M.straight_path = {}

function M.straight_path.new(params)
	params = params or {}
	local result = M.core.straight_path(params)
	M.util.tbl_assign_reserved(params, result, rectangle_reserved)
	return result
end

M.image = {}

function M.image.new(params)
	params = params or {}
	local result = M.core.image(params)
	M.util.tbl_assign_reserved(params, result, bounds_reserved)
	return result
end

function M.shape(params)
	params = params or {}

	local function is_reserved(k)
		return M.util.tbl_contains(bounds_reserved, k)
	end

	local result = M.util.tbl_filter_by_key(function(k)
		return not is_reserved(k)
	end, params)

	local shape = M.core.complex_shape()
	setmetatable(result, {
		__index = function(_, k)
			if is_reserved(k) then
				return shape[k]
			end
		end,
		__newindex = function(self, k, v)
			if is_reserved(k) then
				shape[k] = v
			else
				rawset(self, k, v)
			end
		end,
	})
	M.util.tbl_assign_reserved(params, shape, bounds_reserved)

	return result
end

M.layout = {}

function M.layout.margin(params)
	local result = M.shape(params)

	result.margin_left = result.margin_left or M.float.new()
	result.margin_right = result.margin_right or M.float.new()
	result.margin_top = result.margin_top or M.float.new()
	result.margin_bottom = result.margin_bottom or M.float.new()

	function result:draw()
		M.constrain(self.left:eq(self.content.left - self.margin_left))
		M.constrain(self.right:eq(self.content.right + self.margin_right))
		M.constrain(self.top:eq(self.content.top - self.margin_top))
		M.constrain(self.bottom:eq(self.content.bottom + self.margin_bottom))

		self.content:draw()
	end

	return result
end

function M.layout.margin_left(params)
	return M.layout.margin(M.util.tbl_extend(params, {
		margin_right = 0,
		margin_top = 0,
		margin_bottom = 0,
	}))
end

function M.layout.margin_right(params)
	return M.layout.margin(M.util.tbl_extend(params, {
		margin_left = 0,
		margin_top = 0,
		margin_bottom = 0,
	}))
end

function M.layout.margin_top(params)
	return M.layout.margin(M.util.tbl_extend(params, {
		margin_left = 0,
		margin_right = 0,
		margin_bottom = 0,
	}))
end

function M.layout.margin_bottom(params)
	return M.layout.margin(M.util.tbl_extend(params, {
		margin_left = 0,
		margin_right = 0,
		margin_top = 0,
	}))
end

-- TODO: factor with vstack
-- TODO: also as just a set of constraints
-- TODO: add support for "growing" element height (or elements have same size)
-- TODO: add orientation (ltr or rtl)
function M.layout.hstack(params)
	local result = M.shape(params)

	result.spacing = result.spacing or 0
	result.align = result.align or "top"
	if result.align == "middle" or result.align == "center" then
		result.align = "vert_center"
	end

	function result:draw()
		local len = #self.elements
		assert(len >= 1, "hstack must have at least 1 element in `elements`")

		local tops = {}
		local bottoms = {}

		local previous_right = self.left - self.spacing
		local previous_vert_anchor = self[self.align]

		for _, el in pairs(self.elements) do
			local el_vert_anchor = el[self.align]

			M.constrain(el.left:eq(previous_right + self.spacing))

			if self.align ~= "none" then
				M.constrain(el_vert_anchor:eq(previous_vert_anchor))
			end

			table.insert(tops, el.top)
			table.insert(bottoms, el.bottom)

			el:draw()

			previous_right = el.right
			previous_vert_anchor = el_vert_anchor
		end

		M.constrain(self.top:eq(M.float.min(tops)))
		M.constrain(self.bottom:eq(M.float.max(bottoms)))
		M.constrain(self.right:eq(previous_right))
	end

	return result
end

function M.layout.vstack(params)
	local result = M.shape(params)

	result.spacing = result.spacing or 0
	result.align = result.align or "left"
	if result.align == "middle" or result.align == "center" then
		result.align = "horiz_center"
	end

	function result:draw()
		local len = #self.elements
		assert(len >= 1, "vstack must have at least 1 element in `elements`")

		local lefts = {}
		local rights = {}

		local previous_bottom = self.top - self.spacing
		local previous_horiz_anchor = self[self.align]

		for _, el in pairs(self.elements) do
			local el_horiz_anchor = el[self.align]

			M.constrain(el.top:eq(previous_bottom + self.spacing))

			if self.align ~= "none" then
				M.constrain(el_horiz_anchor:eq(previous_horiz_anchor))
			end

			table.insert(lefts, el.left)
			table.insert(rights, el.right)

			el:draw()

			previous_bottom = el.bottom
			previous_horiz_anchor = el_horiz_anchor
		end

		M.constrain(self.left:eq(M.float.min(lefts)))
		M.constrain(self.right:eq(M.float.max(rights)))
		M.constrain(self.bottom:eq(previous_bottom))
	end

	return result
end

M.constraint = {}

function M.constraint.left_of(lhs, rhs, margin)
	M.constrain(lhs.right:eq(rhs.left - (margin or 0)))
end

function M.constraint.right_of(rhs, lhs, margin)
	M.constrain(lhs.right:eq(rhs.left - (margin or 0)))
end

function M.constraint.above(above, below, margin)
	M.constrain(above.bottom:eq(below.top - (margin or 0)))
end

function M.constraint.below(below, above, margin)
	M.constrain(above.bottom:eq(below.top - (margin or 0)))
end

function M.constraint.same_size(elems)
	local height = M.float.new()
	local width = M.float.new()

	for _, el in pairs(elems) do
		M.constrain(el.height:eq(height))
		M.constrain(el.width:eq(width))
	end
end

function M.constraint.same_height(elems)
	local height = M.float.new()

	for _, el in pairs(elems) do
		M.constrain(el.height:eq(height))
	end
end

function M.constraint.same_width(elems)
	local width = M.float.new()

	for _, el in pairs(elems) do
		M.constrain(el.width:eq(width))
	end
end

-- TODO: factor with rectangle surrounding
function M.constraint.inset(child, parent, params)
	params = params or {}
	local margin = params.margin or 0
	local margin_left = params.margin_left or margin
	local margin_right = params.margin_right or margin
	local margin_top = params.margin_top or margin
	local margin_bottom = params.margin_bottom or margin

	M.constrain(parent.left:eq(child.left - margin_left))
	M.constrain(parent.right:eq(child.right + margin_right))
	M.constrain(parent.top:eq(child.top - margin_top))
	M.constrain(parent.bottom:eq(child.bottom + margin_bottom))
end

M.constrain = M.core.constrain
M.draw = function(params)
	M.core.draw(M.util.tbl_extend(params, {
		draw = function(figure)
			local self = M.shape({ left = 0, top = 0 })
			M.constraint.inset(self, figure)
			params.draw(self)
		end,
	}))
end

return M