use std::marker::PhantomData;
use std::sync::Arc;
use mln_style::bitmap::PremultipliedImage;
use mln_style::layer::{SourceKind, StyleLayerType};
use crate::draw::identity::SimpleIdentity;
use crate::draw::tile_render_data::TileAtlasTextures;
use crate::layer::bucket::Bucket;
use crate::layer::bucket_geometry::BucketGeometry;
use crate::layer::layer_type::EvaluatedLayer;
use crate::layer::layout::Layout;
use crate::layer::render_layer::Layer;
use crate::query::feature_index::FeatureIndex;
use crate::render::tile::{TileContent, ViewPlace};
use crate::source::render_image_source::{ImageQuad, ImageTile};
use crate::source::{DemTiles, RasterTiles};
use crate::text::atlas::ImagePosition;
use crate::tile_store::contents::GeometryContents;
use crate::tile_store::dem::DemContents;
use crate::tile_store::raster::RasterContents;
#[expect(private_bounds, reason = "the engine's tile kinds are the only ones: `input::Find` seals the trait")]
pub trait TileInput<T: StyleLayerType>: input::Find<T> + 'static {
type Data<'a>: Copy;
}
pub type TileData<'a, L> = <<L as Layer>::Input as TileInput<<L as Layer>::Style>>::Data<'a>;
pub struct Features<L>(PhantomData<fn() -> L>);
impl<T: StyleLayerType, L: Layout<T>> TileInput<T> for Features<L> {
type Data<'a> = LayerBucket<'a, T, L::Bucket>;
}
pub enum NoTiles {}
impl<T: StyleLayerType> TileInput<T> for NoTiles {
type Data<'a> = std::convert::Infallible;
}
impl<T: StyleLayerType> TileInput<T> for RasterTiles {
type Data<'a> = RasterTile<'a>;
}
impl<T: StyleLayerType> TileInput<T> for DemTiles {
type Data<'a> = &'a DemContents;
}
pub struct LayerBucket<'a, T: StyleLayerType, B> {
pub bucket: &'a B,
pub(crate) id: SimpleIdentity,
pub bucket_index: usize,
pub layer: &'a EvaluatedLayer<T>,
contents: &'a GeometryContents,
}
impl<T: StyleLayerType, B> Clone for LayerBucket<'_, T, B> {
fn clone(&self) -> Self {
*self
}
}
impl<T: StyleLayerType, B> Copy for LayerBucket<'_, T, B> {}
impl<'a, T: StyleLayerType, B: Bucket> LayerBucket<'a, T, B> {
pub(crate) fn laid(contents: &'a GeometryContents, index: usize) -> Option<Self> {
let laid = contents.result.layer_render_data.laid(index)?;
let layer: &dyn std::any::Any = laid.layer;
Some(LayerBucket {
bucket: laid.bucket.downcast_ref::<B>()?,
id: laid.bucket.id(),
bucket_index: laid.bucket_index,
layer: layer.downcast_ref()?,
contents,
})
}
}
impl<'a, T: StyleLayerType, B> LayerBucket<'a, T, B> {
#[must_use]
pub fn geometry(self) -> &'a BucketGeometry {
&self.contents.bucket_geometry
}
#[must_use]
pub fn feature_index(self) -> &'a Arc<FeatureIndex> {
&self.contents.result.feature_index
}
#[must_use]
pub fn pattern(self, pattern: &str) -> Option<ImagePosition> {
self.contents.pattern_positions()?.get(pattern).cloned()
}
#[must_use]
pub fn atlas_textures(self) -> &'a TileAtlasTextures {
&self.contents.textures
}
}
#[derive(Clone, Copy)]
pub enum RasterTile<'a> {
Tile(&'a RasterContents),
Image(&'a ImageTile),
}
impl<'a> RasterTile<'a> {
#[must_use]
pub fn id(self) -> SimpleIdentity {
match self {
RasterTile::Tile(raster) => raster.id(),
RasterTile::Image(image) => image.id(),
}
}
#[must_use]
pub fn image(self) -> &'a PremultipliedImage {
match self {
RasterTile::Tile(raster) => &raster.image,
RasterTile::Image(image) => &image.image,
}
}
#[must_use]
pub fn quad(self) -> ImageQuad {
match self {
RasterTile::Tile(_) => ImageQuad::TILE,
RasterTile::Image(image) => image.quad,
}
}
}
pub(crate) mod input {
use std::sync::Arc;
use mln_style::error::Error;
use mln_wire::Reader;
use super::{
DemTiles, Features, LayerBucket, NoTiles, RasterTile, RasterTiles, SourceKind, StyleLayerType, TileContent,
TileInput, ViewPlace,
};
use crate::layer::bucket::{ErasedBucket, Stored};
use crate::layer::layer_drawing::MadeFrom;
use crate::layer::layer_type::{self, ErasedEvaluatedLayer, ErasedLayout};
use crate::layer::layout::{Layout, LayoutParameters, SourceLayer};
use crate::render::pass::RenderPass;
use crate::source::GeometryTiles;
use crate::source::kind::Deliver;
use crate::tile_store::contents::{Contents, GeometryContents};
pub(crate) trait Find<T: StyleLayerType> {
const KIND: SourceKind;
fn needs_resources(_group: &[Arc<dyn ErasedEvaluatedLayer>]) -> bool {
false
}
fn create_layout(
_group: &[Arc<dyn ErasedEvaluatedLayer>],
_parameters: LayoutParameters<'_>,
_source_layer: SourceLayer,
) -> Result<Option<Box<dyn ErasedLayout>>, Error> {
Ok(None)
}
fn read_bucket(r: &mut Reader<'_>) -> Option<Box<dyn ErasedBucket>> {
r.fail("a bucket of a layer type that doesn't lay out")
}
fn laid<'a>(_contents: &'a GeometryContents, _layer_id: &str) -> Option<<Self as TileInput<T>>::Data<'a>>
where
Self: TileInput<T>,
{
None
}
fn find<'a>(content: TileContent<'a>, layer_id: &str) -> Option<(<Self as TileInput<T>>::Data<'a>, ViewPlace)>
where
Self: TileInput<T>;
fn at(content: TileContent<'_>, place: ViewPlace) -> Option<<Self as TileInput<T>>::Data<'_>>
where
Self: TileInput<T>;
fn made_from(data: <Self as TileInput<T>>::Data<'_>) -> MadeFrom
where
Self: TileInput<T>;
fn is_for(_data: <Self as TileInput<T>>::Data<'_>, _layer_id: &str) -> bool
where
Self: TileInput<T>,
{
true
}
fn passes(_data: <Self as TileInput<T>>::Data<'_>) -> RenderPass
where
Self: TileInput<T>,
{
RenderPass::None
}
}
impl<T: StyleLayerType, L: Layout<T>> Find<T> for Features<L> {
const KIND: SourceKind = <GeometryTiles as Deliver>::KIND;
fn needs_resources(group: &[Arc<dyn ErasedEvaluatedLayer>]) -> bool {
layer_type::needs_resources::<T, L>(group)
}
fn create_layout(
group: &[Arc<dyn ErasedEvaluatedLayer>],
parameters: LayoutParameters<'_>,
source_layer: SourceLayer,
) -> Result<Option<Box<dyn ErasedLayout>>, Error> {
layer_type::create_layout::<T, L>(group, parameters, source_layer).map(Some)
}
fn read_bucket(r: &mut Reader<'_>) -> Option<Box<dyn ErasedBucket>> {
Some(Box::new(Stored::new(<L::Bucket as mln_wire::Wire>::read(r)?)))
}
fn laid<'a>(contents: &'a GeometryContents, layer_id: &str) -> Option<<Self as TileInput<T>>::Data<'a>> {
LayerBucket::laid(contents, contents.result.layer_render_data.find(layer_id)?)
}
fn find<'a>(content: TileContent<'a>, layer_id: &str) -> Option<(<Self as TileInput<T>>::Data<'a>, ViewPlace)> {
let TileContent::Tile(contents) = content else { return None };
let contents = contents.geometry()?;
let index = contents.result.layer_render_data.find(layer_id)?;
Some((LayerBucket::laid(contents, index)?, ViewPlace::Laid(index)))
}
fn at(content: TileContent<'_>, place: ViewPlace) -> Option<<Self as TileInput<T>>::Data<'_>> {
let ViewPlace::Laid(index) = place else {
debug_assert!(false, "a tile's own contents' place for a laid-out layer");
return None;
};
let TileContent::Tile(contents) = content else { return None };
LayerBucket::laid(contents.geometry()?, index)
}
fn made_from(data: <Self as TileInput<T>>::Data<'_>) -> MadeFrom {
(data.id, [0, 0])
}
fn is_for(data: <Self as TileInput<T>>::Data<'_>, layer_id: &str) -> bool {
data.layer.style.id == layer_id
}
fn passes(data: <Self as TileInput<T>>::Data<'_>) -> RenderPass {
RenderPass(data.layer.render_passes)
}
}
impl<T: StyleLayerType> Find<T> for RasterTiles {
const KIND: SourceKind = <RasterTiles as Deliver>::KIND;
fn find<'a>(
content: TileContent<'a>,
_layer_id: &str,
) -> Option<(<Self as TileInput<T>>::Data<'a>, ViewPlace)> {
Some((<Self as Find<T>>::at(content, ViewPlace::Own)?, ViewPlace::Own))
}
fn at(content: TileContent<'_>, place: ViewPlace) -> Option<<Self as TileInput<T>>::Data<'_>> {
debug_assert_eq!(place, ViewPlace::Own, "a laid-out layer's place for a tile's own contents");
match content {
TileContent::Tile(Contents::Raster(raster)) => Some(RasterTile::Tile(raster)),
TileContent::Image(image) => Some(RasterTile::Image(image)),
TileContent::Tile(_) => None,
}
}
fn made_from(data: <Self as TileInput<T>>::Data<'_>) -> MadeFrom {
match data {
RasterTile::Tile(raster) => raster.stamp(),
RasterTile::Image(image) => image.stamp(),
}
}
}
impl<T: StyleLayerType> Find<T> for DemTiles {
const KIND: SourceKind = <DemTiles as Deliver>::KIND;
fn find<'a>(
content: TileContent<'a>,
_layer_id: &str,
) -> Option<(<Self as TileInput<T>>::Data<'a>, ViewPlace)> {
Some((<Self as Find<T>>::at(content, ViewPlace::Own)?, ViewPlace::Own))
}
fn at(content: TileContent<'_>, place: ViewPlace) -> Option<<Self as TileInput<T>>::Data<'_>> {
debug_assert_eq!(place, ViewPlace::Own, "a laid-out layer's place for a tile's own contents");
match content {
TileContent::Tile(Contents::Dem(dem)) => Some(dem),
_ => None,
}
}
fn made_from(data: <Self as TileInput<T>>::Data<'_>) -> MadeFrom {
data.stamp()
}
}
impl<T: StyleLayerType> Find<T> for NoTiles {
const KIND: SourceKind = SourceKind::None;
fn find<'a>(
_content: TileContent<'a>,
_layer_id: &str,
) -> Option<(<Self as TileInput<T>>::Data<'a>, ViewPlace)> {
None
}
fn at(_content: TileContent<'_>, _place: ViewPlace) -> Option<<Self as TileInput<T>>::Data<'_>> {
None
}
fn made_from(data: <Self as TileInput<T>>::Data<'_>) -> MadeFrom {
match data {}
}
}
}