diff --git a/orx-camera/src/commonMain/kotlin/Camera2D.kt b/orx-camera/src/commonMain/kotlin/Camera2D.kt index ffb82158..eda5c0d8 100644 --- a/orx-camera/src/commonMain/kotlin/Camera2D.kt +++ b/orx-camera/src/commonMain/kotlin/Camera2D.kt @@ -12,6 +12,8 @@ import org.openrndr.events.Event import org.openrndr.math.Matrix44 import org.openrndr.math.Vector2 import org.openrndr.math.transforms.buildTransform +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.contract /** * The [Camera2D] extension enables panning, rotating, and zooming the view @@ -78,7 +80,11 @@ class Camera2D : Extension, ChangeEvents { * * @param function the drawing function to be applied within the isolated scope of the `Drawer`. */ + @OptIn(ExperimentalContracts::class) fun isolated(function: Drawer.() -> Unit) { + contract { + callsInPlace(function, kotlin.contracts.InvocationKind.EXACTLY_ONCE) + } program.drawer.isolated { program.drawer.ortho(RenderTarget.active) diff --git a/orx-camera/src/commonMain/kotlin/OrbitalCamera.kt b/orx-camera/src/commonMain/kotlin/OrbitalCamera.kt index fb3647fc..93e698a1 100644 --- a/orx-camera/src/commonMain/kotlin/OrbitalCamera.kt +++ b/orx-camera/src/commonMain/kotlin/OrbitalCamera.kt @@ -8,6 +8,9 @@ import org.openrndr.events.Event import org.openrndr.math.Matrix44 import org.openrndr.math.Spherical import org.openrndr.math.Vector3 +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract import kotlin.math.abs import kotlin.math.max import kotlin.math.pow @@ -423,15 +426,20 @@ class OrbitalCamera( * in the same program. * @param function the function that is called in the isolation */ +@OptIn(ExperimentalContracts::class) fun OrbitalCamera.isolated(drawer: Drawer, function: Drawer.() -> Unit) { + contract { + callsInPlace(function, InvocationKind.EXACTLY_ONCE) + } drawer.pushTransforms() drawer.pushStyle() - - applyTo(drawer) - function(drawer) - - drawer.popStyle() - drawer.popTransforms() + try { + applyTo(drawer) + function(drawer) + } finally { + drawer.popStyle() + drawer.popTransforms() + } }