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)
executionFlowStartLinks[startBlock] = start to end
}
}
for (output in startBlock.outputs) {
val outputView = startView.outputViews[output] ?: continue
val start = 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)
parameterLinks[startBlock] = start to end
}
}
}
}
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) RenderHelper.drawLine(start, end, zIndex, 5f, Color.GREEN)
} }
}
for (input in block.inputs) {
if (input.source == null) continue
val source = input.source!!
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 ((start, end) in parameterLinks.values) {
RenderHelper.drawLine(start, end, zIndex, 5f, Color.RED) RenderHelper.drawLine(start, end, zIndex, 5f, Color.RED)
} }
} }
}
// 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)
}
}
}
} }