Refactor KeyboardComponent dimensions and remove redundant code

Updated key size calculations to be dynamic based on keyboard dimensions for improved scalability. Removed unnecessary `println` calls and redundant MIDI note calculations in drawing logic for better performance and code clarity.
This commit is contained in:
2025-06-05 21:38:33 +02:00
parent 63d6c01fca
commit 6b52e88cb7

View File

@@ -43,10 +43,9 @@ class KeyboardComponent(
// Key dimensions // Key dimensions
private val keyboardWidth = 210 private val keyboardWidth = 210
private val keyboardHeight = 100 private val keyboardHeight = 100
private val whiteKeyWidth = 30 private val whiteKeyWidth = keyboardWidth / 7
private val whiteKeyHeight = 100 private val blackKeyWidth = whiteKeyWidth * 3 / 2
private val blackKeyWidth = 20 private val blackKeyHeight = keyboardHeight * 60 / 100
private val blackKeyHeight = 60
// Calculate positions for black keys // Calculate positions for black keys
private val blackKeyPositions = listOf( private val blackKeyPositions = listOf(
@@ -58,13 +57,11 @@ class KeyboardComponent(
) )
fun noteDown(midiNote: Int) { fun noteDown(midiNote: Int) {
println("noteDown $midiNote")
pressedNotes.add(midiNote) pressedNotes.add(midiNote)
onNoteDown(midiNote) onNoteDown(midiNote)
} }
fun noteUp(midiNote: Int) { fun noteUp(midiNote: Int) {
println("noteUp $midiNote")
pressedNotes.remove(midiNote) pressedNotes.remove(midiNote)
onNoteUp(midiNote) onNoteUp(midiNote)
} }
@@ -182,12 +179,11 @@ class KeyboardComponent(
// Draw white keys // Draw white keys
for (i in 0 until 7) { for (i in 0 until 7) {
val midiNote = whiteKeys[i] + (octave - 4) * 12
rect( rect(
i * whiteKeyWidth, i * whiteKeyWidth,
0, 0,
whiteKeyWidth, whiteKeyWidth,
whiteKeyHeight, keyboardHeight,
0, 0,
WhiteKeyCls.name WhiteKeyCls.name
) )
@@ -195,7 +191,6 @@ class KeyboardComponent(
// Draw black keys // Draw black keys
for (i in 0 until 5) { for (i in 0 until 5) {
val midiNote = blackKeys[i] + (octave - 4) * 12
rect( rect(
blackKeyPositions[i], blackKeyPositions[i],
0, 0,