//! View menu: ZOOM (with Realtime and Dynamic), PAN, REGEN, REDRAW, named views (VIEW), the view //! twist (DVIEW TWist), layout switching. use cadcraft_doc::{Space, entity_bounds}; use cadcraft_geom::{Bounds2, Vec2}; use serde_json::{Value, json}; use super::machines::{SelectRun, number}; use super::{CommandSpec, bad, bool_or, f64_or, f64_req, ok, point_param, point_req, str_param, targets}; use crate::{Accept, Input, Interactive, Prompt, Result, Session, Step, View}; mod dview; mod named_views; mod vports; pub(super) use vports::{config_names, config_tiles, model_vports, tile_model}; pub fn specs() -> Vec { let mut v = vec![ CommandSpec::new("zoom", "Zoom", run_zoom) .alias(&["z"]) .params("{mode: extents|all|window|previous|in|out|center|dynamic|scale|realtime|object, p1?, p2?, center?, height?, factor?, xp?: bool (scale relative to paper space: in a viewport, `factor` paper units per model unit), limits?: bool (scale relative to the limits)} (center/dynamic: `center` + `height`, or `factor` with `xp`/`limits`; realtime: `factor` about the view centre; typed: Enter at the first prompt starts Realtime)") .noundo() .transparent() .interactive(|_| Ok(Box::new(ZoomM::default()))), CommandSpec::new("zoom.realtime", "Zoom Realtime", |s, p| zoom(s, &with_mode(p.clone(), "realtime", None))) .menu(&["View", "Zoom", "Realtime"]) .params("{factor} (> 1 zooms in, about the view centre); interactive: drag up to zoom in, down to zoom out, Esc or Enter ends") .noundo() .transparent() .interactive(|_| Ok(Box::new(ZoomM { start: ZoomStart::Realtime, ..Default::default() }))), CommandSpec::new("zoom.dynamic", "Zoom Dynamic", |s, p| zoom(s, &with_mode(p.clone(), "dynamic", None))) .menu(&["View", "Zoom", "Dynamic"]) .params("{center, height}; interactive: a view box over the drawing extents, a point places it, the next sizes it, Enter accepts") .noundo() .transparent() .interactive(|_| Ok(Box::new(ZoomM { start: ZoomStart::Dynamic, ..Default::default() }))), CommandSpec::new("zoom.previous", "Zoom Previous", |s, _| zoom(s, &json!({"mode":"previous"}))) .menu(&["View", "Zoom", "Previous"]) .noundo() .transparent(), CommandSpec::new("zoom.window", "Zoom Window", run_zoom) .menu(&["View", "Zoom", "Window"]) .params("{p1, p2}") .noundo() .transparent() .interactive(|_| Ok(Box::new(ZoomM { window: true, ..Default::default() }))), CommandSpec::new("zoom.in", "Zoom In", |s, _| zoom(s, &json!({"mode":"in"}))).menu(&["View", "Zoom", "In"]).noundo().transparent(), CommandSpec::new("zoom.out", "Zoom Out", |s, _| zoom(s, &json!({"mode":"out"}))).menu(&["View", "Zoom", "Out"]).noundo().transparent(), CommandSpec::new("zoom.all", "Zoom All", |s, _| zoom(s, &json!({"mode":"all"}))).menu(&["View", "Zoom", "All"]).noundo().transparent(), CommandSpec::new("zoom.extents", "Zoom Extents", |s, _| zoom(s, &json!({"mode":"extents"}))) .menu(&["View", "Zoom", "Extents"]) .noundo() .transparent(), CommandSpec::new("zoom.object", "Zoom Object", |s, p| { zoom(s, &json!({"mode":"object", "handles": p.get("handles").cloned().unwrap_or(Value::Null)})) }) .menu(&["View", "Zoom", "Object"]) .params("{handles?}") .noundo() .transparent() .interactive(|_| Ok(Box::new(SelectRun::new("zoom.object", "ZOOM")))), CommandSpec::new("pan", "Pan", run_pan) .menu(&["View", "Pan", "Realtime"]) .alias(&["p"]) .params("{delta: [dx,dy]} | {from, to} | {center}") .noundo() .transparent() .interactive(|_| Ok(Box::new(PanM::default()))), CommandSpec::new("pan.left", "Pan Left", |s, _| pan_frac(s, Vec2::new(-0.25, 0.0))).menu(&["View", "Pan", "Left"]).noundo().transparent(), CommandSpec::new("pan.right", "Pan Right", |s, _| pan_frac(s, Vec2::new(0.25, 0.0))).menu(&["View", "Pan", "Right"]).noundo().transparent(), CommandSpec::new("pan.up", "Pan Up", |s, _| pan_frac(s, Vec2::new(0.0, 0.25))).menu(&["View", "Pan", "Up"]).noundo().transparent(), CommandSpec::new("pan.down", "Pan Down", |s, _| pan_frac(s, Vec2::new(0.0, -0.25))).menu(&["View", "Pan", "Down"]).noundo().transparent(), CommandSpec::new("regen", "Regen", |s, _| { // Table formulas are recalculated (as on UPDATEFIELD). super::table::recalc(s, None)?; s.touch(); Ok(json!({"message": "Regenerating model."})) }) .menu(&["View", "Regen"]) .alias(&["re"]) .noundo(), CommandSpec::new("regenall", "Regen All", |s, _| { s.touch(); Ok(json!({"message": "Regenerating all viewports."})) }) .menu(&["View", "Regen All"]) .alias(&["rea"]) .noundo(), CommandSpec::new("redraw", "Redraw", |s, _| { s.touch(); ok() }) .menu(&["View", "Redraw"]) .alias(&["r"]) .noundo() .transparent(), dview::spec(), CommandSpec::new("view.set", "Set View", run_view_set) .params("{center: [x,y], height, twist?: degrees (default: keep the current twist; in a layout viewport it is the viewport's twist, an undoable edit; not in Paper space)}") .noundo(), CommandSpec::new("view.get", "Get View", |s, _| { let v = s.state()?.view(); Ok(json!({"center": [v.center.x, v.center.y], "height": v.height, "twist": v.twist.to_degrees(), "viewportPx": [s.viewport_px.0, s.viewport_px.1]})) }) .noundo(), CommandSpec::new("layout.set", "Switch Layout", run_layout_set).params("{name: \"Model\" | layout name}").noundo(), CommandSpec::new("model", "Model Tab", |s, _| run_layout_set(s, &json!({"name": "Model"}))).noundo(), CommandSpec::new("viewres", "View Resolution", |s, _| { s.touch(); ok() }) .noundo() .interactive(|_| Ok(Box::new(ViewresM::default()))), CommandSpec::new("3dorbit", "3D Orbit", run_3dorbit) .menu(&["View", "3D Orbit"]) .alias(&["3do", "orbit", "3dforbit"]) .params("{yaw?: degrees, pitch?: degrees, preset?: \"top\"|\"front\"|\"back\"|\"left\"|\"right\"|\"seiso\"|\"swiso\"|\"neiso\"|\"nwiso\", reset?: bool}") .noundo() .transparent() .interactive(|_| Ok(Box::new(OrbitM))), CommandSpec::new("view.top", "Top (2D Plan)", |s, _| run_3dorbit(s, &json!({"preset": "top"}))) .menu(&["View", "3D Views", "Top (2D Plan)"]) .noundo() .transparent(), CommandSpec::new("view.front", "Front", |s, _| run_3dorbit(s, &json!({"preset": "front"}))) .menu(&["View", "3D Views", "Front"]) .noundo() .transparent(), CommandSpec::new("view.back", "Back", |s, _| run_3dorbit(s, &json!({"preset": "back"}))) .menu(&["View", "3D Views", "Back"]) .noundo() .transparent(), CommandSpec::new("view.left", "Left", |s, _| run_3dorbit(s, &json!({"preset": "left"}))) .menu(&["View", "3D Views", "Left"]) .noundo() .transparent(), CommandSpec::new("view.right", "Right", |s, _| run_3dorbit(s, &json!({"preset": "right"}))) .menu(&["View", "3D Views", "Right"]) .noundo() .transparent(), CommandSpec::new("view.swiso", "SW Isometric", |s, _| run_3dorbit(s, &json!({"preset": "swiso"}))) .menu(&["View", "3D Views", "SW Isometric"]) .noundo() .transparent(), CommandSpec::new("view.seiso", "SE Isometric", |s, _| run_3dorbit(s, &json!({"preset": "seiso"}))) .menu(&["View", "3D Views", "SE Isometric"]) .noundo() .transparent(), CommandSpec::new("view.neiso", "NE Isometric", |s, _| run_3dorbit(s, &json!({"preset": "neiso"}))) .menu(&["View", "3D Views", "NE Isometric"]) .noundo() .transparent(), CommandSpec::new("view.nwiso", "NW Isometric", |s, _| run_3dorbit(s, &json!({"preset": "nwiso"}))) .menu(&["View", "3D Views", "NW Isometric"]) .noundo() .transparent(), ]; v.extend(named_views::specs()); v.extend(vports::specs()); v } fn ext_bounds(s: &Session) -> Result { let space = s.space(); Ok(s.doc()?.extents(&space)) } fn fit(s: &mut Session, b: Bounds2) -> Result<()> { if b.is_empty() { return Ok(()); } let (w, h) = s.viewport_px; let st = s.state_mut()?; let aspect = st.zoom_aspect(w / h.max(1.0)); // A twisted view fits the box the bounds make on screen. let twist = st.zoom_frame().twist; let (center, bw, bh) = View::twisted_box(b, twist); let height = bh.max(bw / aspect.max(1e-6)).max(1e-9); st.set_zoom_frame(View { center, height, twist }); Ok(()) } pub(crate) fn zoom(s: &mut Session, p: &Value) -> Result { let mode = str_param(p, "mode").unwrap_or("extents").to_ascii_lowercase(); match mode.as_str() { "extents" | "e" => s.zoom_extents()?, "all" | "a" => { let b = ext_bounds(s)?.union(&s.zoom_limits()?); fit(s, b.expand(b.height().max(b.width()) * 0.02))?; } "window" | "w" => { let a = point_req("zoom", p, "p1")?; let b = point_req("zoom", p, "p2")?; fit(s, Bounds2::new(a, b))?; } "previous" | "p" => { let st = s.state_mut()?; if let Some(v) = st.view_history.pop() { st.set_view_quiet(v); } else { return Ok(json!({"message": "No previous view saved."})); } } "in" | "out" => { let f = if mode == "in" { 2.0 } else { 0.5 }; let st = s.state_mut()?; let v = st.zoom_frame(); st.set_zoom_frame(View { height: v.height / f, ..v }); } "center" | "c" | "dynamic" | "d" => { let c = point_req("zoom", p, "center")?; let cur = s.state()?.zoom_frame(); let h = if p.get("factor").is_some() { factor_height(s, p, cur.height)? } else { f64_or(p, "height", cur.height) }; s.state_mut()?.set_zoom_frame(View { center: c, height: h.max(1e-9), ..cur }); } "scale" | "s" => { let v = s.state()?.zoom_frame(); let height = factor_height(s, p, v.height)?; s.state_mut()?.set_zoom_frame(View { height, ..v }); } "realtime" => { let v = s.state()?.zoom_frame(); let f = f64_req("zoom", p, "factor")?; if f <= 0.0 { return Err(bad("zoom", "factor must be positive")); } s.state_mut()?.set_zoom_frame(View { height: (v.height / f).clamp(1e-9, 1e12), ..v }); } "object" | "o" => { let hs = targets(s, p)?; let d = s.doc()?; let b = hs.iter().filter_map(|h| d.entity(*h)).fold(Bounds2::EMPTY, |a, e| a.union(&entity_bounds(d, e, 0))); fit(s, b.expand(b.height().max(b.width()) * 0.05))?; } _ => return Err(bad("zoom", "unknown mode")), } // No `touch`: a zoom changes the view, not the drawing. The canvas follows the view by // itself and regenerates in the background (AutoCAD redraws instead of regenerating); // zooming an unlocked viewport changes the drawing, which shows on its own. Ok(Value::Null) } /// The view height for a zoom `factor`: relative to the current height `cur` (nX), to paper space /// with `xp` (nXP; in a viewport `factor` paper units per model unit, elsewhere like nX), or to /// the limits with `limits` (a plain n: 1 fits the limits). fn factor_height(s: &Session, p: &Value, cur: f64) -> Result { let f = f64_req("zoom", p, "factor")?; if f <= 0.0 { return Err(bad("zoom", "factor must be positive")); } let st = s.state()?; if bool_or(p, "xp", false) && let Some(vp) = st.zoom_viewport() { return Ok(vp.height / f); } if bool_or(p, "limits", false) { let lim = s.zoom_limits()?; let (w, h) = s.viewport_px; let aspect = st.zoom_aspect(w / h.max(1.0)).max(1e-6); return Ok(lim.height().max(lim.width() / aspect).max(1e-9) / f); } Ok(cur / f) } /// A typed zoom factor: `n` (relative to the limits), `nX` (to the current view) or `nXP` (to /// paper space), as `zoom` scale parameters. fn typed_factor(t: &str) -> Option { let tl = t.trim().to_ascii_lowercase(); let (num, rel) = if let Some(n) = tl.strip_suffix("xp") { (n, "xp") } else if let Some(n) = tl.strip_suffix('x') { (n, "x") } else { (tl.as_str(), "limits") }; let f = number(num.trim()).filter(|f| *f > 0.0)?; Some(json!({ "factor": f, "xp": rel == "xp", "limits": rel == "limits" })) } fn with_mode(mut p: Value, mode: &str, center: Option) -> Value { if let Some(o) = p.as_object_mut() { o.insert("mode".into(), json!(mode)); if let Some(c) = center { o.insert("center".into(), json!([c.x, c.y])); } } p } fn run_zoom(s: &mut Session, p: &Value) -> Result { zoom(s, if p.is_null() { &Value::Null } else { p }) } fn pan_frac(s: &mut Session, f: Vec2) -> Result { let (w, h) = s.viewport_px; let st = s.state_mut()?; let v = st.view(); let ww = v.height * w / h.max(1.0); // Screen directions: in a twisted view the drawing is turned by the twist. st.set_view(View { center: v.center + Vec2::new(f.x * ww, f.y * v.height).rotate(-v.twist), ..v }); ok() } fn run_pan(s: &mut Session, p: &Value) -> Result { let st = s.state_mut()?; let v = st.view(); let c = if let Some(c) = point_param(p, "center") { c } else if let Some(d) = point_param(p, "delta") { v.center - d } else { let a = point_req("pan", p, "from")?; let b = point_req("pan", p, "to")?; v.center - (b - a) }; st.set_view(View { center: c, ..v }); ok() } fn run_view_set(s: &mut Session, p: &Value) -> Result { let c = point_req("view.set", p, "center")?; let h = f64_req("view.set", p, "height")?; if h <= 0.0 { return Err(bad("view.set", "height must be positive")); } let twist = match p.get("twist") { Some(t) => t.as_f64().filter(|t| t.is_finite()).ok_or_else(|| bad("view.set", "twist must be a number of degrees"))?.to_radians(), None => s.state()?.view().twist, }; // As DVIEW TWist: the Model tab's view or the active layout viewport turns; a layout sheet // (paper space) is never twisted. let target = match dview::target(s) { Ok(t) => Some(t), Err(_) if twist == 0.0 => None, Err(e) => return Err(e), }; let before = (s.state()?.doc.clone(), s.state()?.selection.clone()); let twist = match target { Some(t @ dview::Target::Viewport(_)) => { dview::set_twist(s, t, twist)?; s.state()?.view().twist } Some(dview::Target::Model) => cadcraft_geom::norm_angle(twist), None => 0.0, }; if !s.state_mut()?.set_view(View { center: c, height: h, twist }) { s.state_mut()?.doc = before.0; return Err(bad("view.set", "the view is too large to show. Nothing was changed")); } if let Some(dview::Target::Viewport(h)) = target { dview::record_viewport_edit(s, before, h)?; } ok() } fn run_layout_set(s: &mut Session, p: &Value) -> Result { let name = str_param(p, "name").unwrap_or("Model"); let space = if name.eq_ignore_ascii_case("model") { Space::Model } else { let l = s.doc()?.layout(name).ok_or_else(|| bad("layout.set", format!("no layout `{name}`")))?; Space::Paper(l.name.clone()) }; s.pending_window = None; let st = s.state_mut()?; st.space = space.clone(); st.mspace = None; st.selection.clear(); if let Space::Paper(n) = &space && !st.views.iter().any(|(sp, _)| sp == &space) { // Default paper view: the sheet. let page = st.doc.layout(n).map(|l| l.page.clone()).unwrap_or_default(); let (w, h) = if page.landscape { (page.height_mm, page.width_mm) } else { (page.width_mm, page.height_mm) }; let unit = if st.doc.header.i64("MEASUREMENT", 0) == 1 { 1.0 } else { 1.0 / 25.4 }; st.set_view_quiet(View { center: Vec2::new(w * unit / 2.0, h * unit / 2.0), height: h * unit * 1.15, twist: 0.0 }); } s.touch(); ok() } #[derive(Default)] struct ZoomM { window: bool, first: Option, stage: ZoomStage, /// Where the command starts (View > Zoom > Realtime / Dynamic skip the first prompt). start: ZoomStart, } #[derive(Clone, Copy, Default, PartialEq, Eq)] enum ZoomStart { #[default] Prompt, Realtime, Dynamic, } /// The Center, Scale, Realtime and Dynamic options' own prompts. #[derive(Clone, Copy, Default)] enum ZoomStage { #[default] Main, Center, CenterHeight(Vec2), Scale, Realtime, Dynamic(DynBox), } /// The prompt of Zoom Realtime; [`Session::realtime_zoom_active`] recognises the mode by it. const REALTIME_PROMPT: &str = "Drag up to zoom in or down to zoom out; press Esc or Enter to exit"; /// Zoom Dynamic: the view box shown over the drawing extents. A point places the box, the next /// one sizes it (its left edge stays), and so on; Enter shows what the box holds. #[derive(Clone, Copy)] struct DynBox { /// The screen view and the zoom frame before the overview. screen: View, frame: View, /// The drawing extents (the limits when empty). extents: Bounds2, center: Vec2, height: f64, /// Width / height of the area being zoomed. aspect: f64, /// The next point sizes the box (else it places it). sizing: bool, } impl DynBox { fn rect(c: Vec2, h: f64, aspect: f64) -> Bounds2 { let half = Vec2::new(h * aspect / 2.0, h / 2.0); Bounds2::new(c - half, c + half) } /// The box a point `p` makes: centred on it, or (sizing) reaching it from the left edge. fn moved(&self, p: Vec2) -> (Vec2, f64) { if !self.sizing { return (p, self.height); } let left = self.center.x - self.height * self.aspect / 2.0; let w = (p.x - left).max(self.height * self.aspect * 1e-3).max(1e-9); (Vec2::new(left + w / 2.0, self.center.y), w / self.aspect) } } impl ZoomM { /// Start Realtime: one view history entry for the whole drag session (Zoom Previous). fn realtime(&mut self, s: &mut Session) -> Result { let st = s.state_mut()?; let v = st.view(); st.view_history.push(v); if st.view_history.len() > 50 { st.view_history.remove(0); } self.stage = ZoomStage::Realtime; Ok(Step::Continue) } /// Start Dynamic: zoom out to show the extents and the current view, and put the view box /// where the view was. (Esc leaves the overview; Zoom Previous returns.) fn dynamic(&mut self, s: &mut Session) -> Result { let (w, h) = s.viewport_px; let st = s.state()?; let (screen, frame) = (st.view(), st.zoom_frame()); let aspect = st.zoom_aspect(w / h.max(1.0)).max(1e-6); let ext = ext_bounds(s)?; let extents = if ext.is_empty() { s.zoom_limits()? } else { ext }; let all = extents.union(&DynBox::rect(frame.center, frame.height, aspect)); fit(s, all.expand(all.height().max(all.width()) * 0.05))?; self.stage = ZoomStage::Dynamic(DynBox { screen, frame, extents, center: frame.center, height: frame.height, aspect, sizing: false }); Ok(Step::Continue) } } impl Interactive for ZoomM { fn name(&self) -> &'static str { "ZOOM" } fn begin(&mut self, s: &mut Session) -> Result { match self.start { ZoomStart::Prompt => Ok(Step::Continue), ZoomStart::Realtime => self.realtime(s), ZoomStart::Dynamic => self.dynamic(s), } } fn prompt(&self, s: &Session) -> Prompt { match self.stage { ZoomStage::Main => {} ZoomStage::Realtime => return Prompt::new(REALTIME_PROMPT, Accept { enter: true, ..Accept::default() }), ZoomStage::Dynamic(b) if b.sizing => { let left = Vec2::new(b.center.x - b.height * b.aspect / 2.0, b.center.y); return Prompt::new("Specify size of view box", Accept::POINT).default("accept").base(left); } ZoomStage::Dynamic(_) => return Prompt::new("Specify center of view box", Accept::POINT).default("accept"), ZoomStage::Center => return Prompt::new("Specify center point", Accept::POINT), ZoomStage::CenterHeight(_) => { let h = s.state().map(|st| st.zoom_frame().height).unwrap_or(1.0); return Prompt::new("Enter magnification or height", Accept::NUMBER).default(format!("{h:.4}")); } ZoomStage::Scale => return Prompt::new("Enter a scale factor (nX or nXP)", Accept::NUMBER), } match (self.window, self.first) { (false, None) => Prompt::new("Specify corner of window, enter a scale factor (nX or nXP)", Accept::POINT_OR_NUMBER) .kw(&["All", "Center", "Dynamic", "Extents", "Previous", "Scale", "Window", "Object"]) .default("real time"), (true, None) => Prompt::new("Specify first corner", Accept::POINT), (_, Some(a)) => Prompt::new("Specify opposite corner", Accept::POINT).corner(a), } } fn input(&mut self, s: &mut Session, i: Input) -> Result { let bad_factor = || crate::EngineError::Other("Requires a scale factor (n, nX or nXP).".into()); match (self.stage, &i) { (_, Input::Cancel) | (ZoomStage::Main, _) => {} (ZoomStage::Realtime, Input::Enter) => return Ok(Step::Done), (ZoomStage::Realtime, _) => return Ok(Step::Continue), (ZoomStage::Dynamic(b), Input::Enter) => { // Back to the view before the overview without a history entry, then zoom from it. let st = s.state_mut()?; st.set_view_quiet(b.screen); if st.view_history.last() == Some(&b.screen) { st.view_history.pop(); } st.set_zoom_frame(View { center: b.center, height: b.height, twist: b.frame.twist }); return Ok(Step::Done); } (ZoomStage::Dynamic(mut b), Input::Point(p)) => { (b.center, b.height) = b.moved(*p); b.sizing = !b.sizing; self.stage = ZoomStage::Dynamic(b); return Ok(Step::Continue); } (ZoomStage::Dynamic(_), _) => return Err(crate::EngineError::Other("Requires a point, or Enter to accept the view.".into())), (ZoomStage::Center, Input::Point(c)) => { self.stage = ZoomStage::CenterHeight(*c); return Ok(Step::Continue); } (ZoomStage::Center, Input::Enter) => return Ok(Step::Done), (ZoomStage::Center, _) => return Err(crate::EngineError::Other("Requires a point.".into())), (ZoomStage::CenterHeight(c), Input::Enter) => { zoom(s, &json!({ "mode": "center", "center": [c.x, c.y] }))?; return Ok(Step::Done); } (ZoomStage::CenterHeight(c), Input::Text(t)) => { let tl = t.trim().to_ascii_lowercase(); // A plain number is the new view height; nX / nXP a magnification. let p = if tl.ends_with('x') || tl.ends_with("xp") { typed_factor(&tl).ok_or_else(bad_factor)? } else { json!({ "height": number(&tl).filter(|h| *h > 0.0).ok_or_else(bad_factor)? }) }; zoom(s, &with_mode(p, "center", Some(c)))?; return Ok(Step::Done); } (ZoomStage::Scale, Input::Text(t)) => { zoom(s, &with_mode(typed_factor(t).ok_or_else(bad_factor)?, "scale", None))?; return Ok(Step::Done); } (ZoomStage::Scale, Input::Enter) => return Ok(Step::Done), (_, _) => return Err(bad_factor()), } match i { Input::Keyword(k) => { let mode = match k.as_str() { "All" => "all", "Extents" => "extents", "Previous" => "previous", "Dynamic" => return self.dynamic(s), "Window" => { self.window = true; return Ok(Step::Continue); } "Center" => { self.stage = ZoomStage::Center; return Ok(Step::Continue); } "Scale" => { self.stage = ZoomStage::Scale; return Ok(Step::Continue); } "Object" => "object", _ => return Err(crate::EngineError::Other("Invalid option keyword.".into())), }; zoom(s, &json!({ "mode": mode }))?; Ok(Step::Done) } Input::Point(p) => match self.first { None => { self.first = Some(p); Ok(Step::Continue) } Some(a) => { fit(s, Bounds2::new(a, p))?; Ok(Step::Done) } }, Input::Text(t) => { // nX: relative to the current view; nXP: to paper space; n: to the limits. let p = typed_factor(&t).ok_or_else(|| crate::EngineError::Other("Requires a point, scale factor or option keyword.".into()))?; zoom(s, &with_mode(p, "scale", None))?; Ok(Step::Done) } // Enter at the first prompt: Realtime (the default), as in AutoCAD. Input::Enter if !self.window && self.first.is_none() => self.realtime(s), Input::Enter => Ok(Step::Done), _ => Ok(Step::Continue), } } fn preview(&self, _s: &Session, c: Vec2) -> Vec { use super::helpers::{line, lwpoly, rect_vertices}; let rect = |r: Bounds2| lwpoly(rect_vertices(r.min, r.max), true); if let ZoomStage::Dynamic(b) = self.stage { // The extents, the view before, and the box: where it is and where the cursor puts it. let (center, height) = b.moved(c); let next = DynBox::rect(center, height, b.aspect); let mut out = vec![ rect(b.extents), rect(DynBox::rect(b.frame.center, b.frame.height, b.aspect)), rect(DynBox::rect(b.center, b.height, b.aspect)), rect(next), ]; if !b.sizing { out.push(line(next.min, next.max)); out.push(line(Vec2::new(next.min.x, next.max.y), Vec2::new(next.max.x, next.min.y))); } return out; } self.first.map(|a| vec![rect(Bounds2::new(a, c))]).unwrap_or_default() } } impl Session { /// ZOOM is in its Realtime mode: the UI turns vertical drags into [`Session::realtime_zoom`]. pub fn realtime_zoom_active(&self) -> bool { self.running.as_ref().is_some_and(|r| r.id.starts_with("zoom")) && self.current_prompt().is_some_and(|p| p.message == REALTIME_PROMPT) } /// One step of a Realtime zoom drag of `dy` pixels (down is positive): dragging up half the /// drawing area's height zooms in 2x, down zooms out, about the centre of the view. Does /// nothing outside Realtime. pub fn realtime_zoom(&mut self, dy: f64) -> Result<()> { if !self.realtime_zoom_active() || !dy.is_finite() { return Ok(()); } let half = (self.viewport_px.1 / 2.0).max(1.0); let f = 2f64.powf((-dy / half).clamp(-16.0, 16.0)); let st = self.state_mut()?; let v = st.view(); st.set_view_quiet(View { height: (v.height / f).clamp(1e-9, 1e12), ..v }); self.touch(); Ok(()) } /// The zoom of a mouse wheel scroll by `delta` points (up zooms in): ZOOMFACTOR scales it /// (60, the default, zooms about 18% per wheel step). pub fn wheel_zoom_factor(&self, delta: f64) -> f64 { if !delta.is_finite() { return 1.0; } let k = f64::from(self.settings.zoomfactor.clamp(3, 100)) / 60.0; (delta / 300.0 * k).clamp(-40.0, 40.0).exp() } /// Whether `3DORBIT` is currently running interactively. pub fn orbit_active(&self) -> bool { self.running.as_ref().is_some_and(|r| r.id == "3dorbit") } /// Rotate the 3D Orbit view by `(dx, dy)` screen pixels. pub fn orbit_drag(&mut self, dx: f64, dy: f64) -> Result<()> { if !(dx.is_finite() && dy.is_finite()) { return Ok(()); } let pivot = self.state()?.orbit.is_top().then(|| default_orbit_pivot(self)); let st = self.state_mut()?; if let Some(pivot) = pivot { st.orbit.pivot = pivot; } st.orbit.yaw = (st.orbit.yaw - dx * 0.008) % std::f64::consts::TAU; st.orbit.pitch = (st.orbit.pitch - dy * 0.008).clamp(0.0, std::f64::consts::PI); if st.orbit.is_top() { st.orbit = cadcraft_render::Orbit3d::default(); } self.touch(); Ok(()) } } /// Default 3D rotation pivot for the active drawing space: uses the current view centre when inside /// the drawing extents (or the extents centre otherwise) and the mid-Z of the space's entities. fn default_orbit_pivot(s: &Session) -> cadcraft_geom::Vec3 { let Ok(st) = s.state() else { return cadcraft_geom::Vec3::ZERO; }; let space = st.edit_space(); let ext = st.doc.extents(&space); let view_c = st.view().center; let xy = if !ext.is_empty() && ext.contains(view_c) { view_c } else if !ext.is_empty() { ext.center() } else { view_c }; let (mut min_z, mut max_z, mut any_z) = (f64::INFINITY, f64::NEG_INFINITY, false); if let Some(store) = st.doc.space(&space) { let mut add_z = |z: f64| { if z.is_finite() { min_z = min_z.min(z); max_z = max_z.max(z); any_z = true; } }; for e in store.iter() { match &e.kind { cadcraft_doc::EntityKind::Line(l) => { add_z(l.a.z); add_z(l.b.z); } cadcraft_doc::EntityKind::Polyline3d(p) => { for pt in &p.points { add_z(pt.z); } } cadcraft_doc::EntityKind::LwPolyline(p) => add_z(p.elevation), cadcraft_doc::EntityKind::Point(p) => add_z(p.p.z), cadcraft_doc::EntityKind::Circle(c) => add_z(c.center.z), cadcraft_doc::EntityKind::Arc(a) => add_z(a.center.z), cadcraft_doc::EntityKind::Insert(i) => add_z(i.insert.z), cadcraft_doc::EntityKind::Text(t) => add_z(t.insert.z), cadcraft_doc::EntityKind::MText(t) => add_z(t.insert.z), _ => {} } } } let z = if any_z { (min_z + max_z) * 0.5 } else { 0.0 }; cadcraft_geom::Vec3::new(xy.x, xy.y, z) } /// Execute `3DORBIT` (`ORBIT`, `3DO`): resets to Top view, applies a named 3D preset, or sets /// explicit `yaw` and `pitch` angles in degrees. fn run_3dorbit(s: &mut Session, p: &Value) -> Result { if p.get("reset").and_then(Value::as_bool) == Some(true) { let st = s.state_mut()?; st.orbit = cadcraft_render::Orbit3d::default(); s.touch(); return Ok(json!({ "yaw": 0.0, "pitch": 0.0, "label": "TOP" })); } let pivot = default_orbit_pivot(s); let st = s.state_mut()?; let iso_pitch = (2.0_f64.sqrt()).atan(); // ~54.7356° if let Some(preset) = str_param(p, "preset") { let (yaw_deg, pitch_rad) = match preset.to_ascii_lowercase().as_str() { "top" | "plan" | "reset" => (0.0_f64, 0.0_f64), "bottom" => (0.0, std::f64::consts::PI), "front" => (0.0, std::f64::consts::FRAC_PI_2), "back" => (180.0, std::f64::consts::FRAC_PI_2), "left" => (90.0, std::f64::consts::FRAC_PI_2), "right" => (-90.0, std::f64::consts::FRAC_PI_2), "swiso" | "iso" => (45.0, iso_pitch), "seiso" => (-45.0, iso_pitch), "neiso" => (-135.0, iso_pitch), "nwiso" => (135.0, iso_pitch), other => return Err(bad("3dorbit", format!("unknown 3D view preset `{other}`"))), }; st.orbit = if yaw_deg == 0.0 && pitch_rad == 0.0 { cadcraft_render::Orbit3d::default() } else { cadcraft_render::Orbit3d { yaw: yaw_deg.to_radians(), pitch: pitch_rad, pivot } }; let (y, pt, lbl) = (st.orbit.yaw.to_degrees(), st.orbit.pitch.to_degrees(), st.orbit.label()); s.touch(); return Ok(json!({ "yaw": y, "pitch": pt, "label": lbl })); } if st.orbit.is_top() { st.orbit.pivot = pivot; } if let Some(yaw_deg) = p.get("yaw").and_then(Value::as_f64).filter(|v| v.is_finite()) { st.orbit.yaw = yaw_deg.to_radians(); } if let Some(pitch_deg) = p.get("pitch").and_then(Value::as_f64).filter(|v| v.is_finite()) { st.orbit.pitch = pitch_deg.to_radians().clamp(0.0, std::f64::consts::PI); } if st.orbit.is_top() { st.orbit = cadcraft_render::Orbit3d::default(); } let (y, pt, lbl) = (st.orbit.yaw.to_degrees(), st.orbit.pitch.to_degrees(), st.orbit.label()); s.touch(); Ok(json!({ "yaw": y, "pitch": pt, "label": lbl })) } struct OrbitM; impl Interactive for OrbitM { fn name(&self) -> &'static str { "3DORBIT" } fn prompt(&self, _s: &Session) -> Prompt { Prompt::new("Press and drag to orbit 3D view, or choose preset (Esc or Enter to exit)", Accept::TEXT) .kw(&["Top", "Front", "Right", "Left", "Back", "SEiso", "SWiso", "Reset"]) } fn input(&mut self, s: &mut Session, i: Input) -> Result { match i { Input::Enter | Input::Cancel => Ok(Step::Done), Input::Keyword(k) => { let preset = match k.as_str() { "Top" | "Reset" => "top", "Front" => "front", "Right" => "right", "Left" => "left", "Back" => "back", "SEiso" => "seiso", "SWiso" => "swiso", _ => "top", }; run_3dorbit(s, &json!({ "preset": preset }))?; Ok(Step::Continue) } _ => Ok(Step::Continue), } } } #[derive(Default)] struct PanM { first: Option, } impl Interactive for PanM { fn name(&self) -> &'static str { "PAN" } fn prompt(&self, _s: &Session) -> Prompt { match self.first { None => Prompt::new("Specify base point or displacement (drag with the middle mouse button for realtime pan)", Accept::POINT), Some(a) => Prompt::new("Specify second point", Accept::POINT).base(a), } } fn input(&mut self, s: &mut Session, i: Input) -> Result { match (self.first, i) { (None, Input::Point(p)) => { self.first = Some(p); Ok(Step::Continue) } (Some(a), Input::Point(b)) => { run_pan(s, &json!({ "from": [a.x, a.y], "to": [b.x, b.y] }))?; Ok(Step::Done) } (_, Input::Enter) => Ok(Step::Done), _ => Ok(Step::Continue), } } } #[derive(Default)] struct ViewresM { step: u8, } impl Interactive for ViewresM { fn name(&self) -> &'static str { "VIEWRES" } fn prompt(&self, _s: &Session) -> Prompt { if self.step == 0 { Prompt::new("Do you want fast zooms?", Accept::TEXT).kw(&["Yes", "No"]).default("Y") } else { Prompt::new("Enter circle zoom percent (1-20000)", Accept::NUMBER).default("1000") } } fn input(&mut self, s: &mut Session, i: Input) -> Result { match i { Input::Cancel => Ok(Step::Cancel), _ if self.step == 0 => { self.step = 1; Ok(Step::Continue) } _ => { s.touch(); Ok(Step::Done) } } } }