2016-07-03 02:37:38 +00:00
|
|
|
use std::cmp::min;
|
2016-07-12 02:24:00 +00:00
|
|
|
use vec::Vec2;
|
2016-07-02 02:19:43 +00:00
|
|
|
|
|
|
|
/// Location of the view on screen
|
2016-07-03 02:46:23 +00:00
|
|
|
#[derive(PartialEq,Debug,Clone)]
|
2016-07-02 02:19:43 +00:00
|
|
|
pub struct Position {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Horizontal offset
|
2016-07-02 02:19:43 +00:00
|
|
|
pub x: Offset,
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Vertical offset
|
2016-07-02 02:19:43 +00:00
|
|
|
pub y: Offset,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Position {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Creates a new position with the given offsets.
|
2016-07-02 02:19:43 +00:00
|
|
|
pub fn new(x: Offset, y: Offset) -> Self {
|
2016-07-02 03:23:58 +00:00
|
|
|
Position { x: x, y: y }
|
2016-07-02 02:19:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Returns a position centered on both axis.
|
2016-07-02 02:19:43 +00:00
|
|
|
pub fn center() -> Self {
|
|
|
|
Position::new(Offset::Center, Offset::Center)
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Returns a position absolute on both axis.
|
2016-07-12 02:24:00 +00:00
|
|
|
pub fn absolute<T: Into<Vec2>>(offset: T) -> Self {
|
|
|
|
let offset = offset.into();
|
2016-07-02 02:19:43 +00:00
|
|
|
Position::new(Offset::Absolute(offset.x), Offset::Absolute(offset.y))
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Returns a position relative to the parent on both axis.
|
2016-07-12 02:24:00 +00:00
|
|
|
pub fn parent<T: Into<Vec2>>(offset: T) -> Self {
|
|
|
|
let offset = offset.into();
|
2016-07-02 02:19:43 +00:00
|
|
|
Position::new(Offset::Parent(offset.x), Offset::Parent(offset.y))
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Computes the offset required to draw a view.
|
|
|
|
///
|
|
|
|
/// When drawing a view with `size` in a container with `available`,
|
|
|
|
/// and a parent with the absolute coordinates `parent`, drawing the
|
|
|
|
/// child with its top-left corner at the returned coordinates will
|
|
|
|
/// position him appropriately.
|
2016-07-10 02:05:51 +00:00
|
|
|
pub fn compute_offset(&self, size: Vec2, available: Vec2, parent: Vec2)
|
|
|
|
-> Vec2 {
|
2016-07-02 02:19:43 +00:00
|
|
|
Vec2::new(self.x.compute_offset(size.x, available.x, parent.x),
|
|
|
|
self.y.compute_offset(size.y, available.y, parent.y))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Single-dimensional offset policy.
|
2016-07-03 02:46:23 +00:00
|
|
|
#[derive(PartialEq,Debug,Clone)]
|
2016-07-02 02:19:43 +00:00
|
|
|
pub enum Offset {
|
|
|
|
/// In the center of the screen
|
|
|
|
Center,
|
|
|
|
/// Place top-left corner at the given absolute coordinates
|
|
|
|
Absolute(usize),
|
|
|
|
|
2016-07-10 02:05:51 +00:00
|
|
|
/// Offset from the previous layer's top-left corner.
|
2016-07-02 02:19:43 +00:00
|
|
|
///
|
|
|
|
/// If this is the first layer, behaves like `Absolute`.
|
|
|
|
Parent(usize), // TODO: use a signed vec for negative offset?
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Offset {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Computes a single-dimension offset requred to draw a view.
|
2016-07-10 02:05:51 +00:00
|
|
|
pub fn compute_offset(&self, size: usize, available: usize, parent: usize)
|
|
|
|
-> usize {
|
2016-07-02 02:19:43 +00:00
|
|
|
match *self {
|
|
|
|
Offset::Center => (available - size) / 2,
|
2016-07-03 02:37:38 +00:00
|
|
|
Offset::Absolute(offset) => min(offset, available - size),
|
|
|
|
Offset::Parent(offset) => min(parent + offset, available - size),
|
2016-07-02 02:19:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|