~hokiegeek/qwingo-roller

09584e5208d37143723bc28b1e2caa05c090e56a — HokieGeek 7 months ago aa050a8
Includes framework for clicks and shaking
M app/src/main/java/net/hokiegeek/qwingoroller/MainActivity.kt => app/src/main/java/net/hokiegeek/qwingoroller/MainActivity.kt +80 -2
@@ 1,11 1,89 @@
package net.hokiegeek.qwingoroller

import androidx.appcompat.app.AppCompatActivity
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import kotlin.math.sqrt

class MainActivity : AppCompatActivity() {
    companion object {
     /*
     * The gForce that is necessary to register as shake.
     * Must be greater than 1G (one earth gravity unit).
     * You can install "G-Force", by Blake La Pierre
     * from the Google Play Store and run it to see how
     *  many G's it takes to register a shake
     */
        private const val SHAKE_THRESHOLD_GRAVITY = 2.7f
        private const val SHAKE_SLOP_TIME_MS = 500
        // private const val SHAKE_COUNT_RESET_TIME_MS = 3000
    }

    private val logTag = "QWINGO"

    private var mSensorMgr: SensorManager? = null

    private val mSensorListener = object : SensorEventListener {
        private var mShakeTimestamp: Long = 0
        // private var mShakeCount = 0
        override fun onSensorChanged(se: SensorEvent) {
            val gX = se.values[0] / SensorManager.GRAVITY_EARTH
            val gY = se.values[1] / SensorManager.GRAVITY_EARTH
            val gZ = se.values[2] / SensorManager.GRAVITY_EARTH
            // gForce will be close to 1 when there is no movement.
            val gForce: Float = sqrt(gX * gX + gY * gY + gZ * gZ)
            if (gForce > SHAKE_THRESHOLD_GRAVITY) {
                val now = System.currentTimeMillis()
                // ignore shake events too close to each other (500ms)
                if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now) {
                    return
                }
                // reset the shake count after 3 seconds of no shakes
                /*
                if (mShakeTimestamp + SHAKE_COUNT_RESET_TIME_MS < now) {
                    mShakeCount = 0
                }
                */
                mShakeTimestamp = now
                // mShakeCount++
                Log.d(logTag, "ACTION: shake?")
                roll()
                // mListener!!.onShake(mShakeCount)
            }
        }

        override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<ConstraintLayout>(R.id.layout).setOnClickListener {
            Log.d(logTag, "ACTION: click")
            roll()
        }

        mSensorMgr = getSystemService(Context.SENSOR_SERVICE) as SensorManager?
    }

    override fun onResume() {
        super.onResume()
        mSensorMgr?.registerListener(mSensorListener, mSensorMgr!!.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI)
    }

    override fun onPause() {
        mSensorMgr?.unregisterListener(mSensorListener);
        super.onPause()
    }

    private fun roll() {
        Log.d(logTag, "TODO: roll!")
    }
}
\ No newline at end of file
}

M app/src/main/res/layout/activity_main.xml => app/src/main/res/layout/activity_main.xml +2 -0
@@ 2,6 2,7 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#1B1B1B"


@@ 11,6 12,7 @@
        android:id="@+id/dieFace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/dieFaceDescription"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"

M app/src/main/res/values/strings.xml => app/src/main/res/values/strings.xml +1 -0
@@ 1,3 1,4 @@
<resources>
    <string name="app_name">Qwingo Roller</string>
    <string name="dieFaceDescription">The face of the die that it landed on after being rolled</string>
</resources>
\ No newline at end of file