Refactor program execution flow to link bidirectionally
This commit is contained in:
parent
56bc870ac5
commit
a61a67cbf3
|
@ -34,8 +34,8 @@ class ManagerBlockEntity: BlockEntity(ASMR.managerEntityType) {
|
|||
it.position = Point(240.0, 0.0)
|
||||
}
|
||||
|
||||
program.startBlock.nextExecutableBlock = divide
|
||||
divide.nextExecutableBlock = print
|
||||
program.startBlock.linkNext(divide)
|
||||
divide.linkNext(print)
|
||||
}
|
||||
|
||||
fun activate() {
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
package net.shadowfacts.asmr.program
|
||||
|
||||
import net.shadowfacts.cacao.geometry.Point
|
||||
|
||||
/**
|
||||
* @author shadowfacts
|
||||
*/
|
||||
abstract class ExecutableBlock: ProgramBlock() {
|
||||
|
||||
var nextExecutableBlock: ExecutableBlock? = null
|
||||
open val executionFlow = ExecutionFlow()
|
||||
|
||||
abstract fun execute()
|
||||
|
||||
fun linkNext(next: ExecutableBlock) {
|
||||
executionFlow.next = next
|
||||
next.executionFlow.prev = this
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package net.shadowfacts.asmr.program
|
||||
|
||||
/**
|
||||
* @author shadowfacts
|
||||
*/
|
||||
open class ExecutionFlow {
|
||||
|
||||
open var next: ExecutableBlock? = null
|
||||
open var prev: ExecutableBlock? = null
|
||||
|
||||
}
|
|
@ -14,11 +14,11 @@ class Program {
|
|||
val blocks = mutableListOf<ProgramBlock>()
|
||||
|
||||
fun execute() {
|
||||
var currentBlock: ExecutableBlock? = startBlock.nextExecutableBlock
|
||||
var currentBlock: ExecutableBlock? = startBlock.executionFlow.next
|
||||
while (currentBlock != null) {
|
||||
currentBlock.execute()
|
||||
|
||||
currentBlock = currentBlock.nextExecutableBlock
|
||||
currentBlock = currentBlock.executionFlow.next
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,10 +2,7 @@ package net.shadowfacts.asmr.program.blocks
|
|||
|
||||
import net.minecraft.util.Identifier
|
||||
import net.shadowfacts.asmr.ASMR
|
||||
import net.shadowfacts.asmr.program.ExecutableBlock
|
||||
import net.shadowfacts.asmr.program.ProgramBlockInput
|
||||
import net.shadowfacts.asmr.program.ProgramBlockOutput
|
||||
import net.shadowfacts.asmr.program.ProgramType
|
||||
import net.shadowfacts.asmr.program.*
|
||||
import net.shadowfacts.cacao.geometry.Point
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
package net.shadowfacts.asmr.program.blocks
|
||||
|
||||
import net.shadowfacts.asmr.program.ExecutableBlock
|
||||
import net.shadowfacts.asmr.program.ProgramBlock
|
||||
import net.shadowfacts.asmr.program.ProgramBlockInput
|
||||
import net.shadowfacts.asmr.program.ProgramBlockOutput
|
||||
|
||||
/**
|
||||
* @author shadowfacts
|
||||
*/
|
||||
class StartBlock: ProgramBlock() {
|
||||
class StartBlock: ExecutableBlock() {
|
||||
|
||||
override val inputs: Array<ProgramBlockInput<*>> = arrayOf()
|
||||
override val outputs: Array<ProgramBlockOutput<*>> = arrayOf()
|
||||
|
||||
var nextExecutableBlock: ExecutableBlock? = null
|
||||
override fun execute() {
|
||||
}
|
||||
|
||||
}
|
|
@ -43,9 +43,9 @@ class ProgramBlockView(val block: ProgramBlock): StackView(Axis.VERTICAL, Distri
|
|||
val titleStack = addArrangedSubview(StackView(Axis.HORIZONTAL, Distribution.CENTER))
|
||||
val title = titleStack.addArrangedSubview(Label(block.javaClass.simpleName))
|
||||
if (block is ExecutableBlock) {
|
||||
val incomingTexture = executionFlowInactiveTexture
|
||||
val incomingTexture = if (block.executionFlow.prev == null) executionFlowInactiveTexture else executionFlowActiveTexture
|
||||
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))
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ class ProgramCanvasView(val program: Program): View() {
|
|||
|
||||
|
||||
for ((block, view) in blocks) {
|
||||
(block as? ExecutableBlock)?.nextExecutableBlock?.let { next ->
|
||||
(block as? ExecutableBlock)?.executionFlow?.next?.let { next ->
|
||||
blocks[next]?.let { nextView ->
|
||||
val outgoing = view.outgoingExeuction!!
|
||||
val incoming = nextView.incomingExecution!!
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 889 B After Width: | Height: | Size: 896 B |
|
@ -20,7 +20,7 @@ class ProgramTests {
|
|||
it.right.source = two.output
|
||||
}
|
||||
|
||||
program.startBlock.nextExecutableBlock = multiply
|
||||
program.startBlock.linkNext(multiply)
|
||||
|
||||
program.execute()
|
||||
|
||||
|
|
Loading…
Reference in New Issue