-
Notifications
You must be signed in to change notification settings - Fork 30
/
TreeGraphComponent.kt
274 lines (218 loc) · 8.94 KB
/
TreeGraphComponent.kt
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
package gg.essential.elementa.components
import gg.essential.elementa.UIComponent
import gg.essential.elementa.constraints.RelativeConstraint
import gg.essential.elementa.dsl.*
import gg.essential.elementa.effects.ScissorEffect
import gg.essential.elementa.utils.LineUtils
import gg.essential.universal.UMatrixStack
import java.awt.Color
import kotlin.math.max
import kotlin.properties.Delegates
data class TreeGraphStyle(
val widthBetweenNodes: Float = 10f,
val heightBetweenRows: Float = 10f,
val lineColor: Color = Color.WHITE,
val lineWidth: Float = 2f,
val isHorizontal: Boolean = false,
val lineDrawer: (UIPoint, UIPoint) -> Unit = { p0, p1 ->
LineUtils.drawLine(p0, p1, lineColor, lineWidth)
}
)
abstract class TreeGraphNode {
var depth by Delegates.notNull<Int>()
val children = mutableListOf<TreeGraphNode>()
val component by lazy { makeComponent() }
// TODO: invalidate these if component tree changes
private var widthBacker: Double? = null
private var heightBacker: Double? = null
fun width(style: TreeGraphStyle): Double {
if (widthBacker == null) {
widthBacker = if (children.isEmpty()) {
component.getWidth().toDouble()
} else {
max(
children.sumOf { it.width(style) } + (children.size - 1) * style.widthBetweenNodes,
component.getWidth().toDouble()
)
}
}
return widthBacker!!
}
fun height(style: TreeGraphStyle): Double {
if (heightBacker == null) {
heightBacker = if (children.isEmpty()) {
component.getHeight().toDouble()
} else {
max(
children.sumOf { it.height(style) } + (children.size - 1) * style.heightBetweenRows,
component.getHeight().toDouble()
)
}
}
return heightBacker!!
}
// Must have absolutely-resolvable width and height. The position
// will be handled by the caller
protected abstract fun makeComponent(): UIComponent
fun withChildren(childBuilder: TreeGraphBuilder.() -> Unit): TreeGraphNode {
val builder = TreeGraphBuilder(this)
builder.apply(childBuilder)
return builder.root
}
fun forAllChildren(action: (TreeGraphNode, depth: Int) -> Unit) {
children.forEach {
action(it, it.depth)
it.forAllChildren(action)
}
}
// Should only be called on the root node component
internal fun layoutChildren(style: TreeGraphStyle) {
component.setX(0.pixels())
component.setY(0.pixels())
if (style.isHorizontal) {
layoutChildrenHelper(style, component.getWidth() + style.widthBetweenNodes.toDouble(), 0.0)
} else {
layoutChildrenHelper(style, 0.0, component.getHeight() + style.heightBetweenRows.toDouble())
}
normalizePositions()
}
private fun layoutChildrenHelper(style: TreeGraphStyle, x_: Double, y_: Double) {
if (children.isEmpty())
return
if (style.isHorizontal) {
val totalHeight = children.sumOf { it.height(style) } + (children.size - 1) * style.heightBetweenRows
val maxWidth = children.maxOf { it.component.getWidth().toDouble() }
var y = y_ - totalHeight / 2.0
children.forEach { node ->
y += node.height(style) / 2f
if (children.size == 1)
y += (component.getHeight() - node.component.getHeight()) / 2f
node.component.setX((x_ + (maxWidth - node.component.getWidth()) / 2.0).pixels())
node.component.setY(y.pixels())
node.layoutChildrenHelper(style, x_ + maxWidth + style.widthBetweenNodes, y)
y += node.height(style) / 2f + style.heightBetweenRows
}
} else {
val totalWidth = children.sumOf { it.width(style) } + (children.size - 1) * style.widthBetweenNodes
val maxHeight = children.maxOf { it.component.getHeight().toDouble() }
var x = x_ - totalWidth / 2.0
children.forEach { node ->
x += node.width(style) / 2f
if (children.size == 1)
x += (component.getWidth() - node.component.getWidth()) / 2f
node.component.setX(x.pixels())
node.component.setY((y_ + (maxHeight - node.component.getHeight()) / 2.0).pixels())
node.layoutChildrenHelper(style, x, y_ + maxHeight + style.heightBetweenRows)
x += node.width(style) / 2f + style.widthBetweenNodes
}
}
}
private fun normalizePositions() {
var minX = component.getLeft()
var minY = component.getTop()
forAllChildren { node, _ ->
node.component.getLeft().also {
if (it < minX)
minX = it
}
node.component.getTop().also {
if (it < minY)
minY = it
}
}
component.setX((component.getLeft() - minX).pixels())
component.setY((component.getTop() - minY).pixels())
forAllChildren { node, _ ->
node.component.apply {
setX((getLeft() - minX).pixels())
setY((getTop() - minY).pixels())
}
}
}
internal fun setDepths(depth: Int = 0) {
this.depth = depth
children.forEach {
it.depth = depth + 1
it.setDepths(depth + 1)
}
}
internal fun collectLines(isHorizontal: Boolean): List<Pair<UIPoint, UIPoint>> {
val modifiers = if (children.isEmpty()) emptyList() else {
val delta = 1f / (children.size + 1)
(1..children.size).map { it * delta }
}.map {
(if (isHorizontal) component.getHeight() else component.getWidth()) * (it - 0.5f)
}
return children.mapIndexed { index, node ->
val p1 = point(isHorizontal, align = true).let {
if (isHorizontal) {
it.withY(it.y + modifiers[index].pixels())
} else it.withX(it.x + modifiers[index].pixels())
}
p1 to node.point(isHorizontal, align = false)
} + children.map {
it.collectLines(isHorizontal)
}.flatten()
}
private fun point(isHorizontal: Boolean, align: Boolean): UIPoint {
return if (isHorizontal) {
val extraWidth = if (align) component.getWidth() else 0f
UIPoint(
(component.getLeft() + extraWidth - component.parent.getLeft()).pixels(),
(component.getTop() + component.getHeight() / 2f - component.parent.getTop()).pixels()
)
} else {
val extraHeight = if (align) component.getHeight() else 0f
UIPoint(
(component.getLeft() + component.getWidth() / 2f - component.parent.getLeft()).pixels(),
(component.getTop() + extraHeight - component.parent.getTop()).pixels()
)
}
}
}
class TreeGraphComponent(
private val rootNode: TreeGraphNode,
private val style: TreeGraphStyle = TreeGraphStyle()
) : UIComponent() {
private var layedOut = false
private val scroll = ScrollComponent(horizontalScrollEnabled = true).constrain {
width = RelativeConstraint()
height = RelativeConstraint()
} effect ScissorEffect() childOf this
private lateinit var lines: List<Pair<UIPoint, UIPoint>>
init {
enableEffect(ScissorEffect())
val horizontalScroll = ScrollComponent.DefaultScrollBar(isHorizontal = true) childOf this
val verticalScroll = ScrollComponent.DefaultScrollBar(isHorizontal = false) childOf this
scroll.setHorizontalScrollBarComponent(horizontalScroll.grip, hideWhenUseless = true)
scroll.setVerticalScrollBarComponent(verticalScroll.grip, hideWhenUseless = true)
rootNode.setDepths(0)
val componentMatrix = mutableListOf(mutableListOf(rootNode))
rootNode.component childOf scroll
rootNode.forAllChildren { node, depth ->
while (depth >= componentMatrix.size)
componentMatrix.add(mutableListOf())
componentMatrix[depth].add(node)
node.component childOf scroll
}
}
override fun draw(matrixStack: UMatrixStack) {
beforeDrawCompat(matrixStack)
if (!layedOut) {
rootNode.layoutChildren(style)
lines = rootNode.collectLines(style.isHorizontal)
lines.forEach { (p0, p1) ->
scroll.insertChildAt(p0, 0)
scroll.insertChildAt(p1, 1)
}
layedOut = true
}
lines.forEach { style.lineDrawer(it.first, it.second) }
super.draw(matrixStack)
}
}
class TreeGraphBuilder(val root: TreeGraphNode) {
fun add(node: TreeGraphNode) {
root.children.add(node)
}
}