Skip to content

Commit

Permalink
Merge branch 'input'
Browse files Browse the repository at this point in the history
  • Loading branch information
qimiko committed Feb 12, 2024
2 parents 420bca5 + 642fdc3 commit 9c7d87f
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
13 changes: 12 additions & 1 deletion app/src/main/java/com/geode/launcher/GeometryDashActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import java.io.FileInputStream
import java.io.FileOutputStream


class GeometryDashActivity : AppCompatActivity(), Cocos2dxHelper.Cocos2dxHelperListener {
class GeometryDashActivity : AppCompatActivity(), Cocos2dxHelper.Cocos2dxHelperListener, GeodeUtils.CapabilityListener {
private var mGLSurfaceView: Cocos2dxGLSurfaceView? = null
private val sTag = GeometryDashActivity::class.simpleName
private var mIsRunning = false
Expand Down Expand Up @@ -107,7 +107,9 @@ class GeometryDashActivity : AppCompatActivity(), Cocos2dxHelper.Cocos2dxHelperL
setupRedirection(gdPackageInfo)

Cocos2dxHelper.init(this, this)

GeodeUtils.setContext(this)
GeodeUtils.setCapabilityListener(this)

tryLoadLibrary(gdPackageInfo, Constants.FMOD_LIB_NAME)
tryLoadLibrary(gdPackageInfo, Constants.COCOS_LIB_NAME)
Expand Down Expand Up @@ -448,4 +450,13 @@ class GeometryDashActivity : AppCompatActivity(), Cocos2dxHelper.Cocos2dxHelperL
}
}
}

override fun onCapabilityAdded(capability: String): Boolean {
if (capability == GeodeUtils.CAPABILITY_EXTENDED_INPUT) {
mGLSurfaceView?.useKeyboardEvents = true
return true
}

return false
}
}
26 changes: 26 additions & 0 deletions app/src/main/java/com/geode/launcher/utils/GeodeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,30 @@ object GeodeUtils {

return null
}

interface CapabilityListener {
fun onCapabilityAdded(capability: String): Boolean
}

const val CAPABILITY_EXTENDED_INPUT = "extended_input"

private var capabilityListener: WeakReference<CapabilityListener?> = WeakReference(null)

fun setCapabilityListener(listener: CapabilityListener) {
capabilityListener = WeakReference(listener)
}

@JvmStatic
fun reportPlatformCapability(capability: String?): Boolean {
if (capability.isNullOrEmpty()) {
return false
}

return capabilityListener.get()?.onCapabilityAdded(capability) ?: false
}

external fun nativeKeyUp(keyCode: Int, modifiers: Int)
external fun nativeKeyDown(keyCode: Int, modifiers: Int, isRepeating: Boolean)
external fun nativeActionScroll(scrollX: Float, scrollY: Float)
external fun resizeSurface(width: Int, height: Int)
}
73 changes: 72 additions & 1 deletion app/src/main/java/org/cocos2dx/lib/Cocos2dxGLSurfaceView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.view.KeyEvent
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import com.customRobTop.BaseRobTopActivity
import com.geode.launcher.utils.GeodeUtils

private const val HANDLER_OPEN_IME_KEYBOARD = 2
private const val HANDLER_CLOSE_IME_KEYBOARD = 3
Expand Down Expand Up @@ -37,6 +38,11 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
}

private lateinit var cocos2dxRenderer: Cocos2dxRenderer
var useKeyboardEvents = false
set(value) {
field = value
cocos2dxRenderer.sendResizeEvents = value
}

var cocos2dxEditText: Cocos2dxEditText? = null
set(value) {
Expand Down Expand Up @@ -207,7 +213,7 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
}
}

override fun onKeyDown(keyCode: Int, keyEvent: KeyEvent): Boolean {
private fun legacyKeyDown(keyCode: Int, keyEvent: KeyEvent): Boolean {
return when (keyCode) {
KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_MENU -> {
if (keyEvent.repeatCount != 0 || BaseRobTopActivity.blockBackButton) {
Expand All @@ -220,6 +226,71 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
}
}

override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
if (!useKeyboardEvents) {
return legacyKeyDown(keyCode, event)
}

return when (keyCode) {
// ignore system keys
KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_MUTE -> {
super.onKeyDown(keyCode, event)
}
else -> {
if (BaseRobTopActivity.blockBackButton) {
return true
}

queueEvent {
GeodeUtils.nativeKeyDown(keyCode, event.modifiers, event.repeatCount != 0)
}
true
}
}
}

override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
if (!useKeyboardEvents) {
return super.onKeyUp(keyCode, event)
}

return when (keyCode) {
KeyEvent.KEYCODE_VOLUME_DOWN, KeyEvent.KEYCODE_VOLUME_UP, KeyEvent.KEYCODE_MUTE -> {
super.onKeyUp(keyCode, event)
}
else -> {
if (event.repeatCount != 0 || BaseRobTopActivity.blockBackButton) {
return true
}

queueEvent {
GeodeUtils.nativeKeyUp(keyCode, event.modifiers)
}

true
}
}
}

override fun onGenericMotionEvent(event: MotionEvent?): Boolean {
if (!useKeyboardEvents) {
return super.onGenericMotionEvent(event)
}

if (event?.action == MotionEvent.ACTION_SCROLL) {
val scrollX = event.getAxisValue(MotionEvent.AXIS_HSCROLL)
val scrollY = event.getAxisValue(MotionEvent.AXIS_VSCROLL)

queueEvent {
GeodeUtils.nativeActionScroll(scrollX, scrollY)
}

return true
}

return super.onGenericMotionEvent(event)
}

fun setCocos2dxRenderer(renderer: Cocos2dxRenderer) {
this.cocos2dxRenderer = renderer
setRenderer(this.cocos2dxRenderer)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/org/cocos2dx/lib/Cocos2dxRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Cocos2dxRenderer : GLSurfaceView.Renderer {
private var lastTickInNanoSeconds: Long = 0
private var screenWidth = 0
private var screenHeight = 0
var sendResizeEvents = false

fun setScreenWidthAndHeight(surfaceWidth: Int, surfaceHeight: Int) {
screenWidth = surfaceWidth
Expand Down

0 comments on commit 9c7d87f

Please sign in to comment.