update
This commit is contained in:
		@@ -1,9 +1,18 @@
 | 
			
		||||
package com.rookiedev.hexapod
 | 
			
		||||
 | 
			
		||||
import androidx.appcompat.app.AppCompatActivity
 | 
			
		||||
import android.annotation.SuppressLint
 | 
			
		||||
import android.content.res.Resources
 | 
			
		||||
import android.os.Bundle
 | 
			
		||||
import android.view.WindowInsets
 | 
			
		||||
import android.view.WindowInsetsController
 | 
			
		||||
import android.util.TypedValue
 | 
			
		||||
import android.view.*
 | 
			
		||||
import android.widget.ImageView
 | 
			
		||||
import androidx.appcompat.app.AppCompatActivity
 | 
			
		||||
import java.lang.Math.min
 | 
			
		||||
import kotlin.math.pow
 | 
			
		||||
import kotlin.math.sqrt
 | 
			
		||||
import kotlin.math.atan2
 | 
			
		||||
import kotlin.math.PI
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Behaviors of immersive mode.
 | 
			
		||||
@@ -53,13 +62,96 @@ enum class TypeOption(
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class ControlActivity : AppCompatActivity() {
 | 
			
		||||
    private var pxMargin = 0f
 | 
			
		||||
    private var width = 0
 | 
			
		||||
    private var height = 0
 | 
			
		||||
    private var radius = 0f
 | 
			
		||||
 | 
			
		||||
    @SuppressLint("ClickableViewAccessibility")
 | 
			
		||||
    override fun onCreate(savedInstanceState: Bundle?) {
 | 
			
		||||
        super.onCreate(savedInstanceState)
 | 
			
		||||
        setContentView(R.layout.activity_control)
 | 
			
		||||
 | 
			
		||||
        controlWindowInsets(true)
 | 
			
		||||
 | 
			
		||||
        val controlCircle = findViewById<ImageView>(R.id.control_image)
 | 
			
		||||
 | 
			
		||||
        val dip = 32f
 | 
			
		||||
        val r: Resources = resources
 | 
			
		||||
        this.pxMargin = TypedValue.applyDimension(
 | 
			
		||||
            TypedValue.COMPLEX_UNIT_DIP,
 | 
			
		||||
            dip,
 | 
			
		||||
            r.displayMetrics
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        val vto: ViewTreeObserver = controlCircle.viewTreeObserver
 | 
			
		||||
        vto.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
 | 
			
		||||
            override fun onPreDraw(): Boolean {
 | 
			
		||||
                controlCircle.viewTreeObserver.removeOnPreDrawListener(this)
 | 
			
		||||
                height = controlCircle.measuredHeight
 | 
			
		||||
                width = controlCircle.measuredWidth
 | 
			
		||||
                radius = width.coerceAtMost(height) / 2f
 | 
			
		||||
                println(radius)
 | 
			
		||||
                return true
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        controlCircle.setOnTouchListener(
 | 
			
		||||
            object : View.OnTouchListener {
 | 
			
		||||
                override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
 | 
			
		||||
                    val touchX = motionEvent.x
 | 
			
		||||
                    val touchY = motionEvent.y
 | 
			
		||||
                    if (touchX < 0) {
 | 
			
		||||
                        return false
 | 
			
		||||
                    }
 | 
			
		||||
                    if (touchY < 0) {
 | 
			
		||||
                        return false
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    val coorX = touchX - width / 2
 | 
			
		||||
                    val coorY = touchY - height / 2
 | 
			
		||||
 | 
			
		||||
                    val length = sqrt(coorX.pow(2) + coorY.pow(2))
 | 
			
		||||
                    if (length < radius / 3) {
 | 
			
		||||
                        println("Standby")
 | 
			
		||||
                    } else if (length >= radius / 3 && length < 2 * radius / 3) {
 | 
			
		||||
                        var angle = atan2(coorY, coorX)
 | 
			
		||||
                        if (angle>-PI/4 && angle<=PI/4)
 | 
			
		||||
                        {
 | 
			
		||||
                            println("Move right")
 | 
			
		||||
                        } else if ( angle>PI/4 && angle<=3*PI/4){
 | 
			
		||||
                            println("Move back")
 | 
			
		||||
                        } else if ( angle>-3*PI/4 && angle<-PI/4){
 | 
			
		||||
                            println("Move forward")
 | 
			
		||||
                        } else {
 | 
			
		||||
                            println("Move left")
 | 
			
		||||
                        }
 | 
			
		||||
                    } else if (length >= 2 * radius / 3 && length < radius) {
 | 
			
		||||
                        var angle = atan2(coorY, coorX)
 | 
			
		||||
                        if (angle>-PI/4 && angle<=PI/4)
 | 
			
		||||
                        {
 | 
			
		||||
                            println("Turn right")
 | 
			
		||||
                        } else if ( angle>PI/4 && angle<=3*PI/4){
 | 
			
		||||
                            println("Fast back")
 | 
			
		||||
                        } else if ( angle>-3*PI/4 && angle<-PI/4){
 | 
			
		||||
                            println("Fast forward")
 | 
			
		||||
                        } else {
 | 
			
		||||
                            println("Turn left")
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
//                    val width = view.width
 | 
			
		||||
//                    val height = view.height
 | 
			
		||||
//                    println(width.toString().plus(":").plus(height.toString()))
 | 
			
		||||
//                    println(touchX.toString().plus(":").plus(touchY.toString()))
 | 
			
		||||
//                    println(coorX.toString().plus(":").plus(coorY.toString()))
 | 
			
		||||
//                    println(radius)
 | 
			
		||||
                    return true
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    private fun controlWindowInsets(hide: Boolean) {
 | 
			
		||||
        // WindowInsetsController can hide or show specified system bars.
 | 
			
		||||
        val insetsController = window.decorView.windowInsetsController ?: return
 | 
			
		||||
 
 | 
			
		||||
@@ -78,7 +78,7 @@
 | 
			
		||||
        android:layout_weight="1">
 | 
			
		||||
 | 
			
		||||
        <ImageView
 | 
			
		||||
            android:id="@+id/imageView"
 | 
			
		||||
            android:id="@+id/control_image"
 | 
			
		||||
            android:layout_width="match_parent"
 | 
			
		||||
            android:layout_height="match_parent"
 | 
			
		||||
            android:layout_marginStart="32dp"
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user