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
47 changes: 35 additions & 12 deletions src/canvas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::document::{self, EdgeEnd, Item, LabelRef};
use crate::history::Document;
use crate::scene::SceneEdge;
use crate::selection::Selection;
use crate::snap::{Axis, Feature, Guide, Side, Targets};
use crate::snap::{Axis, EqualGaps, Feature, Guide, Side, Targets};
use crate::style::Style;
use crate::text_field::{self, End};
use crate::tool::{NodeShape, Tool};
Expand Down Expand Up @@ -61,6 +61,12 @@ const HANDLE_SIZE: f32 = 7.0;
const GUIDE: Color32 = Color32::from_rgb(0xE6, 0x00, 0x8A);
/// How far guides reach past what they span, in screen points.
const GUIDE_OVERHANG: f32 = 8.0;
/// How far above a row's highest node, or left of a column's leftmost, the
/// dimension lines showing equal gaps are, in screen points.
const DIMENSION_OFFSET: f32 = 20.0;
/// How long the arrowheads of dimension lines are along and across them, in
/// screen points, and how far the lines up to them reach past.
const ARROWHEAD: f32 = 5.0;

/// The sides and corners of the selected node that can be dragged, e.g.
/// `RIGHT_CENTER` for its right side.
Expand Down Expand Up @@ -138,6 +144,8 @@ pub struct Canvas {
snapping: bool,
/// What this frame's drag lines up with.
guides: Vec<Guide>,
/// The equal gaps this frame's move makes.
equal_gaps: Vec<EqualGaps>,
}

