From 04519d38752a9c8e71b80c1239b619f9014b2489 Mon Sep 17 00:00:00 2001 From: Zhengyu Peng Date: Mon, 14 Feb 2022 11:42:02 -0500 Subject: [PATCH] update --- .../com/rookiedev/hexapod/ControlActivity.kt | 98 ++++++++++++++++++- .../src/main/res/layout/activity_control.xml | 2 +- 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/software/android/app/src/main/java/com/rookiedev/hexapod/ControlActivity.kt b/software/android/app/src/main/java/com/rookiedev/hexapod/ControlActivity.kt index be55792..51da71e 100644 --- a/software/android/app/src/main/java/com/rookiedev/hexapod/ControlActivity.kt +++ b/software/android/app/src/main/java/com/rookiedev/hexapod/ControlActivity.kt @@ -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(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 diff --git a/software/android/app/src/main/res/layout/activity_control.xml b/software/android/app/src/main/res/layout/activity_control.xml index db9fe41..6a5aa45 100644 --- a/software/android/app/src/main/res/layout/activity_control.xml +++ b/software/android/app/src/main/res/layout/activity_control.xml @@ -78,7 +78,7 @@ android:layout_weight="1">