From 9a76f778a8484fcee97aef5be41fe5e75a821a63 Mon Sep 17 00:00:00 2001 From: Thomas Nelson Date: Sun, 21 Apr 2024 17:40:58 -0500 Subject: [PATCH] Dedup some common patterns in Main.kt Still thinking about how I want to refactor this... --- src/jsMain/kotlin/Main.kt | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/jsMain/kotlin/Main.kt b/src/jsMain/kotlin/Main.kt index 2f22abd..e29a23a 100644 --- a/src/jsMain/kotlin/Main.kt +++ b/src/jsMain/kotlin/Main.kt @@ -19,27 +19,32 @@ class WebGLWrapper { val webgl: GL = canvas.getContext("webgl") as GL val shaderProgram: WebGLProgram = webgl.createProgram() ?: throw IllegalStateException("Could not initialize shader program") + + private fun getInputElem(s: String): HTMLInputElement = document.getElementById(s) as HTMLInputElement + private class HTMLInputProperty(val input: HTMLInputElement): ReadOnlyProperty { override fun getValue(thisRef: WebGLWrapper, property: KProperty<*>): Double = input.valueAsNumber } - val scaleInput = document.getElementById("scaleInput") as HTMLInputElement - val scaleFactor by HTMLInputProperty(scaleInput) + private fun HTMLInputProperty(s: String): HTMLInputProperty { + val elem = getInputElem(s) + return HTMLInputProperty(elem) + } - val lightPosXInput = document.getElementById("lightPosX") as HTMLInputElement - val lightPosYInput = document.getElementById("lightPosY") as HTMLInputElement - val lightPosZInput = document.getElementById("lightPosZ") as HTMLInputElement + val scaleFactor by HTMLInputProperty("scaleInput") + + val lightPosXInput = getInputElem("lightPosX") + val lightPosYInput = getInputElem("lightPosY") + val lightPosZInput = getInputElem("lightPosZ") val lightPos = arrayOf( lightPosXInput.valueAsNumber.toFloat(), lightPosYInput.valueAsNumber.toFloat(), lightPosZInput.valueAsNumber.toFloat() ) - val shininessInput = document.getElementById("shininessInput") as HTMLInputElement - val shininess by HTMLInputProperty(shininessInput) + val shininess by HTMLInputProperty("shininessInput") - val rotationSpeedInput = document.getElementById("rotationSpeedInput") as HTMLInputElement - val rotationSpeed by HTMLInputProperty(rotationSpeedInput) + val rotationSpeed by HTMLInputProperty("rotationSpeedInput") init { webgl.enable(GL.DEPTH_TEST) @@ -147,6 +152,7 @@ class WebGLWrapper { } var rotation = 0.0 + fun draw() { setupUniformVec3Float(lightPos, "uLightPos")