cad-editor/src/modules/layout/mview.rs

116 lines
3.7 KiB
Rust
Raw Normal View History

2026-03-22 14:56:27 +03:00
// MVIEW — interactive paper-space viewport creation.
//
// Two clicks define opposite corners of a new viewport rectangle.
// The created Viewport entity is routed to add_entity_to_layout by apply_cmd_result
// because we're in paper space.
use acadrust::entities::Viewport;
use acadrust::types::Vector3;
use acadrust::EntityType;
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::model::wire_model::WireModel;
use glam::DVec3;
2026-03-22 14:56:27 +03:00
// ── Ribbon definition ─────────────────────────────────────────────────────
pub fn tool() -> ToolDef {
ToolDef {
id: "MVIEW",
label: "Viewport",
icon: IconKind::Svg(include_bytes!("../../../assets/icons/viewport.svg")),
2026-03-22 14:56:27 +03:00
event: ModuleEvent::Command("MVIEW".to_string()),
}
}
// ── Command ───────────────────────────────────────────────────────────────
pub struct MviewCommand {
corner1: Option<DVec3>,
2026-03-22 14:56:27 +03:00
}
impl MviewCommand {
pub fn new() -> Self {
Self { corner1: None }
}
}
impl CadCommand for MviewCommand {
fn name(&self) -> &'static str {
"MVIEW"
}
fn prompt(&self) -> String {
if self.corner1.is_none() {
"MVIEW Specify first corner:".to_string()
2026-03-22 14:56:27 +03:00
} else {
"MVIEW Specify opposite corner:".to_string()
2026-03-22 14:56:27 +03:00
}
}
fn on_point(&mut self, pt: DVec3) -> CmdResult {
2026-03-22 14:56:27 +03:00
if let Some(c1) = self.corner1 {
let w = (pt.x - c1.x).abs();
let h = (pt.y - c1.y).abs();
2026-03-22 14:56:27 +03:00
if w < 1.0 || h < 1.0 {
return CmdResult::Cancel;
}
let cx = (c1.x + pt.x) / 2.0;
let cy = (c1.y + pt.y) / 2.0;
let cz = c1.z;
2026-03-22 14:56:27 +03:00
let mut vp = Viewport::new();
vp.center = Vector3::new(cx, cy, cz);
vp.width = w;
vp.height = h;
vp.id = 2; // user viewport (id > 1)
CmdResult::CommitAndExit(EntityType::Viewport(vp))
} else {
self.corner1 = Some(pt);
CmdResult::NeedPoint
}
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_mouse_move(&mut self, pt: DVec3) -> Option<WireModel> {
let pt = pt.as_vec3();
2026-03-22 14:56:27 +03:00
let c1 = self.corner1?;
Some(WireModel {
dash_from_start: false,
feat(mline): GPU-dash multiline elements with shared A-type alignment MLINE dashed elements were fully CPU-expanded (apply_along) — one wire per dash — to keep the parallel lines in phase. Move pure-dash elements onto the GPU dash shader and end-align every element to the multiline's centre-line so they share one interior phase. - wire_distances now accumulates arc-length from the double-single (high+low) points, so the dash phase stays precise at UTM coordinates. f32-high-only deltas quantised ~0.1 there and drifted the parallel lines apart — the original reason MLINE dashes were CPU-side. - New WireModel.dash_align_end carries a shared begin/end dash length. The shader uses it as align_end for every parallel element (the interior phase depends on align_end, not the wire's own length) while align_total stays the element's own length, so each still ends on a dash at its own endpoint. - MLINE tessellation derives that length once from the centre-line, routes pure dash/space/dot elements to the GPU pattern and text-bearing elements to apply_along. - apply_along gained an "A"-type mode (shared reference length): a solid begin/end dash and a phased interior, so its dashes — and embedded text — line up with the GPU-dashed sibling elements instead of tiling from start. Pure-dash MLINEs now cost one WireModel + pattern per element instead of N segments; single (non-MLINE) entities keep the from-start tiling unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 02:59:11 +03:00
dash_align_end: None,
text_verts: Vec::new(),
2026-03-22 14:56:27 +03:00
name: "mview_preview".to_string(),
points: vec![
[c1.x as f32, c1.y as f32, c1.z as f32],
[pt.x, c1.y as f32, c1.z as f32],
[pt.x, pt.y, c1.z as f32],
[c1.x as f32, pt.y, c1.z as f32],
[c1.x as f32, c1.y as f32, c1.z as f32],
2026-03-22 14:56:27 +03:00
],
points_low: Vec::new(),
2026-03-22 14:56:27 +03:00
color: WireModel::CYAN,
selected: false,
pattern_length: 0.0,
pattern: [0.0; 8],
line_weight_px: 1.0,
snap_pts: vec![],
tangent_geoms: vec![],
aci: 0,
2026-03-22 14:56:27 +03:00
key_vertices: vec![],
aabb: WireModel::UNBOUNDED_AABB,
plinegen: true,
vp_scissor: None,
fill_tris: vec![],
fill_tris_low: Vec::new(),
2026-03-22 14:56:27 +03:00
})
}
}
// ── Autocomplete registry ─────────────────────────────────
inventory::submit!(crate::command::CommandRegistration { names: &["MVIEW"] }); // MviewCommand