-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create MapScale quantity with arithmetic to/from Length and Dp
- Loading branch information
Showing
13 changed files
with
101 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
lib/maplibre-compose/src/commonMain/kotlin/dev/sargunv/maplibrecompose/core/util/MapScale.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package dev.sargunv.maplibrecompose.core.util | ||
|
||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.unit.dp | ||
import io.github.kevincianfarini.alchemist.scalar.toLength | ||
import io.github.kevincianfarini.alchemist.type.Length | ||
import io.github.kevincianfarini.alchemist.unit.LengthUnit.International.Nanometer | ||
import kotlin.jvm.JvmInline | ||
import kotlin.math.roundToLong | ||
|
||
public inline operator fun Length.div(dp: Dp): MapScale = | ||
MapScale((this.toLong(Nanometer) / dp.value).roundToLong().toLength(Nanometer)) | ||
|
||
public inline operator fun Length.div(scale: MapScale): Dp = (this / scale.lengthPerDp).dp | ||
|
||
public inline operator fun Dp.times(scale: MapScale): Length = scale * this | ||
|
||
public inline operator fun Number.times(scale: MapScale): MapScale = scale * this | ||
|
||
/** | ||
* Represents a map scale: a ratio of real-world distance ([Length]) to device-independent pixels | ||
* ([Dp]). | ||
*/ | ||
@JvmInline | ||
public value class MapScale | ||
@PublishedApi | ||
internal constructor(@PublishedApi internal val lengthPerDp: Length) : Comparable<MapScale> { | ||
|
||
public operator fun times(dp: Dp): Length = | ||
(lengthPerDp.toLong(Nanometer) * dp.value).roundToLong().toLength(Nanometer) | ||
|
||
public operator fun div(other: MapScale): Double { | ||
return lengthPerDp / other.lengthPerDp | ||
} | ||
|
||
public operator fun div(other: Number): MapScale = | ||
MapScale((lengthPerDp.toLong(Nanometer) / other.toDouble()).roundToLong().toLength(Nanometer)) | ||
|
||
public operator fun unaryMinus(): MapScale = MapScale(-lengthPerDp) | ||
|
||
public operator fun times(other: Number): MapScale = | ||
MapScale((lengthPerDp.toLong(Nanometer) * other.toDouble()).roundToLong().toLength(Nanometer)) | ||
|
||
override fun toString(): String { | ||
return "$lengthPerDp/dp" | ||
} | ||
|
||
override fun compareTo(other: MapScale): Int = lengthPerDp.compareTo(other.lengthPerDp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.