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>,
}
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,
}
}
#[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>>,
}
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) };
(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 }
}
#[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() }
}
}