add edittext methods

This commit is contained in:
qimiko committed 2022-05-27 02:16:48 -07:00
1 parent 9c66ee3e98
commit 1115bc2bd4
8 files changed
+257 -40

No files matched your search

+1 -1
View File
@@ -32,7 +32,7 @@
android:exported="true"
android:hardwareAccelerated="true"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:theme="@style/Theme.NoActionBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
<meta-data
android:name="android.app.lib_name"
@@ -4,6 +4,8 @@ import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.os.Environment
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
@@ -22,12 +24,17 @@ import com.customRobTop.JniToCpp
import com.robtopx.geometryjump.GJConstants
import com.robtopx.geometryjump.LaunchUtils
import dev.xyze.geodelauncher.ui.theme.GeodeLauncherTheme
import org.cocos2dx.lib.Cocos2dxEditText
import org.cocos2dx.lib.Cocos2dxGLSurfaceView
import org.cocos2dx.lib.Cocos2dxHelper
import org.cocos2dx.lib.Cocos2dxRenderer
import org.fmod.FMOD
class GeometryDashActivity : ComponentActivity(), Cocos2dxHelper.Cocos2dxHelperListener {
private var mGLSurfaceView: Cocos2dxGLSurfaceView? = null
override fun onCreate(savedInstanceState: Bundle?) {
setupUIState()
@@ -66,11 +73,26 @@ class GeometryDashActivity : ComponentActivity(), Cocos2dxHelper.Cocos2dxHelperL
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = {
Cocos2dxGLSurfaceView(this).apply {
setEGLConfigChooser(5, 6, 5, 0, 16, 8)
initView()
setCocos2dxRenderer(Cocos2dxRenderer())
}
val frameLayoutParams = ViewGroup.LayoutParams(-1, -1)
val frameLayout = FrameLayout(this)
frameLayout.layoutParams = frameLayoutParams
val editTextLayoutParams = ViewGroup.LayoutParams(-1, -2)
val editText = Cocos2dxEditText(this)
editText.layoutParams = editTextLayoutParams
frameLayout.addView(editText)
val glSurfaceView = Cocos2dxGLSurfaceView(this)
this.mGLSurfaceView = glSurfaceView
frameLayout.addView(this.mGLSurfaceView)
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8)
glSurfaceView.initView()
glSurfaceView.setCocos2dxRenderer(Cocos2dxRenderer())
editText.inputType = 145
glSurfaceView.setCocos2dxEditText(editText)
frameLayout
}
)
SnackbarHost(
@@ -0,0 +1,34 @@
package org.cocos2dx.lib
import android.content.Context
import android.util.AttributeSet
import android.view.KeyEvent
import androidx.appcompat.widget.AppCompatEditText
class Cocos2dxEditText : AppCompatEditText {
private var mCocos2dxGLSurfaceView: Cocos2dxGLSurfaceView? = null
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet) : super(
context, attrs
) {
}
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
context, attrs, defStyle
) {
}
fun setCocos2dxGLSurfaceView(pCocos2dxGLSurfaceView: Cocos2dxGLSurfaceView?) {
mCocos2dxGLSurfaceView = pCocos2dxGLSurfaceView
}
override fun onKeyDown(pKeyCode: Int, pKeyEvent: KeyEvent): Boolean {
super.onKeyDown(pKeyCode, pKeyEvent)
if (pKeyCode != 4) {
return true
}
mCocos2dxGLSurfaceView!!.requestFocus()
return true
}
}
@@ -6,18 +6,39 @@ import android.opengl.GLSurfaceView
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import com.customRobTop.BaseRobTopActivity
private const val HANDLER_OPEN_IME_KEYBOARD = 2
private const val HANDLER_CLOSE_IME_KEYBOARD = 3
class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
companion object {
lateinit var mCocos2dxGLSurfaceView: Cocos2dxGLSurfaceView
private var sHandler: Handler? = null
private lateinit var sHandler: Handler
private lateinit var sCocos2dxTextInputWrapper: Cocos2dxTextInputWrapper
@JvmStatic
fun openIMEKeyboard() {
val msg = Message()
msg.what = HANDLER_OPEN_IME_KEYBOARD
msg.obj = mCocos2dxGLSurfaceView.getContentText()
sHandler.sendMessage(msg)
}
@JvmStatic
fun closeIMEKeyboard() {
val msg = Message()
msg.what = HANDLER_CLOSE_IME_KEYBOARD
sHandler.sendMessage(msg)
}
}
private lateinit var mCocos2dxRenderer: Cocos2dxRenderer
var mCocos2dxEditText: Cocos2dxEditText? = null
fun initView() {
setEGLContextClientVersion(2)
@@ -26,26 +47,27 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
preserveEGLContextOnPause = true
// }
mCocos2dxGLSurfaceView = this
// sCocos2dxTextInputWraper = Cocos2dxTextInputWraper(this)
sCocos2dxTextInputWrapper = Cocos2dxTextInputWrapper(this)
sHandler = @SuppressLint("HandlerLeak")
object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
/* when (msg.what) {
2 -> {
if (this@Cocos2dxGLSurfaceView.mCocos2dxEditText != null && this@Cocos2dxGLSurfaceView.mCocos2dxEditText.requestFocus()) {
this@Cocos2dxGLSurfaceView.mCocos2dxEditText.removeTextChangedListener(
Cocos2dxGLSurfaceView.sCocos2dxTextInputWraper
)
this@Cocos2dxGLSurfaceView.mCocos2dxEditText.setText("")
val text = msg.obj as String
this@Cocos2dxGLSurfaceView.mCocos2dxEditText.append(text)
Cocos2dxGLSurfaceView.sCocos2dxTextInputWraper.setOriginText(text)
this@Cocos2dxGLSurfaceView.mCocos2dxEditText.addTextChangedListener(
Cocos2dxGLSurfaceView.sCocos2dxTextInputWraper
)
(Cocos2dxGLSurfaceView.mCocos2dxGLSurfaceView.getContext()
.getSystemService("input_method") as InputMethodManager).showSoftInput(
this@Cocos2dxGLSurfaceView.mCocos2dxEditText,
when (msg.what) {
HANDLER_OPEN_IME_KEYBOARD -> {
if (mCocos2dxEditText?.requestFocus() == true) {
mCocos2dxEditText?.apply {
removeTextChangedListener(
sCocos2dxTextInputWrapper
)
setText("")
val text = msg.obj as String
append(text)
sCocos2dxTextInputWrapper.setOriginText(text)
addTextChangedListener(
sCocos2dxTextInputWrapper
)
}
(context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).showSoftInput(
mCocos2dxEditText,
0
)
Log.d("GLSurfaceView", "showSoftInput")
@@ -53,31 +75,28 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
}
return
}
3 -> {
if (this@Cocos2dxGLSurfaceView.mCocos2dxEditText != null) {
this@Cocos2dxGLSurfaceView.mCocos2dxEditText.removeTextChangedListener(
Cocos2dxGLSurfaceView.sCocos2dxTextInputWraper
HANDLER_CLOSE_IME_KEYBOARD -> {
mCocos2dxEditText?.apply {
removeTextChangedListener(
sCocos2dxTextInputWrapper
)
(Cocos2dxGLSurfaceView.mCocos2dxGLSurfaceView.getContext()
.getSystemService("input_method") as InputMethodManager).hideSoftInputFromWindow(
this@Cocos2dxGLSurfaceView.mCocos2dxEditText.getWindowToken(),
(context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(
windowToken,
0
)
this@Cocos2dxGLSurfaceView.requestFocus()
Log.d("GLSurfaceView", "HideSoftInput")
this@Cocos2dxGLSurfaceView.mCocos2dxRenderer.handleTextClosed()
return
}
requestFocus()
Log.d("GLSurfaceView", "HideSoftInput")
mCocos2dxRenderer.handleTextClosed()
return
}
else -> return
}
*/
}
}
}
override fun onTouchEvent(pMotionEvent: MotionEvent): Boolean {
val pointerNumber = pMotionEvent.pointerCount
val ids = IntArray(pointerNumber)
@@ -189,4 +208,30 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
this.mCocos2dxRenderer = renderer
setRenderer(this.mCocos2dxRenderer)
}
private fun getContentText(): String {
return mCocos2dxRenderer.getContentText()
}
fun getCocos2dxEditText(): Cocos2dxEditText? {
return mCocos2dxEditText
}
fun setCocos2dxEditText(pCocos2dxEditText: Cocos2dxEditText) {
mCocos2dxEditText = pCocos2dxEditText
if (sCocos2dxTextInputWrapper != null) {
mCocos2dxEditText?.setOnEditorActionListener(sCocos2dxTextInputWrapper)
mCocos2dxEditText?.setCocos2dxGLSurfaceView(this)
requestFocus()
}
}
fun insertText(pText: String) {
queueEvent { mCocos2dxRenderer.handleInsertText(pText) }
}
fun deleteBackward() {
queueEvent { mCocos2dxRenderer.handleDeleteBackward() }
}
}
@@ -13,11 +13,29 @@ class Cocos2dxRenderer : GLSurfaceView.Renderer {
}
@JvmStatic
private external fun nativeRender()
private external fun nativeDeleteBackward()
@JvmStatic
private external fun nativeGetContentText(): String
@JvmStatic
private external fun nativeInsertText(pText: String)
@JvmStatic
private external fun nativeKeyDown(pKeyCode: Int): Boolean
@JvmStatic
private external fun nativeOnPause()
@JvmStatic
private external fun nativeOnResume()
@JvmStatic
private external fun nativeRender()
@JvmStatic
private external fun nativeTextClosed()
@JvmStatic
private external fun nativeTouchesBegin(pID: Int, pX: Float, pY: Float)
@@ -75,4 +93,28 @@ class Cocos2dxRenderer : GLSurfaceView.Renderer {
fun handleKeyDown(pKeyCode: Int) {
nativeKeyDown(pKeyCode)
}
fun handleOnPause() {
nativeOnPause()
}
fun handleOnResume() {
nativeOnResume()
}
fun handleInsertText(pText: String) {
nativeInsertText(pText)
}
fun handleDeleteBackward() {
nativeDeleteBackward()
}
fun handleTextClosed() {
nativeTextClosed()
}
fun getContentText(): String {
return nativeGetContentText()
}
}
@@ -0,0 +1,70 @@
package org.cocos2dx.lib
import android.content.Context
import android.text.TextWatcher
import android.widget.TextView.OnEditorActionListener
import android.text.Editable
import android.view.KeyEvent
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
class Cocos2dxTextInputWrapper(private val mCocos2dxGLSurfaceView: Cocos2dxGLSurfaceView) : TextWatcher, OnEditorActionListener {
private var mOriginText: String? = null
private var mText: String? = null
private val isFullScreenEdit: Boolean
get() = (mCocos2dxGLSurfaceView.mCocos2dxEditText?.context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).isFullscreenMode
fun setOriginText(pOriginText: String?) {
mOriginText = pOriginText
}
override fun afterTextChanged(s: Editable) {
if (!isFullScreenEdit) {
var nModified = s.length - mText!!.length
if (nModified > 0) {
mCocos2dxGLSurfaceView.insertText(
s.subSequence(mText!!.length, s.length).toString()
)
} else {
while (nModified < 0) {
mCocos2dxGLSurfaceView.deleteBackward()
nModified++
}
}
mText = s.toString()
}
}
override fun beforeTextChanged(
pCharSequence: CharSequence,
start: Int,
count: Int,
after: Int
) {
mText = pCharSequence.toString()
}
override fun onTextChanged(pCharSequence: CharSequence, start: Int, before: Int, count: Int) {}
override fun onEditorAction(pTextView: TextView, pActionID: Int, pKeyEvent: KeyEvent?): Boolean {
if (mCocos2dxGLSurfaceView.getCocos2dxEditText() == pTextView && isFullScreenEdit) {
for (i in mOriginText!!.length downTo 1) {
mCocos2dxGLSurfaceView.deleteBackward()
}
var text = pTextView.text.toString()
if (text.compareTo("") == 0) {
text = "\n"
}
if (10 != text[text.length - 1].code) {
text += 10.toChar()
}
mCocos2dxGLSurfaceView.insertText(text)
}
if (pActionID != 6) {
return false
}
mCocos2dxGLSurfaceView.requestFocus()
Cocos2dxGLSurfaceView.closeIMEKeyboard()
return false
}
}
+6 -1
View File
@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.NoActionBar.Fullscreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="Theme.GeodeLauncher" parent="android:Theme.Material.Light.NoActionBar" />
</resources>