diff --git a/editor/src/messages/portfolio/document/node_graph/node_inspector.rs b/editor/src/messages/portfolio/document/node_graph/node_inspector.rs index bac66c0170..0039e46529 100644 --- a/editor/src/messages/portfolio/document/node_graph/node_inspector.rs +++ b/editor/src/messages/portfolio/document/node_graph/node_inspector.rs @@ -1391,7 +1391,7 @@ pub fn angle_widget(parameter_widgets_info: ParameterWidgetsInfo, number_props: _ => return widgets, }; - let number_props = bounds_in_unit(number_props, |degrees| angle.unit.from_angle(Angle::from_degrees(degrees))); + let number_props = bounds_in_unit(number_props, |degrees| angle.unit.from_degrees(degrees)); widgets.extend_from_slice(&[ Separator::new(SeparatorStyle::Unrelated).widget_instance(), number_props diff --git a/node-graph/graph-craft/src/document/value.rs b/node-graph/graph-craft/src/document/value.rs index 6fbaba9ed0..7ece3dee25 100644 --- a/node-graph/graph-craft/src/document/value.rs +++ b/node-graph/graph-craft/src/document/value.rs @@ -635,7 +635,7 @@ impl TaggedValue { /// An angle input's value in degrees, whether stored as a typed angle or as a plain number of degrees. pub fn as_angle_degrees(&self) -> Option { match self { - Self::Angle(angle) => Some(angle.to_angle().degrees()), + Self::Angle(angle) => Some(angle.to_degrees()), Self::Number(degrees) => Some(*degrees), _ => None, } @@ -648,7 +648,7 @@ impl TaggedValue { _ => AngleUnit::Degree, }; Self::Angle(TypedAngle { - value: unit.from_angle(Angle::from_degrees(degrees)), + value: unit.from_degrees(degrees), unit, }) } diff --git a/node-graph/libraries/core-types/src/units.rs b/node-graph/libraries/core-types/src/units.rs index 84db388687..92d0b3541b 100644 --- a/node-graph/libraries/core-types/src/units.rs +++ b/node-graph/libraries/core-types/src/units.rs @@ -319,6 +319,14 @@ impl AngleUnit { Self::Turn => angle.degrees() / 360., } } + + /// A number of degrees measured in this unit, which stays exact in degrees rather than round-tripping through radians. + pub fn from_degrees(self, degrees: f64) -> f64 { + match self { + Self::Degree => degrees, + _ => self.from_angle(Angle::from_degrees(degrees)), + } + } } /// A length as typed into an input, kept in its unit so a physical size stays physical when the document's resolution changes. @@ -361,6 +369,14 @@ impl TypedAngle { pub fn to_angle(self) -> Angle { self.unit.to_angle(self.value) } + + /// The angle in degrees, exactly as typed when it was typed in degrees. + pub fn to_degrees(self) -> f64 { + match self.unit { + AngleUnit::Degree => self.value, + _ => self.to_angle().degrees(), + } + } } // A plain number wired into a length or angle reads in the unit its field shows, pixels or degrees @@ -462,4 +478,12 @@ mod tests { assert_eq!(Angle::from_degrees(1.) + Angle::from_degrees(5.), Angle::from_degrees(6.)); assert_eq!(AngleUnit::Turn.to_angle(0.25), Angle::from_degrees(90.)); } + + // 15° doesn't survive the trip through radians bit for bit + #[test] + fn degrees_stay_exact_in_degrees() { + assert_eq!(TypedAngle::degrees(15.).to_degrees(), 15.); + assert_eq!(AngleUnit::Degree.from_degrees(15.), 15.); + assert!((AngleUnit::Turn.from_degrees(90.) - 0.25).abs() < 1e-12); + } }