Skip to content

Commit

Permalink
use Length in fill extrusion base and height expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
sargunv committed Jan 8, 2025
1 parent 1e0f74c commit 177f4d0
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 13 deletions.
5 changes: 4 additions & 1 deletion lib/maplibre-compose-expressions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ kotlin {
wasmJs { browser() }

sourceSets {
commonMain.dependencies { implementation(compose.foundation) }
commonMain.dependencies {
implementation(compose.foundation)
api(libs.alchemist)
}

commonTest.dependencies {
implementation(kotlin("test"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.sargunv.maplibrecompose.expressions.ast

import dev.sargunv.maplibrecompose.expressions.ExpressionContext
import dev.sargunv.maplibrecompose.expressions.value.MetersValue
import io.github.kevincianfarini.alchemist.scalar.toLength
import io.github.kevincianfarini.alchemist.type.Length
import io.github.kevincianfarini.alchemist.unit.LengthUnit.International.Meter

/** A [Literal] representing a [Length] value. */
public data class MetersLiteral private constructor(override val value: Length) :
Literal<MetersValue, Length> {

override fun compile(context: ExpressionContext): CompiledLiteral<MetersValue, Float> =
FloatLiteral.of(value.toDouble(Meter).toFloat()).cast()

override fun visit(block: (Expression<*>) -> Unit): Unit = block(this)

public companion object {
private val cache = FloatCache { MetersLiteral(it.toDouble().toLength(Meter)) }

public fun of(value: Length): MetersLiteral = cache[value.toDouble(Meter).toFloat()]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import dev.sargunv.maplibrecompose.expressions.ast.FloatLiteral
import dev.sargunv.maplibrecompose.expressions.ast.IntLiteral
import dev.sargunv.maplibrecompose.expressions.ast.ListLiteral
import dev.sargunv.maplibrecompose.expressions.ast.Literal
import dev.sargunv.maplibrecompose.expressions.ast.MetersLiteral
import dev.sargunv.maplibrecompose.expressions.ast.MillisecondsLiteral
import dev.sargunv.maplibrecompose.expressions.ast.NullLiteral
import dev.sargunv.maplibrecompose.expressions.ast.OffsetLiteral
Expand All @@ -31,6 +32,7 @@ import dev.sargunv.maplibrecompose.expressions.value.SymbolAnchor
import dev.sargunv.maplibrecompose.expressions.value.TextUnitOffsetValue
import dev.sargunv.maplibrecompose.expressions.value.TextVariableAnchorOffsetValue
import dev.sargunv.maplibrecompose.expressions.value.VectorValue
import io.github.kevincianfarini.alchemist.type.Length
import kotlin.jvm.JvmName
import kotlin.time.Duration

Expand Down Expand Up @@ -62,6 +64,13 @@ public fun const(textUnit: TextUnit): TextUnitCalculation = TextUnitCalculation.
*/
public fun const(duration: Duration): MillisecondsLiteral = MillisecondsLiteral.of(duration)

/**
* Creates a literal expression for a [Length] value.
*
* The length will be represented in meters.
*/
public fun const(duration: Length): MetersLiteral = MetersLiteral.of(duration)

/** Creates a literal expression for a [Boolean] value. */
public fun const(bool: Boolean): BooleanLiteral = BooleanLiteral.of(bool)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import dev.sargunv.maplibrecompose.expressions.value.FloatValue
import dev.sargunv.maplibrecompose.expressions.value.IntValue
import dev.sargunv.maplibrecompose.expressions.value.ListValue
import dev.sargunv.maplibrecompose.expressions.value.MapValue
import dev.sargunv.maplibrecompose.expressions.value.MetersValue
import dev.sargunv.maplibrecompose.expressions.value.MillisecondsValue
import dev.sargunv.maplibrecompose.expressions.value.NumberValue
import dev.sargunv.maplibrecompose.expressions.value.StringValue
Expand Down Expand Up @@ -283,6 +284,10 @@ public val Expression<FloatValue>.milliseconds: Expression<MillisecondsValue>
public val Expression<FloatValue>.seconds: Expression<MillisecondsValue>
get() = (this * const(1000f)).cast()

/** Converts a numeric [Expression] in meters to a [MetersValue] expression. */
public val Expression<FloatValue>.meters: Expression<MetersValue>
get() = this.cast()

/** Converts a numeric [Expression] to an [TextUnitValue] expression in SP. */
public val Expression<FloatValue>.sp: Expression<TextUnitValue>
get() = TextUnitCalculation.of(this, TextUnitType.Sp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import dev.sargunv.maplibrecompose.expressions.dsl.format
import dev.sargunv.maplibrecompose.expressions.dsl.image
import dev.sargunv.maplibrecompose.expressions.dsl.linear
import dev.sargunv.maplibrecompose.expressions.dsl.offset
import io.github.kevincianfarini.alchemist.type.Length
import kotlin.time.Duration

/**
Expand Down Expand Up @@ -67,6 +68,9 @@ public typealias TextUnitValue = NumberValue<TextUnit>
*/
public typealias MillisecondsValue = NumberValue<Duration>

/** Represents an [ExpressionValue] that resolves to a [Length] in meters. See [const]. */
public typealias MetersValue = NumberValue<Length>

/** Represents an [ExpressionValue] that resolves to a string value. See [const]. */
public sealed interface StringValue :
ExpressionValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dev.sargunv.maplibrecompose.expressions.value.ColorValue
import dev.sargunv.maplibrecompose.expressions.value.DpOffsetValue
import dev.sargunv.maplibrecompose.expressions.value.FloatValue
import dev.sargunv.maplibrecompose.expressions.value.ImageValue
import dev.sargunv.maplibrecompose.expressions.value.MetersValue
import dev.sargunv.maplibrecompose.expressions.value.TranslateAnchor
import org.maplibre.android.style.expressions.Expression as MLNExpression
import org.maplibre.android.style.layers.FillExtrusionLayer as MLNFillExtrusionLayer
Expand Down Expand Up @@ -43,11 +44,11 @@ internal actual class FillExtrusionLayer actual constructor(id: String, source:
impl.setProperties(PropertyFactory.fillExtrusionPattern(pattern.toMLNExpression()))
}

actual fun setFillExtrusionHeight(height: CompiledExpression<FloatValue>) {
actual fun setFillExtrusionHeight(height: CompiledExpression<MetersValue>) {
impl.setProperties(PropertyFactory.fillExtrusionHeight(height.toMLNExpression()))
}

actual fun setFillExtrusionBase(base: CompiledExpression<FloatValue>) {
actual fun setFillExtrusionBase(base: CompiledExpression<MetersValue>) {
impl.setProperties(PropertyFactory.fillExtrusionBase(base.toMLNExpression()))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import dev.sargunv.maplibrecompose.expressions.value.ColorValue
import dev.sargunv.maplibrecompose.expressions.value.DpOffsetValue
import dev.sargunv.maplibrecompose.expressions.value.FloatValue
import dev.sargunv.maplibrecompose.expressions.value.ImageValue
import dev.sargunv.maplibrecompose.expressions.value.MetersValue
import dev.sargunv.maplibrecompose.expressions.value.TranslateAnchor
import io.github.kevincianfarini.alchemist.scalar.toLength
import io.github.kevincianfarini.alchemist.unit.LengthUnit.International.Meter

/**
* A fill extrusion layer draws polygons from the [sourceLayer] in the given [source] in the given
Expand Down Expand Up @@ -73,8 +76,8 @@ public fun FillExtrusionLayer(
opacity: Expression<FloatValue> = const(1f),
color: Expression<ColorValue> = const(Color.Black),
pattern: Expression<ImageValue> = nil(),
height: Expression<FloatValue> = const(0f),
base: Expression<FloatValue> = const(0f),
height: Expression<MetersValue> = const(0.toLength(Meter)),
base: Expression<MetersValue> = const(0.toLength(Meter)),
verticalGradient: Expression<BooleanValue> = const(true),
onClick: FeaturesClickHandler? = null,
onLongClick: FeaturesClickHandler? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.sargunv.maplibrecompose.expressions.value.ColorValue
import dev.sargunv.maplibrecompose.expressions.value.DpOffsetValue
import dev.sargunv.maplibrecompose.expressions.value.FloatValue
import dev.sargunv.maplibrecompose.expressions.value.ImageValue
import dev.sargunv.maplibrecompose.expressions.value.MetersValue
import dev.sargunv.maplibrecompose.expressions.value.TranslateAnchor

internal expect class FillExtrusionLayer(id: String, source: Source) : FeatureLayer {
Expand All @@ -24,9 +25,9 @@ internal expect class FillExtrusionLayer(id: String, source: Source) : FeatureLa

fun setFillExtrusionPattern(pattern: CompiledExpression<ImageValue>)

fun setFillExtrusionHeight(height: CompiledExpression<FloatValue>)
fun setFillExtrusionHeight(height: CompiledExpression<MetersValue>)

fun setFillExtrusionBase(base: CompiledExpression<FloatValue>)
fun setFillExtrusionBase(base: CompiledExpression<MetersValue>)

fun setFillExtrusionVerticalGradient(verticalGradient: CompiledExpression<BooleanValue>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.sargunv.maplibrecompose.expressions.value.ColorValue
import dev.sargunv.maplibrecompose.expressions.value.DpOffsetValue
import dev.sargunv.maplibrecompose.expressions.value.FloatValue
import dev.sargunv.maplibrecompose.expressions.value.ImageValue
import dev.sargunv.maplibrecompose.expressions.value.MetersValue
import dev.sargunv.maplibrecompose.expressions.value.TranslateAnchor

internal actual class FillExtrusionLayer actual constructor(id: String, source: Source) :
Expand Down Expand Up @@ -39,11 +40,11 @@ internal actual class FillExtrusionLayer actual constructor(id: String, source:
TODO()
}

actual fun setFillExtrusionHeight(height: CompiledExpression<FloatValue>) {
actual fun setFillExtrusionHeight(height: CompiledExpression<MetersValue>) {
TODO()
}

actual fun setFillExtrusionBase(base: CompiledExpression<FloatValue>) {
actual fun setFillExtrusionBase(base: CompiledExpression<MetersValue>) {
TODO()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dev.sargunv.maplibrecompose.expressions.value.ColorValue
import dev.sargunv.maplibrecompose.expressions.value.DpOffsetValue
import dev.sargunv.maplibrecompose.expressions.value.FloatValue
import dev.sargunv.maplibrecompose.expressions.value.ImageValue
import dev.sargunv.maplibrecompose.expressions.value.MetersValue
import dev.sargunv.maplibrecompose.expressions.value.TranslateAnchor

internal actual class FillExtrusionLayer actual constructor(id: String, source: Source) :
Expand Down Expand Up @@ -49,11 +50,11 @@ internal actual class FillExtrusionLayer actual constructor(id: String, source:
if (pattern != NullLiteral) impl.fillExtrusionPattern = pattern.toNSExpression()
}

actual fun setFillExtrusionHeight(height: CompiledExpression<FloatValue>) {
actual fun setFillExtrusionHeight(height: CompiledExpression<MetersValue>) {
impl.fillExtrusionHeight = height.toNSExpression()
}

actual fun setFillExtrusionBase(base: CompiledExpression<FloatValue>) {
actual fun setFillExtrusionBase(base: CompiledExpression<MetersValue>) {
impl.fillExtrusionBase = base.toNSExpression()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.sargunv.maplibrecompose.expressions.value.ColorValue
import dev.sargunv.maplibrecompose.expressions.value.DpOffsetValue
import dev.sargunv.maplibrecompose.expressions.value.FloatValue
import dev.sargunv.maplibrecompose.expressions.value.ImageValue
import dev.sargunv.maplibrecompose.expressions.value.MetersValue
import dev.sargunv.maplibrecompose.expressions.value.TranslateAnchor

internal actual class FillExtrusionLayer actual constructor(id: String, source: Source) :
Expand Down Expand Up @@ -39,11 +40,11 @@ internal actual class FillExtrusionLayer actual constructor(id: String, source:
TODO()
}

actual fun setFillExtrusionHeight(height: CompiledExpression<FloatValue>) {
actual fun setFillExtrusionHeight(height: CompiledExpression<MetersValue>) {
TODO()
}

actual fun setFillExtrusionBase(base: CompiledExpression<FloatValue>) {
actual fun setFillExtrusionBase(base: CompiledExpression<MetersValue>) {
TODO()
}

Expand Down

0 comments on commit 177f4d0

Please sign in to comment.