~hokiegeek/qwingo-roller

ref: b8d026134b4b20083312fe4ad3a6cd74c25e941e qwingo-roller/app/src/main/java/net/hokiegeek/qwingoroller/MainActivity.kt -rw-r--r-- 4.0 KiB
b8d02613HokieGeek got the basic die colors and initial faces in 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package net.hokiegeek.qwingoroller

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 android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import kotlin.math.sqrt
import kotlin.random.Random

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

    // blue coffee cup
    // green watermelon
    // pink tomato
    // purple ice cream cone
    // yellow banana
    // silver (on black) lighting

    private val dieColors = arrayOf(R.color.die_green, R.color.die_blue, R.color.die_pink, R.color.die_purple, R.color.die_yellow, R.color.black)
    private val dieFaces = arrayOf(R.drawable.watermelon, R.drawable.cup, R.drawable.tomato, R.drawable.cone, R.drawable.banana, R.drawable.lightning)

    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?

        roll() // initialize the view
    }

    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, "roll!")

        val selection = Random.nextInt(dieFaces.size)
        findViewById<ImageView>(R.id.dieFace).setImageResource(dieFaces[selection]);
        findViewById<ConstraintLayout>(R.id.layout).setBackgroundResource(dieColors[selection])
    }

    /*
    (new Handler()).postDelayed(this::yourMethod, 5000);

    Glide.with(this).load(R.raw.hamster).into(findViewById<ImageView>(R.id.loading));
     */
}