|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, MapTiler |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: BSD 3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +package com.maptiler.maptilersdk.map.style.layer.background |
| 8 | + |
| 9 | +import android.graphics.Color |
| 10 | +import com.maptiler.maptilersdk.map.style.layer.MTLayer |
| 11 | +import com.maptiler.maptilersdk.map.style.layer.MTLayerType |
| 12 | +import com.maptiler.maptilersdk.map.style.layer.MTLayerVisibility |
| 13 | +import com.maptiler.maptilersdk.map.style.layer.fill.ColorAsHexSerializer |
| 14 | +import kotlinx.serialization.EncodeDefault |
| 15 | +import kotlinx.serialization.SerialName |
| 16 | +import kotlinx.serialization.Serializable |
| 17 | +import kotlinx.serialization.Transient |
| 18 | + |
| 19 | +/** |
| 20 | + * The background style layer covers the entire map. Use it to configure a color or pattern |
| 21 | + * rendered beneath all other map content. If the background layer is transparent or omitted, |
| 22 | + * any area not covered by another layer is transparent. |
| 23 | + */ |
| 24 | +@Serializable |
| 25 | +class MTBackgroundLayer : MTLayer { |
| 26 | + /** |
| 27 | + * Unique layer identifier. |
| 28 | + */ |
| 29 | + @SerialName("id") |
| 30 | + override var identifier: String |
| 31 | + |
| 32 | + /** |
| 33 | + * Type of the layer. Always [MTLayerType.BACKGROUND]. |
| 34 | + */ |
| 35 | + @EncodeDefault(EncodeDefault.Mode.ALWAYS) |
| 36 | + override var type: MTLayerType = MTLayerType.BACKGROUND |
| 37 | + private set |
| 38 | + |
| 39 | + /** |
| 40 | + * Background layers do not have a source. Kept only to satisfy [MTLayer] contract |
| 41 | + * and excluded from serialization. |
| 42 | + */ |
| 43 | + @Transient |
| 44 | + override var sourceIdentifier: String = "" |
| 45 | + |
| 46 | + /** |
| 47 | + * Max zoom of the layer. |
| 48 | + */ |
| 49 | + @SerialName("maxzoom") |
| 50 | + override var maxZoom: Double? = null |
| 51 | + |
| 52 | + /** |
| 53 | + * Min zoom of the layer. |
| 54 | + */ |
| 55 | + @SerialName("minzoom") |
| 56 | + override var minZoom: Double? = null |
| 57 | + |
| 58 | + /** |
| 59 | + * Source layer is not applicable for background layers. Excluded from serialization. |
| 60 | + */ |
| 61 | + @Transient |
| 62 | + override var sourceLayer: String? = null |
| 63 | + |
| 64 | + /** |
| 65 | + * The color with which the background will be drawn. |
| 66 | + * Defaults to black. Disabled by [pattern]. |
| 67 | + */ |
| 68 | + @Serializable(with = ColorAsHexSerializer::class) |
| 69 | + var color: Int? |
| 70 | + get() = _paint.color ?: Color.BLACK |
| 71 | + set(value) { |
| 72 | + _paint.color = value |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * The opacity at which the background will be drawn. |
| 77 | + * Optional number between 0 and 1 inclusive. Defaults to 1. |
| 78 | + */ |
| 79 | + var opacity: Double? |
| 80 | + get() = _paint.opacity ?: 1.0 |
| 81 | + set(value) { |
| 82 | + _paint.opacity = value |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Name of image in sprite to use for drawing an image background. |
| 87 | + * For seamless patterns, image width and height must be a power of two. |
| 88 | + */ |
| 89 | + var pattern: String? |
| 90 | + get() = _paint.pattern |
| 91 | + set(value) { |
| 92 | + _paint.pattern = value |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Whether this layer is displayed. |
| 97 | + */ |
| 98 | + var visibility: MTLayerVisibility |
| 99 | + get() = MTLayerVisibility.from(_layout.visibility) ?: MTLayerVisibility.VISIBLE |
| 100 | + set(value) { |
| 101 | + _layout.visibility = value |
| 102 | + } |
| 103 | + |
| 104 | + @Suppress("PropertyName") |
| 105 | + @SerialName("layout") |
| 106 | + private var _layout: MTBackgroundLayout = MTBackgroundLayout() |
| 107 | + |
| 108 | + @Suppress("PropertyName") |
| 109 | + @SerialName("paint") |
| 110 | + private var _paint: MTBackgroundPaint = MTBackgroundPaint() |
| 111 | + |
| 112 | + constructor(identifier: String) { |
| 113 | + this.identifier = identifier |
| 114 | + this._layout = MTBackgroundLayout(visibility = visibility) |
| 115 | + this._paint = MTBackgroundPaint(color = color, opacity = opacity, pattern = pattern) |
| 116 | + } |
| 117 | + |
| 118 | + constructor( |
| 119 | + identifier: String, |
| 120 | + type: MTLayerType, |
| 121 | + maxZoom: Double?, |
| 122 | + minZoom: Double?, |
| 123 | + color: Int?, |
| 124 | + opacity: Double?, |
| 125 | + pattern: String?, |
| 126 | + visibility: MTLayerVisibility, |
| 127 | + ) { |
| 128 | + this.identifier = identifier |
| 129 | + this.type = type |
| 130 | + this.maxZoom = maxZoom |
| 131 | + this.minZoom = minZoom |
| 132 | + this.color = color |
| 133 | + this.opacity = opacity |
| 134 | + this.pattern = pattern |
| 135 | + this.visibility = visibility |
| 136 | + this._layout = MTBackgroundLayout(visibility = visibility) |
| 137 | + this._paint = MTBackgroundPaint(color = color, opacity = opacity, pattern = pattern) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +@Serializable |
| 142 | +internal data class MTBackgroundLayout( |
| 143 | + var visibility: MTLayerVisibility = MTLayerVisibility.VISIBLE, |
| 144 | +) |
| 145 | + |
| 146 | +@Serializable |
| 147 | +internal data class MTBackgroundPaint( |
| 148 | + @Serializable(with = ColorAsHexSerializer::class) |
| 149 | + @SerialName("background-color") |
| 150 | + var color: Int? = Color.BLACK, |
| 151 | + @SerialName("background-opacity") |
| 152 | + var opacity: Double? = 1.0, |
| 153 | + @SerialName("background-pattern") |
| 154 | + var pattern: String? = null, |
| 155 | +) |
0 commit comments