forked from geode-sdk/android-launcher
add input timestamp api
This commit is contained in:
3 files changed
+57
-30
No files matched your search
@@ -515,12 +515,17 @@ class GeometryDashActivity : AppCompatActivity(), Cocos2dxHelper.Cocos2dxHelperL
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onCapabilityAdded(capability: String): Boolean {
|
override fun onCapabilityAdded(capability: String): Boolean {
|
||||||
if (capability == GeodeUtils.CAPABILITY_EXTENDED_INPUT) {
|
return when (capability) {
|
||||||
|
GeodeUtils.CAPABILITY_EXTENDED_INPUT -> {
|
||||||
mGLSurfaceView?.useKeyboardEvents = true
|
mGLSurfaceView?.useKeyboardEvents = true
|
||||||
return true
|
true
|
||||||
|
}
|
||||||
|
GeodeUtils.CAPABILITY_TIMESTAMP_INPUT -> {
|
||||||
|
mGLSurfaceView?.sendTimestampEvents = true
|
||||||
|
true
|
||||||
|
}
|
||||||
|
else -> false
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -450,6 +450,7 @@ object GeodeUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const val CAPABILITY_EXTENDED_INPUT = "extended_input"
|
const val CAPABILITY_EXTENDED_INPUT = "extended_input"
|
||||||
|
const val CAPABILITY_TIMESTAMP_INPUT = "timestamp_inputs"
|
||||||
|
|
||||||
private var capabilityListener: WeakReference<CapabilityListener?> = WeakReference(null)
|
private var capabilityListener: WeakReference<CapabilityListener?> = WeakReference(null)
|
||||||
|
|
||||||
@@ -470,4 +471,7 @@ object GeodeUtils {
|
|||||||
external fun nativeKeyDown(keyCode: Int, modifiers: Int, isRepeating: Boolean)
|
external fun nativeKeyDown(keyCode: Int, modifiers: Int, isRepeating: Boolean)
|
||||||
external fun nativeActionScroll(scrollX: Float, scrollY: Float)
|
external fun nativeActionScroll(scrollX: Float, scrollY: Float)
|
||||||
external fun resizeSurface(width: Int, height: Int)
|
external fun resizeSurface(width: Int, height: Int)
|
||||||
|
|
||||||
|
// represents the timestamp of the next input callback, in nanoseconds (most events don't send it, but it's there)
|
||||||
|
external fun setNextInputTimestamp(timestamp: Long)
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package org.cocos2dx.lib
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.opengl.GLSurfaceView
|
import android.opengl.GLSurfaceView
|
||||||
|
import android.os.Build
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
import android.os.Message
|
import android.os.Message
|
||||||
@@ -14,6 +15,7 @@ import com.geode.launcher.utils.GeodeUtils
|
|||||||
|
|
||||||
private const val HANDLER_OPEN_IME_KEYBOARD = 2
|
private const val HANDLER_OPEN_IME_KEYBOARD = 2
|
||||||
private const val HANDLER_CLOSE_IME_KEYBOARD = 3
|
private const val HANDLER_CLOSE_IME_KEYBOARD = 3
|
||||||
|
private const val MS_TO_NS = 1_000_000
|
||||||
|
|
||||||
class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
||||||
companion object {
|
companion object {
|
||||||
@@ -44,6 +46,8 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
|||||||
cocos2dxRenderer.sendResizeEvents = value
|
cocos2dxRenderer.sendResizeEvents = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sendTimestampEvents = false
|
||||||
|
|
||||||
var cocos2dxEditText: Cocos2dxEditText? = null
|
var cocos2dxEditText: Cocos2dxEditText? = null
|
||||||
set(value) {
|
set(value) {
|
||||||
field = value
|
field = value
|
||||||
@@ -124,6 +128,9 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
|||||||
val ids = IntArray(pointerNumber)
|
val ids = IntArray(pointerNumber)
|
||||||
val xs = FloatArray(pointerNumber)
|
val xs = FloatArray(pointerNumber)
|
||||||
val ys = FloatArray(pointerNumber)
|
val ys = FloatArray(pointerNumber)
|
||||||
|
val timestamp = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
|
||||||
|
motionEvent.eventTimeNanos else motionEvent.eventTime * MS_TO_NS
|
||||||
|
|
||||||
for (i in 0 until pointerNumber) {
|
for (i in 0 until pointerNumber) {
|
||||||
ids[i] = motionEvent.getPointerId(i)
|
ids[i] = motionEvent.getPointerId(i)
|
||||||
xs[i] = motionEvent.getX(i)
|
xs[i] = motionEvent.getX(i)
|
||||||
@@ -135,11 +142,10 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
|||||||
val xDown = xs[0]
|
val xDown = xs[0]
|
||||||
val f = ys[0]
|
val f = ys[0]
|
||||||
queueEvent {
|
queueEvent {
|
||||||
cocos2dxRenderer.handleActionDown(
|
if (sendTimestampEvents)
|
||||||
idDown,
|
GeodeUtils.setNextInputTimestamp(timestamp)
|
||||||
xDown,
|
|
||||||
f
|
cocos2dxRenderer.handleActionDown(idDown, xDown, f)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
@@ -147,26 +153,29 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
|||||||
val idUp = motionEvent.getPointerId(0)
|
val idUp = motionEvent.getPointerId(0)
|
||||||
val f2 = xs[0]
|
val f2 = xs[0]
|
||||||
val f3 = ys[0]
|
val f3 = ys[0]
|
||||||
queueEvent { cocos2dxRenderer.handleActionUp(idUp, f2, f3) }
|
queueEvent {
|
||||||
|
if (sendTimestampEvents)
|
||||||
|
GeodeUtils.setNextInputTimestamp(timestamp)
|
||||||
|
|
||||||
|
cocos2dxRenderer.handleActionUp(idUp, f2, f3)
|
||||||
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
MotionEvent.ACTION_MOVE -> {
|
MotionEvent.ACTION_MOVE -> {
|
||||||
queueEvent {
|
queueEvent {
|
||||||
cocos2dxRenderer.handleActionMove(
|
if (sendTimestampEvents)
|
||||||
ids,
|
GeodeUtils.setNextInputTimestamp(timestamp)
|
||||||
xs,
|
|
||||||
ys
|
cocos2dxRenderer.handleActionMove(ids, xs, ys)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
MotionEvent.ACTION_CANCEL -> {
|
MotionEvent.ACTION_CANCEL -> {
|
||||||
queueEvent {
|
queueEvent {
|
||||||
cocos2dxRenderer.handleActionCancel(
|
if (sendTimestampEvents)
|
||||||
ids,
|
GeodeUtils.setNextInputTimestamp(timestamp)
|
||||||
xs,
|
|
||||||
ys
|
cocos2dxRenderer.handleActionCancel(ids, xs, ys)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
@@ -176,11 +185,10 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
|||||||
val xPointerDown = motionEvent.getX(indexPointerDown)
|
val xPointerDown = motionEvent.getX(indexPointerDown)
|
||||||
val y = motionEvent.getY(indexPointerDown)
|
val y = motionEvent.getY(indexPointerDown)
|
||||||
queueEvent {
|
queueEvent {
|
||||||
cocos2dxRenderer.handleActionDown(
|
if (sendTimestampEvents)
|
||||||
idPointerDown,
|
GeodeUtils.setNextInputTimestamp(timestamp)
|
||||||
xPointerDown,
|
|
||||||
y
|
cocos2dxRenderer.handleActionDown(idPointerDown, xPointerDown, y)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
@@ -190,11 +198,10 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
|||||||
val xPointerUp = motionEvent.getX(indexPointUp)
|
val xPointerUp = motionEvent.getX(indexPointUp)
|
||||||
val y2 = motionEvent.getY(indexPointUp)
|
val y2 = motionEvent.getY(indexPointUp)
|
||||||
queueEvent {
|
queueEvent {
|
||||||
cocos2dxRenderer.handleActionUp(
|
if (sendTimestampEvents)
|
||||||
idPointerUp,
|
GeodeUtils.setNextInputTimestamp(timestamp)
|
||||||
xPointerUp,
|
|
||||||
y2
|
cocos2dxRenderer.handleActionUp(idPointerUp, xPointerUp, y2)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
@@ -242,6 +249,9 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
queueEvent {
|
queueEvent {
|
||||||
|
if (sendTimestampEvents)
|
||||||
|
GeodeUtils.setNextInputTimestamp(event.eventTime * MS_TO_NS)
|
||||||
|
|
||||||
GeodeUtils.nativeKeyDown(keyCode, event.modifiers, event.repeatCount != 0)
|
GeodeUtils.nativeKeyDown(keyCode, event.modifiers, event.repeatCount != 0)
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
@@ -264,6 +274,9 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
queueEvent {
|
queueEvent {
|
||||||
|
if (sendTimestampEvents)
|
||||||
|
GeodeUtils.setNextInputTimestamp(event.eventTime * MS_TO_NS)
|
||||||
|
|
||||||
GeodeUtils.nativeKeyUp(keyCode, event.modifiers)
|
GeodeUtils.nativeKeyUp(keyCode, event.modifiers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,8 +293,13 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
|||||||
if (event?.action == MotionEvent.ACTION_SCROLL) {
|
if (event?.action == MotionEvent.ACTION_SCROLL) {
|
||||||
val scrollX = event.getAxisValue(MotionEvent.AXIS_HSCROLL)
|
val scrollX = event.getAxisValue(MotionEvent.AXIS_HSCROLL)
|
||||||
val scrollY = event.getAxisValue(MotionEvent.AXIS_VSCROLL)
|
val scrollY = event.getAxisValue(MotionEvent.AXIS_VSCROLL)
|
||||||
|
val timestamp = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
|
||||||
|
event.eventTimeNanos else event.eventTime * MS_TO_NS
|
||||||
|
|
||||||
queueEvent {
|
queueEvent {
|
||||||
|
if (sendTimestampEvents)
|
||||||
|
GeodeUtils.setNextInputTimestamp(timestamp)
|
||||||
|
|
||||||
GeodeUtils.nativeActionScroll(scrollX, scrollY)
|
GeodeUtils.nativeActionScroll(scrollX, scrollY)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user