use vec::Vec2;
#[derive(Clone,Copy,Debug,PartialEq)]
pub enum Orientation {
Horizontal,
Vertical,
}
impl Orientation {
pub fn get(&self, v: &Vec2) -> usize {
*v.get(*self)
}
pub fn swap(&self) -> Self {
match *self {
Orientation::Horizontal => Orientation::Vertical,
Orientation::Vertical => Orientation::Horizontal,
}
}
pub fn get_ref<'a, 'b>(&'a self, v: &'b mut Vec2) -> &'b mut usize {
match *self {
Orientation::Horizontal => &mut v.x,
Orientation::Vertical => &mut v.y,
}
}
pub fn stack<'a, T: Iterator<Item = &'a Vec2>>(&self, iter: T) -> Vec2 {
match *self {
Orientation::Horizontal => {
iter.fold(Vec2::zero(), |a, b| a.stack_horizontal(b))
}
Orientation::Vertical => {
iter.fold(Vec2::zero(), |a, b| a.stack_vertical(b))
}
}
}
pub fn make_vec(&self, main_axis: usize, second_axis: usize) -> Vec2 {
let mut result = Vec2::zero();
*self.get_ref(&mut result) = main_axis;
*self.swap().get_ref(&mut result) = second_axis;
result
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Direction {
Abs(Absolute),
Rel(Relative),
}
impl Direction {
pub fn relative(self, orientation: Orientation) -> Option<Relative> {
match self {
Direction::Abs(abs) => abs.relative(orientation),
Direction::Rel(rel) => Some(rel),
}
}
pub fn absolute(self, orientation: Orientation) -> Absolute {
match self {
Direction::Abs(abs) => abs,
Direction::Rel(rel) => rel.absolute(orientation),
}
}
pub fn back() -> Self {
Direction::Rel(Relative::Back)
}
pub fn front() -> Self {
Direction::Rel(Relative::Front)
}
pub fn left() -> Self {
Direction::Abs(Absolute::Left)
}
pub fn right() -> Self {
Direction::Abs(Absolute::Right)
}
pub fn up() -> Self {
Direction::Abs(Absolute::Up)
}
pub fn down() -> Self {
Direction::Abs(Absolute::Down)
}
pub fn none() -> Self {
Direction::Abs(Absolute::None)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Relative {
Front,
Back,
}
impl Relative {
pub fn absolute(self, orientation: Orientation) -> Absolute {
match (orientation, self) {
(Orientation::Horizontal, Relative::Front) => Absolute::Left,
(Orientation::Horizontal, Relative::Back) => Absolute::Right,
(Orientation::Vertical, Relative::Front) => Absolute::Up,
(Orientation::Vertical, Relative::Back) => Absolute::Down,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Absolute {
Left,
Up,
Right,
Down,
None,
}
impl Absolute {
pub fn relative(self, orientation: Orientation) -> Option<Relative> {
match (orientation, self) {
(Orientation::Horizontal, Absolute::Left) |
(Orientation::Vertical, Absolute::Up) => Some(Relative::Front),
(Orientation::Horizontal, Absolute::Right) |
(Orientation::Vertical, Absolute::Down) => Some(Relative::Back),
_ => None,
}
}
}