graphene/
vec2.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{fmt, ops};
4
5use glib::translate::*;
6
7use crate::{ffi, Vec2};
8
9impl Vec2 {
10    /// Initializes a [`Vec2`][crate::Vec2] using the given values.
11    ///
12    /// This function can be called multiple times.
13    /// ## `x`
14    /// the X field of the vector
15    /// ## `y`
16    /// the Y field of the vector
17    ///
18    /// # Returns
19    ///
20    /// the initialized vector
21    #[doc(alias = "graphene_vec2_init")]
22    pub fn new(x: f32, y: f32) -> Self {
23        assert_initialized_main_thread!();
24        unsafe {
25            let mut vec = Self::uninitialized();
26            ffi::graphene_vec2_init(vec.to_glib_none_mut().0, x, y);
27            vec
28        }
29    }
30
31    /// Initializes `self` with the contents of the given array.
32    /// ## `src`
33    /// an array of floating point values
34    ///  with at least two elements
35    ///
36    /// # Returns
37    ///
38    /// the initialized vector
39    #[doc(alias = "graphene_vec2_init_from_float")]
40    #[doc(alias = "init_from_float")]
41    pub fn from_float(src: [f32; 2]) -> Self {
42        assert_initialized_main_thread!();
43        unsafe {
44            let mut vec = Self::uninitialized();
45            ffi::graphene_vec2_init_from_float(vec.to_glib_none_mut().0, src.as_ptr() as *const _);
46            vec
47        }
48    }
49
50    /// Stores the components of `self` into an array.
51    ///
52    /// # Returns
53    ///
54    ///
55    /// ## `dest`
56    /// return location
57    ///  for an array of floating point values with at least 2 elements
58    #[doc(alias = "graphene_vec2_to_float")]
59    pub fn to_float(&self) -> [f32; 2] {
60        unsafe {
61            let mut out = std::mem::MaybeUninit::uninit();
62            ffi::graphene_vec2_to_float(self.to_glib_none().0, out.as_mut_ptr());
63            out.assume_init()
64        }
65    }
66}
67
68impl fmt::Debug for Vec2 {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.debug_struct("Vec2")
71            .field("x", &self.x())
72            .field("y", &self.y())
73            .finish()
74    }
75}
76
77impl Default for Vec2 {
78    fn default() -> Self {
79        Self::zero()
80    }
81}
82
83// addition/subtraction
84impl ops::Add<Vec2> for Vec2 {
85    type Output = Vec2;
86
87    fn add(self, rhs: Vec2) -> Self::Output {
88        Vec2::add(&self, &rhs)
89    }
90}
91impl ops::AddAssign<Vec2> for Vec2 {
92    fn add_assign(&mut self, rhs: Vec2) {
93        *self = *self + rhs;
94    }
95}
96impl ops::Sub<Vec2> for Vec2 {
97    type Output = Vec2;
98
99    fn sub(self, rhs: Vec2) -> Self::Output {
100        Vec2::subtract(&self, &rhs)
101    }
102}
103impl ops::SubAssign<Vec2> for Vec2 {
104    fn sub_assign(&mut self, rhs: Vec2) {
105        *self = *self - rhs;
106    }
107}
108impl ops::Neg for Vec2 {
109    type Output = Vec2;
110
111    fn neg(self) -> Self::Output {
112        Vec2::negate(&self)
113    }
114}
115
116// scalar multiplication
117impl ops::Mul<f32> for Vec2 {
118    type Output = Vec2;
119
120    fn mul(self, rhs: f32) -> Self::Output {
121        Vec2::scale(&self, rhs)
122    }
123}
124impl ops::MulAssign<f32> for Vec2 {
125    fn mul_assign(&mut self, rhs: f32) {
126        *self = *self * rhs;
127    }
128}
129impl ops::Mul<Vec2> for f32 {
130    type Output = Vec2;
131
132    fn mul(self, rhs: Vec2) -> Self::Output {
133        rhs * self
134    }
135}
136
137// Component-wise multiplication/division
138impl ops::Mul<Vec2> for Vec2 {
139    type Output = Vec2;
140
141    fn mul(self, rhs: Vec2) -> Self::Output {
142        Vec2::multiply(&self, &rhs)
143    }
144}
145impl ops::MulAssign<Vec2> for Vec2 {
146    fn mul_assign(&mut self, rhs: Vec2) {
147        *self = *self * rhs;
148    }
149}
150impl ops::Div<Vec2> for Vec2 {
151    type Output = Vec2;
152
153    fn div(self, rhs: Vec2) -> Self::Output {
154        Vec2::divide(&self, &rhs)
155    }
156}
157impl ops::DivAssign<Vec2> for Vec2 {
158    fn div_assign(&mut self, rhs: Vec2) {
159        *self = *self / rhs;
160    }
161}