Refactor program execution flow to link bidirectionally

This commit is contained in:
Shadowfacts 2019-08-09 23:18:02 -04:00
parent 56bc870ac5
commit a61a67cbf3
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
10 changed files with 29 additions and 18 deletions

View File

@ -34,8 +34,8 @@ class ManagerBlockEntity: BlockEntity(ASMR.managerEntityType) {
it.position = Point(240.0, 0.0) it.position = Point(240.0, 0.0)
} }
program.startBlock.nextExecutableBlock = divide program.startBlock.linkNext(divide)
divide.nextExecutableBlock = print divide.linkNext(print)
} }
fun activate() { fun activate() {

View File

@ -1,14 +1,17 @@
package net.shadowfacts.asmr.program package net.shadowfacts.asmr.program
import net.shadowfacts.cacao.geometry.Point
/** /**
* @author shadowfacts * @author shadowfacts
*/ */
abstract class ExecutableBlock: ProgramBlock() { abstract class ExecutableBlock: ProgramBlock() {
var nextExecutableBlock: ExecutableBlock? = null open val executionFlow = ExecutionFlow()
abstract fun execute() abstract fun execute()
fun linkNext(next: ExecutableBlock) {
executionFlow.next = next
next.executionFlow.prev = this
}
} }

View File

@ -0,0 +1,11 @@
package net.shadowfacts.asmr.program
/**
* @author shadowfacts
*/
open class ExecutionFlow {
open var next: ExecutableBlock? = null
open var prev: ExecutableBlock? = null
}

View File

@ -14,11 +14,11 @@ class Program {
val blocks = mutableListOf<ProgramBlock>() val blocks = mutableListOf<ProgramBlock>()
fun execute() { fun execute() {
var currentBlock: ExecutableBlock? = startBlock.nextExecutableBlock var currentBlock: ExecutableBlock? = startBlock.executionFlow.next
while (currentBlock != null) { while (currentBlock != null) {
currentBlock.execute() currentBlock.execute()
currentBlock = currentBlock.nextExecutableBlock currentBlock = currentBlock.executionFlow.next
} }
} }

View File

@ -2,10 +2,7 @@ package net.shadowfacts.asmr.program.blocks
import net.minecraft.util.Identifier import net.minecraft.util.Identifier
import net.shadowfacts.asmr.ASMR import net.shadowfacts.asmr.ASMR
import net.shadowfacts.asmr.program.ExecutableBlock import net.shadowfacts.asmr.program.*
import net.shadowfacts.asmr.program.ProgramBlockInput
import net.shadowfacts.asmr.program.ProgramBlockOutput
import net.shadowfacts.asmr.program.ProgramType
import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Point
/** /**

View File

@ -1,18 +1,18 @@
package net.shadowfacts.asmr.program.blocks package net.shadowfacts.asmr.program.blocks
import net.shadowfacts.asmr.program.ExecutableBlock import net.shadowfacts.asmr.program.ExecutableBlock
import net.shadowfacts.asmr.program.ProgramBlock
import net.shadowfacts.asmr.program.ProgramBlockInput import net.shadowfacts.asmr.program.ProgramBlockInput
import net.shadowfacts.asmr.program.ProgramBlockOutput import net.shadowfacts.asmr.program.ProgramBlockOutput
/** /**
* @author shadowfacts * @author shadowfacts
*/ */
class StartBlock: ProgramBlock() { class StartBlock: ExecutableBlock() {
override val inputs: Array<ProgramBlockInput<*>> = arrayOf() override val inputs: Array<ProgramBlockInput<*>> = arrayOf()
override val outputs: Array<ProgramBlockOutput<*>> = arrayOf() override val outputs: Array<ProgramBlockOutput<*>> = arrayOf()
var nextExecutableBlock: ExecutableBlock? = null override fun execute() {
}
} }

View File

@ -43,9 +43,9 @@ class ProgramBlockView(val block: ProgramBlock): StackView(Axis.VERTICAL, Distri
val titleStack = addArrangedSubview(StackView(Axis.HORIZONTAL, Distribution.CENTER)) val titleStack = addArrangedSubview(StackView(Axis.HORIZONTAL, Distribution.CENTER))
val title = titleStack.addArrangedSubview(Label(block.javaClass.simpleName)) val title = titleStack.addArrangedSubview(Label(block.javaClass.simpleName))
if (block is ExecutableBlock) { if (block is ExecutableBlock) {
val incomingTexture = executionFlowInactiveTexture val incomingTexture = if (block.executionFlow.prev == null) executionFlowInactiveTexture else executionFlowActiveTexture
incomingExecution = titleStack.addArrangedSubview(TextureView(incomingTexture), index = 0) incomingExecution = titleStack.addArrangedSubview(TextureView(incomingTexture), index = 0)
val outgoingTexture = if (block.nextExecutableBlock == null) executionFlowInactiveTexture else executionFlowActiveTexture val outgoingTexture = if (block.executionFlow.next == null) executionFlowInactiveTexture else executionFlowActiveTexture
outgoingExeuction = titleStack.addArrangedSubview(TextureView(outgoingTexture)) outgoingExeuction = titleStack.addArrangedSubview(TextureView(outgoingTexture))
} }

View File

@ -31,7 +31,7 @@ class ProgramCanvasView(val program: Program): View() {
for ((block, view) in blocks) { for ((block, view) in blocks) {
(block as? ExecutableBlock)?.nextExecutableBlock?.let { next -> (block as? ExecutableBlock)?.executionFlow?.next?.let { next ->
blocks[next]?.let { nextView -> blocks[next]?.let { nextView ->
val outgoing = view.outgoingExeuction!! val outgoing = view.outgoingExeuction!!
val incoming = nextView.incomingExecution!! val incoming = nextView.incomingExecution!!

Binary file not shown.

Before

Width:  |  Height:  |  Size: 889 B

After

Width:  |  Height:  |  Size: 896 B

View File

@ -20,7 +20,7 @@ class ProgramTests {
it.right.source = two.output it.right.source = two.output
} }
program.startBlock.nextExecutableBlock = multiply program.startBlock.linkNext(multiply)
program.execute() program.execute()