aboutsummaryrefslogtreecommitdiffstats
path: root/group.lua
blob: 4889cd4b682ae6851294dae9b8eeaba5332a5c25 (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
--[[
Copyright (c) 2012 Matthias Richter

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--

local stack = {n = 0}
local default = {
	pos = {0,0},
	grow = {0,0},
	spacing = 2,
	size = {100, 30},
	upper_left  = {0,0},
	lower_right = {0,0},
}
local current = default

local Grow = {
	none  = { 0,  0},
	up    = { 0, -1},
	down  = { 0,  1},
	left  = {-1,  0},
	right = { 1,  0}
}

-- {grow = grow, spacing = spacing, size = size, pos = pos}
local function push(info)
	local grow    = info.grow    or "none"
	local spacing = info.spacing or default.spacing

	local size = {
		info.size and info.size[1] or current.size[1],
		info.size and info.size[2] or current.size[2]
	}

	local pos = {current.pos[1], current.pos[2]}
	if info.pos then
		pos[1] = pos[1] + (info.pos[1] or 0)
		pos[2] = pos[2] + (info.pos[2] or 0)
	end

	assert(size, "Size neither specified nor derivable from parent group.")
	assert(pos, "Position neither specified nor derivable from parent group.")
	grow = assert(Grow[grow], "Invalid grow: " .. tostring(grow))

	current = {
		pos         = pos,
		grow        = grow,
		size        = size,
		spacing     = spacing,
		upper_left  = { math.huge,  math.huge},
		lower_right = {-math.huge, -math.huge},
	}
	stack.n = stack.n + 1
	stack[stack.n] = current
end

local function advance(pos, size)
	current.upper_left[1]  = math.min(current.upper_left[1], pos[1])
	current.upper_left[2]  = math.min(current.upper_left[2], pos[2])
	current.lower_right[1] = math.max(current.lower_right[1], pos[1] + size[1])
	current.lower_right[2] = math.max(current.lower_right[2], pos[2] + size[2])

	if current.grow[1] ~= 0 then
		current.pos[1] = pos[1] + current.grow[1] * (size[1] + current.spacing)
	end
	if current.grow[2] ~= 0 then
		current.pos[2] = pos[2] + current.grow[2] * (size[2] + current.spacing)
	end
	return pos, size
end

local function getRect(pos, size)
	pos  = {pos  and pos[1]  or 0,               pos  and pos[2]  or 0}
	size = {size and size[1] or current.size[1], size and size[2] or current.size[2]}

	-- growing left/up: update current position to account for differnt size
	if current.grow[1] < 0 and current.size[1] ~= size[1] then
		current.pos[1] = current.pos[1] + (current.size[1] - size[1])
	end
	if current.grow[2] < 0 and current.size[2] ~= size[2] then
		current.pos[2] = current.pos[2] - (current.size[2] - size[2])
	end

	pos[1] = pos[1] + current.pos[1]
	pos[2] = pos[2] + current.pos[2]

	return advance(pos, size)
end

local function pop()
	assert(stack.n > 0, "Group stack is empty.")
	stack.n = stack.n - 1
	local child = current
	current = stack[stack.n] or default

	local size = {
		child.lower_right[1] - math.max(child.upper_left[1], current.pos[1]),
		child.lower_right[2] - math.max(child.upper_left[2], current.pos[2])
	}
	advance(current.pos, size)
end

local function beginFrame()
	current = default
	stack.n = 0
end

local function endFrame()
	-- future use?
end

return setmetatable({
	push       = push,
	pop        = pop,
	getRect    = getRect,
	advance    = advance,
	beginFrame = beginFrame,
	endFrame   = endFrame,
	default    = default,
}, {
	__index = function(_,k)
		return ({size = current.size, pos = current.pos})[k]
	end,
	__call = function(_, info)
		assert(type(info) == 'table' and type(info[1]) == 'function')
		push(info)
		info[1]()
		pop()
	end,
})