[orx-camera-2d] Add Camera2DManual

This commit is contained in:
Edwin Jakobs
2025-08-06 15:56:04 +02:00
parent 619bd1ea98
commit 68f2505351
2 changed files with 88 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ import org.openrndr.MouseEvents
import org.openrndr.Program
import org.openrndr.draw.Drawer
import org.openrndr.draw.RenderTarget
import org.openrndr.draw.isolated
import org.openrndr.events.Event
import org.openrndr.math.Matrix44
import org.openrndr.math.Vector2
@@ -23,6 +24,8 @@ import org.openrndr.math.transforms.buildTransform
class Camera2D : Extension, ChangeEvents {
override var enabled = true
private lateinit var program: Program
private var controlInitialized = false
var view = Matrix44.IDENTITY
var rotationCenter = Vector2.ZERO
@@ -38,6 +41,30 @@ class Camera2D : Extension, ChangeEvents {
override val hasChanged: Boolean
get() = dirty
/**
* Executes the provided drawing function in an isolated scope, preserving the current
* drawing state and then restoring it after the function is executed. The `ortho` projection
* and custom view transformation are applied during the isolated drawing session.
*
* @param function the drawing function to be applied within the isolated scope of the `Drawer`.
*/
fun isolated(function: Drawer.() -> Unit) {
program.drawer.isolated {
program.drawer.ortho(RenderTarget.active)
program.drawer.view = this@Camera2D.view
program.drawer.function()
}
}
/**
* Configures the mouse interaction events for controlling the camera view and handling
* transformations such as translation, rotation, and scaling via mouse inputs.
*
* @param mouse the MouseEvents instance that provides mouse interaction data, including
* button presses, dragging, and scrolling events.
*/
fun setupMouseEvents(mouse: MouseEvents) {
mouse.buttonDown.listen {
rotationCenter = it.position
@@ -76,10 +103,14 @@ class Camera2D : Extension, ChangeEvents {
dirty = true
}
}
controlInitialized = true
}
override fun setup(program: Program) {
setupMouseEvents(program.mouse)
this.program = program
if (!controlInitialized) {
setupMouseEvents(program.mouse)
}
}
override fun beforeDraw(drawer: Drawer, program: Program) {
@@ -94,4 +125,13 @@ class Camera2D : Extension, ChangeEvents {
}
}
/**
* Creates a new instance of the Camera2D extension suitable for manual application.
*
* @return a configured Camera2D instance ready to be used with the calling Program.
*/
fun Program.Camera2DManual(): Camera2D {
val camera = Camera2D()
camera.setup(this)
return camera
}