update control logic

master
Zhengyu Peng 3 years ago
parent fede08da90
commit a46914e3a0

@ -96,6 +96,9 @@ class ControlActivity : AppCompatActivity() {
private const val CMD_ROTATEZ = "rotatez:" private const val CMD_ROTATEZ = "rotatez:"
private const val CMD_TWIST = "twist:" private const val CMD_TWIST = "twist:"
private const val CTRL_LEFT = 1
private const val CTRL_RIGHT = 0
} }
private var rightWidth = 0 private var rightWidth = 0
@ -124,6 +127,8 @@ class ControlActivity : AppCompatActivity() {
private var rightControlImage: ImageView? = null private var rightControlImage: ImageView? = null
private var leftControlImage: ImageView? = null private var leftControlImage: ImageView? = null
private var controlImg = CTRL_RIGHT
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -163,7 +168,7 @@ class ControlActivity : AppCompatActivity() {
override fun onPreDraw(): Boolean { override fun onPreDraw(): Boolean {
leftControlImage!!.viewTreeObserver.removeOnPreDrawListener(this) leftControlImage!!.viewTreeObserver.removeOnPreDrawListener(this)
leftHeight = leftControlImage!!.measuredHeight leftHeight = leftControlImage!!.measuredHeight
leftWidth = 2*leftHeight/3 leftWidth = 2 * leftHeight / 3
return true return true
} }
}) })
@ -171,6 +176,12 @@ class ControlActivity : AppCompatActivity() {
rightControlImage!!.setOnTouchListener( rightControlImage!!.setOnTouchListener(
object : View.OnTouchListener { object : View.OnTouchListener {
override fun onTouch(view: View, motionEvent: MotionEvent): Boolean { 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 touchX = motionEvent.x
val touchY = motionEvent.y val touchY = motionEvent.y
if (touchX < 0) { if (touchX < 0) {
@ -273,6 +284,20 @@ class ControlActivity : AppCompatActivity() {
} }
leftControlImage!!.setImageResource(R.drawable.ic_control_left) 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 return true
} }
} }
@ -281,6 +306,12 @@ class ControlActivity : AppCompatActivity() {
leftControlImage!!.setOnTouchListener( leftControlImage!!.setOnTouchListener(
object : View.OnTouchListener { object : View.OnTouchListener {
override fun onTouch(view: View, motionEvent: MotionEvent): Boolean { 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 touchX = motionEvent.x
val touchY = motionEvent.y val touchY = motionEvent.y
if (touchX < 0 || touchX > leftWidth) { if (touchX < 0 || touchX > leftWidth) {
@ -290,14 +321,14 @@ class ControlActivity : AppCompatActivity() {
return false return false
} }
if (touchX < leftWidth/2){ if (touchX < leftWidth / 2) {
if(touchY< leftHeight/3){ if (touchY < leftHeight / 3) {
if (currentState != CMD_ROTATEY) { if (currentState != CMD_ROTATEY) {
sendMessageAsync(CMD_ROTATEY) sendMessageAsync(CMD_ROTATEY)
currentState = CMD_ROTATEY currentState = CMD_ROTATEY
leftControlImage!!.setImageResource(R.drawable.ic_control_left_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) { if (currentState != CMD_ROTATEX) {
sendMessageAsync(CMD_ROTATEX) sendMessageAsync(CMD_ROTATEX)
currentState = CMD_ROTATEX currentState = CMD_ROTATEX
@ -311,13 +342,13 @@ class ControlActivity : AppCompatActivity() {
} }
} }
} else { } else {
if(touchY< leftHeight/3){ if (touchY < leftHeight / 3) {
if (currentState != CMD_CLIMBFORWARD) { if (currentState != CMD_CLIMBFORWARD) {
sendMessageAsync(CMD_CLIMBFORWARD) sendMessageAsync(CMD_CLIMBFORWARD)
currentState = CMD_CLIMBFORWARD currentState = CMD_CLIMBFORWARD
leftControlImage!!.setImageResource(R.drawable.ic_control_left_climb_forward) 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) { if (currentState != CMD_TWIST) {
sendMessageAsync(CMD_TWIST) sendMessageAsync(CMD_TWIST)
currentState = CMD_TWIST currentState = CMD_TWIST
@ -332,6 +363,20 @@ class ControlActivity : AppCompatActivity() {
} }
} }
rightControlImage!!.setImageResource(R.drawable.ic_control_circle) 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 return true
} }
} }
@ -458,7 +503,8 @@ class ControlActivity : AppCompatActivity() {
"Unable to connect to the Hexapod." "Unable to connect to the Hexapod."
) )
alert.setOnCancelListener { finish() } alert.setOnCancelListener { finish() }
alert.setButton(AlertDialog.BUTTON_POSITIVE, alert.setButton(
AlertDialog.BUTTON_POSITIVE,
"OK" "OK"
) { _, _ -> finish() } ) { _, _ -> finish() }
} }

Loading…
Cancel
Save