Add a Gradient tool focus gizmo for moving a radial gradient's focal point

This commit is contained in:
Keavon Chambers committed 2026-12-05 04:41:30 -08:00
1 parent 51e2003fea
commit 5d0005f7cf
2 files changed
+313 -18

No files matched your search

@@ -1,6 +1,6 @@
//! Geometry of the Gradient tool's gizmos for each end's contour, in gradient space, where the gradient line runs from the start point
//! at the origin to the end point at (1, 0). Each contour passes through its endpoint with its center on the line, set by its curvature
//! curvature, as described in [`graphene_std::vector::gradient_standard`].
//! Geometry of the Gradient tool's gizmos, in gradient space, where the gradient line runs from the start point at the origin to the end
//! point at (1, 0). Each end's contour passes through its endpoint with its center on the line, set by its curvature, as described
//! in [`graphene_std::vector::gradient_standard`].
use glam::{DAffine2, DVec2};
use graphene_std::vector::gradient_standard::{curvature_radius, radius_curvature};
@@ -87,6 +87,47 @@ pub fn rim_resized_toward(end: GradientEnd, curvature: f64, grabbed: DVec2, poin
resized.is_finite().then(|| (resized, endpoint + resized * direction))
}
/// Whether a gradient with these curvatures has a focus to move: a point for its start contour and a circle for its end contour.
pub fn has_focus(curvatures: [f64; 2]) -> bool {
let end_radius = curvature_radius(curvatures[1]);
curvature_radius(curvatures[0]) == 0. && end_radius.is_finite() && end_radius != 0.
}
/// A gradient line in the gradient space of the line it replaces, with the curvature it gives the end contour.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RefocusedLine {
pub start: DVec2,
pub end: DVec2,
pub end_curvature: f64,
}
/// The line of a gradient whose start point (its focus) moves to `focus` while its end circle from `end_curvature` stays put: from the focus through the
/// circle's center to its far side, with the end curvature keeping that circle. A focus on the center keeps `direction`. `None` unless the end is a circle.
pub fn refocused_line(focus: DVec2, end_curvature: f64, direction: DVec2) -> Option<RefocusedLine> {
let radius = curvature_radius(end_curvature);
if !radius.is_finite() || radius == 0. {
return None;
}
let (center, size) = (DVec2::new(1. - radius, 0.), radius.abs());
let to_center = center - focus;
let distance = to_center.length();
let direction = if distance > 0. { to_center / distance } else { direction.try_normalize()? };
Some(RefocusedLine {
start: focus,
end: center + direction * size,
end_curvature: radius_curvature(size / (distance + size)),
})
}
/// The transform placing a gradient along `line`, given in the gradient space of the `old` transform. Its frame turns and scales uniformly within
/// that space, so a contour that's a circle there keeps its shape, even where `old` stretches it into an ellipse.
pub fn transform_along(old: DAffine2, line: RefocusedLine) -> DAffine2 {
let x_axis = line.end - line.start;
old * DAffine2::from_cols(x_axis, x_axis.perp(), line.start)
}
/// The smallest factor `transform` scales any length by, the least singular value of its linear part.
fn least_scale(transform: DAffine2) -> f64 {
let [x_axis, y_axis] = [transform.matrix2.x_axis, transform.matrix2.y_axis];
@@ -229,6 +270,66 @@ mod tests {
assert!((center_curvature(GradientEnd::End, 0.) - 0.5).abs() < 1e-12);
}
/// The center and radius of the end circle of a gradient along `line`, in the gradient space `line` is given in.
fn end_circle(line: RefocusedLine) -> (DVec2, f64) {
let radius = curvature_radius(line.end_curvature);
(line.start.lerp(line.end, 1. - radius), radius.abs() * line.start.distance(line.end))
}
#[test]
fn a_moved_focus_keeps_the_end_circle_in_place() {
// The end circle of radius 0.75 centered at 0.25, with foci inside it, outside it, and behind its center
let end_curvature = radius_curvature(0.75);
let (center, radius) = (DVec2::new(0.25, 0.), 0.75);
for focus in [DVec2::new(0.1, 0.3), DVec2::new(-0.2, -0.4), DVec2::new(3., 2.), DVec2::new(0.9, 0.), DVec2::new(-5., 0.)] {
let line = refocused_line(focus, end_curvature, DVec2::X).unwrap();
assert_eq!(line.start, focus);
let (new_center, new_radius) = end_circle(line);
assert!(new_center.distance(center) < 1e-12 && (new_radius - radius).abs() < 1e-12, "focus {focus}: {line:?}");
// The line runs on through the center to the circle's far side
assert!((line.end.distance(focus) - (focus.distance(center) + radius)).abs() < 1e-12, "focus {focus}: {line:?}");
}
// A focus off the circle's line still aims the line through its center
let line = refocused_line(DVec2::new(0.25, 2.), end_curvature, DVec2::X).unwrap();
assert!(line.end.abs_diff_eq(DVec2::new(0.25, -0.75), 1e-12));
}
#[test]
fn a_focus_on_the_center_keeps_the_direction_and_makes_the_radial_form() {
let end_curvature = radius_curvature(0.75);
let line = refocused_line(contour_center(GradientEnd::End, end_curvature).unwrap(), end_curvature, DVec2::new(0., 2.)).unwrap();
assert_eq!(line.end_curvature, 0.5);
assert!(line.end.abs_diff_eq(DVec2::new(0.25, 0.75), 1e-12));
assert_eq!(
graphene_std::vector::style::GradientForm::from_curvatures([-1., line.end_curvature]),
Some(graphene_std::vector::style::GradientForm::Radial)
);
// Only an end circle has a focus to move around
assert!(has_focus([-1., 0.5]) && has_focus([1., -0.3]));
assert!(!has_focus([0., 0.5]) && !has_focus([-1., 0.]) && !has_focus([-1., 1.]));
assert_eq!(refocused_line(DVec2::ZERO, 0., DVec2::X), None);
}
#[test]
fn a_refocused_transform_keeps_an_elliptical_end_contour_in_place() {
// A radial gradient squashed to half its height and skewed, whose focus moves off its line
let old = DAffine2::from_cols(DVec2::new(100., 20.), DVec2::new(-5., 50.), DVec2::new(30., 40.));
let line = refocused_line(DVec2::new(0.3, 0.4), 0.5, DVec2::X).unwrap();
let new = transform_along(old, line);
// The end contour's points land on the same ellipse before and after (within its chords' sag), though spread differently around it
let before = contour_polyline(GradientEnd::End, 0.5, old, 1e6);
let after = contour_polyline(GradientEnd::End, line.end_curvature, new, 1e6);
for point in after {
assert!(distance_to_polyline(&before, point).unwrap() <= CONTOUR_SAG_TOLERANCE, "{point} left the ellipse");
}
assert!(new.transform_point2(DVec2::ZERO).abs_diff_eq(old.transform_point2(DVec2::new(0.3, 0.4)), 1e-9));
}
#[test]
fn contours_are_drawn_through_their_endpoints_out_to_their_reach() {
let unit_to_viewport = DAffine2::from_scale_angle_translation(DVec2::new(200., 100.), 0.3, DVec2::new(40., 60.));
@@ -8,7 +8,9 @@ use crate::messages::portfolio::document::overlays::utility_types::{GizmoEmphasi
use crate::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier;
use crate::messages::portfolio::document::utility_types::network_interface::{FlowType, NodeNetworkInterface};
use crate::messages::tool::common_functionality::auto_panning::AutoPanning;
use crate::messages::tool::common_functionality::gradient_gizmos::{GradientEnd, center_curvature, contour_center, contour_polyline, distance_to_polyline, rim_curvature, rim_resized_toward};
use crate::messages::tool::common_functionality::gradient_gizmos::{
GradientEnd, center_curvature, contour_center, contour_polyline, distance_to_polyline, has_focus, refocused_line, rim_curvature, rim_resized_toward, transform_along,
};
use crate::messages::tool::common_functionality::graph_modification_utils::{
self, NodeGraphLayer, get_chain_source_gradient_settings, get_fill_node_id_with_direct_fill_input, get_gradient_stops, get_upstream_gradient_value_node_id, gradient_chain_target_input,
paint_target_layer, replaceable_paint_chain, reverse_direction_tooltip_description,
@@ -408,6 +410,7 @@ fn resolve_gradient(layer: LayerNodeIdentifier, network_interface: &NodeNetworkI
curvatures: drawn_curvatures(gradient.curvatures, gradient_form),
settings: gradient.settings,
transform: gradient.transform,
transform_is_wired: !gradient.transform_is_value,
},
GradientSource::Direct,
));
@@ -424,6 +427,8 @@ fn resolve_gradient(layer: LayerNodeIdentifier, network_interface: &NodeNetworkI
#[derive(Clone, Copy, Debug, Default)]
struct GradientAppearance {
transform: DAffine2,
/// Whether a wire feeds the transform of the Fill node holding it, which leaves the tool unable to write it.
transform_is_wired: bool,
gradient_form: GradientForm,
/// Where its curvatures come from. Unless they make `gradient_form`, the tool draws the linear form's controls.
// TODO: Draw the stops and midpoints of custom curvatures where their contours cross the line, since the ramp can then span just part of it
@@ -434,6 +439,11 @@ struct GradientAppearance {
}
impl GradientAppearance {
/// Whether the tool may move the focus of these curvatures, which takes writing both the end curvature and the transform.
fn can_move_focus(&self, curvatures: [f64; 2]) -> bool {
self.has_editable_curvatures() && !self.transform_is_wired && has_focus(curvatures)
}
/// Whether the tool may adjust its curvatures, which takes ones not fed from the graph.
fn has_editable_curvatures(&self) -> bool {
self.curvature_source != CurvatureSource::Graph
@@ -508,6 +518,7 @@ fn read_gradient_chain_state(layer: LayerNodeIdentifier, network_interface: &Nod
let gradient_form = gradient_form.unwrap_or_default();
GradientAppearance {
transform: composed_transform,
transform_is_wired: false,
gradient_form,
curvature_source,
curvatures: drawn_curvatures(curvatures, gradient_form),
@@ -788,6 +799,8 @@ pub enum GradientDragTarget {
ContourCenter(GradientEnd),
/// The dashed rim of an end's contour, or its dashed line where it's flat.
ContourRim(GradientEnd),
/// The start point of a gradient whose start is a point and end is a circle, moving within the circle while it stays put.
Focus,
}
impl GradientDragTarget {
@@ -798,6 +811,11 @@ impl GradientDragTarget {
_ => None,
}
}
/// Whether dragging this changes the curvatures.
fn changes_curvatures(self) -> bool {
self.contour_end().is_some() || self == Self::Focus
}
}
/// Contains information about the selected gradient handle
@@ -885,7 +903,7 @@ impl SelectedGradient {
if self.dragging != GradientDragTarget::New {
self.gradient = self.initial_gradient.clone();
self.appearance.transform = self.initial_gradient_transform;
if self.dragging.contour_end().is_some() {
if self.dragging.changes_curvatures() {
self.set_curvatures(self.initial_curvatures);
}
self.render_gradient(responses);
@@ -906,13 +924,15 @@ impl SelectedGradient {
}
};
if matches!(self.dragging, GradientDragTarget::End | GradientDragTarget::Start | GradientDragTarget::New) {
if matches!(self.dragging, GradientDragTarget::End | GradientDragTarget::Start | GradientDragTarget::New | GradientDragTarget::Focus) {
let document_to_viewport = view.document.metadata().document_to_viewport;
let point = anchor_point();
let delta = point - mouse;
// Locking keeps the angle and Shift steps it, either way holding the end on that line through the other end
let required = if lock_angle || snap_rotate {
let required = if self.dragging == GradientDragTarget::Focus {
None
} else if lock_angle || snap_rotate {
let mut angle = DVec2::X.try_angle_to(delta).unwrap_or(*gradient_angle);
if lock_angle {
@@ -955,6 +975,22 @@ impl SelectedGradient {
GradientDragTarget::New => {
self.appearance.transform = create_new_gradient_transform(self.gradient_to_viewport_transform.inverse().transform_point2(drag_start), local_mouse);
}
GradientDragTarget::Focus => {
let [start_curvature, end_curvature] = self.initial_curvatures;
let initial_unit_to_viewport = self.gradient_to_viewport_transform * self.initial_gradient_transform;
// The focus snaps onto the end circle's center, making the radial form
let center = contour_center(GradientEnd::End, end_curvature).unwrap_or_default();
let snapped = initial_unit_to_viewport.transform_point2(center).distance(mouse) < GRADIENT_CONTOUR_SNAP_DISTANCE;
let focus = if snapped { center } else { initial_unit_to_viewport.inverse().transform_point2(mouse) };
// Onto the center, the line keeps the direction it last had
let direction = (self.initial_gradient_transform.inverse() * self.appearance.transform).transform_vector2(DVec2::X);
if let Some(line) = refocused_line(focus, end_curvature, direction) {
self.appearance.transform = transform_along(self.initial_gradient_transform, line);
self.set_curvatures([start_curvature, line.end_curvature]);
}
}
GradientDragTarget::Stop(stop) => {
let (viewport_start, viewport_end) = self.viewport_handle_positions();
@@ -1041,6 +1077,11 @@ impl SelectedGradient {
self.render_gradient(responses);
}
/// Whether it can move its focus (a start point inside or beside an end circle) as the press found it.
fn can_move_focus(&self) -> bool {
self.appearance.can_move_focus(self.initial_curvatures)
}
/// Set the curvatures, along with the form they make, if either.
fn set_curvatures(&mut self, curvatures: [f64; 2]) {
let form = GradientForm::from_curvatures(curvatures);
@@ -1073,6 +1114,14 @@ impl SelectedGradient {
transform: Some(self.appearance.transform),
});
}
// Moving the focus re-aims the line and readjusts its end's curvature to keep the end circle
if self.dragging == GradientDragTarget::Focus {
responses.add(GraphOperationMessage::GradientCurvatureSet {
layer,
curvatures: self.appearance.curvatures,
});
}
}
}
@@ -1087,7 +1136,7 @@ impl SelectedGradient {
/// What dragging this moves that could be snapped to. A layer drawing only its gradient has the gradient's line or circle as its geometry, which moves with either end.
fn moving(&self, document: &DocumentMessageHandler) -> Moving {
let gradient_value = DefinitionIdentifier::ProtoNode(graphene_std::math_nodes::gradient_value::IDENTIFIER);
let moves_an_end = matches!(self.dragging, GradientDragTarget::Start | GradientDragTarget::End | GradientDragTarget::New);
let moves_an_end = matches!(self.dragging, GradientDragTarget::Start | GradientDragTarget::End | GradientDragTarget::New | GradientDragTarget::Focus);
let layer = self
.layer
.filter(|&layer| moves_an_end && graph_modification_utils::is_layer_fed_by_node_of_name(layer, &document.network_interface, &gradient_value));
@@ -1275,7 +1324,7 @@ impl Fsm for GradientToolFsmState {
// Determine which stop is selected (being dragged) and hovered (closest to mouse)
// so they can be drawn last to appear on top of other overlapping stops
let selected_stop_id: Option<StopId> = match dragging {
Some(GradientDragTarget::Start) => Some(StopId::Start),
Some(GradientDragTarget::Start | GradientDragTarget::Focus) => Some(StopId::Start),
Some(GradientDragTarget::End) => Some(StopId::End),
Some(GradientDragTarget::Stop(0)) if first_at_start => Some(StopId::Start),
Some(GradientDragTarget::Stop(i)) if last_at_end && i == gradient.len() - 1 => Some(StopId::End),
@@ -1527,7 +1576,7 @@ impl Fsm for GradientToolFsmState {
return ready_default;
}
}
GradientDragTarget::New | GradientDragTarget::ContourCenter(_) | GradientDragTarget::ContourRim(_) => {
GradientDragTarget::New | GradientDragTarget::ContourCenter(_) | GradientDragTarget::ContourRim(_) | GradientDragTarget::Focus => {
responses.add(DocumentMessage::AbortTransaction);
return ready_default;
}
@@ -1777,6 +1826,16 @@ impl Fsm for GradientToolFsmState {
}
}
// Alt takes hold of a start point inside an end circle as its focus, rather than as the line's end
if input.keyboard.get(Key::Alt as usize)
&& let Some(selected_gradient) = &mut tool_data.selected_gradient
&& selected_gradient.dragging == GradientDragTarget::Start
&& selected_gradient.can_move_focus()
{
selected_gradient.dragging = GradientDragTarget::Focus;
drag_hint = Some(GradientDragHintState::Focus);
}
// Initialize `gradient_angle` from the existing gradient so Ctrl (lock angle) works from the first mouse move
if let Some(selected_gradient) = &tool_data.selected_gradient {
let (vp_start, vp_end) = selected_gradient.viewport_handle_positions();
@@ -1833,6 +1892,7 @@ impl Fsm for GradientToolFsmState {
.unwrap_or_else(|| working_color_gradient(global_tool_data.primary_color, global_tool_data.secondary_color)),
GradientAppearance {
transform: DAffine2::IDENTITY,
transform_is_wired: false,
gradient_form: tool_options.gradient_form,
curvature_source: CurvatureSource::Form,
curvatures: tool_options.gradient_form.curvatures(),
@@ -1949,7 +2009,7 @@ impl Fsm for GradientToolFsmState {
GradientDragTarget::End => {
selected.gradient.is_empty() || (1. - selected.gradient.position(selected.gradient.len() - 1, selected.appearance.settings.cyclic)).abs() >= f64::EPSILON * 1000.
}
GradientDragTarget::ContourCenter(_) | GradientDragTarget::ContourRim(_) => true,
GradientDragTarget::ContourCenter(_) | GradientDragTarget::ContourRim(_) | GradientDragTarget::Focus => true,
_ => false,
}) {
tool_data.selected_gradient = None;
@@ -2030,12 +2090,15 @@ impl Fsm for GradientToolFsmState {
GradientHoverTarget::Stop => {
groups.push(HintGroup(vec![HintInfo::mouse(MouseMotion::LmbDrag, "Move Color Stop")]));
}
GradientHoverTarget::Endpoint => {
GradientHoverTarget::Endpoint { moves_focus } => {
groups.push(HintGroup(vec![
HintInfo::mouse(MouseMotion::LmbDrag, "Move Gradient End"),
HintInfo::keys([Key::Shift], "15° Increments").prepend_plus(),
HintInfo::keys([Key::Control], "Lock Angle").prepend_plus(),
]));
if *moves_focus {
groups.push(HintGroup(vec![HintInfo::keys_and_mouse([Key::Alt], MouseMotion::LmbDrag, "Move Focus")]));
}
}
GradientHoverTarget::Midpoint { resettable } => {
groups.push(HintGroup(vec![HintInfo::mouse(MouseMotion::LmbDrag, "Move Midpoint")]));
@@ -2202,12 +2265,18 @@ fn detect_hover_target(mouse: DVec2, document: &DocumentMessageHandler, viewport
}
}
// The start of a gradient with a focus to move can take hold of it instead
let start_moves_focus = appearance.can_move_focus(appearance.curvatures);
// Check stops
for index in 0..gradient.len() {
let position = gradient.position(index, appearance.settings.cyclic);
if start.lerp(end, position).distance_squared(mouse) < stop_tolerance {
return if position.abs() < f64::EPSILON * 1000. || (1. - position).abs() < f64::EPSILON * 1000. {
GradientHoverTarget::Endpoint
let at_start = position.abs() < f64::EPSILON * 1000.;
return if at_start || (1. - position).abs() < f64::EPSILON * 1000. {
GradientHoverTarget::Endpoint {
moves_focus: at_start && start_moves_focus,
}
} else {
GradientHoverTarget::Stop
};
@@ -2215,9 +2284,11 @@ fn detect_hover_target(mouse: DVec2, document: &DocumentMessageHandler, viewport
}
// Check start/end handles (pure endpoints without stops)
for endpoint_position in [start, end] {
for (endpoint_position, is_start) in [(start, true), (end, false)] {
if endpoint_position.distance_squared(mouse) < stop_tolerance {
return GradientHoverTarget::Endpoint;
return GradientHoverTarget::Endpoint {
moves_focus: is_start && start_moves_focus,
};
}
}
@@ -2241,7 +2312,7 @@ fn compute_selected_target(tool_data: &GradientToolData) -> GradientSelectedTarg
let resettable = i < selected_gradient.gradient.len() && midpoint_is_resettable(selected_gradient.gradient.midpoint(i));
GradientSelectedTarget::Midpoint { resettable }
}
GradientDragTarget::New | GradientDragTarget::ContourCenter(_) | GradientDragTarget::ContourRim(_) => GradientSelectedTarget::None,
GradientDragTarget::New | GradientDragTarget::ContourCenter(_) | GradientDragTarget::ContourRim(_) | GradientDragTarget::Focus => GradientSelectedTarget::None,
}
}
@@ -2412,7 +2483,10 @@ enum GradientHoverTarget {
None,
InsertionPoint,
Stop,
Endpoint,
Endpoint {
/// Whether it's the start of a gradient with a focus that Alt takes hold of instead.
moves_focus: bool,
},
Midpoint {
resettable: bool,
},
@@ -2441,6 +2515,7 @@ enum GradientDragHintState {
resettable: bool,
},
Contour,
Focus,
}
#[cfg(test)]
@@ -4413,6 +4488,125 @@ mod test_gradient {
assert_curvatures(fill_curvatures(&editor, fill_node_id), GradientForm::Linear.curvatures(), "undoing both");
}
#[tokio::test]
async fn alt_dragging_a_radial_gradients_start_moves_its_focus_within_the_unmoved_end_circle() {
use graphene_std::vector::gradient_standard::radius_curvature;
let mut editor = EditorTestUtils::create();
editor.new_document().await;
let (layer, curvature_node_id) = radial_gradient_list_layer(&mut editor).await;
let line = |editor: &EditorTestUtils| (gradient_point(editor, layer, DVec2::ZERO), gradient_point(editor, layer, DVec2::X));
let (center, end) = line(&editor);
let radius = center.distance(end);
// The focus moves off the center, re-aiming the line through the circle's center to its far side, which the end curvature keeps the circle through
let focus_offset = DVec2::new(0.25, 0.15) * radius;
drag_between(&mut editor, center, center + focus_offset, ModifierKeys::ALT).await;
let (start, end) = line(&editor);
let length = focus_offset.length() + radius;
assert!(start.abs_diff_eq(center + focus_offset, 1e-6), "the focus should move to the pointer, not {start}");
assert!(
end.abs_diff_eq(center - focus_offset.normalize() * radius, 1e-6),
"the end should land on the circle's far side, not {end}"
);
assert_curvatures(curvature_values(&editor, curvature_node_id), [-1., radius_curvature(radius / length)], "the refocused gradient");
// Dragged back onto the center, the focus makes the radial form again, keeping the line's direction
drag_between(&mut editor, start, center + DVec2::new(1., -1.), ModifierKeys::ALT).await;
let (start, end) = line(&editor);
assert!(start.abs_diff_eq(center, 1e-6), "the focus should snap onto the center, not {start}");
assert!(
end.abs_diff_eq(center - focus_offset.normalize() * radius, 1e-6),
"the line should keep its direction, not end at {end}"
);
assert_curvatures(curvature_values(&editor, curvature_node_id), GradientForm::Radial.curvatures(), "the recentered focus");
// Each move is its own step in the history
editor.handle_message(DocumentMessage::Undo).await;
editor.handle_message(DocumentMessage::Undo).await;
let (start, undone_end) = line(&editor);
assert!(
start.abs_diff_eq(center, 1e-6) && undone_end.abs_diff_eq(center + DVec2::X * radius, 1e-6),
"undoing should restore the line"
);
assert_curvatures(curvature_values(&editor, curvature_node_id), GradientForm::Radial.curvatures(), "undoing both moves");
// Without Alt, the start moves the line's start as it always has, leaving the end curvature alone
drag_between(&mut editor, center, center + focus_offset, ModifierKeys::empty()).await;
let (start, unmoved_end) = line(&editor);
assert!(start.abs_diff_eq(center + focus_offset, 1e-6) && unmoved_end.abs_diff_eq(undone_end, 1e-6));
assert_curvatures(curvature_values(&editor, curvature_node_id), GradientForm::Radial.curvatures(), "moving the start without Alt");
}
#[tokio::test]
async fn a_wired_fill_transform_leaves_the_focus_of_its_gradient_in_place() {
let mut editor = EditorTestUtils::create();
editor.new_document().await;
let (layer, fill_node_id) = fill_gradient_layer(&mut editor).await;
editor
.handle_message(GradientToolMessage::UpdateOptions {
options: super::GradientOptionsUpdate::Form(GradientForm::Radial),
})
.await;
// A wire feeds the transform, which the tool reads as the identity, so zooming in gives the unit-sized gradient room on screen
let transform_source = editor
.create_node_by_name(DefinitionIdentifier::ProtoNode(graphene_std::transform_nodes::extract_transform::IDENTIFIER))
.await;
editor
.handle_message(NodeGraphMessage::CreateWire {
output_connector: OutputConnector::primary_output(transform_source),
input_connector: InputConnector::node(fill_node_id, fill::TransformInput),
})
.await;
editor.handle_message(NodeGraphMessage::SelectedNodesSet { nodes: vec![layer.to_node()] }).await;
editor.handle_message(NavigationMessage::CanvasZoomSet { zoom_factor: 200. }).await;
// The start offers no focus to move, since re-aiming the line can't be written, so Alt-dragging it leaves the end circle's curvature alone
let center = gradient_point(&editor, layer, DVec2::ZERO);
let radius = center.distance(gradient_point(&editor, layer, DVec2::X));
let hovered = detect_hover_target(center, editor.active_document(), &ViewportMessageHandler::default());
assert!(
matches!(hovered, GradientHoverTarget::Endpoint { moves_focus: false }),
"the start should be hovered without a focus, not {hovered:?}"
);
drag_between(&mut editor, center, center + DVec2::new(-0.3, 0.2) * radius, ModifierKeys::ALT).await;
assert_curvatures(fill_curvatures(&editor, fill_node_id), GradientForm::Radial.curvatures(), "the radial gradient with a wired transform");
}
#[tokio::test]
async fn alt_dragging_a_radial_fill_gradients_start_moves_its_focus_writing_its_fill_nodes_curvatures() {
use graphene_std::vector::gradient_standard::radius_curvature;
let mut editor = EditorTestUtils::create();
editor.new_document().await;
let (layer, fill_node_id) = fill_gradient_layer(&mut editor).await;
editor
.handle_message(GradientToolMessage::UpdateOptions {
options: super::GradientOptionsUpdate::Form(GradientForm::Radial),
})
.await;
assert_curvatures(fill_curvatures(&editor, fill_node_id), GradientForm::Radial.curvatures(), "the radial gradient");
// The focus moves off the center while the end circle stays put
let (center, end) = (gradient_point(&editor, layer, DVec2::ZERO), gradient_point(&editor, layer, DVec2::X));
let radius = center.distance(end);
let focus_offset = DVec2::new(-0.3, 0.2) * radius;
drag_between(&mut editor, center, center + focus_offset, ModifierKeys::ALT).await;
let (start, end) = (gradient_point(&editor, layer, DVec2::ZERO), gradient_point(&editor, layer, DVec2::X));
assert!(start.abs_diff_eq(center + focus_offset, 1e-6), "the focus should move to the pointer, not {start}");
assert!(
end.abs_diff_eq(center - focus_offset.normalize() * radius, 1e-6),
"the end should land on the circle's far side, not {end}"
);
let length = focus_offset.length() + radius;
assert_curvatures(fill_curvatures(&editor, fill_node_id), [-1., radius_curvature(radius / length)], "the refocused gradient");
editor.handle_message(DocumentMessage::Undo).await;
assert_curvatures(fill_curvatures(&editor, fill_node_id), GradientForm::Radial.curvatures(), "undoing the move");
}
#[tokio::test]
async fn drag_on_blank_layer_starts_gradient_chain() {
use crate::messages::tool::common_functionality::graph_modification_utils::get_gradient_stops;