Skip to content

Commit

Permalink
Handle multiple clicks on the touch screen
Browse files Browse the repository at this point in the history
This allows games that rely on multi-touch to work
  • Loading branch information
rafaelvcaetano committed Mar 10, 2024
1 parent 3374059 commit 908c664
Showing 1 changed file with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.magnum.melonds.ui.emulator.input

import android.annotation.SuppressLint
import android.view.MotionEvent
import android.view.MotionEvent.PointerCoords
import android.view.View
import me.magnum.melonds.MelonEmulator.onScreenRelease
import me.magnum.melonds.domain.model.Input
Expand All @@ -9,18 +11,15 @@ import me.magnum.melonds.domain.model.Point
class TouchscreenInputHandler(inputListener: IInputListener) : BaseInputHandler(inputListener) {
private val touchPoint: Point = Point()

@SuppressLint("ClickableViewAccessibility")
override fun onTouch(v: View, event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
inputListener.onKeyPress(Input.TOUCHSCREEN)
val x = event.x
val y = event.y
inputListener.onTouch(normalizeTouchCoordinates(x, y, v.width, v.height))
inputListener.onTouch(normalizeTouchCoordinates(event, v.width, v.height))
}
MotionEvent.ACTION_MOVE -> {
val x = event.x
val y = event.y
inputListener.onTouch(normalizeTouchCoordinates(x, y, v.width, v.height))
inputListener.onTouch(normalizeTouchCoordinates(event, v.width, v.height))
}
MotionEvent.ACTION_UP -> {
inputListener.onKeyReleased(Input.TOUCHSCREEN)
Expand All @@ -30,9 +29,24 @@ class TouchscreenInputHandler(inputListener: IInputListener) : BaseInputHandler(
return true
}

private fun normalizeTouchCoordinates(x: Float, y: Float, viewWidth: Int, viewHeight: Int): Point {
touchPoint.x = (x / viewWidth * 256).toInt().coerceIn(0, 255)
touchPoint.y = (y / viewHeight * 192).toInt().coerceIn(0, 191)
private fun normalizeTouchCoordinates(event: MotionEvent, viewWidth: Int, viewHeight: Int): Point {
var averageTouchX = 0f
var averageTouchY = 0f
val pointerCoordinates = PointerCoords()

// Average out touch positions. Even though the DS has a resistive touch screen, some games rely on the nuances
// of this technology for some mechanics. Averaging out the coordinates of the touch position allows us to
// simulate those nuances to some degree
for (i in 0 until event.pointerCount) {
event.getPointerCoords(i, pointerCoordinates)
averageTouchX += pointerCoordinates.x
averageTouchY += pointerCoordinates.y
}
averageTouchX /= event.pointerCount
averageTouchY /= event.pointerCount

touchPoint.x = (averageTouchX / viewWidth * 256).toInt().coerceIn(0, 255)
touchPoint.y = (averageTouchY / viewHeight * 192).toInt().coerceIn(0, 191)
return touchPoint
}
}

0 comments on commit 908c664

Please sign in to comment.