Skip to content

Commit b0cdee9

Browse files
committed
fix distribution snapper visualization going far from layers
1 parent 80b8df8 commit b0cdee9

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

editor/src/messages/tool/common_functionality/snapping/distribution_snapper.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ impl DistributionSnapper {
196196
let mut snap_x: Option<SnappedPoint> = None;
197197
let mut snap_y: Option<SnappedPoint> = None;
198198

199-
self.x(consider_x, bounds, tolerance, &mut snap_x, point);
200-
self.y(consider_y, bounds, tolerance, &mut snap_y, point);
199+
self.horizontal_snap(consider_x, bounds, tolerance / 2., &mut snap_x, point);
200+
self.vertical_snap(consider_y, bounds, tolerance / 2., &mut snap_y, point);
201201

202202
match (snap_x, snap_y) {
203203
(Some(x), Some(y)) => {
@@ -220,7 +220,7 @@ impl DistributionSnapper {
220220
}
221221
}
222222

223-
fn x(&self, consider_x: bool, bounds: Rect, tolerance: f64, snap_x: &mut Option<SnappedPoint>, point: &SnapCandidatePoint) {
223+
fn horizontal_snap(&self, consider_x: bool, bounds: Rect, tolerance: f64, snap_x: &mut Option<SnappedPoint>, point: &SnapCandidatePoint) {
224224
// Right
225225
if consider_x && !self.right.is_empty() {
226226
let (equal_dist, mut vec_right) = Self::top_level_matches(bounds, &self.right, tolerance, dist_right);
@@ -269,7 +269,7 @@ impl DistributionSnapper {
269269
}
270270
}
271271

272-
fn y(&self, consider_y: bool, bounds: Rect, tolerance: f64, snap_y: &mut Option<SnappedPoint>, point: &SnapCandidatePoint) {
272+
fn vertical_snap(&self, consider_y: bool, bounds: Rect, tolerance: f64, snap_y: &mut Option<SnappedPoint>, point: &SnapCandidatePoint) {
273273
// Down
274274
if consider_y && !self.down.is_empty() {
275275
let (equal_dist, mut vec_down) = Self::top_level_matches(bounds, &self.down, tolerance, dist_down);
@@ -322,7 +322,24 @@ impl DistributionSnapper {
322322
}
323323

324324
pub fn free_snap(&mut self, snap_data: &mut SnapData, point: &SnapCandidatePoint, snap_results: &mut SnapResults, config: SnapTypeConfiguration) {
325-
let Some(bounds) = config.bbox else { return };
325+
let Some(config_bbox) = config.bbox else { return };
326+
let Some(layer_bbox) = snap_data
327+
.document
328+
.metadata()
329+
.bounding_box_document(
330+
snap_data
331+
.document
332+
.network_interface
333+
.selected_nodes()
334+
.selected_unlocked_layers(&snap_data.document.network_interface)
335+
.next()
336+
.expect("No selected layers"),
337+
)
338+
.map(|bbox| Rect::from_box(bbox))
339+
else {
340+
return;
341+
};
342+
let bounds = config_bbox.intersection(layer_bbox).expect("No Intersection");
326343
if point.source != SnapSource::BoundingBox(BoundingBoxSnapSource::CenterPoint) || !snap_data.document.snapping_state.bounding_box.distribute_evenly {
327344
return;
328345
}

node-graph/gcore/src/graphic_element/renderer/rect.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,31 @@ impl Rect {
6767
Self::from_box([self[0] - delta, self[1] + delta])
6868
}
6969

70-
/// Expand a rect by a certain amount on top/bottom and on left/right
70+
/// Checks if two rects intersect
7171
#[must_use]
7272
pub fn intersects(&self, other: Self) -> bool {
7373
let [mina, maxa] = [self[0].min(self[1]), self[0].max(self[1])];
7474
let [minb, maxb] = [other[0].min(other[1]), other[0].max(other[1])];
7575
mina.x <= maxb.x && minb.x <= maxa.x && mina.y <= maxb.y && minb.y <= maxa.y
7676
}
7777

78+
/// Find intersection
79+
#[must_use]
80+
pub fn intersection(&self, other: Self) -> Option<Rect> {
81+
if !self.intersects(other) {
82+
return None;
83+
}
84+
let [mina, maxa] = [self[0].min(self[1]), self[0].max(self[1])];
85+
let [minb, maxb] = [other[0].min(other[1]), other[0].max(other[1])];
86+
87+
let min_x = mina.x.max(minb.x);
88+
let min_y = mina.y.max(minb.y);
89+
let max_x = maxa.x.min(maxb.x);
90+
let max_y = maxa.y.min(maxb.y);
91+
92+
Some(Rect::from_box([DVec2::new(min_x, min_y), DVec2::new(max_x, max_y)]))
93+
}
94+
7895
/// Does this rect contain a point
7996
#[must_use]
8097
pub fn contains(&self, p: DVec2) -> bool {

0 commit comments

Comments
 (0)