2016-07-03 02:37:38 +00:00
|
|
|
use std::cmp::min;
|
2016-07-02 03:23:58 +00:00
|
|
|
use vec::{ToVec2, 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 {
|
|
|
|
pub x: Offset,
|
|
|
|
pub y: Offset,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Position {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn center() -> Self {
|
|
|
|
Position::new(Offset::Center, Offset::Center)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn absolute<T: ToVec2>(offset: T) -> Self {
|
|
|
|
let offset = offset.to_vec2();
|
|
|
|
Position::new(Offset::Absolute(offset.x), Offset::Absolute(offset.y))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parent<T: ToVec2>(offset: T) -> Self {
|
|
|
|
let offset = offset.to_vec2();
|
|
|
|
Position::new(Offset::Parent(offset.x), Offset::Parent(offset.y))
|
|
|
|
}
|
|
|
|
|
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-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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|