Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Artboard snapping #1734

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 35 additions & 8 deletions editor/src/messages/tool/tool_messages/artboard_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::messages::portfolio::document::overlays::utility_types::OverlayContex
use crate::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier;
use crate::messages::tool::common_functionality::auto_panning::AutoPanning;
use crate::messages::tool::common_functionality::graph_modification_utils::is_layer_fed_by_node_of_name;
use crate::messages::tool::common_functionality::snapping::SnapCandidatePoint;
use crate::messages::tool::common_functionality::snapping::SnapData;
use crate::messages::tool::common_functionality::snapping::SnapManager;
use crate::messages::tool::common_functionality::transformation_cage::*;

Expand Down Expand Up @@ -209,7 +211,20 @@ impl Fsm for ArtboardToolFsmState {
let from_center = input.keyboard.get(center as usize);
let constrain_square = input.keyboard.get(constrain_axis_or_aspect as usize);
let mouse_position = input.mouse.position;
tool_data.resize_artboard(responses, document, mouse_position, from_center, constrain_square);
let to_viewport = document.metadata().document_to_viewport;

let present_artboard = match tool_data.selected_artboard {
Some(value) => value,
None => LayerNodeIdentifier::default(),
};
let ignore_slice: &[LayerNodeIdentifier] = &[present_artboard];
let snap_data = SnapData::ignore(document, input, ignore_slice);
let document_mouse = to_viewport.inverse().transform_point2(input.mouse.position);
let snapped = tool_data.snap_manager.free_snap(&snap_data, &SnapCandidatePoint::handle(document_mouse), None, false);
let snapped_mouse_position = to_viewport.transform_point2(snapped.snapped_point_document);
tool_data.snap_manager.update_indicator(snapped);

tool_data.resize_artboard(responses, document, snapped_mouse_position, from_center, constrain_square);

// AutoPanning
let messages = [
Expand All @@ -220,10 +235,13 @@ impl Fsm for ArtboardToolFsmState {

ArtboardToolFsmState::ResizingBounds
}

(ArtboardToolFsmState::Dragging, ArtboardToolMessage::PointerMove { constrain_axis_or_aspect, center }) => {
if let Some(ref mut bounds) = &mut tool_data.bounding_box_manager {
let axis_align = input.keyboard.get(constrain_axis_or_aspect as usize);

let mouse_position = axis_align_drag(axis_align, input.mouse.position, tool_data.drag_start);

let size = bounds.bounds[1] - bounds.bounds[0];
let position = bounds.bounds[0] + bounds.transform.inverse().transform_vector2(mouse_position - tool_data.drag_current);

Expand Down Expand Up @@ -251,16 +269,27 @@ impl Fsm for ArtboardToolFsmState {
}
(ArtboardToolFsmState::Drawing, ArtboardToolMessage::PointerMove { constrain_axis_or_aspect, center }) => {
let mouse_position = input.mouse.position;
let snapped_mouse_position = mouse_position; //tool_data.snap_manager.snap_position(responses, document, mouse_position);

let root_transform = document.metadata().document_to_viewport.inverse();

let to_viewport = document.metadata().document_to_viewport;
let present_artboard = match tool_data.selected_artboard {
Some(value) => value,
None => LayerNodeIdentifier::default(),
};

let ignore_slice: &[LayerNodeIdentifier] = &[present_artboard];
let snap_data = SnapData::ignore(document, input, ignore_slice);
let document_mouse = to_viewport.inverse().transform_point2(input.mouse.position);
let snapped = tool_data.snap_manager.free_snap(&snap_data, &SnapCandidatePoint::handle(document_mouse), None, false);
let snapped_mouse_position = to_viewport.transform_point2(snapped.snapped_point_document);
tool_data.snap_manager.update_indicator(snapped);
let mut start = tool_data.drag_start;
let mut size = snapped_mouse_position - start;
let root_transform = document.metadata().document_to_viewport.inverse();

// Constrain axis
if input.keyboard.get(constrain_axis_or_aspect as usize) {
size = size.abs().max(size.abs().yx()) * size.signum();
}

// From center
if input.keyboard.get(center as usize) {
start -= size;
Expand All @@ -280,9 +309,6 @@ impl Fsm for ArtboardToolFsmState {
let id = NodeId(generate_uuid());
tool_data.selected_artboard = Some(LayerNodeIdentifier::new_unchecked(id));

//tool_data.snap_manager.start_snap(document, input, document.bounding_boxes(), true, true);
//tool_data.snap_manager.add_all_document_handles(document, input, &[], &[], &[]);

responses.add(GraphOperationMessage::NewArtboard {
id,
artboard: graphene_core::Artboard {
Expand All @@ -304,6 +330,7 @@ impl Fsm for ArtboardToolFsmState {

ArtboardToolFsmState::Drawing
}

(ArtboardToolFsmState::Ready, ArtboardToolMessage::PointerMove { .. }) => {
let cursor = tool_data.bounding_box_manager.as_ref().map_or(MouseCursorIcon::Default, |bounds| bounds.get_cursor(input, false));

Expand Down
11 changes: 9 additions & 2 deletions editor/src/messages/tool/tool_messages/pen_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,13 @@ impl PenToolData {

if let Some(relative) = relative.map(|layer| transform.transform_point2(layer)).filter(|_| snap_angle || lock_angle) {
let resolution = LINE_ROTATE_SNAP_ANGLE.to_radians();

let angle = if lock_angle {
self.angle
} else {
} else if (relative - document_pos) != DVec2::ZERO && !lock_angle {
(-(relative - document_pos).angle_between(DVec2::X) / resolution).round() * resolution
} else {
self.angle
};
document_pos = relative - (relative - document_pos).project_onto(DVec2::new(angle.cos(), angle.sin()));

Expand Down Expand Up @@ -535,7 +538,11 @@ impl PenToolData {
}

if let Some(relative) = relative.map(|layer| transform.transform_point2(layer)) {
self.angle = -(relative - document_pos).angle_between(DVec2::X)
if (relative - document_pos) != DVec2::ZERO {
self.angle = -(relative - document_pos).angle_between(DVec2::X)
} else {
self.angle = 0.0;
}
}

transform.inverse().transform_point2(document_pos)
Expand Down