[orx-camera] Enable toggling user interaction in OrbitalControls (#276)

This commit is contained in:
Abe Pazos
2022-10-20 19:13:01 +00:00
committed by GitHub
parent 0a0e102da9
commit 390dcf620c
4 changed files with 93 additions and 13 deletions

View File

@@ -38,6 +38,7 @@ kotlin {
val jvmDemo by getting {
dependencies {
implementation(project(":orx-camera"))
implementation(project(":orx-mesh-generators"))
}
}
}

View File

@@ -8,7 +8,11 @@ import kotlin.math.PI
import kotlin.math.abs
import kotlin.math.tan
class OrbitalControls(val orbitalCamera: OrbitalCamera, val userInteraction: Boolean = true, val keySpeed: Double = 1.0) : Extension {
class OrbitalControls(
val orbitalCamera: OrbitalCamera,
var userInteraction: Boolean = true,
val keySpeed: Double = 1.0
) : Extension {
enum class STATE {
NONE,
ROTATE,
@@ -22,7 +26,7 @@ class OrbitalControls(val orbitalCamera: OrbitalCamera, val userInteraction: Boo
private lateinit var lastMousePosition: Vector2
private fun mouseScrolled(event: MouseEvent) {
if (!event.propagationCancelled) {
if (userInteraction && !event.propagationCancelled) {
if (orbitalCamera.projectionType == ProjectionType.PERSPECTIVE) {
if (abs(event.rotation.x) > 0.1) return
@@ -42,7 +46,7 @@ class OrbitalControls(val orbitalCamera: OrbitalCamera, val userInteraction: Boo
private fun mouseMoved(event: MouseEvent) {
if (!event.propagationCancelled) {
if (userInteraction && !event.propagationCancelled) {
if (state == STATE.NONE) return
val delta = lastMousePosition - event.position
lastMousePosition = event.position
@@ -67,7 +71,7 @@ class OrbitalControls(val orbitalCamera: OrbitalCamera, val userInteraction: Boo
}
private fun mouseButtonDown(event: MouseEvent) {
if (!event.propagationCancelled) {
if (userInteraction && !event.propagationCancelled) {
val previousState = state
when (event.button) {
@@ -90,7 +94,7 @@ class OrbitalControls(val orbitalCamera: OrbitalCamera, val userInteraction: Boo
}
fun keyPressed(keyEvent: KeyEvent) {
if (!keyEvent.propagationCancelled) {
if (userInteraction && !keyEvent.propagationCancelled) {
if (keyEvent.key == KEY_ARROW_RIGHT) {
orbitalCamera.pan(keySpeed, 0.0, 0.0)
}
@@ -138,13 +142,11 @@ class OrbitalControls(val orbitalCamera: OrbitalCamera, val userInteraction: Boo
override fun setup(program: Program) {
this.program = program
if (userInteraction) {
program.mouse.moved.listen { mouseMoved(it) }
program.mouse.buttonDown.listen { mouseButtonDown(it) }
program.mouse.buttonUp.listen { state = STATE.NONE }
program.mouse.scrolled.listen { mouseScrolled(it) }
program.keyboard.keyDown.listen { keyPressed(it) }
program.keyboard.keyRepeat.listen { keyPressed(it) }
}
program.mouse.moved.listen { mouseMoved(it) }
program.mouse.buttonDown.listen { mouseButtonDown(it) }
program.mouse.buttonUp.listen { state = STATE.NONE }
program.mouse.scrolled.listen { mouseScrolled(it) }
program.keyboard.keyDown.listen { keyPressed(it) }
program.keyboard.keyRepeat.listen { keyPressed(it) }
}
}

View File

@@ -0,0 +1,25 @@
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.loadFont
import org.openrndr.extra.camera.Camera2D
/**
* # Camera2D demo
*
* click and drag the mouse for panning, use the mouse wheel for zooming
*/
fun main() = application {
program {
val font = loadFont("demo-data/fonts/IBMPlexMono-Regular.ttf", 72.0)
extend(Camera2D())
extend {
drawer.circle(drawer.bounds.center, 300.0)
drawer.fontMap = font
drawer.fill = ColorRGBa.PINK
drawer.text("click and drag mouse", 50.0, 400.0)
drawer.text("use mouse wheel", 50.0, 500.0)
}
}
}

View File

@@ -0,0 +1,52 @@
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.DrawPrimitive
import org.openrndr.extra.camera.AxisHelper
import org.openrndr.extra.camera.GridHelper
import org.openrndr.extra.camera.OrbitalCamera
import org.openrndr.extra.camera.OrbitalControls
import org.openrndr.extra.meshgenerators.boxMesh
import org.openrndr.extra.meshgenerators.sphereMesh
import org.openrndr.math.Vector3
fun main() = application {
program {
val camera = OrbitalCamera(
Vector3.UNIT_Z * 90.0, Vector3.ZERO, 90.0, 0.1, 5000.0
)
val controls = OrbitalControls(camera, keySpeed = 10.0)
controls.userInteraction = false
val sphere = sphereMesh(radius = 25.0)
val cube = boxMesh(20.0, 20.0, 5.0, 5, 5, 2)
extend(camera)
// shows XYZ axes as RGB lines
extend(AxisHelper())
// debug ground plane
extend(GridHelper(100))
// adds mouse and keyboard bindings
extend(controls)
extend {
// mouse and keyboard input can be toggled on and off
controls.userInteraction = seconds.toInt() % 4 < 2
drawer.vertexBuffer(sphere, DrawPrimitive.LINE_LOOP)
drawer.vertexBuffer(cube, DrawPrimitive.LINE_LOOP)
drawer.stroke = if(controls.userInteraction) ColorRGBa.GREEN else ColorRGBa.WHITE
drawer.fill = null
repeat(10) {
drawer.translate(0.0, 0.0, 10.0)
// Note: 2D primitives are not optimized for 3D and can
// occlude each other
drawer.circle(0.0, 0.0, 50.0)
}
}
}
}