use crate::transform::Transform;
use crate::Mat4;
use crate::Ortho;
use crate::Vec3;
pub const FIXED_WIDTH: f32 = 20.0;
pub const FIXED_HEIGHT: f32 = FIXED_WIDTH / 1.77;
#[derive(Debug)]
pub struct Camera {
pub transform: Transform,
orthographic: Ortho,
width: f32,
height: f32,
}
impl Default for Camera {
fn default() -> Self {
Self {
transform: Transform::new(),
width: FIXED_WIDTH,
height: FIXED_HEIGHT,
orthographic: Ortho::new(
-FIXED_WIDTH / 2.0,
FIXED_WIDTH / 2.0,
FIXED_HEIGHT / 2.0,
-FIXED_HEIGHT / 2.0,
0f32,
1000.0f32,
),
}
}
}
impl Camera {
pub fn new() -> Self {
Default::default()
}
pub fn with_position(position: Vec3) -> Self {
Self {
transform: Transform::new_with_position(position),
..Default::default()
}
}
pub fn with_transform(transform: Transform) -> Self {
Self {
transform,
..Default::default()
}
}
pub fn with_width_and_height(width: f32, height: f32) -> Self {
Self {
width,
height,
orthographic: Ortho::new(
-width / 2.0,
width / 2.0,
height / 2.0,
-height / 2.0,
0f32,
1000.0f32,
),
..Default::default()
}
}
pub fn set_width_and_height(&mut self, width: f32, height: f32) {
self.width = width;
self.height = height;
}
pub fn projection(&self) -> &[f32] {
self.orthographic.as_matrix().as_slice()
}
pub fn view_projection_matrix(&self) -> Mat4 {
self.transform.matrix() * self.orthographic.as_matrix()
}
pub fn screen_to_world_coordinates(&self, screen_x: f32, screen_y: f32) -> (f32, f32) {
let clipped_x = screen_x / self.width - 0.5;
let clipped_y = screen_y / self.height - 0.5;
(clipped_x * FIXED_WIDTH, clipped_y * FIXED_HEIGHT)
}
}