mod show;
pub(crate) mod state;
pub use state::PanZoomState;
use crate::{Context, Id, Rect, Response, Transform, Vec2};
use std::{hash::Hash, ops::RangeInclusive};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ZoomWheel {
#[default]
Primary,
Unmodified,
Disabled,
}
pub struct PanZoom {
source: Id,
size: Vec2,
limits: (f32, f32),
wheel: ZoomWheel,
pan: bool,
enabled: bool,
label: String,
}
impl PanZoom {
#[track_caller]
pub fn new(source: impl Hash, size: Vec2) -> Self {
let size = super::sanitize::size("PanZoom::new", size).unwrap_or(Vec2::ZERO);
Self {
source: Id::new(source),
size: size.min(Vec2::splat(state::MAX_POSITION)),
limits: (0.1, 8.0),
wheel: ZoomWheel::Primary,
pan: true,
enabled: true,
label: "Pan and zoom".into(),
}
}
#[track_caller]
pub fn scale_limits(mut self, limits: RangeInclusive<f32>) -> Self {
self.limits = state::limits_for(limits);
self
}
pub fn zoom_wheel(mut self, wheel: ZoomWheel) -> Self {
self.wheel = wheel;
self
}
pub fn pan_drag(mut self, pan: bool) -> Self {
self.pan = pan;
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn accessible_label(mut self, label: impl Into<String>) -> Self {
self.label = label.into();
self
}
}
pub struct PanZoomOutput<R> {
pub inner: R,
pub response: Response,
pub viewport: Rect,
pub visible: Rect,
pub changed: bool,
pub panned: bool,
pub zoomed: bool,
pub camera: PanZoomState,
}
impl<R> PanZoomOutput<R> {
pub fn transform(&self, context: &Context) -> Transform {
context
.visual_transform(self.response.id)
.compose(self.camera.transform(self.viewport.min))
}
pub fn local_to_screen(&self, context: &Context, local: Vec2) -> Vec2 {
self.transform(context).point(local)
}
pub fn screen_to_local(&self, context: &Context, screen: Vec2) -> Vec2 {
self.transform(context).inverse().point(screen)
}
pub fn displayed_viewport(&self, context: &Context) -> Rect {
context.visual_rect(self.response.id, self.viewport)
}
}