use crate::{Bounds, CameraView, Vec3};
use slotmap::new_key_type;
use std::cmp::Ordering;
new_key_type! {
pub struct AscendingKey;
}
pub type Index = AscendingKey;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Default)]
pub struct DrawOrder {
pub order_layer: u32,
pub alpha: bool,
pub x: u32,
pub y: u32,
pub z: u32,
}
impl PartialOrd for DrawOrder {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for DrawOrder {
fn cmp(&self, other: &Self) -> Ordering {
self.order_layer
.cmp(&other.order_layer)
.then(self.alpha.cmp(&other.alpha))
.then(self.y.cmp(&other.y).reverse())
.then(self.x.cmp(&other.x))
.then(self.z.cmp(&other.z).reverse())
}
}
impl DrawOrder {
pub fn new(alpha: bool, pos: Vec3, order_layer: u32) -> Self {
Self {
order_layer,
alpha,
x: (pos.x * 10000.0) as u32,
y: (pos.y * 10000.0) as u32,
z: (pos.z * 10000.0) as u32,
}
}
pub fn set_pos(&mut self, pos: Vec3) {
self.x = (pos.x * 10000.0) as u32;
self.y = (pos.y * 10000.0) as u32;
self.z = (pos.z * 10000.0) as u32;
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct OrderedIndex {
pub(crate) order: DrawOrder,
pub(crate) index: Index,
pub(crate) index_count: u32,
pub(crate) index_max: u32,
pub(crate) bounds: Option<Bounds>,
pub(crate) camera_view: CameraView,
}
impl PartialOrd for OrderedIndex {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for OrderedIndex {
fn eq(&self, other: &Self) -> bool {
self.order == other.order
}
}
impl Eq for OrderedIndex {}
impl Ord for OrderedIndex {
fn cmp(&self, other: &Self) -> Ordering {
self.order.cmp(&other.order)
}
}
impl OrderedIndex {
pub fn new(order: DrawOrder, index: Index, index_max: u32) -> Self {
Self {
order,
index,
index_count: 0,
index_max,
bounds: None,
camera_view: CameraView::MainView,
}
}
pub fn new_with_bounds(
order: DrawOrder,
index: Index,
index_max: u32,
bounds: Option<Bounds>,
camera_view: CameraView,
) -> Self {
Self {
order,
index,
index_count: 0,
index_max,
bounds,
camera_view,
}
}
}