mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 22:14:30 +00:00
Pen: whose anchors a draft's are is a state of the handed-in draft (PenDraft), read by Delete, Backspace and remove_last alike
This commit is contained in:
@@ -18,7 +18,7 @@ const CONVERT_TIP: Vec2 = Vec2::new(ICON_BOX / 2.0, 5.5);
|
||||
/// overlay does nothing, so it never starts a path.
|
||||
///
|
||||
/// - The open pen draft ([`draft`](Self::draft)), when the host passes it, is drawn and keeps its keys (Enter,
|
||||
/// Escape, and Delete unless [`PenTool::deletes`] leaves it to the app) as [`PathMode`](super::PathMode)
|
||||
/// Escape, and Delete unless the draft is [`PenDraft::Foreign`](super::PenDraft::Foreign)) as [`PathMode`](super::PathMode)
|
||||
/// describes; it takes no press.
|
||||
/// - Cursor off the overlay's parts: the crosshair under the canvas's precise cursor
|
||||
/// ([`CanvasSpace::precise`](craft_ui::canvas::CanvasSpace::precise)); with ⌘ Direct Selection's arrow; else Add
|
||||
|
||||
@@ -21,5 +21,5 @@ pub use bezier::{
|
||||
};
|
||||
pub use mode::{PathMode, PathTool, PenMode};
|
||||
pub use options::{PATH_OPTIONS_COLOR_KEY, PathOptionsOutput, PathOptionsPopover, PathOptionsSettings, PathOptionsWords};
|
||||
pub use tool::{PenEnd, PenEvent, PenStyle, PenTool};
|
||||
pub use tool::{PenDraft, PenEnd, PenEvent, PenStyle, PenTool};
|
||||
pub use words::PenWords;
|
||||
|
||||
@@ -57,6 +57,25 @@ pub enum PenEvent {
|
||||
Resumed(u64, KnotRef),
|
||||
}
|
||||
|
||||
/// The host's open draft as [`PenTool::draft`] hands it in, with whose anchors they are to take back.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum PenDraft<'a> {
|
||||
/// The selected tool's draft: Backspace, Delete and [`PenTool::remove_last`] take back its anchors.
|
||||
Own(&'a EditSubpath),
|
||||
/// A draft the app keeps open for another tool (a refused draft kept after a tool switch): drawn, and ended by
|
||||
/// Enter and Escape, but its anchors are not the selected tool's to take back, so Backspace, Delete and
|
||||
/// [`PenTool::remove_last`] leave it to the app.
|
||||
Foreign(&'a EditSubpath),
|
||||
}
|
||||
|
||||
impl<'a> PenDraft<'a> {
|
||||
fn path(self) -> &'a EditSubpath {
|
||||
match self {
|
||||
Self::Own(path) | Self::Foreign(path) => path,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The pen draft's look in a paint call, from the theme and the Path Options.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
@@ -132,8 +151,8 @@ struct Run {
|
||||
/// [`PenEvent::Resumed`].
|
||||
/// - Keys while a draft is open: Enter and Escape end it open and keep it (a draft of one anchor is discarded, as no
|
||||
/// path has fewer than two anchors); Backspace and Delete remove the last anchor, the only one discarding the
|
||||
/// draft, unless the host gives them to another tool ([`deletes`](Self::deletes)); the host's Undo does the same
|
||||
/// through [`remove_last`](Self::remove_last). Each ending is [`PenEvent::Finished`].
|
||||
/// draft, and the host's Undo does the same through [`remove_last`](Self::remove_last), except on a
|
||||
/// [`PenDraft::Foreign`] draft, whose anchors both leave to the app. Each ending is [`PenEvent::Finished`].
|
||||
/// - Look: the path so far, a dashed rubber band from the last anchor to where a click would land (with Path
|
||||
/// Options' Rubber Band), the last anchor's handles, the anchors with the last one filled, and over the first
|
||||
/// anchor the close cue.
|
||||
@@ -156,7 +175,8 @@ struct Run {
|
||||
pub struct PenTool {
|
||||
pub(super) mode: PenMode,
|
||||
pub(super) sensing: bool,
|
||||
deletes: bool,
|
||||
/// The draft is [`PenDraft::Foreign`]: Backspace, Delete and `remove_last` leave its anchors to the app.
|
||||
foreign: bool,
|
||||
pub(super) auto_add_delete: bool,
|
||||
options: PathOptionsSettings,
|
||||
pub(super) ends: Vec<(u64, KnotRef, Point)>,
|
||||
@@ -187,7 +207,7 @@ impl PenTool {
|
||||
Self {
|
||||
mode: PenMode::default(),
|
||||
sensing: true,
|
||||
deletes: true,
|
||||
foreign: false,
|
||||
auto_add_delete: false,
|
||||
options: PathOptionsSettings::default(),
|
||||
ends: Vec::new(),
|
||||
@@ -203,12 +223,15 @@ impl PenTool {
|
||||
}
|
||||
}
|
||||
|
||||
/// The host's open draft. The tool keeps the draft it is drawing and reads this only while it has none: a
|
||||
/// non-empty draft with none open (a resumed path) becomes the draft and begins its gesture at once. Hand in
|
||||
/// `None` once [`PenEvent::Finished`] came. `None` while a run is open and no press is down means the host ended
|
||||
/// the draft itself (a commit, a menu command): the tool drops its run and draft with no event, and the next press
|
||||
/// starts a new draft. A press that is down keeps its run.
|
||||
pub fn draft(&mut self, draft: Option<&EditSubpath>) -> &mut Self {
|
||||
/// The host's open draft. The tool keeps the draft it is drawing and reads its path only while it has none: a
|
||||
/// non-empty draft with none open (a resumed path) becomes the draft and begins its gesture at once. Whose
|
||||
/// anchors it is to take back ([`PenDraft`]) is read on every call, as the selected tool can change while the
|
||||
/// draft stays open. Hand in `None` once [`PenEvent::Finished`] came. `None` while a run is open and no press is
|
||||
/// down means the host ended the draft itself (a commit, a menu command): the tool drops its run and draft with
|
||||
/// no event, and the next press starts a new draft. A press that is down keeps its run.
|
||||
pub fn draft(&mut self, draft: Option<PenDraft<'_>>) -> &mut Self {
|
||||
self.foreign = matches!(draft, Some(PenDraft::Foreign(_)));
|
||||
let draft = draft.map(PenDraft::path);
|
||||
let host_ended = draft.is_none_or(|draft| draft.knots.is_empty()) && self.run.is_some() && self.drag.is_none();
|
||||
if self.run.is_none() || host_ended {
|
||||
self.run = None;
|
||||
@@ -270,13 +293,6 @@ impl PenTool {
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether Backspace and Delete remove the draft's last anchor; true by default. False leaves them to the app, for
|
||||
/// a draft whose anchors are not the selected tool's to take back (a refused draft kept after a tool switch).
|
||||
pub fn deletes(&mut self, on: bool) -> &mut Self {
|
||||
self.deletes = on;
|
||||
self
|
||||
}
|
||||
|
||||
/// The Pen's Auto Add/Delete on the open draft; false by default. No effect in Curvature mode.
|
||||
pub fn auto_add_delete(&mut self, on: bool) -> &mut Self {
|
||||
self.auto_add_delete = on;
|
||||
@@ -297,15 +313,21 @@ impl PenTool {
|
||||
}
|
||||
|
||||
/// Removes the draft's last anchor: the host's Undo while a draft is open. The only anchor discards the draft.
|
||||
/// Returns whether it did anything; a press that is still down keeps the request from running.
|
||||
/// Returns whether it did anything: false, leaving Undo to the app, with no draft, a [`PenDraft::Foreign`] one,
|
||||
/// or a press that is still down.
|
||||
pub fn remove_last(&mut self) -> bool {
|
||||
if self.shown.knots.is_empty() || self.drag.is_some() {
|
||||
if !self.takes_back() {
|
||||
return false;
|
||||
}
|
||||
self.remove_last_anchor(CancelReason::UndoDuringGesture);
|
||||
true
|
||||
}
|
||||
|
||||
/// Whether Backspace, Delete and the host's Undo remove the draft's last anchor now.
|
||||
fn takes_back(&self) -> bool {
|
||||
!self.foreign && !self.shown.knots.is_empty() && self.drag.is_none()
|
||||
}
|
||||
|
||||
/// The events since the last call, in order.
|
||||
pub fn take_events(&mut self) -> Vec<PenEvent> {
|
||||
mem::take(&mut self.events)
|
||||
@@ -380,6 +402,7 @@ impl PenTool {
|
||||
self.resume = false;
|
||||
self.own = 0;
|
||||
self.kept = 0;
|
||||
self.foreign = false;
|
||||
}
|
||||
|
||||
/// Cancels the run back to its start (the handed-in path of a resumed draft) and ends the draft as `Discarded`.
|
||||
@@ -626,7 +649,7 @@ impl CanvasTool for PenTool {
|
||||
self.end(CancelReason::Escape, PenEnd::Escape);
|
||||
true
|
||||
}
|
||||
ToolKey::Delete { .. } if self.deletes => {
|
||||
ToolKey::Delete { .. } if self.takes_back() => {
|
||||
self.remove_last_anchor(CancelReason::UndoDuringGesture);
|
||||
true
|
||||
}
|
||||
@@ -635,7 +658,7 @@ impl CanvasTool for PenTool {
|
||||
}
|
||||
|
||||
fn claims(&self) -> ToolClaims {
|
||||
if self.deletes && !self.shown.knots.is_empty() && self.drag.is_none() { ToolClaims::NONE.deletes() } else { ToolClaims::NONE }
|
||||
if self.takes_back() { ToolClaims::NONE.deletes() } else { ToolClaims::NONE }
|
||||
}
|
||||
|
||||
fn takes(&self, button: PointerButton, mods: Mods) -> bool {
|
||||
|
||||
+33
-12
@@ -3,7 +3,7 @@
|
||||
|
||||
use craft_behavior::kurbo::Point;
|
||||
use craft_behavior::{CanvasView, EditEvent};
|
||||
use craft_draw::ui::pen::{EditSubpath, Knot, PenEnd, PenEvent, PenMode, PenTool};
|
||||
use craft_draw::ui::pen::{EditSubpath, Knot, PenDraft, PenEnd, PenEvent, PenMode, PenTool};
|
||||
use craft_ui::testing::{ToolFixture, trial};
|
||||
use egui::{Event, Modifiers, PointerButton, Pos2, Rect, vec2};
|
||||
|
||||
@@ -173,18 +173,39 @@ fn remove_last_waits_for_a_press_that_is_still_down() {
|
||||
assert_eq!(pen.draft().knots.len(), 3, "one request removes one anchor");
|
||||
}
|
||||
|
||||
// Delete takes back the draft's last anchor; with `deletes` off (the anchors are not the selected tool's to take back)
|
||||
// the key is left to the app and the draft stays.
|
||||
// Delete and the host's Undo agree: both take back the last anchor of the selected tool's draft, and both leave a
|
||||
// draft kept for another tool (`PenDraft::Foreign`) to the app, whether it was handed in kept or became another
|
||||
// tool's while open.
|
||||
#[trial]
|
||||
fn delete_removes_the_last_anchor_unless_the_host_keeps_the_key() {
|
||||
let mut pen = Pen::three();
|
||||
fn delete_and_undo_agree_on_whose_anchors_they_take_back() {
|
||||
let mut drawn = Pen::three();
|
||||
drawn.fixture.fixture().key(egui::Key::Delete, Modifiers::NONE);
|
||||
drawn.sync();
|
||||
assert_eq!(drawn.draft().knots.len(), 2);
|
||||
let open = drawn.draft();
|
||||
drawn.fixture.tool_mut().draft(Some(PenDraft::Foreign(&open)));
|
||||
drawn.fixture.fixture().key(egui::Key::Delete, Modifiers::NONE);
|
||||
assert!(!drawn.fixture.tool_mut().remove_last(), "Undo is the app's");
|
||||
drawn.sync();
|
||||
assert_eq!(drawn.draft().knots.len(), 2, "the key is the app's");
|
||||
|
||||
let mut pen = pen();
|
||||
let kept = EditSubpath::new(vec![Knot::corner(doc(60.0, 60.0)), Knot::corner(doc(160.0, 60.0)), Knot::corner(doc(160.0, 160.0))], false);
|
||||
pen.fixture.tool_mut().draft(Some(PenDraft::Foreign(&kept)));
|
||||
pen.fixture.fixture().settle();
|
||||
pen.fixture.fixture().key(egui::Key::Delete, Modifiers::NONE);
|
||||
assert!(!pen.fixture.tool_mut().remove_last());
|
||||
pen.sync();
|
||||
assert_eq!(pen.kinds(), ["begin"], "a kept draft loses no anchor to either");
|
||||
pen.fixture.tool_mut().draft(Some(PenDraft::Own(&kept)));
|
||||
pen.click_doc(260.0, 160.0);
|
||||
pen.fixture.fixture().key(egui::Key::Delete, Modifiers::NONE);
|
||||
pen.sync();
|
||||
assert_eq!(pen.draft().knots.len(), 2);
|
||||
pen.fixture.tool_mut().deletes(false);
|
||||
pen.fixture.fixture().key(egui::Key::Delete, Modifiers::NONE);
|
||||
assert_eq!(pen.draft(), kept, "back with its tool, Delete takes back the added anchor");
|
||||
pen.click_doc(260.0, 160.0);
|
||||
assert!(pen.fixture.tool_mut().remove_last());
|
||||
pen.sync();
|
||||
assert_eq!(pen.draft().knots.len(), 2, "the key is the app's");
|
||||
assert_eq!(pen.draft(), kept, "and so does Undo");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
@@ -239,7 +260,7 @@ fn space_moves_the_anchor_with_its_handles() {
|
||||
fn removing_a_resumed_drafts_own_anchor_restores_the_path_and_ends_the_draft() {
|
||||
let mut pen = pen();
|
||||
let own = EditSubpath::new(vec![Knot::corner(doc(60.0, 60.0)), Knot::corner(doc(160.0, 60.0))], false);
|
||||
pen.fixture.tool_mut().draft(Some(&own));
|
||||
pen.fixture.tool_mut().draft(Some(PenDraft::Own(&own)));
|
||||
pen.fixture.fixture().settle();
|
||||
pen.sync();
|
||||
assert_eq!(pen.kinds(), ["begin"], "the handed-in draft begins its gesture at once");
|
||||
@@ -269,7 +290,7 @@ fn a_curvature_resume_keeps_the_carried_knots_and_replace_updates_the_draft() {
|
||||
let mut tool = pen.fixture.tool_mut();
|
||||
tool.mode(PenMode::Curvature);
|
||||
tool.kept(2);
|
||||
tool.draft(Some(&carried));
|
||||
tool.draft(Some(PenDraft::Own(&carried)));
|
||||
}
|
||||
pen.fixture.fixture().settle();
|
||||
pen.sync();
|
||||
@@ -313,7 +334,7 @@ fn three_curvature_clicks_give_three_anchors() {
|
||||
if round_trip {
|
||||
let held = pen.draft();
|
||||
let mut tool = pen.fixture.tool_mut();
|
||||
tool.mode(PenMode::Curvature).draft((!held.knots.is_empty()).then_some(&held)).kept(0);
|
||||
tool.mode(PenMode::Curvature).draft((!held.knots.is_empty()).then_some(PenDraft::Own(&held))).kept(0);
|
||||
}
|
||||
pen.fixture.fixture().frame(vec![event]);
|
||||
pen.sync();
|
||||
|
||||
Reference in New Issue
Block a user