mirror of
https://github.com/geode-sdk/android-launcher.git
synced 2026-10-21 16:32:39 +00:00
initial files
This commit is contained in:
63 files changed
+2268
No files matched your search
@@ -0,0 +1,116 @@
|
||||
package org.cocos2dx.lib
|
||||
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.ViewGroup
|
||||
import android.widget.FrameLayout
|
||||
import com.customRobTop.BaseRobTopActivity
|
||||
|
||||
|
||||
open class Cocos2dxActivity : BaseRobTopActivity(), Cocos2dxHelper.Cocos2dxHelperListener {
|
||||
private val sTag = Cocos2dxActivity::class.java.simpleName
|
||||
private lateinit var mGLSurfaceView: Cocos2dxGLSurfaceView
|
||||
private var hasWindowFocus = false
|
||||
private var mIsOnPause = false
|
||||
private var mIsRunning = false
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
init()
|
||||
Cocos2dxHelper.init(this, this)
|
||||
}
|
||||
|
||||
private fun resumeGame() {
|
||||
this.mIsRunning = true
|
||||
mGLSurfaceView.onResume()
|
||||
}
|
||||
|
||||
private fun pauseGame() {
|
||||
this.mIsRunning = false
|
||||
mGLSurfaceView.onPause()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
this.mIsOnPause = false
|
||||
if (this.hasWindowFocus && !this.mIsRunning) {
|
||||
resumeGame()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
this.mIsOnPause = true
|
||||
if (this.mIsRunning) {
|
||||
pauseGame()
|
||||
}
|
||||
}
|
||||
|
||||
override fun showDialog(pTitle: String, pMessage: String) {
|
||||
/*
|
||||
val msg = Message()
|
||||
msg.what = 1
|
||||
msg.obj = DialogMessage(pTitle, pMessage)
|
||||
this.mHandler.sendMessage(msg)
|
||||
*/
|
||||
}
|
||||
|
||||
override fun showEditTextDialog(
|
||||
pTitle: String,
|
||||
pMessage: String,
|
||||
pInputMode: Int,
|
||||
pInputFlag: Int,
|
||||
pReturnType: Int,
|
||||
pMaxLength: Int
|
||||
) {
|
||||
/*
|
||||
val msg = Message()
|
||||
msg.what = 2
|
||||
msg.obj = EditBoxMessage(pTitle, pContent, pInputMode, pInputFlag, pReturnType, pMaxLength)
|
||||
this.mHandler.sendMessage(msg)
|
||||
*/
|
||||
}
|
||||
|
||||
override fun runOnGLThread(pRunnable: Runnable) {
|
||||
mGLSurfaceView.queueEvent(pRunnable)
|
||||
}
|
||||
|
||||
private fun init() {
|
||||
val frameLayoutParams = ViewGroup.LayoutParams(-1, -1)
|
||||
val frameLayout = FrameLayout(this)
|
||||
frameLayout.layoutParams = frameLayoutParams
|
||||
// val edittextLayoutParams = ViewGroup.LayoutParams(-1, -2)
|
||||
// val edittext = Cocos2dxEditText(this)
|
||||
// edittext.setLayoutParams(edittextLayoutParams)
|
||||
// frameLayout.addView(edittext)
|
||||
this.mGLSurfaceView = onCreateView()
|
||||
frameLayout.addView(this.mGLSurfaceView)
|
||||
if (isAndroidEmulator()) {
|
||||
this.mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0)
|
||||
}
|
||||
this.mGLSurfaceView.setCocos2dxRenderer(Cocos2dxRenderer())
|
||||
// edittext.setInputType(145)
|
||||
// this.mGLSurfaceView.setCocos2dxEditText(edittext)
|
||||
setContentView(frameLayout)
|
||||
}
|
||||
|
||||
|
||||
open fun onCreateView(): Cocos2dxGLSurfaceView {
|
||||
val glSurfaceView = Cocos2dxGLSurfaceView(this)
|
||||
glSurfaceView.initView()
|
||||
return glSurfaceView
|
||||
}
|
||||
|
||||
private fun isAndroidEmulator(): Boolean {
|
||||
Log.d(sTag, "model=" + Build.MODEL)
|
||||
val product = Build.PRODUCT
|
||||
Log.d(sTag, "product=$product")
|
||||
var isEmulator = false
|
||||
if (product != null) {
|
||||
isEmulator = product == "sdk" || product.contains("_sdk") || product.contains("sdk_")
|
||||
}
|
||||
Log.d(sTag, "isEmulator=$isEmulator")
|
||||
return isEmulator
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,395 @@
|
||||
package org.cocos2dx.lib
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.graphics.*
|
||||
import android.graphics.Paint.FontMetricsInt
|
||||
import android.text.TextPaint
|
||||
import android.text.TextUtils
|
||||
import android.util.Log
|
||||
import android.view.MotionEvent
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.util.*
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.ceil
|
||||
|
||||
@Suppress("unused")
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
object Cocos2dxBitmap {
|
||||
private lateinit var sContext: Context
|
||||
private external fun nativeInitBitmapDC(i: Int, i2: Int, bArr: ByteArray)
|
||||
fun setContext(pContext: Context) {
|
||||
sContext = pContext
|
||||
}
|
||||
|
||||
fun createTextBitmap(
|
||||
pString: String,
|
||||
pFontName: String,
|
||||
pFontSize: Int,
|
||||
pAlignment: Int,
|
||||
pWidth: Int,
|
||||
pHeight: Int
|
||||
) {
|
||||
createTextBitmapShadowStroke(
|
||||
pString,
|
||||
pFontName,
|
||||
pFontSize,
|
||||
1.0f,
|
||||
1.0f,
|
||||
1.0f,
|
||||
pAlignment,
|
||||
pWidth,
|
||||
pHeight,
|
||||
false,
|
||||
0.0f,
|
||||
0.0f,
|
||||
0.0f,
|
||||
false,
|
||||
1.0f,
|
||||
1.0f,
|
||||
1.0f,
|
||||
1.0f
|
||||
)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun createTextBitmapShadowStroke(
|
||||
pString: String,
|
||||
pFontName: String,
|
||||
pFontSize: Int,
|
||||
fontTintR: Float,
|
||||
fontTintG: Float,
|
||||
fontTintB: Float,
|
||||
pAlignment: Int,
|
||||
pWidth: Int,
|
||||
pHeight: Int,
|
||||
shadow: Boolean,
|
||||
shadowDX: Float,
|
||||
shadowDY: Float,
|
||||
shadowBlur: Float,
|
||||
stroke: Boolean,
|
||||
strokeR: Float,
|
||||
strokeG: Float,
|
||||
strokeB: Float,
|
||||
strokeSize: Float
|
||||
) {
|
||||
val bitmapTotalHeight: Int
|
||||
val horizontalAlignment = pAlignment and 15
|
||||
val verticalAlignment = pAlignment shr 4 and 15
|
||||
val pString2 = refactorString(pString)
|
||||
val paint = newPaint(pFontName, pFontSize, horizontalAlignment)
|
||||
paint.setARGB(
|
||||
MotionEvent.ACTION_MASK,
|
||||
(255.0 * fontTintR.toDouble()).toInt(),
|
||||
(255.0 * fontTintG.toDouble()).toInt(),
|
||||
(255.0 * fontTintB.toDouble()).toInt()
|
||||
)
|
||||
val textProperty = computeTextProperty(pString2, pWidth, pHeight, paint)
|
||||
bitmapTotalHeight = if (pHeight == 0) {
|
||||
textProperty.mTotalHeight
|
||||
} else {
|
||||
pHeight
|
||||
}
|
||||
var bitmapPaddingX = 0.0f
|
||||
var bitmapPaddingY = 0.0f
|
||||
var renderTextDeltaX = 0.0f
|
||||
var renderTextDeltaY = 0.0f
|
||||
if (shadow) {
|
||||
paint.setShadowLayer(shadowBlur, shadowDX, shadowDY, -8553091)
|
||||
bitmapPaddingX = abs(shadowDX)
|
||||
bitmapPaddingY = abs(shadowDY)
|
||||
if (shadowDX.toDouble() < 0.0) {
|
||||
renderTextDeltaX = bitmapPaddingX
|
||||
}
|
||||
if (shadowDY.toDouble() < 0.0) {
|
||||
renderTextDeltaY = bitmapPaddingY
|
||||
}
|
||||
}
|
||||
val bitmap = Bitmap.createBitmap(
|
||||
textProperty.mMaxWidth + bitmapPaddingX.toInt(),
|
||||
bitmapPaddingY.toInt() + bitmapTotalHeight,
|
||||
Bitmap.Config.ARGB_8888
|
||||
)
|
||||
val canvas = Canvas(bitmap)
|
||||
val fontMetricsInt = paint.fontMetricsInt
|
||||
var y = computeY(fontMetricsInt, pHeight, textProperty.mTotalHeight, verticalAlignment)
|
||||
val lines = textProperty.mLines
|
||||
val length = lines.size
|
||||
for (i in 0 until length) {
|
||||
val line = lines[i]
|
||||
canvas.drawText(
|
||||
line!!,
|
||||
computeX(
|
||||
line,
|
||||
textProperty.mMaxWidth,
|
||||
horizontalAlignment
|
||||
).toFloat() + renderTextDeltaX,
|
||||
y.toFloat() + renderTextDeltaY,
|
||||
paint
|
||||
)
|
||||
y += textProperty.mHeightPerLine
|
||||
}
|
||||
if (stroke) {
|
||||
val paintStroke = newPaint(pFontName, pFontSize, horizontalAlignment)
|
||||
paintStroke.style = Paint.Style.STROKE
|
||||
paintStroke.strokeWidth = 0.5f * strokeSize
|
||||
paintStroke.setARGB(
|
||||
MotionEvent.ACTION_MASK,
|
||||
strokeR.toInt() * MotionEvent.ACTION_MASK,
|
||||
strokeG.toInt() * MotionEvent.ACTION_MASK,
|
||||
strokeB.toInt() * MotionEvent.ACTION_MASK
|
||||
)
|
||||
var y2 = computeY(fontMetricsInt, pHeight, textProperty.mTotalHeight, verticalAlignment)
|
||||
val lines2 = textProperty.mLines
|
||||
val length2 = lines2.size
|
||||
for (i2 in 0 until length2) {
|
||||
val line2 = lines2[i2]
|
||||
canvas.drawText(
|
||||
line2!!,
|
||||
computeX(
|
||||
line2,
|
||||
textProperty.mMaxWidth,
|
||||
horizontalAlignment
|
||||
).toFloat() + renderTextDeltaX,
|
||||
y2.toFloat() + renderTextDeltaY,
|
||||
paintStroke
|
||||
)
|
||||
y2 += textProperty.mHeightPerLine
|
||||
}
|
||||
}
|
||||
initNativeObject(bitmap)
|
||||
}
|
||||
|
||||
private fun newPaint(pFontName: String, pFontSize: Int, pHorizontalAlignment: Int): Paint {
|
||||
val paint = Paint()
|
||||
paint.color = -1
|
||||
paint.textSize = pFontSize.toFloat()
|
||||
paint.isAntiAlias = true
|
||||
if (pFontName.endsWith(".ttf")) {
|
||||
try {
|
||||
paint.typeface = Cocos2dxTypefaces[sContext, pFontName]
|
||||
} catch (e: Exception) {
|
||||
Log.e("Cocos2dxBitmap", "error to create ttf type face: $pFontName")
|
||||
paint.typeface = Typeface.create(pFontName, Typeface.NORMAL)
|
||||
}
|
||||
} else {
|
||||
paint.typeface = Typeface.create(pFontName, Typeface.NORMAL)
|
||||
}
|
||||
when (pHorizontalAlignment) {
|
||||
2 -> paint.textAlign = Paint.Align.RIGHT
|
||||
3 -> paint.textAlign = Paint.Align.CENTER
|
||||
else -> paint.textAlign = Paint.Align.LEFT
|
||||
}
|
||||
return paint
|
||||
}
|
||||
|
||||
private fun computeTextProperty(
|
||||
pString: String,
|
||||
pWidth: Int,
|
||||
pHeight: Int,
|
||||
pPaint: Paint
|
||||
): TextProperty {
|
||||
val fm = pPaint.fontMetricsInt
|
||||
val h = ceil((fm.bottom - fm.top).toDouble()).toInt()
|
||||
var maxContentWidth = 0
|
||||
val lines = splitString(pString, pWidth, pHeight, pPaint)
|
||||
if (pWidth != 0) {
|
||||
maxContentWidth = pWidth
|
||||
} else {
|
||||
for (line in lines) {
|
||||
val temp = ceil(pPaint.measureText(line, 0, line!!.length).toDouble()).toInt()
|
||||
if (temp > maxContentWidth) {
|
||||
maxContentWidth = temp
|
||||
}
|
||||
}
|
||||
}
|
||||
return TextProperty(maxContentWidth, h, lines)
|
||||
}
|
||||
|
||||
private fun computeX(@Suppress("UNUSED_PARAMETER") pText: String?, pMaxWidth: Int, pHorizontalAlignment: Int): Int {
|
||||
return when (pHorizontalAlignment) {
|
||||
2 -> pMaxWidth
|
||||
3 -> pMaxWidth / 2
|
||||
else -> 0
|
||||
}
|
||||
}
|
||||
|
||||
private fun computeY(
|
||||
pFontMetricsInt: FontMetricsInt,
|
||||
pConstrainHeight: Int,
|
||||
pTotalHeight: Int,
|
||||
pVerticalAlignment: Int
|
||||
): Int {
|
||||
val y = -pFontMetricsInt.top
|
||||
return if (pConstrainHeight <= pTotalHeight) {
|
||||
y
|
||||
} else when (pVerticalAlignment) {
|
||||
1 -> -pFontMetricsInt.top
|
||||
2 -> -pFontMetricsInt.top + (pConstrainHeight - pTotalHeight)
|
||||
3 -> -pFontMetricsInt.top + (pConstrainHeight - pTotalHeight) / 2
|
||||
else -> y
|
||||
}
|
||||
}
|
||||
|
||||
private fun splitString(
|
||||
pString: String,
|
||||
pMaxWidth: Int,
|
||||
pMaxHeight: Int,
|
||||
pPaint: Paint
|
||||
): Array<String?> {
|
||||
val lines: Array<String?> = pString.split("\\n").toTypedArray()
|
||||
val fm = pPaint.fontMetricsInt
|
||||
val maxLines = pMaxHeight / ceil((fm.bottom - fm.top).toDouble()).toInt()
|
||||
return if (pMaxWidth != 0) {
|
||||
val strList = LinkedList<String?>()
|
||||
for (line in lines) {
|
||||
if (ceil(pPaint.measureText(line).toDouble()).toInt() > pMaxWidth) {
|
||||
strList.addAll(divideStringWithMaxWidth(line, pMaxWidth, pPaint))
|
||||
} else {
|
||||
strList.add(line)
|
||||
}
|
||||
if (maxLines > 0 && strList.size >= maxLines) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if (maxLines > 0 && strList.size > maxLines) {
|
||||
while (strList.size > maxLines) {
|
||||
strList.removeLast()
|
||||
}
|
||||
}
|
||||
val ret = arrayOfNulls<String>(strList.size)
|
||||
strList.toArray(ret)
|
||||
ret
|
||||
} else if (pMaxHeight == 0 || lines.size <= maxLines) {
|
||||
lines
|
||||
} else {
|
||||
val strList2 = LinkedList<String?>()
|
||||
for (i in 0 until maxLines) {
|
||||
strList2.add(lines[i])
|
||||
}
|
||||
val ret2 = arrayOfNulls<String>(strList2.size)
|
||||
strList2.toArray(ret2)
|
||||
ret2
|
||||
}
|
||||
}
|
||||
|
||||
private fun divideStringWithMaxWidth(
|
||||
pString: String?,
|
||||
pMaxWidth: Int,
|
||||
pPaint: Paint
|
||||
): LinkedList<String?> {
|
||||
val charLength = pString!!.length
|
||||
var start = 0
|
||||
val strList = LinkedList<String?>()
|
||||
var i = 1
|
||||
while (i <= charLength) {
|
||||
val tempWidth = ceil(pPaint.measureText(pString, start, i).toDouble()).toInt()
|
||||
if (tempWidth >= pMaxWidth) {
|
||||
val lastIndexOfSpace = pString.substring(0, i).lastIndexOf(" ")
|
||||
if (lastIndexOfSpace != -1 && lastIndexOfSpace > start) {
|
||||
strList.add(pString.substring(start, lastIndexOfSpace))
|
||||
i = lastIndexOfSpace + 1
|
||||
} else if (tempWidth > pMaxWidth) {
|
||||
strList.add(pString.substring(start, i - 1))
|
||||
i--
|
||||
} else {
|
||||
strList.add(pString.substring(start, i))
|
||||
}
|
||||
while (i < charLength && pString[i] == ' ') {
|
||||
i++
|
||||
}
|
||||
start = i
|
||||
}
|
||||
i++
|
||||
}
|
||||
if (start < charLength) {
|
||||
strList.add(pString.substring(start))
|
||||
}
|
||||
return strList
|
||||
}
|
||||
|
||||
private fun refactorString(pString: String): String {
|
||||
if (pString.compareTo("") == 0) {
|
||||
return " "
|
||||
}
|
||||
val strBuilder = StringBuilder(pString)
|
||||
var start = 0
|
||||
var index = strBuilder.indexOf("\n")
|
||||
while (index != -1) {
|
||||
start = if (index == 0 || strBuilder[index - 1].code == 10) {
|
||||
strBuilder.insert(start, " ")
|
||||
index + 2
|
||||
} else {
|
||||
index + 1
|
||||
}
|
||||
if (start > strBuilder.length || index == strBuilder.length) {
|
||||
break
|
||||
}
|
||||
index = strBuilder.indexOf("\n", start)
|
||||
}
|
||||
return strBuilder.toString()
|
||||
}
|
||||
|
||||
private fun initNativeObject(pBitmap: Bitmap) {
|
||||
val pixels = getPixels(pBitmap)
|
||||
if (pixels != null) {
|
||||
nativeInitBitmapDC(pBitmap.width, pBitmap.height, pixels)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getPixels(pBitmap: Bitmap?): ByteArray? {
|
||||
if (pBitmap == null) {
|
||||
return null
|
||||
}
|
||||
val pixels = ByteArray(pBitmap.width * pBitmap.height * 4)
|
||||
val buf = ByteBuffer.wrap(pixels)
|
||||
buf.order(ByteOrder.nativeOrder())
|
||||
pBitmap.copyPixelsToBuffer(buf)
|
||||
return pixels
|
||||
}
|
||||
|
||||
private fun getFontSizeAccordingHeight(height: Int): Int {
|
||||
val paint = Paint()
|
||||
val bounds = Rect()
|
||||
paint.typeface = Typeface.DEFAULT
|
||||
var incrTextSize = 1
|
||||
var foundDesiredSize = false
|
||||
while (!foundDesiredSize) {
|
||||
paint.textSize = incrTextSize.toFloat()
|
||||
paint.getTextBounds("SghMNy", 0, "SghMNy".length, bounds)
|
||||
incrTextSize++
|
||||
if (height - bounds.height() <= 2) {
|
||||
foundDesiredSize = true
|
||||
}
|
||||
Log.d("font size", "incr size:$incrTextSize")
|
||||
}
|
||||
return incrTextSize
|
||||
}
|
||||
|
||||
private fun getStringWithEllipsis(pString: String, width: Float, fontSize: Float): String {
|
||||
if (TextUtils.isEmpty(pString)) {
|
||||
return ""
|
||||
}
|
||||
val paint = TextPaint()
|
||||
paint.typeface = Typeface.DEFAULT
|
||||
paint.textSize = fontSize
|
||||
return TextUtils.ellipsize(pString, paint, width, TextUtils.TruncateAt.END).toString()
|
||||
}
|
||||
|
||||
private class TextProperty constructor(/* access modifiers changed from: private */
|
||||
val mMaxWidth: Int, /* access modifiers changed from: private */
|
||||
val mHeightPerLine: Int, pLines: Array<String?>
|
||||
) {
|
||||
/* access modifiers changed from: private */
|
||||
val mLines: Array<String?>
|
||||
|
||||
/* access modifiers changed from: private */
|
||||
val mTotalHeight: Int
|
||||
|
||||
init {
|
||||
mTotalHeight = pLines.size * mHeightPerLine
|
||||
mLines = pLines
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package org.cocos2dx.lib
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.opengl.GLSurfaceView
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.os.Message
|
||||
import android.view.KeyEvent
|
||||
import android.view.MotionEvent
|
||||
import com.customRobTop.BaseRobTopActivity
|
||||
|
||||
|
||||
class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
||||
companion object {
|
||||
lateinit var mCocos2dxGLSurfaceView: Cocos2dxGLSurfaceView
|
||||
private var sHandler: Handler? = null
|
||||
}
|
||||
|
||||
private lateinit var mCocos2dxRenderer: Cocos2dxRenderer
|
||||
|
||||
fun initView() {
|
||||
setEGLContextClientVersion(2)
|
||||
isFocusableInTouchMode = true
|
||||
// if (Build.VERSION.SDK_INT >= 11) {
|
||||
preserveEGLContextOnPause = true
|
||||
// }
|
||||
mCocos2dxGLSurfaceView = this
|
||||
// sCocos2dxTextInputWraper = Cocos2dxTextInputWraper(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,
|
||||
0
|
||||
)
|
||||
Log.d("GLSurfaceView", "showSoftInput")
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
3 -> {
|
||||
if (this@Cocos2dxGLSurfaceView.mCocos2dxEditText != null) {
|
||||
this@Cocos2dxGLSurfaceView.mCocos2dxEditText.removeTextChangedListener(
|
||||
Cocos2dxGLSurfaceView.sCocos2dxTextInputWraper
|
||||
)
|
||||
(Cocos2dxGLSurfaceView.mCocos2dxGLSurfaceView.getContext()
|
||||
.getSystemService("input_method") as InputMethodManager).hideSoftInputFromWindow(
|
||||
this@Cocos2dxGLSurfaceView.mCocos2dxEditText.getWindowToken(),
|
||||
0
|
||||
)
|
||||
this@Cocos2dxGLSurfaceView.requestFocus()
|
||||
Log.d("GLSurfaceView", "HideSoftInput")
|
||||
this@Cocos2dxGLSurfaceView.mCocos2dxRenderer.handleTextClosed()
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
else -> return
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override fun onTouchEvent(pMotionEvent: MotionEvent): Boolean {
|
||||
val pointerNumber = pMotionEvent.pointerCount
|
||||
val ids = IntArray(pointerNumber)
|
||||
val xs = FloatArray(pointerNumber)
|
||||
val ys = FloatArray(pointerNumber)
|
||||
for (i in 0 until pointerNumber) {
|
||||
ids[i] = pMotionEvent.getPointerId(i)
|
||||
xs[i] = pMotionEvent.getX(i)
|
||||
ys[i] = pMotionEvent.getY(i)
|
||||
}
|
||||
return when (pMotionEvent.action and MotionEvent.ACTION_MASK) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
val idDown = pMotionEvent.getPointerId(0)
|
||||
val xDown = xs[0]
|
||||
val f = ys[0]
|
||||
queueEvent {
|
||||
mCocos2dxRenderer.handleActionDown(
|
||||
idDown,
|
||||
xDown,
|
||||
f
|
||||
)
|
||||
}
|
||||
true
|
||||
}
|
||||
MotionEvent.ACTION_UP -> {
|
||||
val idUp = pMotionEvent.getPointerId(0)
|
||||
val f2 = xs[0]
|
||||
val f3 = ys[0]
|
||||
queueEvent { mCocos2dxRenderer.handleActionUp(idUp, f2, f3) }
|
||||
true
|
||||
}
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
queueEvent {
|
||||
mCocos2dxRenderer.handleActionMove(
|
||||
ids,
|
||||
xs,
|
||||
ys
|
||||
)
|
||||
}
|
||||
true
|
||||
}
|
||||
MotionEvent.ACTION_CANCEL -> {
|
||||
queueEvent {
|
||||
mCocos2dxRenderer.handleActionCancel(
|
||||
ids,
|
||||
xs,
|
||||
ys
|
||||
)
|
||||
}
|
||||
true
|
||||
}
|
||||
MotionEvent.ACTION_POINTER_DOWN -> {
|
||||
val indexPointerDown = pMotionEvent.action shr 8
|
||||
val idPointerDown = pMotionEvent.getPointerId(indexPointerDown)
|
||||
val xPointerDown = pMotionEvent.getX(indexPointerDown)
|
||||
val y = pMotionEvent.getY(indexPointerDown)
|
||||
queueEvent {
|
||||
mCocos2dxRenderer.handleActionDown(
|
||||
idPointerDown,
|
||||
xPointerDown,
|
||||
y
|
||||
)
|
||||
}
|
||||
true
|
||||
}
|
||||
MotionEvent.ACTION_POINTER_UP -> {
|
||||
val indexPointUp = pMotionEvent.action shr 8
|
||||
val idPointerUp = pMotionEvent.getPointerId(indexPointUp)
|
||||
val xPointerUp = pMotionEvent.getX(indexPointUp)
|
||||
val y2 = pMotionEvent.getY(indexPointUp)
|
||||
queueEvent {
|
||||
mCocos2dxRenderer.handleActionUp(
|
||||
idPointerUp,
|
||||
xPointerUp,
|
||||
y2
|
||||
)
|
||||
}
|
||||
true
|
||||
}
|
||||
else -> true
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSizeChanged(
|
||||
pNewSurfaceWidth: Int,
|
||||
pNewSurfaceHeight: Int,
|
||||
pOldSurfaceWidth: Int,
|
||||
pOldSurfaceHeight: Int
|
||||
) {
|
||||
if (!isInEditMode) {
|
||||
mCocos2dxRenderer.setScreenWidthAndHeight(pNewSurfaceWidth, pNewSurfaceHeight)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onKeyDown(pKeyCode: Int, pKeyEvent: KeyEvent): Boolean {
|
||||
return when (pKeyCode) {
|
||||
KeyEvent.KEYCODE_BACK, KeyEvent.KEYCODE_MENU -> {
|
||||
if (pKeyEvent.repeatCount != 0 || BaseRobTopActivity.blockBackButton_) {
|
||||
return true
|
||||
}
|
||||
queueEvent { mCocos2dxRenderer.handleKeyDown(pKeyCode) }
|
||||
true
|
||||
}
|
||||
else -> super.onKeyDown(pKeyCode, pKeyEvent)
|
||||
}
|
||||
}
|
||||
|
||||
fun setCocos2dxRenderer(renderer: Cocos2dxRenderer) {
|
||||
this.mCocos2dxRenderer = renderer
|
||||
setRenderer(this.mCocos2dxRenderer)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.cocos2dx.lib
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.pm.ApplicationInfo
|
||||
|
||||
|
||||
@Suppress("unused")
|
||||
class Cocos2dxHelper {
|
||||
companion object {
|
||||
// private val sCocos2dMusic: Cocos2dxMusic? = null
|
||||
// private val sCocos2dSound: Cocos2dxSound? = null
|
||||
// private val sAssetManager: AssetManager? = null
|
||||
// private val sCocos2dxAccelerometer: Cocos2dxAccelerometer? = null
|
||||
// private val sAccelerometerEnabled = false
|
||||
private var sPackageName: String? = null
|
||||
private var sFileDirectory: String? = null
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private var sContext: Context? = null
|
||||
private var sCocos2dxHelperListener: Cocos2dxHelperListener? = null
|
||||
|
||||
@JvmStatic
|
||||
external fun nativeSetApkPath(pApkPath: String)
|
||||
|
||||
@JvmStatic
|
||||
fun getCocos2dxWritablePath(): String? {
|
||||
return sFileDirectory
|
||||
}
|
||||
|
||||
fun init(pContext: Context, pCocos2dxHelperListener: Cocos2dxHelperListener) {
|
||||
val applicationInfo: ApplicationInfo = pContext.applicationInfo
|
||||
sContext = pContext
|
||||
sCocos2dxHelperListener = pCocos2dxHelperListener
|
||||
sPackageName = applicationInfo.packageName
|
||||
sFileDirectory = pContext.filesDir.absolutePath
|
||||
|
||||
nativeSetApkPath(applicationInfo.sourceDir)
|
||||
// Cocos2dxHelper.sCocos2dxAccelerometer = Cocos2dxAccelerometer(pContext)
|
||||
// Cocos2dxHelper.sCocos2dMusic = Cocos2dxMusic(pContext)
|
||||
// var simultaneousStreams: Int = Cocos2dxSound.MAX_SIMULTANEOUS_STREAMS_DEFAULT
|
||||
// if (Cocos2dxHelper.getDeviceModel().indexOf("GT-I9100") !== -1) {
|
||||
// simultaneousStreams = Cocos2dxSound.MAX_SIMULTANEOUS_STREAMS_I9100
|
||||
// }
|
||||
// Cocos2dxHelper.sCocos2dSound = Cocos2dxSound(pContext, simultaneousStreams)
|
||||
// Cocos2dxHelper.sAssetManager = pContext.getAssets()
|
||||
Cocos2dxBitmap.setContext(pContext)
|
||||
// Cocos2dxETCLoader.setContext(pContext)
|
||||
}
|
||||
}
|
||||
|
||||
interface Cocos2dxHelperListener {
|
||||
fun runOnGLThread(pRunnable: Runnable)
|
||||
|
||||
fun showDialog(pTitle: String, pMessage: String)
|
||||
|
||||
fun showEditTextDialog(
|
||||
pTitle: String,
|
||||
pMessage: String,
|
||||
pInputMode: Int,
|
||||
pInputFlag: Int,
|
||||
pReturnType: Int,
|
||||
pMaxLength: Int
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package org.cocos2dx.lib
|
||||
|
||||
import android.opengl.GLSurfaceView
|
||||
import javax.microedition.khronos.egl.EGLConfig
|
||||
import javax.microedition.khronos.opengles.GL10
|
||||
|
||||
@Suppress("unused")
|
||||
class Cocos2dxRenderer : GLSurfaceView.Renderer {
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun setAnimationInterval(@Suppress("UNUSED_PARAMETER") pAnimationInterval: Double) {
|
||||
// this function is useless for gd
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
private external fun nativeRender()
|
||||
|
||||
@JvmStatic
|
||||
private external fun nativeKeyDown(pKeyCode: Int): Boolean
|
||||
|
||||
@JvmStatic
|
||||
private external fun nativeTouchesBegin(pID: Int, pX: Float, pY: Float)
|
||||
|
||||
@JvmStatic
|
||||
private external fun nativeTouchesCancel(
|
||||
pIDs: IntArray,
|
||||
pXs: FloatArray,
|
||||
pYs: FloatArray
|
||||
)
|
||||
|
||||
@JvmStatic
|
||||
private external fun nativeTouchesEnd(pID: Int, pX: Float, pY: Float)
|
||||
|
||||
@JvmStatic
|
||||
private external fun nativeTouchesMove(pIDs: IntArray, pXs: FloatArray, pYs: FloatArray)
|
||||
}
|
||||
|
||||
private var mLastTickInNanoSeconds: Long = 0
|
||||
private var mScreenWidth = 0
|
||||
private var mScreenHeight = 0
|
||||
private external fun nativeInit(pWidth: Int, pHeight: Int)
|
||||
|
||||
fun setScreenWidthAndHeight(pSurfaceWidth: Int, pSurfaceHeight: Int) {
|
||||
mScreenWidth = pSurfaceWidth
|
||||
mScreenHeight = pSurfaceHeight
|
||||
}
|
||||
|
||||
override fun onSurfaceCreated(pGL10: GL10?, pEGLConfig: EGLConfig?) {
|
||||
nativeInit(this.mScreenWidth, this.mScreenHeight)
|
||||
this.mLastTickInNanoSeconds = System.nanoTime()
|
||||
}
|
||||
|
||||
override fun onSurfaceChanged(pGL10: GL10?, pWidth: Int, pHeight: Int) {}
|
||||
|
||||
override fun onDrawFrame(gl: GL10?) {
|
||||
nativeRender()
|
||||
}
|
||||
|
||||
fun handleActionDown(pID: Int, pX: Float, pY: Float) {
|
||||
nativeTouchesBegin(pID, pX, pY)
|
||||
}
|
||||
|
||||
fun handleActionUp(pID: Int, pX: Float, pY: Float) {
|
||||
nativeTouchesEnd(pID, pX, pY)
|
||||
}
|
||||
|
||||
fun handleActionCancel(pIDs: IntArray, pXs: FloatArray, pYs: FloatArray) {
|
||||
nativeTouchesCancel(pIDs, pXs, pYs)
|
||||
}
|
||||
|
||||
fun handleActionMove(pIDs: IntArray, pXs: FloatArray, pYs: FloatArray) {
|
||||
nativeTouchesMove(pIDs, pXs, pYs)
|
||||
}
|
||||
|
||||
fun handleKeyDown(pKeyCode: Int) {
|
||||
nativeKeyDown(pKeyCode)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.cocos2dx.lib
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Typeface
|
||||
import kotlin.jvm.Synchronized
|
||||
import java.util.HashMap
|
||||
|
||||
object Cocos2dxTypefaces {
|
||||
private val sTypefaceCache = HashMap<String, Typeface>()
|
||||
|
||||
@Synchronized
|
||||
operator fun get(pContext: Context, pAssetName: String): Typeface? {
|
||||
var typeface: Typeface?
|
||||
var typeface2: Typeface
|
||||
synchronized(Cocos2dxTypefaces::class.java) {
|
||||
if (!sTypefaceCache.containsKey(pAssetName)) {
|
||||
typeface2 = if (pAssetName.startsWith("/")) {
|
||||
Typeface.createFromFile(pAssetName)
|
||||
} else {
|
||||
Typeface.createFromAsset(pContext.assets, pAssetName)
|
||||
}
|
||||
sTypefaceCache[pAssetName] = typeface2
|
||||
}
|
||||
typeface = sTypefaceCache[pAssetName]
|
||||
}
|
||||
return typeface
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user