forked from geode-sdk/android-launcher
bring back the accelerometer
This commit is contained in:
3 files changed
+147
-5
No files matched your search
@@ -0,0 +1,102 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
Copyright (c) 2010-2011 cocos2d-x.org
|
||||||
|
|
||||||
|
http://www.cocos2d-x.org
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
****************************************************************************/
|
||||||
|
package org.cocos2dx.lib
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.res.Configuration
|
||||||
|
import android.hardware.Sensor
|
||||||
|
import android.hardware.SensorEvent
|
||||||
|
import android.hardware.SensorEventListener
|
||||||
|
import android.hardware.SensorManager
|
||||||
|
import android.os.Build
|
||||||
|
import android.view.Surface
|
||||||
|
import android.view.WindowManager
|
||||||
|
import java.lang.ref.WeakReference
|
||||||
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
|
class Cocos2dxAccelerometer : SensorEventListener {
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
external fun onSensorChanged(pX: Float, pY: Float, pZ: Float, pTimestamp: Long)
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(context: Context) {
|
||||||
|
mContext = WeakReference(context)
|
||||||
|
mSensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
||||||
|
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
|
||||||
|
val display = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||||
|
context.display
|
||||||
|
} else {
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
(context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
|
||||||
|
}
|
||||||
|
this.mNaturalOrientation = display.rotation
|
||||||
|
}
|
||||||
|
|
||||||
|
private val mContext: WeakReference<Context>
|
||||||
|
private val mSensorManager: SensorManager
|
||||||
|
private val mAccelerometer: Sensor?
|
||||||
|
private val mNaturalOrientation: Int
|
||||||
|
|
||||||
|
fun enable() {
|
||||||
|
this.mSensorManager.registerListener(this, this.mAccelerometer, SensorManager.SENSOR_DELAY_GAME)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setInterval(interval: Float) {
|
||||||
|
this.mSensorManager.registerListener(this, this.mAccelerometer, (interval * 100000).roundToInt())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun disable() {
|
||||||
|
this.mSensorManager.unregisterListener(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSensorChanged(pSensorEvent: SensorEvent) {
|
||||||
|
if (pSensorEvent.sensor.type != Sensor.TYPE_ACCELEROMETER) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var (x, y, z) = pSensorEvent.values
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Because the axes are not swapped when the device's screen orientation
|
||||||
|
* changes. So we should swap it here. In tablets such as Motorola Xoom,
|
||||||
|
* the default orientation is landscape, so should consider this.
|
||||||
|
*/
|
||||||
|
val orientation = this.mContext.get()!!.resources.configuration.orientation
|
||||||
|
|
||||||
|
if ((orientation == Configuration.ORIENTATION_LANDSCAPE) && (this.mNaturalOrientation != Surface.ROTATION_0)) {
|
||||||
|
val tmp = x
|
||||||
|
x = -y
|
||||||
|
y = tmp
|
||||||
|
} else if ((orientation == Configuration.ORIENTATION_PORTRAIT) && (this.mNaturalOrientation != Surface.ROTATION_0)) {
|
||||||
|
val tmp = x
|
||||||
|
x = y
|
||||||
|
y = -tmp
|
||||||
|
}
|
||||||
|
|
||||||
|
Cocos2dxGLSurfaceView.queueAccelerometer(x, y, z, pSensorEvent.timestamp)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAccuracyChanged(pSensor: Sensor, pAccuracy: Int) {}
|
||||||
|
}
|
||||||
@@ -39,6 +39,11 @@ class Cocos2dxGLSurfaceView(context: Context) : GLSurfaceView(context) {
|
|||||||
msg.what = HANDLER_CLOSE_IME_KEYBOARD
|
msg.what = HANDLER_CLOSE_IME_KEYBOARD
|
||||||
handler.sendMessage(msg)
|
handler.sendMessage(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun queueAccelerometer(x: Float, y: Float, z: Float, timestamp: Long) =
|
||||||
|
cocos2dxGLSurfaceView.queueEvent {
|
||||||
|
Cocos2dxAccelerometer.onSensorChanged(x, y, z, timestamp)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private lateinit var cocos2dxRenderer: Cocos2dxRenderer
|
private lateinit var cocos2dxRenderer: Cocos2dxRenderer
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.cocos2dx.lib
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.pm.ApplicationInfo
|
import android.content.pm.ApplicationInfo
|
||||||
|
import android.content.res.AssetManager
|
||||||
import android.os.Process
|
import android.os.Process
|
||||||
import com.customRobTop.BaseRobTopActivity
|
import com.customRobTop.BaseRobTopActivity
|
||||||
import com.geode.launcher.utils.LaunchUtils
|
import com.geode.launcher.utils.LaunchUtils
|
||||||
@@ -11,9 +12,9 @@ import com.geode.launcher.utils.LaunchUtils
|
|||||||
object Cocos2dxHelper {
|
object Cocos2dxHelper {
|
||||||
// private val sCocos2dMusic: Cocos2dxMusic? = null
|
// private val sCocos2dMusic: Cocos2dxMusic? = null
|
||||||
// private val sCocos2dSound: Cocos2dxSound? = null
|
// private val sCocos2dSound: Cocos2dxSound? = null
|
||||||
// private val sAssetManager: AssetManager? = null
|
private var sAssetManager: AssetManager? = null
|
||||||
// private val sCocos2dxAccelerometer: Cocos2dxAccelerometer? = null
|
private var sCocos2dxAccelerometer: Cocos2dxAccelerometer? = null
|
||||||
// private val sAccelerometerEnabled = false
|
private var sAccelerometerEnabled = false
|
||||||
private var packageName: String? = null
|
private var packageName: String? = null
|
||||||
private var fileDirectory: String? = null
|
private var fileDirectory: String? = null
|
||||||
|
|
||||||
@@ -36,6 +37,40 @@ object Cocos2dxHelper {
|
|||||||
Process.killProcess(Process.myPid())
|
Process.killProcess(Process.myPid())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun getAssetManager(): AssetManager? {
|
||||||
|
return sAssetManager
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun enableAccelerometer() {
|
||||||
|
sAccelerometerEnabled = true
|
||||||
|
sCocos2dxAccelerometer?.enable()
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun setAccelerometerInterval(interval: Float) {
|
||||||
|
sCocos2dxAccelerometer?.setInterval(interval)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun disableAccelerometer() {
|
||||||
|
sAccelerometerEnabled = false
|
||||||
|
sCocos2dxAccelerometer?.disable()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onResume() {
|
||||||
|
if (sAccelerometerEnabled) {
|
||||||
|
sCocos2dxAccelerometer?.enable()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onPause() {
|
||||||
|
if (sAccelerometerEnabled) {
|
||||||
|
sCocos2dxAccelerometer?.disable()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getDPI(): Int {
|
fun getDPI(): Int {
|
||||||
return BaseRobTopActivity.me.get()?.resources?.configuration?.densityDpi ?: -1
|
return BaseRobTopActivity.me.get()?.resources?.configuration?.densityDpi ?: -1
|
||||||
@@ -52,14 +87,14 @@ object Cocos2dxHelper {
|
|||||||
this.cocos2dxHelperListener = cocos2dxHelperListener
|
this.cocos2dxHelperListener = cocos2dxHelperListener
|
||||||
packageName = applicationInfo.packageName
|
packageName = applicationInfo.packageName
|
||||||
fileDirectory = LaunchUtils.getSaveDirectory(context).absolutePath
|
fileDirectory = LaunchUtils.getSaveDirectory(context).absolutePath
|
||||||
// Cocos2dxHelper.sCocos2dxAccelerometer = Cocos2dxAccelerometer(pContext)
|
sCocos2dxAccelerometer = Cocos2dxAccelerometer(context)
|
||||||
// Cocos2dxHelper.sCocos2dMusic = Cocos2dxMusic(pContext)
|
// Cocos2dxHelper.sCocos2dMusic = Cocos2dxMusic(pContext)
|
||||||
// var simultaneousStreams: Int = Cocos2dxSound.MAX_SIMULTANEOUS_STREAMS_DEFAULT
|
// var simultaneousStreams: Int = Cocos2dxSound.MAX_SIMULTANEOUS_STREAMS_DEFAULT
|
||||||
// if (Cocos2dxHelper.getDeviceModel().indexOf("GT-I9100") !== -1) {
|
// if (Cocos2dxHelper.getDeviceModel().indexOf("GT-I9100") !== -1) {
|
||||||
// simultaneousStreams = Cocos2dxSound.MAX_SIMULTANEOUS_STREAMS_I9100
|
// simultaneousStreams = Cocos2dxSound.MAX_SIMULTANEOUS_STREAMS_I9100
|
||||||
// }
|
// }
|
||||||
// Cocos2dxHelper.sCocos2dSound = Cocos2dxSound(pContext, simultaneousStreams)
|
// Cocos2dxHelper.sCocos2dSound = Cocos2dxSound(pContext, simultaneousStreams)
|
||||||
// Cocos2dxHelper.sAssetManager = pContext.getAssets()
|
sAssetManager = context.assets
|
||||||
Cocos2dxBitmap.setContext(context)
|
Cocos2dxBitmap.setContext(context)
|
||||||
// Cocos2dxETCLoader.setContext(pContext)
|
// Cocos2dxETCLoader.setContext(pContext)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user