Skip to content

Commit f2d3d01

Browse files
authored
Merge branch 'master' into qr-code-shape
2 parents 4123523 + 86e41a1 commit f2d3d01

26 files changed

Lines changed: 600 additions & 1045 deletions

File tree

editor/src/messages/input_mapper/utility_types/misc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ impl FrameTimeInfo {
139139
}
140140

141141
pub fn advance_timestamp(&mut self, next_timestamp: Duration) {
142-
debug_assert!(next_timestamp >= self.timestamp);
142+
// Guard against non-monotonic timestamps from the browser (Keavon observed this once in Chrome)
143+
let next_timestamp = next_timestamp.max(self.timestamp);
143144

144145
self.prev_timestamp = Some(self.timestamp);
145146
self.timestamp = next_timestamp;

editor/src/messages/portfolio/document/graph_operation/transform_utils.rs

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,21 @@ use glam::{DAffine2, DVec2};
33
use graph_craft::document::value::TaggedValue;
44
use graph_craft::document::{NodeId, NodeInput};
55
use graphene_std::subpath::Subpath;
6+
use graphene_std::transform::Transform;
67
use graphene_std::vector::PointId;
78

8-
/// Convert an affine transform into the tuple `(scale, angle, translation, shear)` assuming `shear.y = 0`.
9-
pub fn compute_scale_angle_translation_shear(transform: DAffine2) -> (DVec2, f64, DVec2, DVec2) {
10-
let x_axis = transform.matrix2.x_axis;
11-
let y_axis = transform.matrix2.y_axis;
12-
13-
// Assuming there is no vertical shear
14-
let angle = x_axis.y.atan2(x_axis.x);
15-
let (sin, cos) = angle.sin_cos();
16-
let scale_x = if cos.abs() > 1e-10 { x_axis.x / cos } else { x_axis.y / sin };
17-
18-
let mut shear_x = (sin * y_axis.y + cos * y_axis.x) / (sin * sin * scale_x + cos * cos * scale_x);
19-
if !shear_x.is_finite() {
20-
shear_x = 0.;
21-
}
22-
let scale_y = if cos.abs() > 1e-10 {
23-
(y_axis.y - scale_x * sin * shear_x) / cos
24-
} else {
25-
(scale_x * cos * shear_x - y_axis.x) / sin
26-
};
27-
let translation = transform.translation;
28-
let scale = DVec2::new(scale_x, scale_y);
29-
let shear = DVec2::new(shear_x, 0.);
30-
(scale, angle, translation, shear)
31-
}
32-
339
/// Update the inputs of the transform node to match a new transform
3410
pub fn update_transform(network_interface: &mut NodeNetworkInterface, node_id: &NodeId, transform: DAffine2) {
35-
let (scale, rotation, translation, shear) = compute_scale_angle_translation_shear(transform);
11+
let (rotation, scale, skew) = transform.decompose_rotation_scale_skew();
12+
let translation = transform.translation;
3613

3714
let rotation = rotation.to_degrees();
38-
let shear = DVec2::new(shear.x.atan().to_degrees(), shear.y.atan().to_degrees());
15+
let skew = DVec2::new(skew.atan().to_degrees(), 0.);
3916

4017
network_interface.set_input(&InputConnector::node(*node_id, 1), NodeInput::value(TaggedValue::DVec2(translation), false), &[]);
4118
network_interface.set_input(&InputConnector::node(*node_id, 2), NodeInput::value(TaggedValue::F64(rotation), false), &[]);
4219
network_interface.set_input(&InputConnector::node(*node_id, 3), NodeInput::value(TaggedValue::DVec2(scale), false), &[]);
43-
network_interface.set_input(&InputConnector::node(*node_id, 4), NodeInput::value(TaggedValue::DVec2(shear), false), &[]);
20+
network_interface.set_input(&InputConnector::node(*node_id, 4), NodeInput::value(TaggedValue::DVec2(skew), false), &[]);
4421
}
4522

4623
// TODO: This should be extracted from the graph at the location of the transform node.
@@ -81,12 +58,12 @@ pub fn get_current_transform(inputs: &[NodeInput]) -> DAffine2 {
8158
};
8259
let rotation = if let Some(&TaggedValue::F64(rotation)) = inputs[2].as_value() { rotation } else { 0. };
8360
let scale = if let Some(&TaggedValue::DVec2(scale)) = inputs[3].as_value() { scale } else { DVec2::ONE };
84-
let shear = if let Some(&TaggedValue::DVec2(shear)) = inputs[4].as_value() { shear } else { DVec2::ZERO };
61+
let skew = if let Some(&TaggedValue::DVec2(skew)) = inputs[4].as_value() { skew } else { DVec2::ZERO };
8562

8663
let rotation = rotation.to_radians();
87-
let shear = DVec2::new(shear.x.to_radians().tan(), shear.y.to_radians().tan());
64+
let skew = DVec2::new(skew.x.to_radians().tan(), skew.y.to_radians().tan());
8865

89-
DAffine2::from_scale_angle_translation(scale, rotation, translation) * DAffine2::from_cols_array(&[1., shear.y, shear.x, 1., 0., 0.])
66+
DAffine2::from_scale_angle_translation(scale, rotation, translation) * DAffine2::from_cols_array(&[1., skew.y, skew.x, 1., 0., 0.])
9067
}
9168

