2015-06-08 22:38:10 +00:00
|
|
|
//! Define an Orientation and associated methods.
|
2015-06-08 19:23:36 +00:00
|
|
|
use vec::Vec2;
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Describes a vertical or horizontal orientation for a view.
|
2015-06-08 22:11:44 +00:00
|
|
|
#[derive(Clone,Copy,PartialEq)]
|
2015-06-08 19:23:36 +00:00
|
|
|
pub enum Orientation {
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Horizontal orientation
|
2015-06-08 19:23:36 +00:00
|
|
|
Horizontal,
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Vertical orientation
|
2015-06-08 19:23:36 +00:00
|
|
|
Vertical,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Orientation {
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Returns the component of the given vector corresponding to this orientation.
|
|
|
|
/// (Horizontal will return the x value, and Vertical will return the y value.)
|
2015-06-08 19:23:36 +00:00
|
|
|
pub fn get(&self, v: &Vec2) -> usize {
|
|
|
|
match *self {
|
|
|
|
Orientation::Horizontal => v.x,
|
|
|
|
Orientation::Vertical => v.y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Returns the other orientation.
|
2015-06-08 19:23:36 +00:00
|
|
|
pub fn swap(&self) -> Self {
|
|
|
|
match *self {
|
|
|
|
Orientation::Horizontal => Orientation::Vertical,
|
|
|
|
Orientation::Vertical => Orientation::Horizontal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Returns a mutable reference to the component of the given vector
|
|
|
|
/// corresponding to this orientation.
|
2015-06-08 19:23:36 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:10 +00:00
|
|
|
/// Takes an iterator on sizes, and stack them in the current orientation,
|
|
|
|
/// returning the size of the required bounding box.
|
|
|
|
///
|
|
|
|
/// For an horizontal view, returns (Sum(x), Max(y)).
|
|
|
|
/// For a vertical view, returns (Max(x),Sum(y)).
|
2015-06-08 19:23:36 +00:00
|
|
|
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)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|