Calculate program connection points on updates, instead of every frame

This commit is contained in:
Shadowfacts 2019-08-10 22:48:38 -04:00
parent b893f83032
commit 971c41ed2f
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 39 additions and 31 deletions

View File

@ -13,7 +13,11 @@ import net.shadowfacts.cacao.view.View
*/ */
class ProgramCanvasView(val program: Program): View() { class ProgramCanvasView(val program: Program): View() {
lateinit var blocks: Map<ProgramBlock, ProgramBlockView> private lateinit var blocks: Map<ProgramBlock, ProgramBlockView>
// map of starting block to (start, end) points
private val executionFlowStartLinks = mutableMapOf<ExecutableBlock, Pair<Point, Point>>()
private val parameterLinks = mutableMapOf<ProgramBlock, Pair<Point, Point>>()
override fun wasAdded() { override fun wasAdded() {
super.wasAdded() super.wasAdded()
@ -26,48 +30,52 @@ class ProgramCanvasView(val program: Program): View() {
} }
} }
override fun drawContent(mouse: Point, delta: Float) { private fun updateBlockLinks() {
super.drawContent(mouse, delta) for ((startBlock, startView) in blocks) {
(startBlock as? ExecutableBlock)?.executionFlow?.next?.let { endBlock ->
blocks[endBlock]?.let { endView ->
for ((block, view) in blocks) { val outgoing = startView.outgoingExeuction!!
(block as? ExecutableBlock)?.executionFlow?.next?.let { next -> val incoming = endView.incomingExecution!!
blocks[next]?.let { nextView ->
val outgoing = view.outgoingExeuction!!
val incoming = nextView.incomingExecution!!
val start = outgoing.convert(outgoing.bounds.center, to = this) val start = outgoing.convert(outgoing.bounds.center, to = this)
val end = incoming.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) { for (output in startBlock.outputs) {
if (input.source == null) continue val outputView = startView.outputViews[output] ?: continue
val source = input.source!! val start = outputView.convert(outputView.bounds.center, to = this)
blocks[source.block]?.let { sourceView -> for (destination in output.destinations) {
view.inputViews[input]?.let { inputView -> val endView = blocks[destination.block] ?: continue
sourceView.outputViews[source]?.let { outputView -> val inputView = endView.inputViews[destination] ?: continue
val start = inputView.convert(inputView.bounds.center, to = this) val end = inputView.convert(inputView.bounds.center, to = this)
val end = outputView.convert(outputView.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)
}
}
} }