scoop:scoop.ui:0.1.0
src/layout/paint.scoop
Selected production source · src/layout/paint.scoop
1
package scoop.ui3
import scoop.graphics.Canvas4
import scoop.graphics.Size5
import scoop.platform.time.MonotonicInstant7
/** A pure drawing borrow. Input and child placement belong to SceneContext. */8
public class PaintContext internal constructor(9
private val root: UiRoot,10
private val target: Canvas,11
private val extent: Size,12
private val sample: MonotonicInstant13
) {14
private var active = true15
private var borrowed = false16
public val canvas: Canvas get() {17
checkActive()18
return target19
}20
public val size: Size get() {21
checkActive()22
return extent23
}24
public val time: MonotonicInstant get() {25
checkActive()26
observeFrameTime()27
return sample28
}29
private fun checkActive() {30
root.checkThread()31
if (!active)32
throw IllegalStateException(Some("paint context has ended"))33
if (borrowed)34
throw IllegalStateException(Some("paint context has an active child scope"))35
}36
/** Isolates pixel operations without changing the surrounding scene geometry. */37
public fun <T> withState(block: (PaintContext) -> T): T {38
checkActive()39
borrowed = true40
try {41
return target.withState {42
canvas ->43
val child = PaintContext(root, canvas, extent, sample)44
try {45
block(child)46
} finally {47
child.end()48
}49
}50
} finally {51
borrowed = false52
}53
}54
internal fun end() {55
active = false56
}57
}