56 lines
1.5 KiB
Kotlin
56 lines
1.5 KiB
Kotlin
|
package net.shadowfacts.asmr.ui
|
||
|
|
||
|
import net.shadowfacts.asmr.program.ProgramBlock
|
||
|
import net.shadowfacts.cacao.geometry.Point
|
||
|
import net.shadowfacts.cacao.util.Color
|
||
|
import net.shadowfacts.cacao.util.MouseButton
|
||
|
import net.shadowfacts.cacao.view.Label
|
||
|
import net.shadowfacts.cacao.view.View
|
||
|
import net.shadowfacts.kiwidsl.dsl
|
||
|
import no.birkett.kiwi.Constraint
|
||
|
|
||
|
/**
|
||
|
* @author shadowfacts
|
||
|
*/
|
||
|
class ProgramBlockView(val block: ProgramBlock): View() {
|
||
|
|
||
|
var xConstraint: Constraint? = null
|
||
|
var yConstraint: Constraint? = null
|
||
|
|
||
|
override fun wasAdded() {
|
||
|
super.wasAdded()
|
||
|
|
||
|
respondsToDragging = true
|
||
|
|
||
|
backgroundColor = Color.BLACK
|
||
|
zIndex = 5.0
|
||
|
|
||
|
val title = addSubview(Label(block.javaClass.simpleName))
|
||
|
|
||
|
solver.dsl {
|
||
|
widthAnchor equalTo (title.widthAnchor + 8)
|
||
|
heightAnchor equalTo (title.heightAnchor + 8)
|
||
|
title.centerXAnchor equalTo centerXAnchor
|
||
|
title.centerYAnchor equalTo centerYAnchor
|
||
|
}
|
||
|
|
||
|
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()
|
||
|
}
|
||
|
|
||
|
}
|