Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions crates/engine/src/cmd/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::machines::{SelOutcome, SelectPhase, number};
use super::{CommandSpec, bad, bool_or, f64_or, find_command, ok, point_param, point_req, point_value, str_param, targets};

use super::has_doc;
use crate::{Accept, EngineError, Input, Interactive, Prompt, Result, Session, Step};
use crate::{Accept, AttribAsk, AttribEntry, EngineError, Input, Interactive, Prompt, Result, Session, Step};

mod attedit;
mod burst;
Expand Down Expand Up @@ -588,7 +588,15 @@ struct InsertM {
x_scale: Option<f64>,
scale: Option<Vec2>,
rotation: Option<f64>,
attdefs: Vec<(String, String, String)>,
/// The attributes to ask for, in order: not the constant or Preset ones (they take their
/// value without asking), none with ATTREQ 0 (every one takes its default).
attdefs: Vec<AttribAsk>,
/// How many of `attdefs` have their value.
asked: usize,
/// A Verify attribute's value waiting to be confirmed.
verifying: Option<String>,
/// ATTDIA 1: the values are asked for in the Edit Attributes dialog.
dialog: bool,
values: serde_json::Map<String, Value>,
/// `*name`: insert the block's objects (one uniform scale factor, no attribute values).
explode: bool,
Expand All @@ -607,7 +615,7 @@ impl InsertM {
InsStage::Attribs
};
if self.stage == InsStage::Attribs
&& self.values.len() >= self.attdefs.len()
&& self.asked >= self.attdefs.len()
&& let (Some(n), Some(a), Some(sc), Some(r)) = (self.name.clone(), self.at, self.scale, self.rotation)
{
let h = insert_with(s, &n, a, sc, r, self.base, &self.values)?;
Expand All @@ -625,20 +633,24 @@ impl InsertM {
s.echo(format!("Block \"{name}\" not found."));
return Ok(());
};
self.attdefs = if self.explode {
let ask = s.settings.attreq;
self.dialog = s.settings.attdia;
self.attdefs = if self.explode || !ask {
Vec::new()
} else {
b.entities
.iter()
.filter_map(|e| {
if let EntityKind::AttDef(a) = &e.kind {
(!a.constant).then(|| (a.tag.clone(), a.prompt.clone(), a.text.value.clone()))
} else {
None
.filter_map(|e| match &e.kind {
EntityKind::AttDef(a) if !a.constant && !a.modes.preset => {
Some(AttribAsk { tag: a.tag.clone(), prompt: a.prompt.clone(), default: a.text.value.clone(), verify: a.modes.verify })
}
_ => None,
})
.collect()
};
self.asked = 0;
self.verifying = None;
self.values.clear();
s.doc_mut()?.header.set_str("INSNAME", &b.name);
self.name = Some(b.name.clone());
self.stage = InsStage::Point;
Expand Down Expand Up @@ -697,8 +709,11 @@ impl Interactive for InsertM {
InsStage::YScale => Prompt::new("Enter Y scale factor", Accept::NUMBER).default("use X scale factor"),
InsStage::Rotation => Prompt::new("Specify rotation angle", Accept::POINT_OR_NUMBER).default("0").base(at),
InsStage::Attribs => {
let (_, prompt, default) = self.attdefs.get(self.values.len()).cloned().unwrap_or_default();
Prompt::new(if prompt.is_empty() { "Enter attribute value".into() } else { prompt }, Accept::TEXT).default(default)
if let Some(v) = &self.verifying {
return Prompt::new("Verify attribute value", Accept::TEXT).default(v.clone());
}
let a = self.attdefs.get(self.asked).cloned().unwrap_or_default();
Prompt::new(if a.prompt.is_empty() { "Enter attribute value".into() } else { a.prompt }, Accept::TEXT).default(a.default)
}
}
}
Expand Down Expand Up @@ -816,12 +831,24 @@ impl Interactive for InsertM {
return self.advance(s);
}
InsStage::Attribs => {
let (tag, _, default) = self.attdefs.get(self.values.len()).cloned().unwrap_or_default();
let v = match i {
Input::Text(t) => t,
_ => default,
let a = self.attdefs.get(self.asked).cloned().unwrap_or_default();
let v = match (i, self.verifying.take()) {
// Verify: Enter keeps the value given, a typed one replaces it.
(Input::Text(t), Some(_)) => t,
(_, Some(v)) => v,
(Input::Text(t), None) if a.verify => {
self.verifying = Some(t);
return Ok(Step::Continue);
}
(_, None) if a.verify => {
self.verifying = Some(a.default);
return Ok(Step::Continue);
}
(Input::Text(t), None) => t,
(_, None) => a.default,
};
self.values.insert(tag, Value::String(v));
self.values.insert(a.tag, Value::String(v));
self.asked += 1;
return self.advance(s);
}
}
Expand All @@ -843,6 +870,13 @@ impl Interactive for InsertM {
let rot = self.rotation.unwrap_or(if self.stage == InsStage::Rotation { at.angle_to(c) } else { 0.0 });
vec![EntityKind::Insert(insert_entity(b, at, scale, rot, self.base))]
}
fn attribute_entry(&self) -> Option<AttribEntry> {
// Only before the first answer: values typed at the command line go on there.
if !self.dialog || self.stage != InsStage::Attribs || self.asked > 0 || self.verifying.is_some() || self.attdefs.is_empty() {
return None;
}
Some(AttribEntry { block: self.name.clone().unwrap_or_default(), rows: self.attdefs.clone() })
}
}

#[derive(Default)]
Expand Down
9 changes: 9 additions & 0 deletions crates/engine/src/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ impl Interactive for Guarded {
None
})
}
fn attribute_entry(&self) -> Option<crate::prompt::AttribEntry> {
if self.broken.get().is_some() {
return None;
}
catch_unwind(AssertUnwindSafe(|| self.inner.attribute_entry())).unwrap_or_else(|_| {
self.broken.set(Some("panic in prompt"));
None
})
}
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub use cadcraft_geom as geom;
pub use cadcraft_render as render;
pub use cmd::hatch::{HatchPicks, PatternChoice, PickMode, pattern_choices};
pub use cmd::{CommandInfo, CommandSpec, command_specs, find_command};
pub use prompt::{Accept, Drag, Input, Interactive, Picker, Prompt, Step};
pub use prompt::{Accept, AttribAsk, AttribEntry, Drag, Input, Interactive, Picker, Prompt, Step};

mod doc_state;
mod error;
Expand Down
40 changes: 40 additions & 0 deletions crates/engine/src/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,46 @@ pub trait Interactive: Send {
fn editing_mtext(&self) -> Option<cadcraft_doc::MText> {
None
}
/// The attribute values the command waits for, all at once, when the UI should ask for them
/// in the Edit Attributes dialog (INSERT with ATTDIA 1). Typed answers still work.
fn attribute_entry(&self) -> Option<AttribEntry> {
None
}
}

/// See [`Interactive::attribute_entry`]: the block being inserted and the attributes asked for,
/// in order.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AttribEntry {
pub block: String,
pub rows: Vec<AttribAsk>,
}

/// One attribute an INSERT asks for: its tag, prompt and default value; Verify ones are asked
/// twice at the command line.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct AttribAsk {
pub tag: String,
pub prompt: String,
pub default: String,
pub verify: bool,
}

