open-ar-samples/solar-system/app/src/main/java/it/norangeb/solarsystem/RotationNode.kt

85 lines
2.5 KiB
Kotlin
Raw Normal View History

2019-01-12 15:39:42 +00:00
/*
* Copyright (c) 2019, norangebit
*
* This file is part of solar-system.
*
* solar-system is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* solar-system is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with solar-system. If not, see <http://www.gnu.org/licenses/>
*
*/
package it.norangeb.solarsystem
import android.animation.ObjectAnimator
import android.view.animation.LinearInterpolator
import com.google.ar.sceneform.Node
import com.google.ar.sceneform.math.Quaternion
import com.google.ar.sceneform.math.QuaternionEvaluator
import com.google.ar.sceneform.math.Vector3
2019-01-16 20:35:55 +00:00
class RotationNode : Node() {
private val DEGREE = 360000
2019-01-12 15:39:42 +00:00
private var orbitAnimation: ObjectAnimator? = null
var degreesPerSecond = 90.0f
private val animationDuration: Long
2019-01-16 20:35:55 +00:00
get() = (DEGREE / degreesPerSecond)
2019-01-12 15:39:42 +00:00
.toLong()
override fun onActivate() {
startAnimation()
}
override fun onDeactivate() {
stopAnimation()
}
private fun startAnimation() {
2019-01-16 20:35:55 +00:00
if (orbitAnimation != null)
2019-01-12 15:39:42 +00:00
return
2019-01-16 20:35:55 +00:00
2019-01-12 15:39:42 +00:00
orbitAnimation = createAnimator()
orbitAnimation!!.target = this
orbitAnimation!!.duration = animationDuration
orbitAnimation!!.start()
}
private fun stopAnimation() {
2019-01-16 20:35:55 +00:00
if (orbitAnimation == null)
2019-01-12 15:39:42 +00:00
return
2019-01-16 20:35:55 +00:00
2019-01-12 15:39:42 +00:00
orbitAnimation!!.cancel()
orbitAnimation = null
}
private fun createAnimator(): ObjectAnimator {
2019-01-16 20:35:55 +00:00
val orientations = arrayOf(0f, 120f, 240f, 360f)
.map { Quaternion.axisAngle(Vector3(0.0f, 1.0f, 0.0f), it) }
2019-01-12 15:39:42 +00:00
val orbitAnimation = ObjectAnimator()
2019-01-16 20:35:55 +00:00
orbitAnimation.setObjectValues(*orientations.toTypedArray())
2019-01-12 15:39:42 +00:00
orbitAnimation.propertyName = "localRotation"
orbitAnimation.setEvaluator(QuaternionEvaluator())
orbitAnimation.repeatCount = ObjectAnimator.INFINITE
orbitAnimation.repeatMode = ObjectAnimator.RESTART
orbitAnimation.interpolator = LinearInterpolator()
orbitAnimation.setAutoCancel(true)
return orbitAnimation
}
}