graphene/
point.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::fmt;
4
5use glib::translate::*;
6
7use crate::{ffi, Point, Vec2};
8
9impl Point {
10    /// Initializes `self` to the given `x` and `y` coordinates.
11    ///
12    /// It's safe to call this function multiple times.
13    /// ## `x`
14    /// the X coordinate
15    /// ## `y`
16    /// the Y coordinate
17    ///
18    /// # Returns
19    ///
20    /// the initialized point
21    #[doc(alias = "graphene_point_init")]
22    pub fn new(x: f32, y: f32) -> Self {
23        assert_initialized_main_thread!();
24        unsafe {
25            let mut p = Self::uninitialized();
26            ffi::graphene_point_init(p.to_glib_none_mut().0, x, y);
27            p
28        }
29    }
30
31    /// Initializes `self` with the coordinates inside the given [`Vec2`][crate::Vec2].
32    /// ## `src`
33    /// a [`Vec2`][crate::Vec2]
34    ///
35    /// # Returns
36    ///
37    /// the initialized point
38    #[doc(alias = "graphene_point_init_from_vec2")]
39    #[doc(alias = "init_from_vec2")]
40    pub fn from_vec2(src: &Vec2) -> Point {
41        assert_initialized_main_thread!();
42        unsafe {
43            let mut p = Self::uninitialized();
44            ffi::graphene_point_init_from_vec2(p.to_glib_none_mut().0, src.to_glib_none().0);
45            p
46        }
47    }
48
49    #[inline]
50    pub fn x(&self) -> f32 {
51        self.inner.x
52    }
53
54    #[inline]
55    pub fn set_x(&mut self, x: f32) {
56        self.inner.x = x;
57    }
58
59    #[inline]
60    pub fn y(&self) -> f32 {
61        self.inner.y
62    }
63
64    #[inline]
65    pub fn set_y(&mut self, y: f32) {
66        self.inner.y = y;
67    }
68}
69
70impl fmt::Debug for Point {
71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72        f.debug_struct("Point")
73            .field("x", &self.x())
74            .field("y", &self.y())
75            .finish()
76    }
77}
78
79impl Default for Point {
80    fn default() -> Self {
81        Self::zero()
82    }
83}