use std::any::Any;
use mln_style::feature::{GeometryCollection, GeometryTileFeature};
use mln_style::tile_id::CanonicalTileID;
use mln_wire::{Reader, Wire, Writer};
use crate::draw::identity::SimpleIdentity;
use crate::layer::bucket_geometry::GeometryUpload;
use crate::layout::pattern_layout::PatternLayerMap;
use crate::text::atlas::ImagePositions;
use crate::tile::TileLayer;
use crate::util::tessellator::Tessellator;
pub(crate) type FeatureStates = mln_style::hash::HashMap<String, mln_style::feature::FeatureState>;
pub type LayerFeatureStates = mln_style::hash::HashMap<String, FeatureStates>;
#[derive(Clone, Debug, mln_wire::Wire)]
pub(crate) struct BucketBase {
#[wire(skip)]
pub(crate) uploaded: bool,
#[wire(skip, default = "SimpleIdentity::new")]
pub(crate) bucket_id: SimpleIdentity,
}
impl Default for BucketBase {
fn default() -> Self {
BucketBase { uploaded: false, bucket_id: SimpleIdentity::new() }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct UploadNeeds {
pub(crate) fill_outline_ends: bool,
pub(crate) wall_quads: bool,
}
pub(crate) trait Bucket: Any + Send + CloneBucket {
fn base(&self) -> &BucketBase;
#[expect(clippy::too_many_arguments, reason = "the feature and what the layout lays it out with")]
fn add_feature(
&mut self,
_feature: &dyn GeometryTileFeature,
_geometries: &GeometryCollection,
_image_positions: &ImagePositions,
_patterns: &PatternLayerMap,
_index: usize,
_canonical: &CanonicalTileID,
_tessellator: &mut Tessellator,
) {
}
fn update(
&mut self,
_states: &FeatureStates,
_layer: &TileLayer<'_>,
_layer_id: &str,
_image_positions: &ImagePositions,
) -> bool {
false
}
fn max_content_elevation(&self) -> f32 {
0.0
}
fn record_sdf_pattern(&mut self, _layer_id: &str, _sdf: bool) {}
fn is_sdf_pattern(&self, _layer_id: &str) -> bool {
false
}
fn upload(&mut self, geometry: &mut GeometryUpload<'_>, needs: UploadNeeds);
fn has_data(&self) -> bool;
fn write_wire(&self, w: &mut Writer);
fn needs_upload(&self) -> bool {
!self.base().uploaded && self.has_data()
}
fn id(&self) -> SimpleIdentity {
self.base().bucket_id
}
}
pub(crate) trait CloneBucket {
fn clone_bucket(&self) -> Box<dyn Bucket>;
}
impl<B: Bucket + Clone> CloneBucket for B {
fn clone_bucket(&self) -> Box<dyn Bucket> {
Box::new(self.clone())
}
}
#[derive(Clone)]
pub(crate) enum NoBucket {}
impl Bucket for NoBucket {
fn base(&self) -> &BucketBase {
match *self {}
}
fn upload(&mut self, _geometry: &mut GeometryUpload<'_>, _needs: UploadNeeds) {
match *self {}
}
fn has_data(&self) -> bool {
match *self {}
}
fn write_wire(&self, _w: &mut Writer) {
match *self {}
}
}
impl Wire for NoBucket {
fn write(&self, _w: &mut Writer) {
match *self {}
}
fn read(r: &mut Reader<'_>) -> Option<Self> {
r.fail("a bucket of a layer type that doesn't lay out")
}
}
#[cfg(test)]
impl dyn Bucket {
pub(crate) fn downcast_ref<B: Bucket>(&self) -> Option<&B> {
let any: &dyn Any = self;
any.downcast_ref()
}
}
#[cfg(feature = "bench")]
pub(crate) use crate::layer::render_layer::hold_symbol_bucket as recover;