[orx-camera] Re-add Camera2D (#275)

This commit is contained in:
Abe Pazos
2022-09-15 19:32:22 +00:00
committed by GitHub
parent 819537c6c4
commit d8c73af3a0
3 changed files with 87 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
package org.openrndr.extra.camera
import org.openrndr.Extension
import org.openrndr.Program
import org.openrndr.draw.Drawer
import org.openrndr.math.Matrix44
import org.openrndr.math.transforms.buildTransform
/**
* The [Camera2D] extension enables:
* - **panning** the view by moving the mouse while a mouse button is pressed
* - **zooming** in and out by using the mouse wheel
*
* Usage: `extend(Camera2D())`
*/
class Camera2D : Extension {
override var enabled = true
var view = Matrix44.IDENTITY
override fun setup(program: Program) {
program.mouse.dragged.listen {
view = buildTransform { translate(it.dragDisplacement) } * view
}
program.mouse.scrolled.listen {
val scaleFactor = 1.0 - it.rotation.y * 0.03
view = buildTransform {
translate(it.position)
scale(scaleFactor)
translate(-it.position)
} * view
}
}
override fun beforeDraw(drawer: Drawer, program: Program) {
drawer.view = view
}
}