2020-05-12 21:00:20 +00:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
|
2020-05-13 18:16:59 +00:00
|
|
|
use crate::math::*;
|
2018-12-28 22:53:15 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-05-12 21:00:20 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
2019-01-06 15:34:01 +00:00
|
|
|
pub enum Direction {
|
2018-12-28 09:39:08 +00:00
|
|
|
Horizontal,
|
|
|
|
Vertical,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Direction {
|
|
|
|
fn default() -> Direction {
|
|
|
|
Direction::Vertical
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 21:00:20 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
2019-01-14 13:54:06 +00:00
|
|
|
pub enum Align {
|
|
|
|
/// Left/Top
|
|
|
|
Min,
|
|
|
|
|
|
|
|
/// Note: requires a bounded/known available_width.
|
|
|
|
Center,
|
|
|
|
|
|
|
|
/// Right/Bottom
|
|
|
|
/// Note: requires a bounded/known available_width.
|
|
|
|
Max,
|
2020-05-10 11:07:33 +00:00
|
|
|
|
|
|
|
/// Full width/height.
|
|
|
|
/// Use this when you want
|
|
|
|
Justified,
|
2019-01-14 13:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Align {
|
|
|
|
fn default() -> Align {
|
|
|
|
Align::Min
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 11:07:33 +00:00
|
|
|
/// Give a position within the rect, specified by the aligns
|
2020-05-05 17:12:00 +00:00
|
|
|
pub fn align_rect(rect: Rect, align: (Align, Align)) -> Rect {
|
2020-04-19 09:13:24 +00:00
|
|
|
let x = match align.0 {
|
2020-05-10 11:07:33 +00:00
|
|
|
Align::Min | Align::Justified => rect.left(),
|
2020-04-22 22:17:37 +00:00
|
|
|
Align::Center => rect.left() - 0.5 * rect.width(),
|
|
|
|
Align::Max => rect.left() - rect.width(),
|
2020-04-19 09:13:24 +00:00
|
|
|
};
|
|
|
|
let y = match align.1 {
|
2020-05-10 11:07:33 +00:00
|
|
|
Align::Min | Align::Justified => rect.top(),
|
2020-04-22 22:17:37 +00:00
|
|
|
Align::Center => rect.top() - 0.5 * rect.height(),
|
|
|
|
Align::Max => rect.top() - rect.height(),
|
2020-04-19 09:13:24 +00:00
|
|
|
};
|
|
|
|
Rect::from_min_size(pos2(x, y), rect.size())
|
|
|
|
}
|