[orx-camera] Re-add Camera2D (#275)
This commit is contained in:
36
orx-camera/src/commonMain/kotlin/Camera2D.kt
Normal file
36
orx-camera/src/commonMain/kotlin/Camera2D.kt
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user