Skip to content

Commit 00aec2b

Browse files
committed
Render first node of collapsed sequence + expand graph with right click
1 parent 42eb2c9 commit 00aec2b

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

src/main/kotlin/be/ugent/topl/mio/ui/GraphPanel.kt

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ class GraphPanel(private val graph: MultiverseGraph) : JPanel(),
6161
g2.stroke = BasicStroke(2.0f)
6262

6363
drawPaths(g, width - 100, graph.rootNode)
64+
//drawGraphNew(g)
65+
println("Height ${leafCounter.getLeafCount(graph.rootNode)}")
66+
}
67+
68+
val leafCounter = LeafCounter(graph)
69+
70+
private fun drawGraphNew(g: Graphics2D) {
71+
drawNode(g, Point(10, 10), graph.rootNode)
72+
// Draw node in the middle of the children's height
6473
}
6574

6675
private fun drawPaths(g: Graphics2D, width: Int, rootNode: MultiverseNode) {
@@ -99,30 +108,44 @@ class GraphPanel(private val graph: MultiverseGraph) : JPanel(),
99108
}
100109
}*/
101110

102-
//var collapsed = mutableMapOf<MultiverseNode, Boolean>()
111+
var collapsed = mutableMapOf<MultiverseNode, Boolean>()
103112

104113
private fun drawGraph(g: Graphics2D, node: MultiverseNode, x: Int = 0, y: Int = 0): Pair<Point, Int> {
105-
if (node.children.size == 1) {
114+
if (node.children.size == 1 && node.displayName.isEmpty()) {
115+
val collapse = collapsed.getOrDefault(node, true)
116+
106117
val stack = mutableListOf<MultiverseNode>()
107118
var x = x
108119
var currentNode = node
109-
while(currentNode.children.size == 1) {
110-
stack.add(currentNode)
111-
x += currentNode.edgeLength
120+
while(currentNode.children.size == 1 && currentNode.displayName.isEmpty()) {
121+
if (!collapse) {
122+
stack.add(currentNode)
123+
x += currentNode.edgeLength
124+
}
112125
currentNode = currentNode.children.first()
113126
}
127+
// If collapsed, just put the first node on the render stack.
128+
if (collapse) {
129+
stack.add(node)
130+
x += node.edgeLength
131+
}
114132

115133
val result = drawGraph(g, currentNode, x + currentNode.edgeLength, y)
116134
renderedWidth = Integer.max(renderedWidth, x + node.edgeLength + 500)
117-
118135
var newPoint = result.first
119136
var currentHeight = result.second
120137
var point = Point(x, y + currentHeight / 2 - d / 2)
121-
currentHeight = Math.max(d + 0, currentHeight)
138+
currentHeight = Math.max(d, currentHeight)
122139
while (stack.isNotEmpty()) {
123140
val node = stack.removeLast()
124141
point = Point(x, y + currentHeight / 2 - d / 2)
142+
if (collapse) {
143+
val textWidth = g.fontMetrics.stringWidth("+")
144+
g.color = borderColour
145+
g.drawString("+", point.x + d/2 - textWidth/2, point.y - 5)
146+
}
125147
drawNodeAndEdges(g, point, node, listOf(newPoint))
148+
nodes.add(Node(point.x, point.y, d, d, node))
126149
x -= node.edgeLength // Next node should be further forward
127150
newPoint = Point(x + node.edgeLength, y + currentHeight / 2 - d / 2)
128151
}
@@ -279,14 +302,23 @@ class GraphPanel(private val graph: MultiverseGraph) : JPanel(),
279302
return
280303
}
281304

305+
var selected: Node? = null
282306
for (node in nodes) {
283307
if (e.x > node.x && e.y > node.y && e.x < node.x + node.w && e.y < node.y + node.h) {
284-
selectedNode = node
308+
selected = node
285309
}
286310
}
287-
if (selectedNode == null) return
311+
if (selected == null) return
312+
313+
if (e.button == MouseEvent.BUTTON3) {
314+
collapsed[selected.value] = !collapsed.getOrDefault(selected.value, true)
315+
repaint()
316+
return
317+
}
318+
319+
selectedNode = selected
288320

289-
println(graph.rootNode.findPath(graph.currentNode, selectedValue!!))
321+
//println(graph.rootNode.findPath(graph.currentNode, selectedValue!!))
290322
selectedPath = graph.rootNode.findPath(graph.currentNode, selectedValue!!)
291323
selectedNodes = selectedPath!!.first.toMutableSet()
292324
selectedNodes.addAll(selectedPath!!.second.toSet())

0 commit comments

Comments
 (0)