Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soundboard: Don't use the top app bar and modernize UI #2186

Merged
merged 3 commits into from
Mar 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified samples/SoundBoard/soundboard_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -24,6 +24,8 @@ import android.graphics.Rect
import android.media.AudioManager
import android.os.Build
import android.os.Bundle
import android.view.WindowInsets
import android.view.WindowManager
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import kotlin.math.min
@@ -38,7 +40,8 @@ class MainActivity : AppCompatActivity() {
private const val DIMENSION_MAX_SIZE = 8
private var mNumColumns : Int = 0;
private var mNumRows : Int = 0;
private var mRectangles = ArrayList<Rect>()
private var mTiles = ArrayList<Rect>()
private var mBorders = ArrayList<Rect>()

private var mEngineHandle: Long = 0

@@ -81,19 +84,25 @@ class MainActivity : AppCompatActivity() {
}

private fun calculateAndSetRectangles(context: Context) {
val width: Int
val height: Int
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display = windowManager.defaultDisplay
val size = Point()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
width = windowManager.currentWindowMetrics.bounds.width()
height = windowManager.currentWindowMetrics.bounds.height()
val windowMetrics = windowManager.currentWindowMetrics
val windowInsets = windowMetrics.windowInsets
val insets = windowInsets.getInsetsIgnoringVisibility(
WindowInsets.Type.navigationBars() or WindowInsets.Type.statusBars())
val width = windowMetrics.bounds.width() - insets.left - insets.right
val height = windowMetrics.bounds.height() - insets.top - insets.bottom
size.set(width, height)
} else {
val size = Point()
windowManager.defaultDisplay.getRealSize(size)
height = size.y
width = size.x
display.getSize(size) // Use getSize to exclude navigation bar if visible
}

val width = size.x
val height = size.y

if (height > width) {
mNumColumns = DIMENSION_MIN_SIZE
mNumRows = min(DIMENSION_MIN_SIZE * height / width, DIMENSION_MAX_SIZE)
@@ -103,8 +112,9 @@ class MainActivity : AppCompatActivity() {
}
val tileLength = min(height / mNumRows, width / mNumColumns)
val xStartLocation = (width - tileLength * mNumColumns) / 2
val yStartLocation = 0
mRectangles = ArrayList<Rect>()
val yStartLocation = (height - tileLength * mNumRows) / 2

mTiles = ArrayList<Rect>()
for (i in 0 until mNumRows) {
for (j in 0 until mNumColumns) {
val rectangle = Rect(
@@ -113,13 +123,23 @@ class MainActivity : AppCompatActivity() {
xStartLocation + j * tileLength + tileLength,
yStartLocation + i * tileLength + tileLength
)
mRectangles.add(rectangle)
mTiles.add(rectangle)
}
}

mBorders = ArrayList<Rect>()
// Top border
mBorders.add(Rect(0, 0, width, yStartLocation))
// Bottom border
mBorders.add(Rect(0, yStartLocation + tileLength * mNumRows, width, height))
// Left border
mBorders.add(Rect(0, 0, xStartLocation, height))
// Right border
mBorders.add(Rect(xStartLocation + tileLength * mNumColumns, 0, width, height))
}

private fun createMusicTiles(context: Context) {
setContentView(MusicTileView(this, mRectangles, NoteListener(mEngineHandle),
setContentView(MusicTileView(this, mTiles, mBorders, NoteListener(mEngineHandle),
ScreenChangeListener { setup() }))
}

Original file line number Diff line number Diff line change
@@ -22,14 +22,16 @@ import android.graphics.*
import android.util.SparseArray
import android.view.MotionEvent
import android.view.View
import androidx.core.content.ContextCompat

class MusicTileView(
context: Context?,
private val mRectangles: ArrayList<Rect>,
private val mTiles: ArrayList<Rect>,
private val mBorders: ArrayList<Rect>,
tileListener: TileListener,
configChangeListener: ConfigChangeListener
) : View(context) {
private val mIsPressedPerRectangle: BooleanArray = BooleanArray(mRectangles.size)
private val mIsPressedPerRectangle: BooleanArray = BooleanArray(mTiles.size)
private val mPaint: Paint = Paint()
private val mLocationsOfFingers: SparseArray<PointF> = SparseArray()
private val mTileListener: TileListener
@@ -45,29 +47,34 @@ class MusicTileView(
}

private fun getIndexFromLocation(pointF: PointF): Int {
for (i in mRectangles.indices) {
if (pointF.x > mRectangles[i].left && pointF.x < mRectangles[i].right && pointF.y > mRectangles[i].top && pointF.y < mRectangles[i].bottom) {
for (i in mTiles.indices) {
if (pointF.x > mTiles[i].left && pointF.x < mTiles[i].right && pointF.y > mTiles[i].top && pointF.y < mTiles[i].bottom) {
return i
}
}
return -1
}

override fun onDraw(canvas: Canvas) {
for (i in mRectangles.indices) {
for (i in mTiles.indices) {
mPaint.style = Paint.Style.FILL
if (mIsPressedPerRectangle[i]) {
mPaint.color = Color.rgb(128, 0, 0)
mPaint.color = ContextCompat.getColor(context, R.color.colorPrimary)
} else {
mPaint.color = Color.BLACK
}
canvas.drawRect(mRectangles[i], mPaint)
canvas.drawRect(mTiles[i], mPaint)

// border
// white border
mPaint.style = Paint.Style.STROKE
mPaint.strokeWidth = 10f
mPaint.color = Color.WHITE
canvas.drawRect(mRectangles[i], mPaint)
canvas.drawRect(mTiles[i], mPaint)
}
for (i in mBorders.indices) {
mPaint.style = Paint.Style.FILL
mPaint.color = ContextCompat.getColor(context, R.color.colorPrimaryDark)
canvas.drawRect(mBorders[i], mPaint)
}
}

@@ -82,7 +89,7 @@ class MusicTileView(
// Create an array to check for finger changes as multiple fingers may be on the
// same tile. This two-pass algorithm records the overall difference before changing
// the actual tiles.
val notesChangedBy = IntArray(mRectangles.size)
val notesChangedBy = IntArray(mTiles.size)
run {
val size = event.pointerCount
var i = 0
@@ -108,7 +115,7 @@ class MusicTileView(

// Now go through the rectangles to see if they have changed
var i = 0
while (i < mRectangles.size) {
while (i < mTiles.size) {
if (notesChangedBy[i] > 0) {
mIsPressedPerRectangle[i] = true
mTileListener.onTileOn(i)
4 changes: 2 additions & 2 deletions samples/SoundBoard/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#800000</color>
<color name="colorPrimaryDark">#300000</color>
<color name="colorPrimary">#700000</color>
<color name="colorPrimaryDark">#280000</color>
<color name="colorAccent">#D81B60</color>
</resources>
2 changes: 1 addition & 1 deletion samples/SoundBoard/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>