Skip to content

Commit

Permalink
Allowing initialise multiple instances of location component.
Browse files Browse the repository at this point in the history
  • Loading branch information
pengdev committed Oct 25, 2022
1 parent a453f5a commit 2ba1e74
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,35 @@ package com.mapbox.maps.plugin.locationcomponent

import com.mapbox.maps.plugin.LocationPuck2D
import com.mapbox.maps.plugin.LocationPuck3D
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_SOURCE

internal class LayerSourceProvider {
internal class LayerSourceProvider(private val locationComponentInitOptions: LocationComponentInitOptions) {

fun getModelSource(locationModelLayerOptions: LocationPuck3D): ModelSourceWrapper {
if (locationModelLayerOptions.modelUri.isEmpty()) {
throw IllegalArgumentException("Model Url must not be empty!")
}
return ModelSourceWrapper(
MODEL_SOURCE,
locationComponentInitOptions.puck3DSourceId,
locationModelLayerOptions.modelUri,
locationModelLayerOptions.position.map { it.toDouble() }
)
}

fun getModelLayer(locationModelLayerOptions: LocationPuck3D) =
ModelLayerWrapper(
MODEL_LAYER,
MODEL_SOURCE,
locationComponentInitOptions.puck3DLayerId,
locationComponentInitOptions.puck3DSourceId,
locationModelLayerOptions.modelScale.map { it.toDouble() },
locationModelLayerOptions.modelRotation.map { it.toDouble() },
locationModelLayerOptions.modelTranslation.map { it.toDouble() },
locationModelLayerOptions.modelOpacity.toDouble()
)

fun getLocationIndicatorLayer() = LocationIndicatorLayerWrapper(LOCATION_INDICATOR_LAYER)
fun getLocationIndicatorLayer() = LocationIndicatorLayerWrapper(locationComponentInitOptions.puck2DLayerId)

fun getLocationIndicatorLayerRenderer(puckOptions: LocationPuck2D) =
LocationIndicatorLayerRenderer(puckOptions, this)
LocationIndicatorLayerRenderer(locationComponentInitOptions, puckOptions, this)

fun getModelLayerRenderer(locationModelLayerOptions: LocationPuck3D) =
ModelLayerRenderer(this, locationModelLayerOptions)
ModelLayerRenderer(locationComponentInitOptions, this, locationModelLayerOptions)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package com.mapbox.maps.plugin.locationcomponent

import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.BEARING_ICON
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_SOURCE
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.SHADOW_ICON
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.TOP_ICON
import java.util.Objects
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.String
import kotlin.Unit
import kotlin.jvm.JvmSynthetic

/**
* Initialisation options for location component to allow multiple instances
* of LocationComponent.
*/
public class LocationComponentInitOptions private constructor(
public val puck2DLayerId: String,
public val puck3DLayerId: String,
public val puck3DSourceId: String,
public val topIconImageId: String,
public val shadowIconImageId: String,
public val bearingIconImageId: String
) {
public override fun toString() = "LocationComponentInitOptions(puck2DLayerId=$puck2DLayerId,puck3DLayerId=$puck3DLayerId, puck3DSourceId=$puck3DSourceId, topIconImageId=$topIconImageId,shadowIconImageId=$shadowIconImageId, bearingIconImageId=$bearingIconImageId)"

public override fun equals(other: Any?): Boolean = other is LocationComponentInitOptions
&& puck2DLayerId == other.puck2DLayerId
&& puck3DLayerId == other.puck3DLayerId
&& puck3DSourceId == other.puck3DSourceId
&& topIconImageId == other.topIconImageId
&& shadowIconImageId == other.shadowIconImageId
&& bearingIconImageId == other.bearingIconImageId

public override fun hashCode(): Int = Objects.hash(puck2DLayerId, puck3DLayerId, puck3DSourceId,
topIconImageId, shadowIconImageId, bearingIconImageId)

/**
* Composes and builds a [LocationComponentInitOptions] object.
*
* This is a concrete implementation of the builder design pattern.
*
* @property
*/
public class Builder {
@set:JvmSynthetic
public var puck2DLayerId: String = LOCATION_INDICATOR_LAYER

@set:JvmSynthetic
public var puck3DLayerId: String = MODEL_LAYER

@set:JvmSynthetic
public var puck3DSourceId: String = MODEL_SOURCE

@set:JvmSynthetic
public var topIconImageId: String = TOP_ICON

@set:JvmSynthetic
public var shadowIconImageId: String = SHADOW_ICON

@set:JvmSynthetic
public var bearingIconImageId: String = BEARING_ICON

/**
* Set puck2DLayerId
*
* @param puck2DLayerId puck2DLayerId
* @return Builder
*/
public fun setPuck2DLayerId(puck2DLayerId: String): Builder {
this.puck2DLayerId = puck2DLayerId
return this
}

/**
* Set puck3DLayerId
*
* @param puck3DLayerId puck3DLayerId
* @return Builder
*/
public fun setPuck3DLayerId(puck3DLayerId: String): Builder {
this.puck3DLayerId = puck3DLayerId
return this
}

/**
* Set puck3DSourceId
*
* @param puck3DSourceId puck3DSourceId
* @return Builder
*/
public fun setPuck3DSourceId(puck3DSourceId: String): Builder {
this.puck3DSourceId = puck3DSourceId
return this
}

/**
* Set topIconImageId
*
* @param topIconImageId topIconImageId
* @return Builder
*/
public fun setTopIconImageId(topIconImageId: String): Builder {
this.topIconImageId = topIconImageId
return this
}

/**
* Set shadowIconImageId
*
* @param shadowIconImageId shadowIconImageId
* @return Builder
*/
public fun setShadowIconImageId(shadowIconImageId: String): Builder {
this.shadowIconImageId = shadowIconImageId
return this
}

/**
* Set bearingIconImageId
*
* @param bearingIconImageId bearingIconImageId
* @return Builder
*/
public fun setBearingIconImageId(bearingIconImageId: String): Builder {
this.bearingIconImageId = bearingIconImageId
return this
}

/**
* Returns a [LocationComponentInitOptions] reference to the object being constructed by the
* builder.
*
* Throws an [IllegalArgumentException] when a non-null property wasn't initialised.
*
* @return LocationComponentInitOptions
*/
public fun build(): LocationComponentInitOptions {
return LocationComponentInitOptions(puck2DLayerId, puck3DLayerId, puck3DSourceId,
topIconImageId, shadowIconImageId, bearingIconImageId)
}
}
}

/**
* Creates a [LocationComponentInitOptions] through a DSL-style builder.
*
* @param initializer the intialisation block
* @return LocationComponentInitOptions
*/
@JvmSynthetic
public fun LocationComponentInitOptions(initializer: LocationComponentInitOptions.Builder.() -> Unit):
LocationComponentInitOptions = LocationComponentInitOptions.Builder().apply(initializer).build()
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import com.mapbox.maps.RenderedQueryGeometry
import com.mapbox.maps.RenderedQueryOptions
import com.mapbox.maps.extension.style.StyleInterface
import com.mapbox.maps.plugin.delegates.MapDelegateProvider
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER
import com.mapbox.maps.plugin.locationcomponent.animators.PuckAnimatorManager
import com.mapbox.maps.plugin.locationcomponent.generated.*
import com.mapbox.maps.plugin.locationcomponent.generated.LocationComponentAttributeParser
Expand All @@ -23,7 +21,10 @@ import java.util.concurrent.CopyOnWriteArraySet
* Default implementation of the LocationComponentPlugin, it renders the configured location puck
* to the user's current location.
*/
class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2,
class LocationComponentPluginImpl(
val locationComponentInitOptions: LocationComponentInitOptions = LocationComponentInitOptions.Builder()
.build()
) : LocationComponentPlugin2, LocationConsumer2,
LocationComponentSettingsBase2() {
private lateinit var delegateProvider: MapDelegateProvider

Expand Down Expand Up @@ -119,8 +120,8 @@ class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2,
RenderedQueryGeometry(delegateProvider.mapCameraManagerDelegate.pixelForCoordinate(point)),
RenderedQueryOptions(
listOf(
LOCATION_INDICATOR_LAYER,
MODEL_LAYER
locationComponentInitOptions.puck2DLayerId,
locationComponentInitOptions.puck3DLayerId
),
null
)
Expand Down Expand Up @@ -195,7 +196,7 @@ class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2,
internalSettings.layerAbove,
internalSettings.layerBelow
),
layerSourceProvider = LayerSourceProvider(),
layerSourceProvider = LayerSourceProvider(locationComponentInitOptions),
animationManager = PuckAnimatorManager(
indicatorPositionChangedListener,
indicatorBearingChangedListener,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import com.mapbox.bindgen.Value
import com.mapbox.geojson.Point
import com.mapbox.maps.extension.style.StyleInterface
import com.mapbox.maps.plugin.LocationPuck2D
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.BEARING_ICON
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.LOCATION_INDICATOR_LAYER
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.SHADOW_ICON
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.TOP_ICON
import com.mapbox.maps.plugin.locationcomponent.utils.BitmapUtils
import java.text.DecimalFormat
import java.text.NumberFormat
import java.util.Locale

internal class LocationIndicatorLayerRenderer(
private val locationComponentInitOptions: LocationComponentInitOptions,
private val puckOptions: LocationPuck2D,
layerSourceProvider: LayerSourceProvider
) : LocationLayerRenderer {
Expand All @@ -28,7 +25,7 @@ internal class LocationIndicatorLayerRenderer(
}

override fun isRendererInitialised(): Boolean {
return style?.styleLayerExists(LOCATION_INDICATOR_LAYER) ?: false
return style?.styleLayerExists(locationComponentInitOptions.puck2DLayerId) ?: false
}

override fun addLayers(positionManager: LocationComponentPositionManager) {
Expand Down Expand Up @@ -76,22 +73,22 @@ internal class LocationIndicatorLayerRenderer(

private fun setupBitmaps() {
puckOptions.topImage?.let { BitmapUtils.getBitmapFromDrawable(it) }
?.let { style?.addImage(TOP_ICON, it) }
?.let { style?.addImage(locationComponentInitOptions.topIconImageId, it) }
puckOptions.bearingImage?.let { BitmapUtils.getBitmapFromDrawable(it) }
?.let { style?.addImage(BEARING_ICON, it) }
?.let { style?.addImage(locationComponentInitOptions.bearingIconImageId, it) }

puckOptions.shadowImage?.let { BitmapUtils.getBitmapFromDrawable(it) }
?.let { style?.addImage(SHADOW_ICON, it) }
layer.topImage(TOP_ICON)
layer.bearingImage(BEARING_ICON)
layer.shadowImage(SHADOW_ICON)
?.let { style?.addImage(locationComponentInitOptions.shadowIconImageId, it) }
layer.topImage(locationComponentInitOptions.topIconImageId)
layer.bearingImage(locationComponentInitOptions.bearingIconImageId)
layer.shadowImage(locationComponentInitOptions.shadowIconImageId)
layer.opacity(puckOptions.opacity.toDouble())
}

override fun clearBitmaps() {
style?.removeStyleImage(TOP_ICON)
style?.removeStyleImage(BEARING_ICON)
style?.removeStyleImage(SHADOW_ICON)
style?.removeStyleImage(locationComponentInitOptions.topIconImageId)
style?.removeStyleImage(locationComponentInitOptions.bearingIconImageId)
style?.removeStyleImage(locationComponentInitOptions.shadowIconImageId)
}

override fun updateStyle(style: StyleInterface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import com.mapbox.bindgen.Value
import com.mapbox.geojson.Point
import com.mapbox.maps.extension.style.StyleInterface
import com.mapbox.maps.plugin.LocationPuck3D
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_LAYER
import com.mapbox.maps.plugin.locationcomponent.LocationComponentConstants.MODEL_SOURCE

internal class ModelLayerRenderer(
private val locationComponentInitOptions: LocationComponentInitOptions,
layerSourceProvider: LayerSourceProvider,
private val locationModelLayerOptions: LocationPuck3D
) : LocationLayerRenderer {
Expand All @@ -32,11 +31,11 @@ internal class ModelLayerRenderer(
}

private fun isLayerInitialised(): Boolean {
return style?.styleLayerExists(MODEL_LAYER) ?: false
return style?.styleLayerExists(locationComponentInitOptions.puck3DLayerId) ?: false
}

private fun isSourceInitialised(): Boolean {
return style?.styleSourceExists(MODEL_SOURCE) ?: false
return style?.styleSourceExists(locationComponentInitOptions.puck3DSourceId) ?: false
}

override fun addLayers(positionManager: LocationComponentPositionManager) {
Expand Down

0 comments on commit 2ba1e74

Please sign in to comment.