mirror of
https://github.com/penpot/penpot.git
synced 2026-06-09 17:02:05 +00:00
* ♻️ Refactor viewbox * 🎉 Add draw_atlas alternative to draw tiles * 🐛 Fix minor glitches * ♻️ Change how process_animation_frame works * ♻️ Refactor document atlas * ♻️ Refactor max texture size * ♻️ Refactor entrypoints and dead_code
97 lines
2.1 KiB
Rust
97 lines
2.1 KiB
Rust
use crate::math::{Matrix, Point, Rect, Size};
|
|
use std::ops::Mul;
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub(crate) struct Viewbox {
|
|
pub pan: Point,
|
|
pub size: Size,
|
|
pub zoom: f32,
|
|
pub dpr: f32,
|
|
pub area: Rect,
|
|
}
|
|
|
|
impl Default for Viewbox {
|
|
fn default() -> Self {
|
|
Self {
|
|
pan: Point::new(0.0, 0.0),
|
|
size: Size::new(0.0, 0.0),
|
|
zoom: 1.0,
|
|
dpr: 1.0,
|
|
area: Rect::new_empty(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
impl Viewbox {
|
|
pub fn new(width: f32, height: f32) -> Self {
|
|
let size = Size::new(width, height);
|
|
let area = Rect::from_size(size);
|
|
Self {
|
|
size,
|
|
area,
|
|
..Self::default()
|
|
}
|
|
}
|
|
|
|
pub fn dpr_width(&self) -> f32 {
|
|
self.size.width * self.dpr
|
|
}
|
|
|
|
pub fn dpr_height(&self) -> f32 {
|
|
self.size.height * self.dpr
|
|
}
|
|
|
|
pub fn width(&self) -> f32 {
|
|
self.size.width
|
|
}
|
|
|
|
pub fn height(&self) -> f32 {
|
|
self.size.height
|
|
}
|
|
|
|
pub fn set_all(&mut self, zoom: f32, pan_x: f32, pan_y: f32) {
|
|
self.pan.set(pan_x, pan_y);
|
|
self.zoom = zoom;
|
|
self.area.set_xywh(
|
|
-self.pan.x,
|
|
-self.pan.y,
|
|
self.size.width / self.zoom,
|
|
self.size.height / self.zoom,
|
|
);
|
|
}
|
|
|
|
pub fn set_wh(&mut self, width: f32, height: f32) {
|
|
self.size.set(width, height);
|
|
self.area
|
|
.set_wh(self.size.width / self.zoom, self.size.height / self.zoom);
|
|
}
|
|
|
|
pub fn set_dpr(&mut self, dpr: f32) {
|
|
self.dpr = dpr;
|
|
}
|
|
|
|
pub fn get_scale(&self) -> f32 {
|
|
self.zoom * self.dpr
|
|
}
|
|
|
|
pub fn get_offset(&self) -> Point {
|
|
self.area.tl().mul(self.get_scale())
|
|
}
|
|
|
|
pub fn pan(&self) -> Point {
|
|
self.pan
|
|
}
|
|
|
|
pub fn zoom(&self) -> f32 {
|
|
self.zoom
|
|
}
|
|
|
|
pub fn get_matrix(&self) -> Matrix {
|
|
let mut matrix = Matrix::new_identity();
|
|
matrix.post_translate(self.pan());
|
|
matrix.post_scale((self.zoom, self.zoom), None);
|
|
matrix
|
|
}
|
|
}
|