TranslationController.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * TranslationController.js
  3. *
  4. * @author realor
  5. */
  6. import { AnimationController } from './AnimationController.js'
  7. import { Controller } from './Controller.js'
  8. class TranslationController extends AnimationController {
  9. constructor(object, name) {
  10. super(object, name)
  11. let minPosition = object.position.x
  12. let maxPosition = object.position.x + 1
  13. this.axis = 'x'
  14. this.minPosition = minPosition
  15. this.maxPosition = maxPosition
  16. this.minValue = 0
  17. this.maxValue = 1
  18. this.maxSpeed = 1
  19. }
  20. animate(event) {
  21. let value = this.input
  22. if (value === null) return
  23. value = parseFloat(value)
  24. if (typeof value !== 'number') return
  25. let axis = this.axis || 'x'
  26. let minValue = parseFloat(this.minValue)
  27. let maxValue = parseFloat(this.maxValue)
  28. let minPosition = parseFloat(this.minPosition)
  29. let maxPosition = parseFloat(this.maxPosition)
  30. let maxSpeed = parseFloat(this.maxSpeed)
  31. let factor = (value - minValue) / (maxValue - minValue) // [0..1]
  32. let targetPosition = minPosition + factor * (maxPosition - minPosition)
  33. let position = this.object.position[axis]
  34. let delta = targetPosition - position
  35. if (Math.abs(delta) < 0.001) {
  36. this.stopAnimation()
  37. } else {
  38. let speed = delta / 0.2
  39. if (speed > maxSpeed) speed = maxSpeed
  40. else if (speed < -maxSpeed) speed = -maxSpeed
  41. this.object.position[axis] += speed * event.delta
  42. this.object.updateMatrix()
  43. this.application.notifyObjectsChanged(this.object, this)
  44. }
  45. }
  46. }
  47. Controller.addClass(TranslationController)
  48. export { TranslationController }