trackball/
scale.rs

1use nalgebra::{RealField, convert};
2use simba::scalar::SubsetOf;
3
4/// Scale induced by relative input.
5///
6/// Implements [`Default`] and can be created with `Scale::default()`.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct Scale<N: Copy + RealField> {
9	/// Denominator. Default is scroll unit of `120.0`.
10	den: N,
11}
12
13impl<N: Copy + RealField> Default for Scale<N> {
14	fn default() -> Self {
15		Self {
16			den: convert(120.0),
17		}
18	}
19}
20
21impl<N: Copy + RealField> Scale<N> {
22	/// Computes scale ratio from relative value.
23	#[must_use]
24	pub fn compute(&self, num: N) -> N {
25		N::one() - num / self.den
26	}
27	/// Denominator. Default is scroll unit of `120.0`.
28	#[must_use]
29	pub const fn denominator(&self) -> N {
30		self.den
31	}
32	/// Sets denominator. Default is scroll unit of `120.0`.
33	pub const fn set_denominator(&mut self, den: N) {
34		self.den = den;
35	}
36	/// Casts components to another type, e.g., between [`f32`] and [`f64`].
37	#[must_use]
38	pub fn cast<M: Copy + RealField>(self) -> Scale<M>
39	where
40		N: SubsetOf<M>,
41	{
42		Scale {
43			den: self.den.to_superset(),
44		}
45	}
46}