mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 00:34:25 +00:00
fwb2-fix: Perspective Crop session keyed by document
(cherry picked from commit 6dc5333d88c831f1782741dfa48f1932077babd7)
This commit is contained in:
@@ -16,6 +16,7 @@ use craft_ui::primitives::{Draw, PaintShape, StrokeWidth};
|
||||
use craft_ui::runtime::env::Env;
|
||||
use egui::Pos2;
|
||||
|
||||
use super::session::{CropDocKey, DocOwner};
|
||||
use super::perspective_crop::{PerspectiveCropOutput, PerspectiveCropQuad, PerspectiveQuad, grid_color, grid_lines, grid_step, is_valid, ordered};
|
||||
use super::words::CropWords;
|
||||
|
||||
@@ -29,8 +30,13 @@ const CORNER_KEYS: [&str; 4] = ["topLeft", "topRight", "bottomRight", "bottomLef
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub enum PerspectiveCropEvent {
|
||||
/// A session began: the press that starts a quad, before its box drag or its first clicked corner. The app runs
|
||||
/// what a new quad does first here (Photoshop drops the selection).
|
||||
Started,
|
||||
/// The tool drew the quad: a box drag (`Begin`, `Commit`; `Cancel` on Esc) or the fourth click (`Set`).
|
||||
Edit(EditEvent<PerspectiveQuad>),
|
||||
/// Another document became active: the pending quad, clicks or press of this one were dropped.
|
||||
Discard(CropDocKey),
|
||||
}
|
||||
|
||||
/// A press the tool owns, on the background.
|
||||
@@ -57,6 +63,9 @@ struct Press {
|
||||
/// - Esc drops the pending clicks. Once a quad exists, the quad's control takes Enter, Esc and the arrows
|
||||
/// ([`PerspectiveCropOutput::apply`](super::PerspectiveCropOutput::apply) and
|
||||
/// [`discard`](super::PerspectiveCropOutput::discard)).
|
||||
/// - The first press of a session reports [`PerspectiveCropEvent::Started`]. What is pending belongs to the document
|
||||
/// it was made on ([`PerspectiveInput::document`]): another document becoming active drops it and reports
|
||||
/// [`PerspectiveCropEvent::Discard`], the same rule as the Crop tool's frame.
|
||||
/// - Its keyboard path is the Perspective Crop To... command ([`perspective_to_form`]).
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct PerspectiveCropTool {
|
||||
@@ -64,6 +73,8 @@ pub struct PerspectiveCropTool {
|
||||
clicks: Vec<Point>,
|
||||
press: Option<Press>,
|
||||
events: Vec<PerspectiveCropEvent>,
|
||||
/// The document the pending quad, clicks or press belong to.
|
||||
owner: DocOwner,
|
||||
}
|
||||
|
||||
impl PerspectiveCropTool {
|
||||
@@ -88,10 +99,18 @@ impl PerspectiveCropTool {
|
||||
self.clicks.clear();
|
||||
}
|
||||
|
||||
/// Drops the quad and the pending clicks: the active document changed, or the crop was applied.
|
||||
/// Drops the quad and the pending clicks: the crop was applied or cancelled. A change of the active document
|
||||
/// drops them by itself ([`PerspectiveInput::document`]).
|
||||
pub fn clear(&mut self) {
|
||||
self.set(None);
|
||||
self.press = None;
|
||||
self.owner = DocOwner::default();
|
||||
}
|
||||
|
||||
/// Something is pending: a quad, clicked corners or a press that may start one. The options bar's Commit and
|
||||
/// Cancel end it.
|
||||
pub fn pending(&self) -> bool {
|
||||
self.quad.is_some() || !self.clicks.is_empty() || self.press.is_some()
|
||||
}
|
||||
|
||||
/// The corners clicked so far, before the fourth makes the quad.
|
||||
@@ -120,6 +139,9 @@ fn convex(q: &[Point; 4]) -> bool {
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
#[non_exhaustive]
|
||||
pub struct PerspectiveInput<'a> {
|
||||
/// The active document: pending work of another document is dropped ([`PerspectiveCropEvent::Discard`]). `None`
|
||||
/// keys nothing.
|
||||
pub document: Option<CropDocKey>,
|
||||
/// Whether the quad's grid shows.
|
||||
pub show_grid: bool,
|
||||
/// The shield over the area outside the quad; `None` shows none.
|
||||
@@ -152,6 +174,20 @@ impl PerspectiveCropPass<'_> {
|
||||
}
|
||||
|
||||
impl CanvasTool for PerspectiveCropPass<'_> {
|
||||
/// Drops the pending work of another document ([`PerspectiveCropEvent::Discard`]), and keys what is pending to
|
||||
/// the active one.
|
||||
fn each_pass(&mut self, _: &mut ToolContext<'_>) {
|
||||
let Some(document) = self.input.document else { return };
|
||||
if let Some(dropped) = self.tool.owner.stale_for(document) {
|
||||
let had = self.tool.pending();
|
||||
self.tool.clear();
|
||||
if had {
|
||||
self.tool.events.push(PerspectiveCropEvent::Discard(dropped));
|
||||
}
|
||||
}
|
||||
self.tool.owner = DocOwner::of(self.tool.pending().then_some(document));
|
||||
}
|
||||
|
||||
fn press(&mut self, pointer: &ToolPointer, cx: &mut ToolContext<'_>) -> ToolAction {
|
||||
let Some(space) = cx.space() else { return ToolAction::Ignored };
|
||||
let anchor = space.view.to_doc(pointer.pos);
|
||||
@@ -159,7 +195,11 @@ impl CanvasTool for PerspectiveCropPass<'_> {
|
||||
return ToolAction::Ignored;
|
||||
}
|
||||
let Some(id) = cx.gesture_id() else { return ToolAction::Ignored };
|
||||
if !self.tool.pending() {
|
||||
self.tool.events.push(PerspectiveCropEvent::Started);
|
||||
}
|
||||
self.tool.press = Some(Press { id, anchor, origin: pointer.pos, before: self.tool.quad, began: false });
|
||||
self.tool.owner = DocOwner::of(self.input.document.or(self.tool.owner.key()));
|
||||
ToolAction::Claimed
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,28 @@ impl CropDocKey {
|
||||
}
|
||||
}
|
||||
|
||||
/// The document a crop tool's pending work belongs to: the Crop tool's frame, the Perspective Crop tool's quad.
|
||||
/// Switching documents drops that work (as Photoshop discards a pending crop), and this one rule says when.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub(super) struct DocOwner(Option<CropDocKey>);
|
||||
|
||||
impl DocOwner {
|
||||
/// Work held for `held`, `None` for no document.
|
||||
pub(super) const fn of(held: Option<CropDocKey>) -> Self {
|
||||
Self(held)
|
||||
}
|
||||
|
||||
/// The document the work belongs to.
|
||||
pub(super) const fn key(self) -> Option<CropDocKey> {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// The document the work belongs to when that is another one than `key`: the work is stale and goes.
|
||||
pub(super) fn stale_for(self, key: CropDocKey) -> Option<CropDocKey> {
|
||||
self.0.filter(|held| *held != key)
|
||||
}
|
||||
}
|
||||
|
||||
/// What the Crop tool needs to know of the active document each pass.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
@@ -303,14 +325,10 @@ impl CropSession {
|
||||
/// drawn frame was dropped, so the tool reports [`CropEvent::Discard`](super::CropEvent): the document whose
|
||||
/// drawn frame it dropped.
|
||||
pub fn for_document(&mut self, key: CropDocKey) -> Option<CropDocKey> {
|
||||
let stale = self.frame_for.is_some_and(|(held, _)| held != key);
|
||||
if !stale {
|
||||
return None;
|
||||
}
|
||||
let dropped = self.frame_for.map(|(held, _)| held);
|
||||
let dropped = DocOwner::of(self.frame_document()).stale_for(key)?;
|
||||
let drawn = self.pending();
|
||||
self.clear();
|
||||
dropped.filter(|_| drawn)
|
||||
drawn.then_some(dropped)
|
||||
}
|
||||
|
||||
/// The document the frame was made for.
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
//! The Perspective Crop quad and tool through the public API: the pure quad rules, the quad's gestures and keys as a
|
||||
//! canvas control, and the tool's box drag and four clicks on the background.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
use craft_behavior::CanvasView;
|
||||
use craft_behavior::geometry::Constraint;
|
||||
use craft_behavior::kurbo::{Point, Size};
|
||||
use craft_draw::ui::crop::{PerspectiveCropTool, PerspectiveInput, grid_lines, grid_steps, is_valid};
|
||||
use craft_draw::ui::crop::{CropDocKey, PerspectiveCropEvent, PerspectiveCropTool, PerspectiveInput, grid_lines, grid_steps, is_valid};
|
||||
use craft_ui::canvas::{Canvas, ToolHost};
|
||||
use craft_ui::contract::ShowContainer as _;
|
||||
use craft_ui::runtime::access::Role;
|
||||
@@ -23,24 +23,29 @@ const BOTH: Modifiers = Modifiers { shift: true, alt: true, ..Modifiers::NONE };
|
||||
struct Rig {
|
||||
fixture: Fixture,
|
||||
tool: Rc<RefCell<PerspectiveCropTool>>,
|
||||
/// The active document the pass is given; `None` keys nothing.
|
||||
document: Rc<Cell<Option<CropDocKey>>>,
|
||||
}
|
||||
|
||||
impl Rig {
|
||||
fn new() -> Self {
|
||||
let tool = Rc::new(RefCell::new(PerspectiveCropTool::new()));
|
||||
let shown = Rc::clone(&tool);
|
||||
let document = Rc::new(Cell::new(None));
|
||||
let (shown, active) = (Rc::clone(&tool), Rc::clone(&document));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut tool = shown.borrow_mut();
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let view = CanvasView::new(room, Point::new(200.0, 150.0), 1.0);
|
||||
let mut pass = tool.pass(PerspectiveInput::default());
|
||||
let mut input = PerspectiveInput::default();
|
||||
input.document = active.get();
|
||||
let mut pass = tool.pass(input);
|
||||
let canvas = Canvas::new("Document").document(SIZE).view(view).show(ui, |ui, body| {
|
||||
let _ = pass.show_quad(ui, body.rect);
|
||||
});
|
||||
let _ = ToolHost::new().run(ui, &canvas.output, &mut pass);
|
||||
});
|
||||
fixture.settle();
|
||||
Rig { fixture, tool }
|
||||
Rig { fixture, tool, document }
|
||||
}
|
||||
|
||||
/// The screen point of document point `(x, y)`: the view shows (200, 150) at the canvas's centre.
|
||||
@@ -201,3 +206,30 @@ fn perspective_box_and_corner_modifiers_and_clicks() {
|
||||
rig.click((300.0, 220.0));
|
||||
assert!(near(rig.quad(), upright), "a click moves nothing: {:?}", rig.quad());
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_session_starts_once_and_a_document_switch_discards_it() {
|
||||
let mut rig = Rig::new();
|
||||
let (first, second) = (CropDocKey::new(1), CropDocKey::new(2));
|
||||
rig.document.set(Some(first));
|
||||
rig.fixture.frame(Vec::new());
|
||||
let started = |events: &[PerspectiveCropEvent]| events.iter().filter(|event| matches!(event, PerspectiveCropEvent::Started)).count();
|
||||
|
||||
rig.click((100.0, 50.0));
|
||||
rig.click((300.0, 200.0));
|
||||
let events = rig.tool.borrow_mut().take_events();
|
||||
assert_eq!(started(&events), 1, "the first click starts the session, the second does not: {events:?}");
|
||||
assert!(rig.tool.borrow().pending(), "clicked corners are pending");
|
||||
|
||||
rig.document.set(Some(second));
|
||||
rig.fixture.frame(Vec::new());
|
||||
let events = rig.tool.borrow_mut().take_events();
|
||||
assert!(events.contains(&PerspectiveCropEvent::Discard(first)), "switching documents drops the clicks of the first: {events:?}");
|
||||
assert!(!rig.tool.borrow().pending() && rig.placing() == 0, "nothing is left pending");
|
||||
|
||||
rig.drag((20.0, 20.0), (120.0, 120.0), Modifiers::NONE);
|
||||
let events = rig.tool.borrow_mut().take_events();
|
||||
assert_eq!(started(&events), 1, "a box drag on the new document starts a new session: {events:?}");
|
||||
rig.fixture.frame(Vec::new());
|
||||
assert!(rig.tool.borrow_mut().take_events().is_empty(), "the same document keeps its quad");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user