emath/
vec2b.rs

1use crate::Vec2;
2
3/// Two bools, one for each axis (X and Y).
4#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
5#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
6pub struct Vec2b {
7    pub x: bool,
8    pub y: bool,
9}
10
11impl Vec2b {
12    pub const FALSE: Self = Self { x: false, y: false };
13    pub const TRUE: Self = Self { x: true, y: true };
14
15    #[inline]
16    pub fn new(x: bool, y: bool) -> Self {
17        Self { x, y }
18    }
19
20    #[inline]
21    pub fn any(&self) -> bool {
22        self.x || self.y
23    }
24
25    /// Are both `x` and `y` true?
26    #[inline]
27    pub fn all(&self) -> bool {
28        self.x && self.y
29    }
30
31    #[inline]
32    pub fn and(&self, other: impl Into<Self>) -> Self {
33        let other = other.into();
34        Self {
35            x: self.x && other.x,
36            y: self.y && other.y,
37        }
38    }
39
40    #[inline]
41    pub fn or(&self, other: impl Into<Self>) -> Self {
42        let other = other.into();
43        Self {
44            x: self.x || other.x,
45            y: self.y || other.y,
46        }
47    }
48
49    /// Convert to a float `Vec2` where the components are 1.0 for `true` and 0.0 for `false`.
50    #[inline]
51    pub fn to_vec2(self) -> Vec2 {
52        Vec2::new(self.x.into(), self.y.into())
53    }
54}
55
56impl From<bool> for Vec2b {
57    #[inline]
58    fn from(val: bool) -> Self {
59        Self { x: val, y: val }
60    }
61}
62
63impl From<[bool; 2]> for Vec2b {
64    #[inline]
65    fn from([x, y]: [bool; 2]) -> Self {
66        Self { x, y }
67    }
68}
69
70impl std::ops::Index<usize> for Vec2b {
71    type Output = bool;
72
73    #[inline(always)]
74    fn index(&self, index: usize) -> &bool {
75        match index {
76            0 => &self.x,
77            1 => &self.y,
78            _ => panic!("Vec2b index out of bounds: {index}"),
79        }
80    }
81}
82
83impl std::ops::IndexMut<usize> for Vec2b {
84    #[inline(always)]
85    fn index_mut(&mut self, index: usize) -> &mut bool {
86        match index {
87            0 => &mut self.x,
88            1 => &mut self.y,
89            _ => panic!("Vec2b index out of bounds: {index}"),
90        }
91    }
92}
93
94impl std::ops::Not for Vec2b {
95    type Output = Self;
96
97    #[inline]
98    fn not(self) -> Self::Output {
99        Self {
100            x: !self.x,
101            y: !self.y,
102        }
103    }
104}