impl AttribEntry {
/// The answers that give the attributes `values` (one per row, in order; a missing one is
/// the default) when typed in turn: a Verify attribute's value twice, as the dialog has
/// already shown it.
pub fn answers(&self, values: &[String]) -> Vec<String> {
let mut out = Vec::with_capacity(self.rows.len().saturating_mul(2));
for (i, r) in self.rows.iter().enumerate() {
let v = values.get(i).cloned().unwrap_or_else(|| r.default.clone());
if r.verify {
out.push(v.clone());
}
out.push(v);
}
out
}
}

/// See [`Interactive::drag`].
Expand Down
6 changes: 6 additions & 0 deletions crates/engine/src/session/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,12 @@ impl Session {
self.running.as_ref().and_then(|r| r.machine.editing_mtext())
}

/// The attribute values the running command asks for in the Edit Attributes dialog
/// ([`Interactive::attribute_entry`]).
pub fn attribute_entry(&self) -> Option<crate::AttribEntry> {
self.running.as_ref().and_then(|r| r.machine.attribute_entry())
}

pub fn current_prompt(&self) -> Option<Prompt> {
if let Some(p) = self.lisp_prompt() {
return Some(p);
Expand Down
8 changes: 8 additions & 0 deletions crates/engine/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ pub struct Settings {
pub polardist: f64,
/// AUDITCTL: 1 creates an audit report (.adt) file when AUDIT is run (default: 0).
pub auditctl: bool,
/// ATTREQ: INSERT asks for attribute values (on by default); off gives every attribute its
/// default value. A user setting, as in AutoCAD (not saved in the drawing).
pub attreq: bool,
/// ATTDIA: INSERT typed or from a menu asks for attribute values in the Edit Attributes
/// dialog; off (the default, as in AutoCAD) asks at the command line.
pub attdia: bool,
}

impl Default for Settings {
Expand Down Expand Up @@ -191,6 +197,8 @@ impl Default for Settings {
polarsnap: false,
polardist: 0.0,
auditctl: false,
attreq: true,
attdia: false,
}
}
}
10 changes: 8 additions & 2 deletions crates/engine/src/sysvars/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,14 @@ const LINEWEIGHTS: [i64; 24] = [0, 5, 9, 13, 15, 18, 20, 25, 30, 35, 40, 50, 53,

/// On/off session settings: name, read, write.
type Toggle = (&'static str, fn(&Settings) -> bool, fn(&mut Settings, bool));
const SETTINGS: &[Toggle] =
&[("AUDITCTL", |s| s.auditctl, |s, v| s.auditctl = v), ("TRANSPARENCYDISPLAY", |s| s.transparency_display, |s, v| s.transparency_display = v)];
const SETTINGS: &[Toggle] = &[
// ATTDIA 1: INSERT typed or from a menu asks for attribute values in the Edit Attributes
// dialog; 0 (the default): at the command line. ATTREQ 0: INSERT gives every attribute its default.
("ATTDIA", |s| s.attdia, |s, v| s.attdia = v),
("ATTREQ", |s| s.attreq, |s, v| s.attreq = v),
("AUDITCTL", |s| s.auditctl, |s, v| s.auditctl = v),
("TRANSPARENCYDISPLAY", |s| s.transparency_display, |s, v| s.transparency_display = v),
];

fn header_var(name: &str) -> Option<&'static (&'static str, Kind, Def)> {
HEADER.iter().find(|(n, _, _)| *n == name)
Expand Down
Loading