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)
@ -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) {
@ -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