Skip to main content

blockworx/
camera.rs

1//! How a [`Vantage`] and a manifest row's camera convert into each other.
2//!
3//! The pair sits here rather than beside `Vantage` because the backend does not
4//! know the store: it knows where a camera stands, not that anything writes one
5//! down.
6
7use blockworx_egui::Vantage;
8use blockworx_geom::Vec2;
9use blockworx_paint::Zoom;
10use blockworx_store::record::Camera;
11
12/// How a manifest row records `vantage` (ยง10.1 of `docs/log-vs-snapshot.md`):
13/// the world point at the centre of `viewport`, and the zoom.
14///
15/// Centre-and-zoom rather than the translation, which is measured against a
16/// window size the reader may no longer have.
17pub fn recorded(vantage: Vantage, viewport: Vec2) -> Camera {
18    let centre = (viewport / 2.0 - vantage.translation) / vantage.zoom.get();
19    Camera {
20        x: centre.x,
21        y: centre.y,
22        zoom: vantage.zoom.get(),
23    }
24}
25
26/// And back, against the viewport the reader has now.
27pub fn vantage(camera: Camera, viewport: Vec2) -> Vantage {
28    let zoom = Zoom::new(camera.zoom);
29    Vantage {
30        zoom,
31        translation: viewport / 2.0 - Vec2::new(camera.x, camera.y) * zoom.get(),
32    }
33}