9269
/// Extract the current normalized pivot from the layer
@@ -135,31 +112,32 @@ mod tests {
135112
/// ```
136113
#[test]
137114
fn derive_transform() {
138-
for shear_x in -10..=10 {
139-
let shear_x = (shear_x as f64) / 2.;
115+
for skew_x in -10..=10 {
116+
let skew_x = (skew_x as f64) / 2.;
140117
for angle in (0..=360).step_by(15) {
141118
let angle = (angle as f64).to_radians();
142119
for scale_x in 1..10 {
143120
let scale_x = (scale_x as f64) / 5.;
144121
for scale_y in 1..10 {
145122
let scale_y = (scale_y as f64) / 5.;
146123

147-
let shear = DVec2::new(shear_x, 0.);
124+
let skew = DVec2::new(skew_x, 0.);
148125
let scale = DVec2::new(scale_x, scale_y);
149126
let translate = DVec2::new(5666., 644.);
150127

151128
let original_transform = DAffine2::from_cols(
152-
DVec2::new(scale.x * angle.cos() - scale.y * angle.sin() * shear.y, scale.x * angle.sin() + scale.y * angle.cos() * shear.y),
153-
DVec2::new(scale.x * angle.cos() * shear.x - scale.y * angle.sin(), scale.x * angle.sin() * shear.x + scale.y * angle.cos()),
129+
DVec2::new(scale.x * angle.cos() - scale.y * angle.sin() * skew.y, scale.x * angle.sin() + scale.y * angle.cos() * skew.y),
130+
DVec2::new(scale.x * angle.cos() * skew.x - scale.y * angle.sin(), scale.x * angle.sin() * skew.x + scale.y * angle.cos()),
154131
translate,
155132
);
156133

157-
let (new_scale, new_angle, new_translation, new_shear) = compute_scale_angle_translation_shear(original_transform);
158-
let new_transform = DAffine2::from_scale_angle_translation(new_scale, new_angle, new_translation) * DAffine2::from_cols_array(&[1., new_shear.y, new_shear.x, 1., 0., 0.]);
134+
let (new_angle, new_scale, new_skew) = original_transform.decompose_rotation_scale_skew();
135+
let new_translation = original_transform.translation;
136+
let new_transform = DAffine2::from_scale_angle_translation(new_scale, new_angle, new_translation) * DAffine2::from_cols_array(&[1., 0., new_skew, 1., 0., 0.]);
159137

160138
assert!(
161139
new_transform.abs_diff_eq(original_transform, 1e-10),
162-
"original_transform {original_transform} new_transform {new_transform} / scale {scale} new_scale {new_scale} / angle {angle} new_angle {new_angle} / shear {shear} / new_shear {new_shear}",
140+
"original_transform {original_transform} new_transform {new_transform} / scale {scale} new_scale {new_scale} / angle {angle} new_angle {new_angle} / skew {skew} / new_skew {new_skew}",
163141
);
164142
}
165143
}

editor/src/messages/portfolio/document/node_graph/node_properties.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use graphene_std::raster::{
2323
};
2424
use graphene_std::table::{Table, TableRow};
2525
use graphene_std::text::{Font, TextAlign};
26-
use graphene_std::transform::{Footprint, ReferencePoint, Transform};
26+
use graphene_std::transform::{Footprint, ReferencePoint, ScaleType, Transform};
2727
use graphene_std::vector::QRCodeErrorCorrectionLevel;
2828
use graphene_std::vector::misc::BooleanOperation;
2929
use graphene_std::vector::misc::{ArcType, CentroidType, ExtrudeJoiningAlgorithm, GridType, MergeByDistanceAlgorithm, PointSpacingType, RowsOrColumns, SpiralType};
@@ -265,6 +265,7 @@ pub(crate) fn property_from_type(
265265
Some(x) if x == TypeId::of::<CentroidType>() => enum_choice::<CentroidType>().for_socket(default_info).property_row(),
266266
Some(x) if x == TypeId::of::<LuminanceCalculation>() => enum_choice::<LuminanceCalculation>().for_socket(default_info).property_row(),
267267
Some(x) if x == TypeId::of::<QRCodeErrorCorrectionLevel>() => enum_choice::<QRCodeErrorCorrectionLevel>().for_socket(default_info).property_row(),
268+
Some(x) if x == TypeId::of::<ScaleType>() => enum_choice::<ScaleType>().for_socket(default_info).property_row(),
268269
// =====
269270
// OTHER
270271
// =====
@@ -566,8 +567,8 @@ pub fn transform_widget(parameter_widgets_info: ParameterWidgetsInfo, extra_widg
566567

567568
let widgets = if let Some(&TaggedValue::DAffine2(transform)) = input.as_non_exposed_value() {
568569
let translation = transform.translation;
569-
let rotation = transform.decompose_rotation();
570-
let scale = transform.decompose_scale();
570+
let (rotation, scale, skew) = transform.decompose_rotation_scale_skew();
571+
let skew_matrix = DAffine2::from_cols_array(&[1., 0., skew, 1., 0., 0.]);
571572

572573
location_widgets.extend_from_slice(&[
573574
NumberInput::new(Some(translation.x))
@@ -608,7 +609,7 @@ pub fn transform_widget(parameter_widgets_info: ParameterWidgetsInfo, extra_widg
608609
.range_max(Some(180.))
609610
.on_update(update_value(
610611
move |r: &NumberInput| {
611-
let transform = DAffine2::from_scale_angle_translation(scale, r.value.map(|r| r.to_radians()).unwrap_or(rotation), translation);
612+
let transform = DAffine2::from_scale_angle_translation(scale, r.value.map(|r| r.to_radians()).unwrap_or(rotation), translation) * skew_matrix;
612613
TaggedValue::DAffine2(transform)
613614
},
614615
node_id,
@@ -623,7 +624,7 @@ pub fn transform_widget(parameter_widgets_info: ParameterWidgetsInfo, extra_widg
623624
.unit("x")
624625
.on_update(update_value(
625626
move |w: &NumberInput| {
626-
let transform = DAffine2::from_scale_angle_translation(DVec2::new(w.value.unwrap_or(scale.x), scale.y), rotation, translation);
627+
let transform = DAffine2::from_scale_angle_translation(DVec2::new(w.value.unwrap_or(scale.x), scale.y), rotation, translation) * skew_matrix;
627628
TaggedValue::DAffine2(transform)
628629
},
629630
node_id,
@@ -637,7 +638,7 @@ pub fn transform_widget(parameter_widgets_info: ParameterWidgetsInfo, extra_widg
637638
.unit("x")
638639
.on_update(update_value(
639640
move |h: &NumberInput| {
640-
let transform = DAffine2::from_scale_angle_translation(DVec2::new(scale.x, h.value.unwrap_or(scale.y)), rotation, translation);
641+
let transform = DAffine2::from_scale_angle_translation(DVec2::new(scale.x, h.value.unwrap_or(scale.y)), rotation, translation) * skew_matrix;
641642
TaggedValue::DAffine2(transform)
642643
},
643644
node_id,

editor/src/messages/portfolio/document/utility_types/network_interface.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4005,6 +4005,14 @@ impl NodeNetworkInterface {
40054005
}
40064006
}
40074007
(_, NodeInput::Node { node_id: upstream_node_id, .. }) => {
4008+
// If the old input wasn't exposed but the new one is (`Node` inputs are always exposed),
4009+
// the node's port count changed, so its click targets need to be recomputed
4010+
if !old_input.is_exposed()
4011+
&& let InputConnector::Node { node_id, .. } = input_connector
4012+
{
4013+
self.unload_node_click_targets(node_id, network_path);
4014+
}
4015+
40084016
// Load structure if the change is to the document network and to the first or second
40094017
if network_path.is_empty() {
40104018
if matches!(input_connector, InputConnector::Export(0)) {

editor/src/messages/portfolio/document_migration.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use graphene_std::ProtoNodeIdentifier;
1212
use graphene_std::subpath::Subpath;
1313
use graphene_std::table::Table;
1414
use graphene_std::text::{TextAlign, TypesettingConfig};
15+
use graphene_std::transform::ScaleType;
1516
use graphene_std::uuid::NodeId;
1617
use graphene_std::vector::Vector;
1718
use graphene_std::vector::style::{PaintOrder, StrokeAlign};
@@ -1848,6 +1849,17 @@ fn migrate_node(node_id: &NodeId, node: &DocumentNode, network_path: &[NodeId],
18481849
document.network_interface.set_context_features(node_id, network_path, context_features);
18491850
}
18501851

1852+
// Add the "Scale Type" parameter to the "Decompose Scale" node
1853+
if reference == DefinitionIdentifier::ProtoNode(graphene_std::transform_nodes::decompose_scale::IDENTIFIER) && inputs_count == 1 {
1854+
let mut node_template = resolve_document_node_type(&reference)?.default_node_template();
1855+
let old_inputs = document.network_interface.replace_inputs(node_id, network_path, &mut node_template)?;
1856+
1857+
document.network_interface.set_input(&InputConnector::node(*node_id, 0), old_inputs[0].clone(), network_path);
1858+
document
1859+
.network_interface
1860+
.set_input(&InputConnector::node(*node_id, 1), NodeInput::value(TaggedValue::ScaleType(ScaleType::Magnitude), false), network_path);
1861+
}
1862+
18511863
// ==================================
18521864
// PUT ALL MIGRATIONS ABOVE THIS LINE
18531865
// ==================================

editor/src/messages/tool/common_functionality/transformation_cage.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,15 @@ impl SelectedEdges {
212212
let original_from_pivot = updated - pivot; // The original vector from the point to the pivot
213213
let mut scale_factor = new_from_pivot / original_from_pivot;
214214

215-
// Constrain should always scale by the same factor in x and y
215+
// Constrain should always scale by the same factor in x and y.
216+
// When one axis of `original_from_pivot` is near zero (e.g. for a line's degenerate bounding box),
217+
// the scale factor for that axis is numerically unstable, so we copy from the more stable axis.
216218
if constrain {
217-
// When the point is on the pivot, we simply copy the other axis.
218-
if original_from_pivot.x.abs() < 1e-5 {
219+
if original_from_pivot.x.abs() < original_from_pivot.y.abs() {
219220
scale_factor.x = scale_factor.y;
220-
} else if original_from_pivot.y.abs() < 1e-5 {
221+
} else {
221222
scale_factor.y = scale_factor.x;
222223
}
223-
224-
debug_assert!((scale_factor.x - scale_factor.y).abs() < 1e-5);
225224
}
226225

227226
if !(self.left || self.right || constrain) {

0 commit comments

Comments
 (0)