From 971c41ed2fde1e1708fa9e8b5b80e9187791f85b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 10 Aug 2019 22:48:38 -0400 Subject: [PATCH] Calculate program connection points on updates, instead of every frame --- .../shadowfacts/asmr/ui/ProgramCanvasView.kt | 70 +++++++++++-------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/src/main/kotlin/net/shadowfacts/asmr/ui/ProgramCanvasView.kt b/src/main/kotlin/net/shadowfacts/asmr/ui/ProgramCanvasView.kt index 804f9a7..6407e28 100644 --- a/src/main/kotlin/net/shadowfacts/asmr/ui/ProgramCanvasView.kt +++ b/src/main/kotlin/net/shadowfacts/asmr/ui/ProgramCanvasView.kt @@ -13,7 +13,11 @@ import net.shadowfacts.cacao.view.View */ class ProgramCanvasView(val program: Program): View() { - lateinit var blocks: Map + private lateinit var blocks: Map + + // map of starting block to (start, end) points + private val executionFlowStartLinks = mutableMapOf>() + private val parameterLinks = mutableMapOf>() override fun wasAdded() { super.wasAdded() @@ -26,48 +30,52 @@ class ProgramCanvasView(val program: Program): View() { } } - override fun drawContent(mouse: Point, delta: Float) { - super.drawContent(mouse, delta) - - - for ((block, view) in blocks) { - (block as? ExecutableBlock)?.executionFlow?.next?.let { next -> - blocks[next]?.let { nextView -> - val outgoing = view.outgoingExeuction!! - val incoming = nextView.incomingExecution!! + private fun updateBlockLinks() { + for ((startBlock, startView) in blocks) { + (startBlock as? ExecutableBlock)?.executionFlow?.next?.let { endBlock -> + blocks[endBlock]?.let { endView -> + val outgoing = startView.outgoingExeuction!! + val incoming = endView.incomingExecution!! val start = outgoing.convert(outgoing.bounds.center, to = this) val end = incoming.convert(outgoing.bounds.center, to = this) - RenderHelper.drawLine(start, end, zIndex, 5f, Color.GREEN) + executionFlowStartLinks[startBlock] = start to end } } - for (input in block.inputs) { - if (input.source == null) continue - val source = input.source!! + for (output in startBlock.outputs) { + val outputView = startView.outputViews[output] ?: continue + val start = outputView.convert(outputView.bounds.center, to = this) - blocks[source.block]?.let { sourceView -> - view.inputViews[input]?.let { inputView -> - sourceView.outputViews[source]?.let { outputView -> - val start = inputView.convert(inputView.bounds.center, to = this) - val end = outputView.convert(outputView.bounds.center, to = this) + for (destination in output.destinations) { + val endView = blocks[destination.block] ?: continue + val inputView = endView.inputViews[destination] ?: continue + val end = inputView.convert(inputView.bounds.center, to = this) - RenderHelper.drawLine(start, end, zIndex, 5f, Color.RED) - } - } + parameterLinks[startBlock] = start to end } - -// val start = block.position -// val sourcePosition = source.block.position -// val end = Point(sourcePosition.x + blocks[source.block]!!.bounds.width, sourcePosition.y) - -// val inputView = view.inputViews[input] -// val outputView = -// -// RenderHelper.drawLine(start, end, zIndex, 5f, Color.RED) } } } + override fun didLayout() { + super.didLayout() + + // we can't calculate the link points until layout is completed and all of the views have concrete positions/sizes + updateBlockLinks() + } + + override fun drawContent(mouse: Point, delta: Float) { + super.drawContent(mouse, delta) + + for ((start, end) in executionFlowStartLinks.values) { + RenderHelper.drawLine(start, end, zIndex, 5f, Color.GREEN) + } + + for ((start, end) in parameterLinks.values) { + RenderHelper.drawLine(start, end, zIndex, 5f, Color.RED) + } + } + } \ No newline at end of file