/// Where the graph shows on the canvas.
Expand Down Expand Up @@ -307,6 +315,7 @@ impl Canvas {
// Alt skips snapping, e.g. to place a node just beside a line.
self.snapping = self.options.snap_lines && !ui.input(|input| input.modifiers.alt);
self.guides.clear();
self.equal_gaps.clear();
// A shortcut can switch tools mid-drag, which drops the shape.
self.shape_drag.take_if(|drag| drag.tool != tool);
self.edge_draw.take_if(|draw| draw.tool != tool);
Expand Down Expand Up @@ -1120,13 +1129,13 @@ impl Canvas {
let to_graph = |pos: Pos2| graph_point(shown.to_screen, pos);
if !canvas.drag_stopped() {
let to = canvas.interact_pointer_pos().map(to_graph)?;
let (delta, guides) = self.snap_move(shown, selected, to - drag.from);
self.guides = guides;
let (delta, guides, gaps) = self.snap_move(shown, selected, to - drag.from);
(self.guides, self.equal_gaps) = (guides, gaps);
return Some(delta);
}
self.move_drag = None;
if let Some(to) = ui.input(primary_release).map(to_graph) {
let ([dx, dy], _) = self.snap_move(shown, selected, to - drag.from);
let ([dx, dy], ..) = self.snap_move(shown, selected, to - drag.from);
// Ending where it started changes nothing.
if dx != 0.0 || dy != 0.0 {
document::move_items(doc.graphml_mut(), selected.items(), dx, dy);
Expand All @@ -1137,23 +1146,32 @@ impl Canvas {

/// How far right and down to move the `selected` nodes for a drag by
/// `delta`, in graph points, so that one lines up with a node left in
/// place, with the guides showing what lines up. With no nodes selected,
/// the selected edges' bends line up instead.
fn snap_move(&self, shown: Shown, selected: &Selection, delta: Vec2) -> ([f64; 2], Vec<Guide>) {
/// place, or they make equal gaps with nodes beside them, with the guides
/// and gaps showing it. With no nodes selected, the selected edges' bends
/// line up instead.
fn snap_move(
&self,
shown: Shown,
selected: &Selection,
delta: Vec2,
) -> ([f64; 2], Vec<Guide>, Vec<EqualGaps>) {
let mut delta = [delta.x, delta.y].map(f64::from);
let scene = shown.scene;
let moving = |node: usize| selected.contains_node(&scene.nodes[node].id);
let Some(targets) = self.targets(shown, moving) else {
return (delta, Vec::new());
return (delta, Vec::new(), Vec::new());
};
let features = |[dx, dy]: [f64; 2]| {
let nodes = (0..scene.nodes.len()).filter(|&node| moving(node) && shown.is_drawn(node));
let lines = nodes.flat_map(|node| {
let moved = nodes.map(|node| {
let mut rect = shown.geometry(node);
(rect.x, rect.y) = (rect.x + dx, rect.y + dy);
Feature::box_lines(&rect)
rect
});
let mut features: Vec<_> = lines.collect();
let rects: Vec<_> = moved.collect();
let mut features: Vec<_> = rects.iter().flat_map(Feature::box_lines).collect();
// The gaps are those of the selection as a whole.
features.extend(Feature::block(&rects));
if features.is_empty() {
let edges = (scene.edges.iter()).filter(|edge| selected.contains_edge(edge.index));
let bends = edges.flat_map(|edge| edge.graphics.path.points.iter());
Expand All @@ -1169,7 +1187,12 @@ impl Canvas {
for (delta, shift) in delta.iter_mut().zip(shift) {
*delta += shift.unwrap_or_default();
}
(delta, targets.guides(&features(delta)))
let features = features(delta);
(
delta,
targets.guides(&features),
targets.equal_gaps(&features),
)
}

/// Selects the nodes inside the box that a drag [`Self::start_drag`]
Expand Down
60 changes: 56 additions & 4 deletions src/canvas/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ use crate::document::Item;
use crate::fonts;
use crate::line_type;
use crate::selection::Selection;
use crate::snap::{Axis, Guide};
use crate::snap::{Axis, EqualGaps, Guide};
use crate::style::Style;
use crate::svg_image::SvgImage;

use super::shown::{Anchor, EdgeLook, Outline, Shown, egui_rect, shape};
use super::textures::Textures;
use super::{
BACKGROUND, Canvas, EdgeDrag, EdgePart, GUIDE, GUIDE_OVERHANG, HALO, HANDLE_SIZE,
LABEL_PADDING, MAX_TEXT_PIXELS, Owner, Preview, SELECTION, SELECTION_BOX_FILL, SELECTION_GAP,
edge, grid,
ARROWHEAD, BACKGROUND, Canvas, DIMENSION_OFFSET, EdgeDrag, EdgePart, GUIDE, GUIDE_OVERHANG,
HALO, HANDLE_SIZE, LABEL_PADDING, MAX_TEXT_PIXELS, Owner, Preview, SELECTION,
SELECTION_BOX_FILL, SELECTION_GAP, edge, grid,
};

impl Canvas {
Expand Down Expand Up @@ -118,6 +118,7 @@ impl Canvas {
painter.rect_stroke(outline, 0.0, SELECTION, StrokeKind::Outside);
}
paint_guides(painter, shown.to_screen, &self.guides);
paint_equal_gaps(painter, shown.to_screen, &self.equal_gaps);
if kept
&& let Some(index) = selected_edge
&& let Some(edge) = scene.edge(index)
Expand Down Expand Up @@ -397,6 +398,57 @@ fn paint_guides(painter: &Painter, to_screen: TSTransform, guides: &[Guide]) {
painter.add(mesh);
}

/// Paints `runs` of equal gaps as yEd's dimension lines, a pixel wide: a line
/// with arrowheads across each gap, a little above the run's highest node (or
/// left of a column's leftmost), and lines from just past it along the sides
/// facing the gap, down to the middle of the band the run overlaps.
fn paint_equal_gaps(painter: &Painter, to_screen: TSTransform, runs: &[EqualGaps]) {
if runs.is_empty() {
return;
}
let pixels_per_point = painter.pixels_per_point();
// Diagonal, so a little wider than the lines to look as strong.
let stroke = Stroke::new(1.5 / pixels_per_point, GUIDE);
let mut mesh = Mesh::default();
for run in runs {
// Screen coordinates along the run's axis and across it.
let along = run.axis as usize;
let across = 1 - along;
let screen =
|at: f64, index: usize| to_screen.translation[index] + to_screen.scaling * at as f32;
let pos = |along_at: f32, across_at: f32| match run.axis {
Axis::X => pos2(along_at, across_at),
Axis::Y => pos2(across_at, along_at),
};
let rect = |along_at: Rangef, across_at: Rangef| match run.axis {
Axis::X => Rect::from_x_y_ranges(along_at, across_at),
Axis::Y => Rect::from_x_y_ranges(across_at, along_at),
};
let middle = screen((run.band[0] + run.band[1]) / 2.0, across);
let level = pixel(
screen(run.start, across) - DIMENSION_OFFSET,
pixels_per_point,
);
for gap in &run.gaps {
let ends = gap.map(|at| pixel(screen(at, along), pixels_per_point));
let extension = Rangef::new(level.min - ARROWHEAD, middle);
for end in ends {
mesh.add_colored_rect(rect(end, extension), GUIDE);
}
let line = Rangef::new(ends[0].min, ends[1].max);
mesh.add_colored_rect(rect(line, level), GUIDE);
let [from, to] = ends.map(|end| end.center());
let length = ARROWHEAD.min((to - from) / 2.0);
for (tip, back) in [(from, length), (to, -length)] {
let wing = |side: f32| pos(tip + back, level.center() + side * length);
let points = vec![wing(-1.0), pos(tip, level.center()), wing(1.0)];
painter.add(PathShape::line(points, stroke));
}
}
}
painter.add(mesh);
}

/// The pixel a line a pixel wide at `at` on screen is drawn on, as egui puts
/// a 1 px border there.
pub(super) fn pixel(at: f32, pixels_per_point: f32) -> Rangef {
Expand Down
133 changes: 132 additions & 1 deletion src/canvas/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3490,12 +3490,26 @@ fn guides(shapes: &[Shape]) -> Vec<Rect> {
/// Drags the node in [`NODE`], selected, by `by`, returning what the last
/// frame before the release painted.
fn drag_node_by(harness: &mut Harness, by: Vec2) -> Vec<Shape> {
let from = NODE.center().to_vec2();
drag_by(harness, NODE.center(), by)
}

/// Drags from `from` by `by`, returning what the last frame before the
/// release painted.
fn drag_by(harness: &mut Harness, from: Pos2, by: Vec2) -> Vec<Shape> {
let from = from.to_vec2();
let held = harness.drag_and_hold(from, from + by);
harness.frame(vec![button(harness.origin + from + by, false)]);
held
}

/// Adds the boxes in [`NODE`] and [`OTHER`], and a third 50 wide below the
/// gap between them, which it selects.
fn add_three_boxes(harness: &mut Harness) {
harness.add_two_boxes();
let node = harness.add_box(pos2(300.0, 300.0), pos2(350.0, 340.0));
harness.selected = Selection::node(node);
}

#[test]
fn moved_nodes_snap_into_line_with_other_nodes() {
let mut harness = Harness::new(Tool::Cursor);
Expand Down Expand Up @@ -3523,6 +3537,123 @@ fn guides_show_what_the_moved_node_lines_up_with() {
assert_eq!(guides(&harness.frame(vec![])), []);
}

#[test]
fn moved_nodes_snap_halfway_between_the_nodes_beside_them() {
let mut harness = Harness::new(Tool::Cursor);
add_three_boxes(&mut harness);
// Up into the gap, 3 right of halfway.
drag_by(&mut harness, pos2(325.0, 320.0), vec2(3.0, -240.0));
assert_eq!(harness.boxes()[2], geometry(300.0, 60.0, 50.0, 40.0));
}

#[test]
fn equal_gaps_show_as_dimension_lines_above_the_row() {
let mut harness = Harness::new(Tool::Cursor);
add_three_boxes(&mut harness);
let held = drag_by(&mut harness, pos2(325.0, 320.0), vec2(3.0, -240.0));
let origin = harness.origin.to_vec2();
// Up each side facing a gap from the middle of what the boxes all span
// across, to past a line 20 above the highest, a pixel wide.
let side = |x: f32| Rect::from_min_max(pos2(x, 25.0), pos2(x + 1.0, 80.0)).translate(origin);
let line = |from: f32, to: f32| {
Rect::from_min_max(pos2(from, 30.0), pos2(to + 1.0, 31.0)).translate(origin)
};
assert_eq!(
guides(&held),
[
side(250.0),
side(300.0),
line(250.0, 300.0),
side(350.0),
side(400.0),
line(350.0, 400.0),
]
);
// Arrowheads, wing to tip to wing, pointing out at both ends of each line.
let arrowhead = |tip: f32, back: f32| {
let points = [(tip + back, 25.5), (tip, 30.5), (tip + back, 35.5)];
(points.iter())
.map(|&(x, y)| pos2(x, y) + origin)
.collect::<Vec<_>>()
};
assert_eq!(
lines(&held, GUIDE),
[
arrowhead(250.5, 5.0),
arrowhead(300.5, -5.0),
arrowhead(350.5, 5.0),
arrowhead(400.5, -5.0),
]
);
let released = harness.frame(vec![]);
assert_eq!(
(guides(&released), lines(&released, GUIDE)),
(vec![], vec![])
);
}

#[test]
fn moved_nodes_snap_as_far_past_a_node_as_it_is_past_the_next() {
let mut harness = Harness::new(Tool::Cursor);
harness.add_two_boxes();
let node = harness.add_box(pos2(600.0, 300.0), pos2(700.0, 340.0));
harness.selected = Selection::node(node);
// 150 right of the second box, as it is of the first, and 4 more.
drag_by(&mut harness, pos2(650.0, 320.0), vec2(104.0, -240.0));
assert_eq!(harness.boxes()[2], geometry(700.0, 60.0, 100.0, 40.0));
}

#[test]
fn equal_gaps_in_a_column_show_left_of_it() {
let mut harness = Harness::new(Tool::Cursor);
harness.add_box(NODE.min, NODE.max);
harness.add_box(pos2(100.0, 330.0), pos2(250.0, 410.0));
let node = harness.add_box(pos2(400.0, 400.0), pos2(440.0, 440.0));
harness.selected = Selection::node(node);
// Between the two boxes, 3 below halfway.
let held = drag_by(&mut harness, pos2(420.0, 420.0), vec2(-290.0, -187.0));
assert_eq!(harness.boxes()[2], geometry(110.0, 210.0, 40.0, 40.0));
let origin = harness.origin.to_vec2();
let side = |y: f32| Rect::from_min_max(pos2(75.0, y), pos2(130.0, y + 1.0)).translate(origin);
let line = |from: f32, to: f32| {
Rect::from_min_max(pos2(80.0, from), pos2(81.0, to + 1.0)).translate(origin)
};
assert_eq!(
guides(&held),
[
side(130.0),
side(210.0),
line(130.0, 210.0),
side(250.0),
side(330.0),
line(250.0, 330.0),
]
);
}

#[test]
fn a_moved_selection_snaps_into_equal_gaps_as_one() {
let mut harness = Harness::new(Tool::Cursor);
harness.add_two_boxes();
let first = harness.add_box(pos2(300.0, 300.0), pos2(320.0, 340.0));
let second = harness.add_box(pos2(330.0, 300.0), pos2(350.0, 340.0));
harness.selected = Selection::of([Item::Node(first), Item::Node(second)]);
drag_by(&mut harness, pos2(310.0, 320.0), vec2(3.0, -240.0));
let boxes = harness.boxes();
assert_eq!((boxes[2].x, boxes[3].x), (300.0, 330.0));
}

#[test]
fn alt_skips_equal_gaps_too() {
let mut harness = Harness::new(Tool::Cursor);
add_three_boxes(&mut harness);
let held = harness.holding(Modifiers::ALT, |harness| {
drag_by(harness, pos2(325.0, 320.0), vec2(3.0, -240.0))
});
assert_eq!((guides(&held), lines(&held, GUIDE)), (vec![], vec![]));
assert_eq!(harness.boxes()[2], geometry(303.0, 60.0, 50.0, 40.0));
}

#[test]
fn moved_nodes_touch_the_nodes_beside_them() {
let mut harness = Harness::new(Tool::Cursor);
Expand Down
Loading
Loading