Skip to content

Commit

Permalink
VRScale: Add debug log to visualize error in button handling
Browse files Browse the repository at this point in the history
  • Loading branch information
moreApi committed Aug 16, 2021
1 parent bd7ac28 commit 94eed12
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions src/main/kotlin/graphics/scenery/controls/behaviours/VRScale.kt
Original file line number Diff line number Diff line change
@@ -1,57 +1,76 @@
package graphics.scenery.controls.behaviours

import graphics.scenery.Node
import graphics.scenery.attribute.spatial.Spatial
import graphics.scenery.controls.OpenVRHMD
import graphics.scenery.controls.TrackedDeviceType
import graphics.scenery.controls.TrackerRole
import graphics.scenery.utils.LazyLogger
import org.scijava.ui.behaviour.DragBehaviour
import kotlin.concurrent.thread


class VRScale(
private val name: String,
private val controller: Node,
private val controller: Spatial,
private val offhand: VRScaleOffhand,
private val setScale: (Float) -> Unit
) :
DragBehaviour {

protected val logger by LazyLogger(System.getProperty("scenery.LogLevel", "info"))

// --- two hand drag behavior ---
//TODO fix make order of presses irrelevant
private var bothPressed = false
override fun init(x: Int, y: Int) {}
override fun init(x: Int, y: Int) {
if (offhand.pressed){
bothPressed = true
bothInit()
}
}
override fun drag(x: Int, y: Int) {
if (!offhand.pressed) {
bothPressed = false
bothEnd()
} else if (!bothPressed) {
logger.info("both drag OH:${offhand.pressed} Both:${bothPressed}")
if (offhand.pressed && !bothPressed){
bothPressed = true
bothInit()
} else {
}
if (!offhand.pressed && bothPressed){
bothPressed = false
bothEnd()
}
if (bothPressed){
bothDrag()
}
}

override fun end(x: Int, y: Int) {}
override fun end(x: Int, y: Int) {
if (bothPressed){
bothEnd()
bothPressed = false
}
}


// --- actual behavior ---
var startDistance: Float = 0f
var lastDistance: Float = 0f
private fun bothInit() {
startDistance = controller.spatialOrNull()!!.worldPosition()
.distance(offhand.controller.spatialOrNull()!!.worldPosition())
logger.warn("both init")
lastDistance = controller.worldPosition()
.distance(offhand.controller.worldPosition())
}

private fun bothDrag() {
val newDistance = controller.spatialOrNull()!!.worldPosition()
.distance(offhand.controller.spatialOrNull()!!.worldPosition())
val scale = newDistance / startDistance
startDistance = controller.spatialOrNull()!!.worldPosition()
.distance(offhand.controller.spatialOrNull()!!.worldPosition())
val newDistance = controller.worldPosition()
.distance(offhand.controller.worldPosition())
val scale = newDistance / lastDistance
lastDistance = newDistance
setScale(scale)
}

private fun bothEnd() {
startDistance = 0f
logger.warn("both end")
lastDistance = 0f
}


Expand Down Expand Up @@ -92,11 +111,11 @@ class VRScale(
Thread.sleep(1000)
}

val offhand = VRScaleOffhand("offhand", offhandController!!)
val offhand = VRScaleOffhand("offhand", offhandController!!.spatialOrNull()!!)
hmd.addBehaviour("offhandZoom", offhand)
hmd.addKeyBinding("offhandZoom", TrackerRole.RightHand, button)

hmd.addBehaviour("mainhandZoom", VRScale("mainhand", mainhandController!!, offhand, setScale))
hmd.addBehaviour("mainhandZoom", VRScale("mainhand", mainhandController!!.spatialOrNull()!!, offhand, setScale))
hmd.addKeyBinding("mainhandZoom", TrackerRole.LeftHand, button)
}

Expand All @@ -105,16 +124,19 @@ class VRScale(

}

class VRScaleOffhand(val name: String, val controller: Node) : DragBehaviour {
class VRScaleOffhand(val name: String, val controller: Spatial) : DragBehaviour {
var pressed = false
protected val logger by LazyLogger(System.getProperty("scenery.LogLevel", "info"))
override fun init(x: Int, y: Int) {
logger.warn("offhand init")
pressed = true
}

override fun drag(x: Int, y: Int) {
}

override fun end(x: Int, y: Int) {
logger.warn("offhand end")
pressed = false
}
}

0 comments on commit 94eed12

Please sign in to comment.