kiss3d/planar_camera/
planar_camera.rs

1use crate::event::WindowEvent;
2use crate::resource::ShaderUniform;
3use crate::window::Canvas;
4use na::{Matrix3, Point2, Vector2};
5
6/// Trait that all 2D camera implementations must implement.
7///
8/// Planar cameras control the view for 2D overlays and planar scene elements.
9/// Unlike 3D cameras, planar cameras work with 2D transformations and projections.
10///
11/// # Implementations
12/// kiss3d provides built-in 2D camera types:
13/// - [`PlanarFixedView`](crate::planar_camera::PlanarFixedView) - Static 2D camera
14/// - [`Sidescroll`](crate::planar_camera::Sidescroll) - Side-scrolling camera
15pub trait PlanarCamera {
16    /// Handles window events to update camera state.
17    ///
18    /// Called for each window event, allowing the camera to respond to user input.
19    ///
20    /// # Arguments
21    /// * `canvas` - Reference to the rendering canvas
22    /// * `event` - The window event to handle
23    fn handle_event(&mut self, canvas: &Canvas, event: &WindowEvent);
24
25    /// Updates the camera state for the current frame.
26    ///
27    /// Called once at the beginning of each frame before rendering.
28    ///
29    /// # Arguments
30    /// * `canvas` - Reference to the rendering canvas
31    fn update(&mut self, canvas: &Canvas);
32
33    /// Uploads the camera's view and projection matrices to the GPU.
34    ///
35    /// This can be called multiple times during the render loop.
36    ///
37    /// # Arguments
38    /// * `proj` - Shader uniform for the 2D projection matrix
39    /// * `view` - Shader uniform for the 2D view matrix
40    fn upload(
41        &self,
42        proj: &mut ShaderUniform<Matrix3<f32>>,
43        view: &mut ShaderUniform<Matrix3<f32>>,
44    );
45
46    /// Converts screen coordinates to 2D world coordinates.
47    ///
48    /// # Arguments
49    /// * `window_coord` - The point in screen space (pixels)
50    /// * `window_size` - The size of the window in pixels
51    ///
52    /// # Returns
53    /// The corresponding point in 2D world space
54    fn unproject(&self, window_coord: &Point2<f32>, window_size: &Vector2<f32>) -> Point2<f32>;
55}