2019-08-09 18:36:12 +00:00
|
|
|
package net.shadowfacts.asmr.ui
|
|
|
|
|
2019-08-10 02:39:01 +00:00
|
|
|
import net.minecraft.util.Identifier
|
|
|
|
import net.shadowfacts.asmr.ASMR
|
|
|
|
import net.shadowfacts.asmr.program.ExecutableBlock
|
2019-08-09 18:36:12 +00:00
|
|
|
import net.shadowfacts.asmr.program.ProgramBlock
|
2019-08-10 02:39:01 +00:00
|
|
|
import net.shadowfacts.cacao.geometry.Axis
|
2019-08-09 18:36:12 +00:00
|
|
|
import net.shadowfacts.cacao.geometry.Point
|
|
|
|
import net.shadowfacts.cacao.util.Color
|
|
|
|
import net.shadowfacts.cacao.util.MouseButton
|
2019-08-10 02:39:01 +00:00
|
|
|
import net.shadowfacts.cacao.util.texture.Texture
|
2019-08-09 18:36:12 +00:00
|
|
|
import net.shadowfacts.cacao.view.Label
|
2019-08-10 02:39:01 +00:00
|
|
|
import net.shadowfacts.cacao.view.StackView
|
|
|
|
import net.shadowfacts.cacao.view.TextureView
|
2019-08-09 18:36:12 +00:00
|
|
|
import net.shadowfacts.cacao.view.View
|
|
|
|
import net.shadowfacts.kiwidsl.dsl
|
|
|
|
import no.birkett.kiwi.Constraint
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
2019-08-10 02:39:01 +00:00
|
|
|
class ProgramBlockView(val block: ProgramBlock): StackView(Axis.VERTICAL, Distribution.CENTER, spacing = 4.0) {
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
val executionFlowInactiveTexture = Texture(Identifier(ASMR.modid, "textures/gui/programmer/execution_flow.png"), u = 0, v = 0)
|
|
|
|
val executionFlowActiveTexture = Texture(Identifier(ASMR.modid, "textures/gui/programmer/execution_flow.png"), u = 7, v = 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
var incomingExecution: View? = null
|
|
|
|
var outgoingExeuction: View? = null
|
2019-08-09 18:36:12 +00:00
|
|
|
|
|
|
|
var xConstraint: Constraint? = null
|
|
|
|
var yConstraint: Constraint? = null
|
|
|
|
|
|
|
|
override fun wasAdded() {
|
|
|
|
super.wasAdded()
|
|
|
|
|
|
|
|
respondsToDragging = true
|
|
|
|
|
|
|
|
backgroundColor = Color.BLACK
|
2019-08-09 19:23:48 +00:00
|
|
|
zIndex = 10.0
|
2019-08-09 18:36:12 +00:00
|
|
|
|
2019-08-10 02:39:01 +00:00
|
|
|
val titleStack = addArrangedSubview(StackView(Axis.HORIZONTAL, Distribution.CENTER))
|
|
|
|
val title = titleStack.addArrangedSubview(Label(block.javaClass.simpleName))
|
|
|
|
if (block is ExecutableBlock) {
|
2019-08-10 03:18:02 +00:00
|
|
|
val incomingTexture = if (block.executionFlow.prev == null) executionFlowInactiveTexture else executionFlowActiveTexture
|
2019-08-10 02:39:01 +00:00
|
|
|
incomingExecution = titleStack.addArrangedSubview(TextureView(incomingTexture), index = 0)
|
2019-08-10 03:18:02 +00:00
|
|
|
val outgoingTexture = if (block.executionFlow.next == null) executionFlowInactiveTexture else executionFlowActiveTexture
|
2019-08-10 02:39:01 +00:00
|
|
|
outgoingExeuction = titleStack.addArrangedSubview(TextureView(outgoingTexture))
|
|
|
|
}
|
|
|
|
|
|
|
|
val pairs = block.inputs.zip(block.outputs)
|
|
|
|
val remainingInputs = if (block.inputs.size > block.outputs.size) block.inputs.drop(block.outputs.size) else listOf()
|
|
|
|
val remainingOutputs = if (block.outputs.size > block.inputs.size) block.outputs.drop(block.inputs.size) else listOf()
|
|
|
|
for ((input, output) in pairs) {
|
|
|
|
val hStack = addArrangedSubview(StackView(Axis.HORIZONTAL, spacing = 8.0))
|
|
|
|
hStack.addArrangedSubview(Label(input.identifier.toString()))
|
|
|
|
hStack.addArrangedSubview(Label(output.identifier.toString()))
|
|
|
|
}
|
|
|
|
for (input in remainingInputs) {
|
|
|
|
addArrangedSubview(Label(input.identifier.toString()))
|
|
|
|
}
|
|
|
|
for (output in remainingOutputs) {
|
|
|
|
addArrangedSubview(Label(output.identifier.toString(), textAlignment = Label.TextAlignment.RIGHT))
|
|
|
|
}
|
2019-08-09 18:36:12 +00:00
|
|
|
|
|
|
|
solver.dsl {
|
2019-08-10 02:39:01 +00:00
|
|
|
// we have to constrain the titleStack height, because it's distribution is set to CENTER, so it doesn't do it itself
|
|
|
|
titleStack.heightAnchor equalTo title.heightAnchor
|
|
|
|
widthAnchor greaterThanOrEqualTo titleStack.widthAnchor
|
|
|
|
|
|
|
|
if (block is ExecutableBlock) {
|
|
|
|
incomingExecution!!.widthAnchor equalTo incomingExecution!!.heightAnchor
|
|
|
|
incomingExecution!!.widthAnchor equalTo 7
|
|
|
|
outgoingExeuction!!.widthAnchor equalTo outgoingExeuction!!.heightAnchor
|
|
|
|
outgoingExeuction!!.widthAnchor equalTo 7
|
|
|
|
}
|
|
|
|
|
|
|
|
for (view in arrangedSubviews.drop(1)) {
|
|
|
|
widthAnchor.equalTo(view.widthAnchor, strength = STRONG)
|
|
|
|
}
|
2019-08-09 18:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateDraggingConstraints()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun mouseDragged(startPoint: Point, delta: Point, mouseButton: MouseButton): Boolean {
|
|
|
|
block.position += delta
|
|
|
|
updateDraggingConstraints()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateDraggingConstraints() {
|
|
|
|
if (xConstraint != null) solver.removeConstraint(xConstraint)
|
|
|
|
if (yConstraint != null) solver.removeConstraint(yConstraint)
|
|
|
|
solver.dsl {
|
|
|
|
xConstraint = (leftAnchor equalTo superview!!.leftAnchor + block.position.x)
|
|
|
|
yConstraint = (topAnchor equalTo superview!!.topAnchor + block.position.y)
|
|
|
|
}
|
|
|
|
window!!.layout()
|
|
|
|
}
|
|
|
|
|
2019-08-10 02:39:01 +00:00
|
|
|
override fun drawContent(mouse: Point, delta: Float) {
|
|
|
|
super.drawContent(mouse, delta)
|
|
|
|
}
|
|
|
|
|
2019-08-09 18:36:12 +00:00
|
|
|
}
|