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