From 08f208edbdd207fa49e2333db76d7efccfa78edb Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 11 Nov 2026 23:49:19 +0000 Subject: [PATCH] Ruler tool story runs live over the object: a press draws a new line, Shift holds it to 45 degrees, the ends drag and Alt from an end draws the protractor arm --- crates/draw/src/ui/measure/stories.rs | 36 +++++++++++++++++++++------ 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/crates/draw/src/ui/measure/stories.rs b/crates/draw/src/ui/measure/stories.rs index 95df3f586..5466d7e91 100644 --- a/crates/draw/src/ui/measure/stories.rs +++ b/crates/draw/src/ui/measure/stories.rs @@ -1,14 +1,15 @@ -//! Stories of the Ruler tool's overlay and options bar. +//! Stories of the Ruler tool, run live, and of its overlay and options bar. use super::model::RulerPart; use super::overlay::POINT; -use super::{RulerBar, RulerLine, RulerOverlay}; +use super::{RulerBar, RulerLine, RulerOverlay, RulerTool}; +use craft_behavior::CanvasView; use craft_behavior::kurbo::{Point, Rect, Size}; use craft_ui::bars::{Bar, BarKind}; use craft_ui::canvas::RasterSource as _; use craft_ui::contract::{Bind, Show as _, ShowContainer as _}; use craft_ui::testing::canvas_story::{OUTLINE, StoryDocument, StoryPicture, StoryShape, accent, on_picture, paint_shapes}; -use craft_ui::testing::{Drive, Grip, StoryData, StoryOutput, story}; +use craft_ui::testing::{Drive, Grip, StoryData, StoryOutput, ToolStory, story, tool_story}; use egui::{Color32, Ui}; fn line() -> RulerLine { @@ -18,14 +19,33 @@ fn line() -> RulerLine { /// The object the ruler measures, centred on the story document. const OBJECT: Size = Size::new(220.0, 140.0); -/// Shows the overlay over `line` as the only canvas control of a canvas over the object, filled and outlined in the -/// accent. -fn on_canvas(ui: &mut Ui, line: &mut Option) -> StoryOutput { +/// The story document with the object the ruler measures, filled and outlined in the accent. +fn object(ui: &Ui) -> StoryPicture Color32 + use<>> { let document = StoryDocument::STANDARD; let object = StoryShape::filled(&Rect::from_center_size(document.centre(), OBJECT), accent(ui, 0.25)).outlined(OUTLINE, accent(ui, 1.0)); let size = document.size(); - let picture = StoryPicture::new(size.width, size.height, move |p| paint_shapes(std::slice::from_ref(&object), Color32::TRANSPARENT, p)); - on_picture(ui, &picture, |ui, at, _| RulerOverlay::new("Ruler", Bind::bound(&mut *line)).show_at(ui, at)) + StoryPicture::new(size.width, size.height, move |p| paint_shapes(std::slice::from_ref(&object), Color32::TRANSPARENT, p)) +} + +/// Shows the overlay over `line` as the only canvas control of a canvas over the object. +fn on_canvas(ui: &mut Ui, line: &mut Option) -> StoryOutput { + on_picture(ui, &object(ui), |ui, at, _| RulerOverlay::new("Ruler", Bind::bound(&mut *line)).show_at(ui, at)) +} + +/// The Ruler tool live over the object: a press on the background draws a new line, Shift holds its end to 45 +/// degrees, a line too short is cleared, the ends drag, and Alt from an end draws the protractor arm. The line the +/// tool and its overlay report is the story's document. +#[story(RulerOverlay<'static>, covers = [], drive = Drive::Never)] +fn ruler_tool(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let picture = object(ui); + let body = |ui: &mut Ui, at: egui::Rect, _: &CanvasView, tool: &mut RulerTool, line: &Option| tool.pass(*line).show_line(ui, at); + tool_story(ui, data, ToolStory::new("Document", &picture), RulerTool::new, Some(line()), body, |run| { + let _ = run.host.run(&mut run.tool.pass(*run.document)); + for event in run.tool.take_events() { + let _ = event.apply(run.document); + } + StoryOutput::default() + }) } #[story(RulerOverlay<'static>, covers = [Default], drive = Drive::Drag(Grip::part(POINT.at(RulerPart::End.key()))))]