update control logic
This commit is contained in:
parent
e75a1c983d
commit
83c6621482
@ -96,6 +96,9 @@ class ControlActivity : AppCompatActivity() {
|
||||
private const val CMD_ROTATEZ = "rotatez:"
|
||||
|
||||
private const val CMD_TWIST = "twist:"
|
||||
|
||||
private const val CTRL_LEFT = 1
|
||||
private const val CTRL_RIGHT = 0
|
||||
}
|
||||
|
||||
private var rightWidth = 0
|
||||
@ -124,6 +127,8 @@ class ControlActivity : AppCompatActivity() {
|
||||
private var rightControlImage: ImageView? = null
|
||||
private var leftControlImage: ImageView? = null
|
||||
|
||||
private var controlImg = CTRL_RIGHT
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@ -163,7 +168,7 @@ class ControlActivity : AppCompatActivity() {
|
||||
override fun onPreDraw(): Boolean {
|
||||
leftControlImage!!.viewTreeObserver.removeOnPreDrawListener(this)
|
||||
leftHeight = leftControlImage!!.measuredHeight
|
||||
leftWidth = 2*leftHeight/3
|
||||
leftWidth = 2 * leftHeight / 3
|
||||
return true
|
||||
}
|
||||
})
|
||||
@ -171,6 +176,12 @@ class ControlActivity : AppCompatActivity() {
|
||||
rightControlImage!!.setOnTouchListener(
|
||||
object : View.OnTouchListener {
|
||||
override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
|
||||
when (motionEvent.action) {
|
||||
MotionEvent.ACTION_MOVE, MotionEvent.ACTION_DOWN -> {
|
||||
if (motionEvent.action == MotionEvent.ACTION_DOWN) {
|
||||
controlImg = CTRL_RIGHT
|
||||
}
|
||||
if (controlImg == CTRL_RIGHT) {
|
||||
val touchX = motionEvent.x
|
||||
val touchY = motionEvent.y
|
||||
if (touchX < 0) {
|
||||
@ -273,6 +284,20 @@ class ControlActivity : AppCompatActivity() {
|
||||
}
|
||||
leftControlImage!!.setImageResource(R.drawable.ic_control_left)
|
||||
}
|
||||
}
|
||||
}
|
||||
MotionEvent.ACTION_UP -> {
|
||||
if (controlImg == CTRL_RIGHT) {
|
||||
sendMessageAsync(CMD_STANDBY)
|
||||
currentState = CMD_STANDBY
|
||||
rightControlImage!!.setImageResource(R.drawable.ic_control_circle_standby)
|
||||
|
||||
leftControlImage!!.setImageResource(R.drawable.ic_control_left)
|
||||
|
||||
controlImg = CTRL_LEFT
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
@ -281,6 +306,12 @@ class ControlActivity : AppCompatActivity() {
|
||||
leftControlImage!!.setOnTouchListener(
|
||||
object : View.OnTouchListener {
|
||||
override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
|
||||
when (motionEvent.action) {
|
||||
MotionEvent.ACTION_MOVE, MotionEvent.ACTION_DOWN -> {
|
||||
if (motionEvent.action == MotionEvent.ACTION_DOWN) {
|
||||
controlImg = CTRL_LEFT
|
||||
}
|
||||
if (controlImg == CTRL_LEFT) {
|
||||
val touchX = motionEvent.x
|
||||
val touchY = motionEvent.y
|
||||
if (touchX < 0 || touchX > leftWidth) {
|
||||
@ -290,14 +321,14 @@ class ControlActivity : AppCompatActivity() {
|
||||
return false
|
||||
}
|
||||
|
||||
if (touchX < leftWidth/2){
|
||||
if(touchY< leftHeight/3){
|
||||
if (touchX < leftWidth / 2) {
|
||||
if (touchY < leftHeight / 3) {
|
||||
if (currentState != CMD_ROTATEY) {
|
||||
sendMessageAsync(CMD_ROTATEY)
|
||||
currentState = CMD_ROTATEY
|
||||
leftControlImage!!.setImageResource(R.drawable.ic_control_left_rotatey)
|
||||
}
|
||||
} else if( touchY >= leftHeight/3 && touchY < 2*leftHeight/3) {
|
||||
} else if (touchY >= leftHeight / 3 && touchY < 2 * leftHeight / 3) {
|
||||
if (currentState != CMD_ROTATEX) {
|
||||
sendMessageAsync(CMD_ROTATEX)
|
||||
currentState = CMD_ROTATEX
|
||||
@ -311,13 +342,13 @@ class ControlActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(touchY< leftHeight/3){
|
||||
if (touchY < leftHeight / 3) {
|
||||
if (currentState != CMD_CLIMBFORWARD) {
|
||||
sendMessageAsync(CMD_CLIMBFORWARD)
|
||||
currentState = CMD_CLIMBFORWARD
|
||||
leftControlImage!!.setImageResource(R.drawable.ic_control_left_climb_forward)
|
||||
}
|
||||
} else if( touchY >= leftHeight/3 && touchY < 2*leftHeight/3) {
|
||||
} else if (touchY >= leftHeight / 3 && touchY < 2 * leftHeight / 3) {
|
||||
if (currentState != CMD_TWIST) {
|
||||
sendMessageAsync(CMD_TWIST)
|
||||
currentState = CMD_TWIST
|
||||
@ -332,6 +363,20 @@ class ControlActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
rightControlImage!!.setImageResource(R.drawable.ic_control_circle)
|
||||
}
|
||||
}
|
||||
MotionEvent.ACTION_UP -> {
|
||||
if (controlImg == CTRL_LEFT) {
|
||||
sendMessageAsync(CMD_STANDBY)
|
||||
currentState = CMD_STANDBY
|
||||
rightControlImage!!.setImageResource(R.drawable.ic_control_circle_standby)
|
||||
|
||||
leftControlImage!!.setImageResource(R.drawable.ic_control_left)
|
||||
|
||||
controlImg = CTRL_RIGHT
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
@ -458,7 +503,8 @@ class ControlActivity : AppCompatActivity() {
|
||||
"Unable to connect to the Hexapod."
|
||||
)
|
||||
alert.setOnCancelListener { finish() }
|
||||
alert.setButton(AlertDialog.BUTTON_POSITIVE,
|
||||
alert.setButton(
|
||||
AlertDialog.BUTTON_POSITIVE,
|
||||
"OK"
|
||||
) { _, _ -> finish() }
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user