use std::time::Duration;
use glam::{DMat4, Vec2};
use mln_style::constants;
use mln_style::types::TranslateAnchorType;
use crate::camera::{ProjectionData, View};
use crate::draw::identity::SimpleIdentity;
use crate::layer::layer_drawing::MadeFrom;
use crate::layer::render_layer::Layer;
use crate::layer::tile_input::TileData;
use crate::layer::tile_input::input::Find;
use crate::render::paint_parameters::TransformParameters;
use crate::render::pass::RenderPass;
use crate::source::render_image_source::ImageTile;
use crate::tile::{OverscaledTileID, UnwrappedTileID};
use crate::tile_store::contents::Contents;
use crate::tile_store::tile::Tile;
pub 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>,
pub(crate) fade: Option<RenderFade>,
pub(crate) fade_parent_only: bool,
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct RenderFade {
pub(crate) parent: Option<usize>,
pub(crate) elapsed: Duration,
pub(crate) fade_in: bool,
}
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,
fade: None,
fade_parent_only: false,
}
}
#[must_use]
pub 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 fn id(&self) -> UnwrappedTileID {
self.id
}
#[must_use]
pub fn overscaled_tile_id(&self) -> &OverscaledTileID {
&self.tile_id
}
#[must_use]
pub fn slot(&self) -> Option<usize> {
self.slot
}
#[must_use]
pub fn hold_for_fade(&self) -> bool {
self.tile_hold_for_fade
}
#[must_use]
pub fn projection(&self) -> &ProjectionData {
&self.projection
}
#[must_use]
pub fn matrix(&self) -> &DMat4 {
&self.matrix
}
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)]
pub(crate) enum TileContent<'a> {
Tile(&'a Contents),
Image(&'a ImageTile),
}
impl TileContent<'_> {
pub(crate) fn stamp(self, place: ViewPlace) -> Option<(SimpleIdentity, [u64; 2])> {
match (self, place) {
(TileContent::Tile(contents), place) => contents.stamp(place),
(TileContent::Image(image), ViewPlace::Own) => Some(image.stamp()),
(TileContent::Image(_), ViewPlace::Laid(_)) => None,
}
}
pub(crate) fn bounds(self) -> [i32; 4] {
match self {
TileContent::Tile(_) => TILE_BOUNDS,
TileContent::Image(image) => image.bounds,
}
}
}
pub(crate) const TILE_BOUNDS: [i32; 4] = [0, 0, constants::EXTENT, constants::EXTENT];
pub(crate) fn overlaps(tile: &UnwrappedTileID, bounds: [i32; 4], other: &UnwrappedTileID) -> bool {
let zoom = tile.canonical.z.max(other.canonical.z);
let place = |id: &UnwrappedTileID| {
let scale = 1i64 << (zoom - id.canonical.z);
let extent = i64::from(constants::EXTENT) * scale;
let x = ((i64::from(id.wrap) << id.canonical.z) + i64::from(id.canonical.x)) * extent;
(x, i64::from(id.canonical.y) * extent, scale)
};
let (left, top, scale) = place(tile);
let [min_x, min_y, max_x, max_y] = bounds.map(|bound| i64::from(bound) * scale);
let (other_left, other_top, other_scale) = place(other);
let other_extent = i64::from(constants::EXTENT) * other_scale;
left + min_x < other_left + other_extent
&& other_left < left + max_x
&& top + min_y < other_top + other_extent
&& other_top < top + max_y
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ViewPlace {
Laid(usize),
Own,
}
#[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 struct FrameTile<'a, T: Layer> {
pub(crate) tile: &'a RenderTile,
wrap: i16,
data: Option<TileData<'a, T>>,
fade: Option<TileFade<'a, T>>,
}
pub struct TileFade<'a, T: Layer> {
parent: Option<(OverscaledTileID, TileData<'a, T>)>,
elapsed: Duration,
fade_in: bool,
}
impl<T: Layer> Clone for TileFade<'_, T> {
fn clone(&self) -> Self {
*self
}
}
impl<T: Layer> Copy for TileFade<'_, T> {}
impl<'a, T: Layer> TileFade<'a, T> {
pub(crate) fn new(parent: Option<(OverscaledTileID, TileData<'a, T>)>, elapsed: Duration, fade_in: bool) -> Self {
TileFade { parent, elapsed, fade_in }
}
#[must_use]
pub fn parent(self) -> Option<(OverscaledTileID, TileData<'a, T>)> {
self.parent
}
#[must_use]
pub fn shown(self, duration: Duration) -> f32 {
let done =
if duration.is_zero() { 1.0 } else { (self.elapsed.as_secs_f32() / duration.as_secs_f32()).min(1.0) };
if self.fade_in { done } else { 1.0 - done }
}
}
impl<T: Layer> Clone for FrameTile<'_, T> {
fn clone(&self) -> Self {
*self
}
}
impl<T: Layer> Copy for FrameTile<'_, T> {}
impl<'a, T: Layer> FrameTile<'a, T> {
#[must_use]
pub(crate) fn find(
tile: &'a RenderTile,
content: Option<TileContent<'a>>,
layer_id: &str,
) -> (Self, Option<ViewPlace>) {
let found = content.and_then(|content| <T::Input as Find<T::Style>>::find(content, layer_id));
let frame_tile = FrameTile { tile, wrap: tile.id.wrap, data: found.map(|(data, _)| data), fade: None };
(frame_tile, found.map(|(_, place)| place))
}
#[must_use]
pub(crate) fn at(
tile: &'a RenderTile,
content: Option<TileContent<'a>>,
layer_id: &str,
view: Option<ViewPlace>,
) -> Self {
let data = content.zip(view).and_then(|(content, place)| <T::Input as Find<T::Style>>::at(content, place));
debug_assert_eq!(data.is_some(), view.is_some(), "a tile's contents changed since the frame's update");
debug_assert!(
data.is_none_or(|data| <T::Input as Find<T::Style>>::is_for(data, layer_id)),
"a layer found at another's place"
);
FrameTile { tile, wrap: tile.id.wrap, data, fade: None }
}
#[must_use]
pub(crate) fn with_fade(self, fade: Option<TileFade<'a, T>>) -> Self {
FrameTile { fade, ..self }
}
#[must_use]
pub fn fade(self) -> Option<TileFade<'a, T>> {
self.fade
}
#[must_use]
pub(crate) fn in_copy(self, wrap: i16) -> Self {
FrameTile { wrap, ..self }
}
pub(crate) fn made_from(self) -> Option<MadeFrom> {
self.data.map(<T::Input as Find<T::Style>>::made_from)
}
#[must_use]
pub fn render_tile(self) -> &'a RenderTile {
self.tile
}
pub fn data(self) -> Option<TileData<'a, T>> {
self.data
}
pub fn data_for_pass(self, pass: RenderPass) -> Option<TileData<'a, T>> {
self.data.filter(|&data| <T::Input as Find<T::Style>>::passes(data) & pass != RenderPass::NONE)
}
#[must_use]
pub fn id(self) -> OverscaledTileID {
*self.tile.overscaled_tile_id()
}
#[must_use]
pub fn draw_id(self) -> OverscaledTileID {
OverscaledTileID { wrap: self.wrap, ..self.id() }
}
}
#[cfg(test)]
mod tests {
use mln_style::constants::EXTENT;
use mln_style::tile_id::CanonicalTileID;
use super::*;
fn tile(wrap: i16, z: u8, x: u32, y: u32) -> UnwrappedTileID {
UnwrappedTileID::with_wrap(wrap, CanonicalTileID::new(z, x, y))
}
#[test]
fn a_box_overlaps_the_tiles_it_covers_part_of() {
let inside = [1000, 1000, 7000, 7000];
let east = [EXTENT / 2, 0, EXTENT + EXTENT / 4, EXTENT];
let west = [-EXTENT / 4, 0, EXTENT / 4, EXTENT];
let sliver = [EXTENT / 2, 0, EXTENT + 8, EXTENT / 2];
let cases = [
(tile(0, 3, 5, 2), inside, tile(0, 3, 5, 2), true),
(tile(0, 3, 5, 2), inside, tile(-1, 3, 5, 2), false),
(tile(-1, 3, 5, 2), inside, tile(-1, 3, 5, 2), true),
(tile(-1, 3, 5, 2), inside, tile(0, 3, 5, 2), false),
(tile(0, 3, 5, 2), east, tile(0, 3, 6, 2), true),
(tile(0, 3, 5, 2), east, tile(0, 3, 4, 2), false),
(tile(0, 3, 5, 2), east, tile(0, 3, 5, 3), false),
(tile(0, 3, 5, 2), east, tile(0, 3, 5, 1), false),
(tile(1, 3, 0, 2), west, tile(0, 3, 7, 2), true),
(tile(1, 3, 0, 2), west, tile(1, 3, 0, 2), true),
(tile(1, 3, 0, 2), west, tile(1, 3, 7, 2), false),
(tile(0, 3, 5, 2), sliver, tile(0, 2, 2, 1), true),
(tile(0, 3, 5, 2), sliver, tile(0, 0, 0, 0), true),
(tile(0, 3, 5, 2), sliver, tile(0, 2, 3, 1), true),
(tile(0, 3, 5, 2), sliver, tile(0, 2, 2, 0), false),
(tile(0, 3, 5, 2), sliver, tile(0, 4, 11, 4), true),
(tile(0, 3, 5, 2), sliver, tile(0, 4, 10, 4), false),
(tile(0, 3, 5, 2), sliver, tile(0, 4, 11, 5), false),
(tile(0, 3, 5, 2), sliver, tile(0, 4, 12, 4), true),
(tile(0, 3, 5, 2), sliver, tile(0, 4, 12, 5), false),
(tile(0, 3, 5, 2), sliver, tile(0, 10, 6 * 128, 2 * 128), true),
(tile(0, 3, 5, 2), sliver, tile(0, 10, 6 * 128 + 1, 2 * 128), false),
];
for (tile, bounds, other, expected) in cases {
assert_eq!(expected, overlaps(&tile, bounds, &other), "{bounds:?} of {tile:?} over {other:?}");
}
}
}