1use crate::transform::Transform;
4use crate::Mat4;
5use crate::Ortho;
6use crate::Vec3;
7
8pub const FIXED_WIDTH: f32 = 20.0;
10pub const FIXED_HEIGHT: f32 = FIXED_WIDTH / 1.77;
12
13#[derive(Debug)]
15pub struct Camera {
16 pub transform: Transform,
18 orthographic: Ortho,
19 width: f32,
20 height: f32,
21}
22
23impl Default for Camera {
24 fn default() -> Self {
25 Self {
26 transform: Transform::new(),
27 width: FIXED_WIDTH,
28 height: FIXED_HEIGHT,
29 orthographic: Ortho::new(
30 -FIXED_WIDTH / 2.0,
31 FIXED_WIDTH / 2.0,
32 FIXED_HEIGHT / 2.0,
33 -FIXED_HEIGHT / 2.0,
34 0f32,
35 1000.0f32,
36 ),
37 }
38 }
39}
40
41impl Camera {
42 pub fn new() -> Self {
44 Default::default()
45 }
46 pub fn with_position(position: Vec3) -> Self {
48 Self {
49 transform: Transform::new_with_position(position),
50 ..Default::default()
51 }
52 }
53 pub fn with_transform(transform: Transform) -> Self {
55 Self {
56 transform,
57 ..Default::default()
58 }
59 }
60 pub fn with_width_and_height(width: f32, height: f32) -> Self {
62 Self {
63 width,
64 height,
65 orthographic: Ortho::new(
66 -width / 2.0,
67 width / 2.0,
68 height / 2.0,
69 -height / 2.0,
70 0f32,
71 1000.0f32,
72 ),
73 ..Default::default()
74 }
75 }
76
77 pub fn set_width_and_height(&mut self, width: f32, height: f32) {
79 self.width = width;
80 self.height = height;
81 }
82
83 pub fn projection(&self) -> &[f32] {
85 self.orthographic.as_matrix().as_slice()
86 }
87
88 pub fn view_projection_matrix(&self) -> Mat4 {
90 self.transform.matrix() * self.orthographic.as_matrix()
91 }
92
93 pub fn screen_to_world_coordinates(&self, screen_x: f32, screen_y: f32) -> (f32, f32) {
97 let clipped_x = screen_x / self.width - 0.5;
98 let clipped_y = screen_y / self.height - 0.5;
99
100 (clipped_x * FIXED_WIDTH, clipped_y * FIXED_HEIGHT)
101 }
102}