Compare commits

...

2 Commits

Author SHA1 Message Date
b48d6f3aca Add dynamic keyboard dimensions and integrate adjustable knob
Introduced adjustable `keyboardWidth` and `keyboardHeight` for `KeyboardComponent`, making dimensions dynamic. Added a knob component to control keyboard width in `MainView`. Enhanced layouts for flexibility and customization.
2025-06-06 19:25:50 +02:00
6b52e88cb7 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.
2025-06-05 21:38:33 +02:00
3 changed files with 35 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
package nl.astraeus.vst.ui.components package nl.astraeus.vst.ui.components
import kotlinx.html.classes import kotlinx.html.classes
import kotlinx.html.div
import kotlinx.html.js.onMouseDownFunction import kotlinx.html.js.onMouseDownFunction
import kotlinx.html.js.onMouseLeaveFunction import kotlinx.html.js.onMouseLeaveFunction
import kotlinx.html.js.onMouseMoveFunction import kotlinx.html.js.onMouseMoveFunction
@@ -87,7 +88,7 @@ open class BaseKnobComponent(
private fun getRadius() = min(getMiddleX(), getMiddleY() - 16) - 5 private fun getRadius() = min(getMiddleX(), getMiddleY() - 16) - 5
override fun HtmlBuilder.render() { override fun HtmlBuilder.render() {
span(KnobCls.name) { div(KnobCls.name) {
style = "width: ${width}px; height: ${height}px" style = "width: ${width}px; height: ${height}px"
knobElement = currentElement() knobElement = currentElement()

View File

@@ -29,6 +29,8 @@ import org.w3c.dom.events.MouseEvent
class KeyboardComponent( class KeyboardComponent(
val title: String = "Keyboard", val title: String = "Keyboard",
val octave: Int = 4, val octave: Int = 4,
val keyboardWidth: Int = 210,
val keyboardHeight: Int = keyboardWidth / 2,
val onNoteDown: (Int) -> Unit = {}, val onNoteDown: (Int) -> Unit = {},
val onNoteUp: (Int) -> Unit = {} val onNoteUp: (Int) -> Unit = {}
) : Komponent() { ) : Komponent() {
@@ -41,12 +43,9 @@ class KeyboardComponent(
private val blackKeys = listOf(61, 63, 66, 68, 70) private val blackKeys = listOf(61, 63, 66, 68, 70)
// Key dimensions // Key dimensions
private val keyboardWidth = 210 private val whiteKeyWidth = keyboardWidth / 7
private val keyboardHeight = 100 private val blackKeyWidth = (keyboardWidth / 9)
private val whiteKeyWidth = 30 private val blackKeyHeight = keyboardHeight * 60 / 100
private val whiteKeyHeight = 100
private val blackKeyWidth = 20
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,

View File

@@ -4,10 +4,12 @@ package nl.astraeus.vst.ui.view
import kotlinx.html.div import kotlinx.html.div
import kotlinx.html.h1 import kotlinx.html.h1
import kotlinx.html.hr
import kotlinx.html.js.onClickFunction import kotlinx.html.js.onClickFunction
import kotlinx.html.option import kotlinx.html.option
import kotlinx.html.select import kotlinx.html.select
import kotlinx.html.span import kotlinx.html.span
import kotlinx.html.style
import nl.astraeus.css.properties.AlignItems import nl.astraeus.css.properties.AlignItems
import nl.astraeus.css.properties.BoxSizing import nl.astraeus.css.properties.BoxSizing
import nl.astraeus.css.properties.Display import nl.astraeus.css.properties.Display
@@ -39,6 +41,7 @@ import nl.astraeus.vst.ui.css.hover
class MainView : Komponent() { class MainView : Komponent() {
private var messages: MutableList<String> = ArrayList() private var messages: MutableList<String> = ArrayList()
var started = true var started = true
var keyboardWidth = 500
fun addMessage(message: String) { fun addMessage(message: String) {
messages.add(message) messages.add(message)
@@ -113,7 +116,30 @@ class MainView : Komponent() {
} }
} }
div { div {
include(KeyboardComponent()) style = "height: 150px;"
include(
KnobComponent(
value = keyboardWidth.toDouble(),
label = "Keyboard",
minValue = 100.0,
maxValue = 2000.0,
step = 5.0,
width = 100,
height = 120,
) { value ->
keyboardWidth = value.toInt()
requestUpdate()
}
)
}
hr {}
div {
include(KeyboardComponent(
keyboardWidth = keyboardWidth,
keyboardHeight = keyboardWidth / 2,
onNoteDown = { println("Note down: $it") },
onNoteUp = { println("Note up: $it") },
))
} }
/* /*
div { div {