forked from 1b-agent/opencadstudio-upstream
feat(text): in-place editors for all text entities
Route every text-bearing entity to an in-place editor instead of the command line: a plain text box for single-line text (Text, attributes, dimension override, tolerance) and the rich MText editor for MText / MultiLeader. A Leader resolves to the entity it annotates. - TEXT places a plain text box at the click; double-click edits in place. - DDEDIT and double-click on any text entity open the matching editor. - MLEADER places the leader then opens the MText editor on its content. - LEADER follows the classic flow: points, Enter, then a linked empty MText annotation opens in the editor; the leader's annotation_handle links the pair so they edit/erase as a unit. Adds shared field read/write helpers and a begin_text_edit router, plus CommitAndEditText / CommitManyAndEditText results that commit an entity and open its editor (commit_entity now returns the new handle). Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
1 parent
ac9022676f
commit
2627d06224
13 files changed
+551
-346
No files matched your search
@@ -1,10 +1,13 @@
|
||||
// DDEDIT — edit text content of a Text or MText entity in-place.
|
||||
// DDEDIT — edit the text content of a text-bearing entity in-place.
|
||||
//
|
||||
// Workflow:
|
||||
// 1. Pick a Text or MText entity (or fire from double-click with handle pre-set).
|
||||
// 2. Enter new text. Press Enter to commit, Escape to cancel.
|
||||
// 1. Pick a text entity (or fire from double-click / a current selection).
|
||||
// 2. The picked entity opens its in-place editor: a plain text box for
|
||||
// single-line text (Text, attributes, dimension override, tolerance) or
|
||||
// the rich MText editor for MText / MultiLeader. A Leader resolves to the
|
||||
// entity it annotates.
|
||||
|
||||
use acadrust::{EntityType, Handle};
|
||||
use acadrust::Handle;
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
@@ -21,27 +24,11 @@ pub fn tool() -> ToolDef {
|
||||
}
|
||||
}
|
||||
|
||||
enum DdeditStep {
|
||||
PickEntity,
|
||||
EnterText { handle: Handle, current: String },
|
||||
}
|
||||
|
||||
pub struct DdeditCommand {
|
||||
step: DdeditStep,
|
||||
}
|
||||
pub struct DdeditCommand;
|
||||
|
||||
impl DdeditCommand {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
step: DdeditStep::PickEntity,
|
||||
}
|
||||
}
|
||||
|
||||
/// Start with a pre-picked entity (for double-click use).
|
||||
pub fn with_handle(handle: Handle, current: String) -> Self {
|
||||
Self {
|
||||
step: DdeditStep::EnterText { handle, current },
|
||||
}
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,51 +38,20 @@ impl CadCommand for DdeditCommand {
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
match &self.step {
|
||||
DdeditStep::PickEntity => "DDEDIT Select text entity:".into(),
|
||||
DdeditStep::EnterText { current, .. } => {
|
||||
format!("DDEDIT Enter new text <{current}>:")
|
||||
}
|
||||
}
|
||||
"DDEDIT Select text entity:".into()
|
||||
}
|
||||
|
||||
fn needs_entity_pick(&self) -> bool {
|
||||
matches!(self.step, DdeditStep::PickEntity)
|
||||
true
|
||||
}
|
||||
|
||||
fn on_entity_pick(&mut self, handle: Handle, _pt: Vec3) -> CmdResult {
|
||||
if handle.is_null() {
|
||||
return CmdResult::NeedPoint;
|
||||
}
|
||||
// The current value will be filled in by the caller (commands.rs dispatch)
|
||||
// via on_text_input once the entity is known. Store handle here.
|
||||
self.step = DdeditStep::EnterText {
|
||||
handle,
|
||||
current: String::new(),
|
||||
};
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
|
||||
fn wants_text_input(&self) -> bool {
|
||||
matches!(self.step, DdeditStep::EnterText { .. })
|
||||
}
|
||||
|
||||
fn wants_text_with_spaces(&self) -> bool {
|
||||
matches!(self.step, DdeditStep::EnterText { .. })
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||
let (handle, current) = match &self.step {
|
||||
DdeditStep::EnterText { handle, current } => (*handle, current.clone()),
|
||||
_ => return None,
|
||||
};
|
||||
// Empty input → keep existing text
|
||||
let new_text = if text.trim().is_empty() {
|
||||
current
|
||||
} else {
|
||||
text.to_string()
|
||||
};
|
||||
Some(CmdResult::DdeditEntity { handle, new_text })
|
||||
// Hand the picked entity to the in-place editor (plain box or rich
|
||||
// MText editor, chosen by the entity's type).
|
||||
CmdResult::EditTextEntity { handle }
|
||||
}
|
||||
|
||||
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
|
||||
@@ -108,14 +64,3 @@ impl CadCommand for DdeditCommand {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract the text content from a Text or MText entity.
|
||||
pub fn entity_text(entity: &EntityType) -> Option<String> {
|
||||
match entity {
|
||||
EntityType::Text(t) => Some(t.value.clone()),
|
||||
EntityType::MText(t) => Some(t.value.clone()),
|
||||
EntityType::AttributeDefinition(a) => Some(a.default_value.clone()),
|
||||
EntityType::AttributeEntity(a) => Some(a.get_value().to_string()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,16 @@
|
||||
// LEADER command
|
||||
//
|
||||
// Flow:
|
||||
// 1. CollectPoints — click arrowhead, then bend points; Enter (≥2) to finish
|
||||
// 2. AskCreationType— wants_text_input; N/T/B/TL → default Text on blank Enter
|
||||
// 3. AskAnnotation — text string (Text) or block name (Block); blank = skip
|
||||
// → commit Leader [+ MText | + Insert]
|
||||
// Flow (matching the classic AutoCAD LEADER):
|
||||
// 1. Click the arrowhead point, then one or more bend points.
|
||||
// 2. Enter (≥2 points) places the leader line plus a linked, empty MText
|
||||
// annotation at the landing, and opens the in-place MText editor so the
|
||||
// user types the annotation. Escape leaves the leader without text.
|
||||
//
|
||||
// The MText is a separate entity referenced by the leader's annotation_handle
|
||||
// (DXF group 340); editing/erasing them stays in sync via that link.
|
||||
|
||||
use acadrust::entities::{Insert, Leader, LeaderCreationType, MText};
|
||||
use acadrust::entities::mtext::AttachmentPoint;
|
||||
use acadrust::entities::{Leader, LeaderCreationType, MText};
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::EntityType;
|
||||
use glam::Vec3;
|
||||
@@ -26,22 +30,13 @@ pub fn tool() -> ToolDef {
|
||||
}
|
||||
}
|
||||
|
||||
enum Step {
|
||||
CollectPoints { verts: Vec<Vec3> },
|
||||
AskCreationType { verts: Vec<Vec3> },
|
||||
AskText { verts: Vec<Vec3> },
|
||||
AskBlock { verts: Vec<Vec3> },
|
||||
}
|
||||
|
||||
pub struct LeaderCommand {
|
||||
step: Step,
|
||||
verts: Vec<Vec3>,
|
||||
}
|
||||
|
||||
impl LeaderCommand {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
step: Step::CollectPoints { verts: Vec::new() },
|
||||
}
|
||||
Self { verts: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,102 +46,33 @@ impl CadCommand for LeaderCommand {
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
match &self.step {
|
||||
Step::CollectPoints { verts } if verts.is_empty() => {
|
||||
"LEADER Specify arrowhead point:".into()
|
||||
}
|
||||
Step::CollectPoints { verts } => format!(
|
||||
"LEADER Specify next point [{} pts — Enter to finish]:",
|
||||
verts.len()
|
||||
),
|
||||
Step::AskCreationType { .. } => {
|
||||
"LEADER Annotation type [None/Text/Block/Tolerance] <Text>:".into()
|
||||
}
|
||||
Step::AskText { verts } => format!(
|
||||
"LEADER Annotation text [{} pts — blank = skip]:",
|
||||
verts.len()
|
||||
),
|
||||
Step::AskBlock { verts } => {
|
||||
format!("LEADER Block name [{} pts — blank = skip]:", verts.len())
|
||||
}
|
||||
if self.verts.is_empty() {
|
||||
"LEADER Specify arrowhead point:".into()
|
||||
} else {
|
||||
format!(
|
||||
"LEADER Specify next point [{} pts — Enter to place text]:",
|
||||
self.verts.len()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn wants_text_input(&self) -> bool {
|
||||
!matches!(self.step, Step::CollectPoints { .. })
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
||||
if let Step::CollectPoints { verts } = &mut self.step {
|
||||
verts.push(pt);
|
||||
}
|
||||
self.verts.push(pt);
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
if let Step::CollectPoints { verts } = &self.step {
|
||||
if verts.len() < 2 {
|
||||
return CmdResult::Cancel;
|
||||
}
|
||||
let verts = verts.clone();
|
||||
self.step = Step::AskCreationType { verts };
|
||||
CmdResult::NeedPoint
|
||||
} else {
|
||||
CmdResult::Cancel
|
||||
if self.verts.len() < 2 {
|
||||
return CmdResult::Cancel;
|
||||
}
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, raw: &str) -> Option<CmdResult> {
|
||||
let text = raw.trim();
|
||||
match &self.step {
|
||||
Step::AskCreationType { verts } => {
|
||||
let verts = verts.clone();
|
||||
match parse_ct(text) {
|
||||
LeaderCreationType::NoAnnotation | LeaderCreationType::WithTolerance => {
|
||||
let ct = parse_ct(text);
|
||||
Some(CmdResult::CommitAndExit(EntityType::Leader(build_leader(
|
||||
&verts, ct,
|
||||
))))
|
||||
}
|
||||
LeaderCreationType::WithBlock => {
|
||||
self.step = Step::AskBlock { verts };
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
LeaderCreationType::WithText => {
|
||||
self.step = Step::AskText { verts };
|
||||
Some(CmdResult::NeedPoint)
|
||||
}
|
||||
}
|
||||
}
|
||||
Step::AskText { verts } => {
|
||||
let verts = verts.clone();
|
||||
let leader = build_leader(&verts, LeaderCreationType::WithText);
|
||||
if text.is_empty() {
|
||||
return Some(CmdResult::CommitAndExit(EntityType::Leader(leader)));
|
||||
}
|
||||
let mtext = build_mtext(
|
||||
text,
|
||||
landing_pt(&verts, leader.text_height),
|
||||
leader.text_height,
|
||||
);
|
||||
Some(CmdResult::ReplaceMany(
|
||||
vec![],
|
||||
vec![EntityType::Leader(leader), EntityType::MText(mtext)],
|
||||
))
|
||||
}
|
||||
Step::AskBlock { verts } => {
|
||||
let verts = verts.clone();
|
||||
let leader = build_leader(&verts, LeaderCreationType::WithBlock);
|
||||
if text.is_empty() {
|
||||
return Some(CmdResult::CommitAndExit(EntityType::Leader(leader)));
|
||||
}
|
||||
let ins = Insert::new(text, v3(landing_pt(&verts, leader.text_height)));
|
||||
Some(CmdResult::ReplaceMany(
|
||||
vec![],
|
||||
vec![EntityType::Leader(leader), EntityType::Insert(ins)],
|
||||
))
|
||||
}
|
||||
Step::CollectPoints { .. } => None,
|
||||
// Place the leader plus an empty MText annotation, link them, then open
|
||||
// the in-place MText editor so the user types the annotation text.
|
||||
let leader = build_leader(&self.verts);
|
||||
let (anchor, attach) = annotation_anchor(&self.verts, leader.text_height);
|
||||
let mtext = build_mtext("", anchor, leader.text_height, attach);
|
||||
CmdResult::CommitManyAndEditText {
|
||||
entities: vec![EntityType::Leader(leader), EntityType::MText(mtext)],
|
||||
edit_index: 1,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,53 +81,51 @@ impl CadCommand for LeaderCommand {
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
|
||||
if let Step::CollectPoints { verts } = &self.step {
|
||||
if verts.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut pts = verts.clone();
|
||||
pts.push(pt);
|
||||
Some(preview_wire(&pts))
|
||||
} else {
|
||||
None
|
||||
if self.verts.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut pts = self.verts.clone();
|
||||
pts.push(pt);
|
||||
Some(preview_wire(&pts))
|
||||
}
|
||||
}
|
||||
|
||||
// ── Helpers ─────────────────────────────────────────────────────────────────
|
||||
|
||||
fn parse_ct(s: &str) -> LeaderCreationType {
|
||||
match s.to_ascii_uppercase().as_str() {
|
||||
"N" | "NONE" => LeaderCreationType::NoAnnotation,
|
||||
"B" | "BLOCK" => LeaderCreationType::WithBlock,
|
||||
"TL" | "TOLERANCE" => LeaderCreationType::WithTolerance,
|
||||
_ => LeaderCreationType::WithText,
|
||||
}
|
||||
}
|
||||
|
||||
fn v3(p: Vec3) -> Vector3 {
|
||||
Vector3::new(p.x as f64, p.y as f64, p.z as f64)
|
||||
}
|
||||
|
||||
fn build_leader(verts: &[Vec3], ct: LeaderCreationType) -> Leader {
|
||||
fn build_leader(verts: &[Vec3]) -> Leader {
|
||||
let mut l = Leader::from_vertices(verts.iter().map(|p| v3(*p)).collect());
|
||||
l.creation_type = ct;
|
||||
l.hookline_enabled = !matches!(ct, LeaderCreationType::NoAnnotation);
|
||||
l.creation_type = LeaderCreationType::WithText;
|
||||
l.hookline_enabled = true;
|
||||
l
|
||||
}
|
||||
|
||||
fn landing_pt(verts: &[Vec3], text_height: f64) -> Vec3 {
|
||||
/// Text anchor at the end of the landing line, and the attachment point that
|
||||
/// keeps the text reading away from the leader (text to the right of a
|
||||
/// left-pointing landing, to the left of a right-pointing one).
|
||||
fn annotation_anchor(verts: &[Vec3], text_height: f64) -> (Vec3, AttachmentPoint) {
|
||||
let last = *verts.last().unwrap();
|
||||
let prev = verts[verts.len() - 2];
|
||||
let sign = if last.x >= prev.x { 1.0_f32 } else { -1.0_f32 };
|
||||
Vec3::new(last.x + sign * text_height as f32 * 1.5, last.y, last.z)
|
||||
let to_right = last.x >= prev.x;
|
||||
let sign = if to_right { 1.0_f32 } else { -1.0_f32 };
|
||||
let anchor = Vec3::new(last.x + sign * text_height as f32 * 1.5, last.y, last.z);
|
||||
let attach = if to_right {
|
||||
AttachmentPoint::MiddleLeft
|
||||
} else {
|
||||
AttachmentPoint::MiddleRight
|
||||
};
|
||||
(anchor, attach)
|
||||
}
|
||||
|
||||
fn build_mtext(text: &str, pos: Vec3, height: f64) -> MText {
|
||||
fn build_mtext(text: &str, pos: Vec3, height: f64, attach: AttachmentPoint) -> MText {
|
||||
let mut m = MText::new();
|
||||
m.value = text.to_string();
|
||||
m.insertion_point = v3(pos);
|
||||
m.height = height;
|
||||
m.attachment_point = attach;
|
||||
m
|
||||
}
|
||||
|
||||
|
||||
@@ -25,20 +25,13 @@ pub fn tool() -> ToolDef {
|
||||
}
|
||||
}
|
||||
|
||||
enum Step {
|
||||
CollectPoints { verts: Vec<Vec3> },
|
||||
AskText { verts: Vec<Vec3> },
|
||||
}
|
||||
|
||||
pub struct MLeaderCommand {
|
||||
step: Step,
|
||||
verts: Vec<Vec3>,
|
||||
}
|
||||
|
||||
impl MLeaderCommand {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
step: Step::CollectPoints { verts: Vec::new() },
|
||||
}
|
||||
Self { verts: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,53 +41,29 @@ impl CadCommand for MLeaderCommand {
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
match &self.step {
|
||||
Step::CollectPoints { verts } if verts.is_empty() => {
|
||||
"MLEADER Specify arrowhead point:".into()
|
||||
}
|
||||
Step::CollectPoints { verts } => format!(
|
||||
"MLEADER Specify next point [{} pts — Enter to finish]:",
|
||||
verts.len()
|
||||
),
|
||||
Step::AskText { verts } => format!(
|
||||
"MLEADER Enter annotation text [{} pts — blank = no text]:",
|
||||
verts.len()
|
||||
),
|
||||
if self.verts.is_empty() {
|
||||
"MLEADER Specify arrowhead point:".into()
|
||||
} else {
|
||||
format!(
|
||||
"MLEADER Specify next point [{} pts — Enter to place text]:",
|
||||
self.verts.len()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn wants_text_input(&self) -> bool {
|
||||
matches!(self.step, Step::AskText { .. })
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
||||
if let Step::CollectPoints { verts } = &mut self.step {
|
||||
verts.push(pt);
|
||||
}
|
||||
self.verts.push(pt);
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
if let Step::CollectPoints { verts } = &self.step {
|
||||
if verts.len() < 2 {
|
||||
return CmdResult::Cancel;
|
||||
}
|
||||
let verts = verts.clone();
|
||||
self.step = Step::AskText { verts };
|
||||
CmdResult::NeedPoint
|
||||
} else {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, raw: &str) -> Option<CmdResult> {
|
||||
if let Step::AskText { verts } = &self.step {
|
||||
let text = raw.trim();
|
||||
let ml = build_mleader(text, verts);
|
||||
Some(CmdResult::CommitAndExit(EntityType::MultiLeader(ml)))
|
||||
} else {
|
||||
None
|
||||
if self.verts.len() < 2 {
|
||||
return CmdResult::Cancel;
|
||||
}
|
||||
// Place the leader with empty text, then open the in-place MText editor
|
||||
// so the user types the annotation into the rich editor.
|
||||
let ml = build_mleader("", &self.verts);
|
||||
CmdResult::CommitAndEditText(EntityType::MultiLeader(ml))
|
||||
}
|
||||
|
||||
fn on_escape(&mut self) -> CmdResult {
|
||||
@@ -102,16 +71,12 @@ impl CadCommand for MLeaderCommand {
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, pt: Vec3) -> Option<WireModel> {
|
||||
if let Step::CollectPoints { verts } = &self.step {
|
||||
if verts.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut pts = verts.clone();
|
||||
pts.push(pt);
|
||||
Some(preview_wire(&pts))
|
||||
} else {
|
||||
None
|
||||
if self.verts.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut pts = self.verts.clone();
|
||||
pts.push(pt);
|
||||
Some(preview_wire(&pts))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
use acadrust::types::Vector3;
|
||||
use acadrust::{EntityType, Text};
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::modules::{IconKind, ModuleEvent, ToolDef};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
@@ -19,7 +16,6 @@ pub fn tool() -> ToolDef {
|
||||
|
||||
enum Step {
|
||||
InsertPoint,
|
||||
WaitText(Vec3),
|
||||
}
|
||||
|
||||
pub struct TextCommand {
|
||||
@@ -42,16 +38,17 @@ impl CadCommand for TextCommand {
|
||||
fn prompt(&self) -> String {
|
||||
match &self.step {
|
||||
Step::InsertPoint => "TEXT Specify insertion point:".into(),
|
||||
Step::WaitText(pos) => format!(
|
||||
"TEXT Type text, press Enter [at {:.2},{:.2}]:",
|
||||
pos.x, pos.z
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
||||
self.step = Step::WaitText(pt);
|
||||
CmdResult::NeedPoint
|
||||
// Hand off to the in-place plain-text editor anchored at the click.
|
||||
CmdResult::OpenTextEditor {
|
||||
pos: pt,
|
||||
handle: None,
|
||||
initial: String::new(),
|
||||
height: 0.25,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
@@ -61,27 +58,6 @@ impl CadCommand for TextCommand {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
|
||||
fn wants_text_input(&self) -> bool {
|
||||
matches!(self.step, Step::WaitText(_))
|
||||
}
|
||||
|
||||
fn wants_text_with_spaces(&self) -> bool {
|
||||
matches!(self.step, Step::WaitText(_))
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||
if let Step::WaitText(pos) = &self.step {
|
||||
if text.trim().is_empty() {
|
||||
return Some(CmdResult::Cancel);
|
||||
}
|
||||
let t = Text::with_value(text, Vector3::new(pos.x as f64, pos.y as f64, pos.z as f64))
|
||||
.with_height(0.25);
|
||||
Some(CmdResult::CommitEntity(EntityType::Text(t)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn on_mouse_move(&mut self, _pt: Vec3) -> Option<WireModel> {
|
||||
None
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user