use glam::{DMat4, Vec2};
use mln_style::types::TranslateAnchorType;
use crate::camera::{ProjectionData, View};
use crate::draw::identity::SimpleIdentity;
use crate::layer::layer_type::LayerType;
use crate::render::paint_parameters::{PaintParameters, TransformParameters};
use crate::render::pass::RenderPass;
use crate::tile::{OverscaledTileID, UnwrappedTileID};
use crate::tile_store::contents::Contents;
use crate::tile_store::dem::DemContents;
use crate::tile_store::raster::RasterContents;
use crate::tile_store::tile::Tile;
pub(crate) struct RenderTile {
pub(crate) id: UnwrappedTileID,
pub(crate) projection: ProjectionData,
pub(crate) matrix: DMat4,
tile_id: OverscaledTileID,
tile_hold_for_fade: bool,
slot: Option<usize>,
}
impl RenderTile {
pub(crate) fn new(id: UnwrappedTileID, tile: &Tile, slot: usize, hold_for_fade: bool) -> Self {
RenderTile { slot: Some(slot), ..RenderTile::of_tile_id(id, tile.id, hold_for_fade) }
}
pub(crate) fn of_tile_id(id: UnwrappedTileID, tile_id: OverscaledTileID, hold_for_fade: bool) -> Self {
RenderTile {
id,
projection: ProjectionData::default(),
matrix: DMat4::ZERO,
tile_id,
tile_hold_for_fade: hold_for_fade,
slot: None,
}
}
pub(crate) fn tile_unit_translation(
id: &UnwrappedTileID,
translation: [f32; 2],
anchor: TranslateAnchorType,
state: &View,
) -> [f32; 2] {
if translation[0] == 0.0 && translation[1] == 0.0 {
return [0.0, 0.0];
}
let angle = if anchor == TranslateAnchorType::Viewport { (-state.angle()) as f32 } else { 0.0f32 };
let translate = Vec2::from_angle(angle).rotate(Vec2::new(translation[0], translation[1]));
let zoom = state.zoom() as f32;
[id.pixels_to_tile_units(translate.x, zoom), id.pixels_to_tile_units(translate.y, zoom)]
}
#[must_use]
pub(crate) fn overscaled_tile_id(&self) -> &OverscaledTileID {
&self.tile_id
}
#[must_use]
pub(crate) fn slot(&self) -> Option<usize> {
self.slot
}
#[must_use]
pub(crate) fn hold_for_fade(&self) -> bool {
self.tile_hold_for_fade
}
pub(crate) fn set_projection(&mut self, transform: &TransformParameters) {
self.projection = transform.view.projection_data_with(&self.id, &transform.proj_matrix);
self.matrix = self.projection.main_matrix;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ViewPlace {
Laid(usize),
Own,
}
pub(crate) trait TileView<'a>: Copy {
fn find(contents: &'a Contents, layer_id: &str) -> Option<(Self, ViewPlace)>;
fn at(contents: &'a Contents, place: ViewPlace) -> Option<Self>;
fn is_for(self, _layer_id: &str) -> bool {
true
}
fn passes(self) -> RenderPass {
RenderPass::None
}
}
impl<'a> TileView<'a> for &'a RasterContents {
fn find(contents: &'a Contents, _layer_id: &str) -> Option<(Self, ViewPlace)> {
Some((Self::at(contents, ViewPlace::Own)?, ViewPlace::Own))
}
fn at(contents: &'a Contents, place: ViewPlace) -> Option<Self> {
debug_assert_eq!(place, ViewPlace::Own, "a laid-out layer's place for a tile's own contents");
match contents {
Contents::Raster(raster) => Some(raster),
_ => None,
}
}
}
impl<'a> TileView<'a> for &'a DemContents {
fn find(contents: &'a Contents, _layer_id: &str) -> Option<(Self, ViewPlace)> {
Some((Self::at(contents, ViewPlace::Own)?, ViewPlace::Own))
}
fn at(contents: &'a Contents, place: ViewPlace) -> Option<Self> {
debug_assert_eq!(place, ViewPlace::Own, "a laid-out layer's place for a tile's own contents");
match contents {
Contents::Dem(dem) => Some(dem),
_ => None,
}
}
}
#[derive(Clone, Copy)]
pub(crate) enum NoTileBucket {}
impl TileView<'_> for NoTileBucket {
fn find(_contents: &Contents, _layer_id: &str) -> Option<(Self, ViewPlace)> {
None
}
fn at(_contents: &Contents, _place: ViewPlace) -> Option<Self> {
None
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct TileStamp {
pub(crate) tile: OverscaledTileID,
pub(crate) bucket: Option<SimpleIdentity>,
pub(crate) generations: [u64; 2],
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct FoundTile {
pub(crate) tile: usize,
pub(crate) view: Option<ViewPlace>,
}
pub(crate) struct FrameTile<'a, T: LayerType> {
pub(crate) tile: &'a RenderTile,
bucket: Option<T::TileBucket<'a>>,
}
impl<T: LayerType> Clone for FrameTile<'_, T> {
fn clone(&self) -> Self {
*self
}
}
impl<T: LayerType> Copy for FrameTile<'_, T> {}
impl<'a, T: LayerType> FrameTile<'a, T> {
#[must_use]
pub(crate) fn find(
tile: &'a RenderTile,
contents: Option<&'a Contents>,
layer_id: &str,
) -> (Self, Option<ViewPlace>) {
let found = contents.and_then(|contents| T::TileBucket::find(contents, layer_id));
(FrameTile { tile, bucket: found.map(|(bucket, _)| bucket) }, found.map(|(_, place)| place))
}
#[must_use]
pub(crate) fn at(
tile: &'a RenderTile,
contents: Option<&'a Contents>,
layer_id: &str,
view: Option<ViewPlace>,
) -> Self {
let bucket = contents.zip(view).and_then(|(contents, place)| T::TileBucket::at(contents, place));
debug_assert_eq!(bucket.is_some(), view.is_some(), "a tile's contents changed since the frame's update");
debug_assert!(bucket.is_none_or(|bucket| bucket.is_for(layer_id)), "a layer found at another's place");
FrameTile { tile, bucket }
}
pub(crate) fn bucket(self) -> Option<T::TileBucket<'a>> {
self.bucket
}
pub(crate) fn bucket_for_pass(self, pass: RenderPass) -> Option<T::TileBucket<'a>> {
self.bucket.filter(|bucket| bucket.passes() & pass != RenderPass::None)
}
#[must_use]
pub(crate) fn id(self) -> OverscaledTileID {
*self.tile.overscaled_tile_id()
}
#[must_use]
pub(crate) fn draw_id(self, parameters: &PaintParameters<'_>) -> OverscaledTileID {
let id = self.id();
match parameters.drape_wrap() {
Some(wrap) => OverscaledTileID { wrap, ..id },
None => id,
}
}
}