clean up code

This commit is contained in:
Raffaele Mignone 2019-01-16 21:35:55 +01:00
parent 7d2342692e
commit 2d8254b264
Signed by: norangebit
GPG Key ID: 4B9DF72AB9508845
6 changed files with 24 additions and 37 deletions

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

@ -62,6 +62,7 @@ class MainActivity : AppCompatActivity() {
GlobalScope.launch(Dispatchers.Main) { GlobalScope.launch(Dispatchers.Main) {
loadPlanetsJob.join() loadPlanetsJob.join()
val solarSystem = createSolarSystem(renderablePlanets) val solarSystem = createSolarSystem(renderablePlanets)
addNodeToScene(arFragment, hitResult.createAnchor(), solarSystem) addNodeToScene(arFragment, hitResult.createAnchor(), solarSystem)
isModelAdded = true isModelAdded = true

View File

@ -20,7 +20,6 @@
package it.norangeb.solarsystem package it.norangeb.solarsystem
import com.google.ar.sceneform.FrameTime
import com.google.ar.sceneform.Node import com.google.ar.sceneform.Node
import com.google.ar.sceneform.math.Vector3 import com.google.ar.sceneform.math.Vector3
import com.google.ar.sceneform.rendering.ModelRenderable import com.google.ar.sceneform.rendering.ModelRenderable
@ -28,7 +27,7 @@ import com.google.ar.sceneform.rendering.ModelRenderable
class PlanetNode( class PlanetNode(
private val planetRenderable: ModelRenderable private val planetRenderable: ModelRenderable
) : Node() { ) : Node() {
private val planetScale = 0.6f private val PLANET_SCALE = 0.6f
private var planetVisual: RotationNode? = null private var planetVisual: RotationNode? = null
override fun onActivate() { override fun onActivate() {
@ -37,18 +36,12 @@ class PlanetNode(
if (planetVisual == null) if (planetVisual == null)
initRotationNode() initRotationNode()
} }
override fun onUpdate(frameTime: FrameTime?) { private fun initRotationNode() {
if (scene == null) planetVisual = RotationNode()
return
}
fun initRotationNode() {
planetVisual = RotationNode(false)
planetVisual?.setParent(this) planetVisual?.setParent(this)
planetVisual?.renderable = planetRenderable planetVisual?.renderable = planetRenderable
planetVisual?.localScale = Vector3(planetScale, planetScale, planetScale) planetVisual?.localScale = Vector3(PLANET_SCALE, PLANET_SCALE, PLANET_SCALE)
} }
} }

View File

@ -22,33 +22,21 @@ package it.norangeb.solarsystem
import android.animation.ObjectAnimator import android.animation.ObjectAnimator
import android.view.animation.LinearInterpolator import android.view.animation.LinearInterpolator
import com.google.ar.sceneform.FrameTime
import com.google.ar.sceneform.Node import com.google.ar.sceneform.Node
import com.google.ar.sceneform.math.Quaternion import com.google.ar.sceneform.math.Quaternion
import com.google.ar.sceneform.math.QuaternionEvaluator import com.google.ar.sceneform.math.QuaternionEvaluator
import com.google.ar.sceneform.math.Vector3 import com.google.ar.sceneform.math.Vector3
class RotationNode(private val isOrbit: Boolean = true) : Node() { class RotationNode : Node() {
val SPEED_MULTIPLIER = 1 private val DEGREE = 360000
val ROTATION_MULTIPLIER = 1
private var orbitAnimation: ObjectAnimator? = null private var orbitAnimation: ObjectAnimator? = null
var degreesPerSecond = 90.0f var degreesPerSecond = 90.0f
private val animationDuration: Long private val animationDuration: Long
get() = (1000 * 360 / (degreesPerSecond * if (isOrbit) get() = (DEGREE / degreesPerSecond)
SPEED_MULTIPLIER
else
ROTATION_MULTIPLIER))
.toLong() .toLong()
override fun onUpdate(frameTime: FrameTime?) {
super.onUpdate(frameTime)
if (orbitAnimation == null)
return
}
override fun onActivate() { override fun onActivate() {
startAnimation() startAnimation()
} }
@ -58,9 +46,9 @@ class RotationNode(private val isOrbit: Boolean = true) : Node() {
} }
private fun startAnimation() { private fun startAnimation() {
if (orbitAnimation != null) { if (orbitAnimation != null)
return return
}
orbitAnimation = createAnimator() orbitAnimation = createAnimator()
orbitAnimation!!.target = this orbitAnimation!!.target = this
orbitAnimation!!.duration = animationDuration orbitAnimation!!.duration = animationDuration
@ -68,21 +56,19 @@ class RotationNode(private val isOrbit: Boolean = true) : Node() {
} }
private fun stopAnimation() { private fun stopAnimation() {
if (orbitAnimation == null) { if (orbitAnimation == null)
return return
}
orbitAnimation!!.cancel() orbitAnimation!!.cancel()
orbitAnimation = null orbitAnimation = null
} }
private fun createAnimator(): ObjectAnimator { private fun createAnimator(): ObjectAnimator {
val orientation1 = Quaternion.axisAngle(Vector3(0.0f, 1.0f, 0.0f), 0f) val orientations = arrayOf(0f, 120f, 240f, 360f)
val orientation2 = Quaternion.axisAngle(Vector3(0.0f, 1.0f, 0.0f), 120f) .map { Quaternion.axisAngle(Vector3(0.0f, 1.0f, 0.0f), it) }
val orientation3 = Quaternion.axisAngle(Vector3(0.0f, 1.0f, 0.0f), 240f)
val orientation4 = Quaternion.axisAngle(Vector3(0.0f, 1.0f, 0.0f), 360f)
val orbitAnimation = ObjectAnimator() val orbitAnimation = ObjectAnimator()
orbitAnimation.setObjectValues(orientation1, orientation2, orientation3, orientation4) orbitAnimation.setObjectValues(*orientations.toTypedArray())
orbitAnimation.propertyName = "localRotation" orbitAnimation.propertyName = "localRotation"

View File

@ -7,7 +7,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.2.1' classpath 'com.android.tools.build:gradle:3.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong

View File

@ -1,5 +1,6 @@
#Wed Jan 16 18:00:47 CET 2019
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip