From 390dcf620cbcc1c827567754b8bb1731aaf7811e Mon Sep 17 00:00:00 2001 From: Abe Pazos Date: Thu, 20 Oct 2022 19:13:01 +0000 Subject: [PATCH] [orx-camera] Enable toggling user interaction in OrbitalControls (#276) --- orx-camera/build.gradle.kts | 1 + .../src/commonMain/kotlin/OrbitalControls.kt | 28 +++++----- orx-camera/src/demo/kotlin/DemoCamera2D_01.kt | 25 +++++++++ .../src/demo/kotlin/DemoOrbitalCamera01.kt | 52 +++++++++++++++++++ 4 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 orx-camera/src/demo/kotlin/DemoCamera2D_01.kt create mode 100644 orx-camera/src/demo/kotlin/DemoOrbitalCamera01.kt diff --git a/orx-camera/build.gradle.kts b/orx-camera/build.gradle.kts index 33731f43..e8a732d5 100644 --- a/orx-camera/build.gradle.kts +++ b/orx-camera/build.gradle.kts @@ -38,6 +38,7 @@ kotlin { val jvmDemo by getting { dependencies { implementation(project(":orx-camera")) + implementation(project(":orx-mesh-generators")) } } } diff --git a/orx-camera/src/commonMain/kotlin/OrbitalControls.kt b/orx-camera/src/commonMain/kotlin/OrbitalControls.kt index 3a2a3d8c..16c606b8 100644 --- a/orx-camera/src/commonMain/kotlin/OrbitalControls.kt +++ b/orx-camera/src/commonMain/kotlin/OrbitalControls.kt @@ -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) } } } diff --git a/orx-camera/src/demo/kotlin/DemoCamera2D_01.kt b/orx-camera/src/demo/kotlin/DemoCamera2D_01.kt new file mode 100644 index 00000000..bf12a1e4 --- /dev/null +++ b/orx-camera/src/demo/kotlin/DemoCamera2D_01.kt @@ -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) + } + } +} \ No newline at end of file diff --git a/orx-camera/src/demo/kotlin/DemoOrbitalCamera01.kt b/orx-camera/src/demo/kotlin/DemoOrbitalCamera01.kt new file mode 100644 index 00000000..ba136d12 --- /dev/null +++ b/orx-camera/src/demo/kotlin/DemoOrbitalCamera01.kt @@ -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) + } + } + } +} \ No newline at end of file