package io.gitlab.jfronny.hc05ac import android.os.Bundle import android.view.MotionEvent import android.widget.TextView import androidx.appcompat.widget.LinearLayoutCompat import io.gitlab.jfronny.hc05ac.util.Action import io.gitlab.jfronny.hc05ac.util.BaseActivity import io.gitlab.jfronny.hc05ac.util.clamp import io.gitlab.jfronny.hc05ac.util.jAction /** * Activity that divides the screen into two halves, each serving as an input controller. * Implementations define a send function which is called for every update. */ abstract class InputActivity : BaseActivity() { private var root: LinearLayoutCompat? = null private var leftView: TextView? = null private var rightView: TextView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) root = findViewById(R.id.root) leftView = findViewById(R.id.left) rightView = findViewById(R.id.right) } override fun onTouchEvent(event: MotionEvent?): Boolean { if (event != null) { if (event.x > root!!.width / 2) right(event.y, root!!.height.toFloat(), event.jAction) else left(event.y, root!!.height.toFloat(), event.jAction) } return false } private var left: Byte = 0 private var right: Byte = 0 private fun left(y: Float, height: Float, action: Action): Boolean { left = when (action) { Action.DOWN, Action.MOVE -> (127 - (y * 256 / height).clamp(0f, 256f)).toInt().toByte() Action.UP -> 0 Action.OTHER -> return false } update() return false } private fun right(y: Float, height: Float, action: Action): Boolean { right = when (action) { Action.DOWN, Action.MOVE -> (127 - (y * 256 / height).clamp(0f, 256f)).toInt().toByte() Action.UP -> 0 Action.OTHER -> return false } update() return false } private fun update() { leftView!!.text = left.toString() rightView!!.text = right.toString() send(left, right) } protected abstract fun send(left: Byte, right: Byte) }