Advertisement
yclee126

Doubtful multitouch onTouchEvent

Mar 1st, 2021 (edited)
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.78 KB | None | 0 0
  1.     override fun onTouchEvent(event: MotionEvent): Boolean {
  2.         // abuse-proof multitouch code - minimize the use of event type
  3.  
  4.         val lastUp: Boolean = when (event.action and MotionEvent.ACTION_MASK) {
  5.             MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> true
  6.             else -> false
  7.         }
  8.         val pointerCount = event.pointerCount
  9.  
  10.         // collect all points
  11.         var points: MutableList<Array<Float>> = mutableListOf()
  12.         for (i in 0 until pointerCount) {
  13.             val id = event.getPointerId(i) // stable value
  14.             val index = event.findPointerIndex(id) // temporary value
  15.             val x = event.getX(index)
  16.             val y = event.getY(index)
  17.             points.add(arrayOf(x, y))
  18.         }
  19.  
  20.         // find nearest path
  21.         val getDist = { a: MyPath, b: Array<Float> -> (a.lastX - b[0]).pow(2) + (a.lastY - b[1]).pow(2) }
  22.         var nearestDist: MutableMap<Int, Float> = mutableMapOf()
  23.         var nearestKey: MutableMap<Int, Int> = mutableMapOf()
  24.         //TODO: convert above two to MutableList
  25.  
  26.         for (i in 0 until pointerCount) {
  27.             val smallest = mPaths.minByOrNull { getDist(it.value, points[i]) } ?: break
  28.             nearestDist[i] = getDist(smallest.value, points[i])
  29.             nearestKey[i] = smallest.key
  30.         }
  31.         val sortedPointsIndex: List<Int> = (0 until pointerCount).toList().sortedBy { nearestDist[it] }
  32.  
  33.         // iterate through points and update paths
  34.         mPathsStatus.forEach { mPathsStatus[it.key] = 0 } // 0: new point not mapped, 1: new point mapped
  35.         for (i in sortedPointsIndex) {
  36.  
  37.             val x = points[i][0]
  38.             val y = points[i][1]
  39.  
  40.             if (mPathsStatus[nearestKey[i]] != 0) { // new point
  41.                 mTouchPoints ++
  42.                 // assign new unused index
  43.                 var newIndex = mTouchPoints-1
  44.                 for (i in 0 until mTouchPoints-1) {
  45.                     if (mPaths[i] == null) {
  46.                         newIndex = i
  47.                         break
  48.                     }
  49.                 }
  50.                 // update path
  51.                 mPaths[newIndex] = MyPath()
  52.                 mPaths[newIndex]!!.moveTo(x, y)
  53.                 mPathsStatus[newIndex] = 1
  54.  
  55.             } else if (!lastUp) { // nearest point
  56.                 mPaths[nearestKey[i]]!!.lineTo(x, y)
  57.                 mPathsStatus[nearestKey[i]!!] = 1
  58.             }
  59.         }
  60.  
  61.         // migrate inactive paths to prevPaths
  62.         val notUpdated = mPathsStatus.filter { it.value == 0 }
  63.         notUpdated.forEach {
  64.             mTouchPoints --
  65.             mPrevPaths.add(mPaths[it.key]!!)
  66.             mPaths.remove(it.key)
  67.             mPathsStatus.remove(it.key)
  68.         }
  69.  
  70.         invalidate() // force draw new
  71.         return true
  72.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement