Skip to content

Commit

Permalink
minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Chentai-Kao committed May 13, 2019
1 parent 808f270 commit 3ab532e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
10 changes: 4 additions & 6 deletions src/main/kotlin/callgraph/Canvas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Canvas(private val callGraphToolWindow: CallGraphToolWindow): JPanel() {
Colors.ORANGE.color,
Colors.RED.color
)

private var graph = Graph()
private var hoveredNode: Node? = null

Expand Down Expand Up @@ -125,8 +126,7 @@ class Canvas(private val callGraphToolWindow: CallGraphToolWindow): JPanel() {
this.nodeShapesMap.clear()
this.hoveredNode = null
this.cameraOrigin.setLocation(defaultCameraOrigin)
this.zoomRatio.x = this.defaultZoomRatio
this.zoomRatio.y = this.defaultZoomRatio
this.zoomRatio.setLocation(this.defaultZoomRatio, this.defaultZoomRatio)
}

fun setHoveredNode(node: Node?): Canvas {
Expand Down Expand Up @@ -169,17 +169,15 @@ class Canvas(private val callGraphToolWindow: CallGraphToolWindow): JPanel() {
val bestFitBlueprint = Utils.fitLayoutToViewport(blueprint)
Utils.applyLayoutBlueprintToGraph(bestFitBlueprint, this.graph)
this.cameraOrigin.setLocation(defaultCameraOrigin)
this.zoomRatio.x = defaultZoomRatio
this.zoomRatio.y = defaultZoomRatio
this.zoomRatio.setLocation(defaultZoomRatio, defaultZoomRatio)
repaint()
}

fun fitCanvasToBestRatio() {
// set every node coordinate to its original raw layout by GraphViz
this.graph.getNodes().forEach { it.point.setLocation(it.rawLayoutPoint) }
this.cameraOrigin.setLocation(defaultCameraOrigin)
this.zoomRatio.x = defaultZoomRatio
this.zoomRatio.y = defaultZoomRatio
this.zoomRatio.setLocation(defaultZoomRatio, defaultZoomRatio)
repaint()
}

Expand Down
24 changes: 10 additions & 14 deletions src/main/kotlin/callgraph/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,13 @@ object Utils {
fun fitLayoutToViewport(blueprint: Map<String, Point2D.Float>): Map<String, Point2D.Float> {
val maxPoint = blueprint.values.reduce { a, b -> Point2D.Float(maxOf(a.x, b.x), maxOf(a.y, b.y)) }
val minPoint = blueprint.values.reduce { a, b -> Point2D.Float(minOf(a.x, b.x), minOf(a.y, b.y)) }
val xSize = maxPoint.x - minPoint.x
val ySize = maxPoint.y - minPoint.y
val graphSize = Point2D.Float(maxPoint.x - minPoint.x, maxPoint.y - minPoint.y)
val bestFitBaseline = 0.1f // make the best fit window between 0.1 - 0.9 of the viewport
val bestFitSize = 1 - 2 * bestFitBaseline
return blueprint.mapValues { (_, point) ->
Point2D.Float(
(point.x - minPoint.x) / xSize * bestFitSize + bestFitBaseline,
(point.y - minPoint.y) / ySize * bestFitSize + bestFitBaseline
(point.x - minPoint.x) / graphSize.x * bestFitSize + bestFitBaseline,
(point.y - minPoint.y) / graphSize.y * bestFitSize + bestFitBaseline
)
}
}
Expand Down Expand Up @@ -333,22 +332,19 @@ object Utils {
val blueprintSizes = blueprints
.map { blueprint ->
val xPoints = blueprint.values.map { it.x }
val xMax = xPoints.max() ?: 0f
val xMin = xPoints.min() ?: 0f
val width = xMax - xMin + normalizedGridSize
val yPoints = blueprint.values.map { it.y }
val yMax = yPoints.max() ?: 0f
val yMin = yPoints.min() ?: 0f
val height = yMax - yMin + normalizedGridSize
val max = Point2D.Float(xPoints.max() ?: 0f, yPoints.max() ?: 0f)
val min = Point2D.Float(xPoints.min() ?: 0f, yPoints.min() ?: 0f)
val width = max.x - min.x + normalizedGridSize
val height = max.y - min.y + normalizedGridSize
Triple(blueprint, height, width)
}
val sortedHeights = blueprintSizes.map { (_, height, _) -> height }.sortedBy { -it }
val sortedBlueprints = blueprintSizes
.toList()
.sortedWith(compareBy({ (_, height, _) -> -height }, { (_, _, width) -> -width }))
.map { (blueprint, _, _) -> blueprint }
val xBaseline = 0.5f
val yBaseline = 0.5f
val baseline = Point2D.Float(0.5f, 0.5f)
// put the left-most point of the first sub-graph in the view center, by using its y value as central line
val yCentralLine = sortedBlueprints.first().values.minBy { it.x }?.y ?: 0f
return sortedBlueprints
Expand All @@ -360,8 +356,8 @@ object Utils {
//noinspection UnnecessaryLocalVariable
blueprint.mapValues { (_, point) ->
Point2D.Float(
point.x - minX + xBaseline,
point.y + yOffset - yCentralLine + yBaseline
point.x - minX + baseline.x,
point.y + yOffset - yCentralLine + baseline.y
)
}
}
Expand Down

0 comments on commit 3ab532e

Please sign in to comment.