2020-04-06 21:20:53 +00:00
|
|
|
use super::{Anchors, Margins};
|
|
|
|
use bevy_render::Color;
|
|
|
|
use glam::{self, Vec2};
|
2020-05-03 00:56:30 +00:00
|
|
|
use crate::Rect;
|
2020-01-13 00:51:21 +00:00
|
|
|
|
2020-03-11 08:44:46 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-01-13 00:51:21 +00:00
|
|
|
enum GrowDirection {
|
|
|
|
Negative,
|
|
|
|
Positive,
|
|
|
|
}
|
|
|
|
|
2020-03-11 08:44:46 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-01-13 00:51:21 +00:00
|
|
|
pub struct Node {
|
|
|
|
pub position: Vec2,
|
2020-01-13 06:18:17 +00:00
|
|
|
pub size: Vec2,
|
2020-01-13 00:51:21 +00:00
|
|
|
pub anchors: Anchors,
|
|
|
|
pub margins: Margins,
|
2020-03-10 06:43:40 +00:00
|
|
|
pub color: Color,
|
2020-01-13 00:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Node {
|
|
|
|
fn default() -> Self {
|
|
|
|
Node {
|
|
|
|
position: Vec2::default(),
|
2020-01-13 06:18:17 +00:00
|
|
|
size: Vec2::default(),
|
2020-01-13 00:51:21 +00:00
|
|
|
anchors: Anchors::default(),
|
|
|
|
margins: Margins::default(),
|
2020-03-10 06:43:40 +00:00
|
|
|
color: Color::rgb(0.0, 0.0, 0.0),
|
2020-01-13 00:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Node {
|
2020-03-10 06:43:40 +00:00
|
|
|
pub fn new(position: Vec2, anchors: Anchors, margins: Margins, color: Color) -> Self {
|
2020-01-13 00:51:21 +00:00
|
|
|
Node {
|
|
|
|
position,
|
2020-01-13 06:18:17 +00:00
|
|
|
size: Vec2::default(),
|
2020-01-13 00:51:21 +00:00
|
|
|
anchors,
|
|
|
|
margins,
|
|
|
|
color,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 00:56:30 +00:00
|
|
|
pub fn update(&mut self, rect: &mut Rect, parent_dimensions: Vec2, parent_position: Vec2) {
|
2020-01-13 00:51:21 +00:00
|
|
|
let (rect_x, rect_width) = Self::compute_dimension_properties(
|
|
|
|
self.position.x(),
|
|
|
|
self.margins.left,
|
|
|
|
self.margins.right,
|
|
|
|
self.anchors.left,
|
|
|
|
self.anchors.right,
|
|
|
|
parent_dimensions.x(),
|
|
|
|
);
|
|
|
|
let (rect_y, rect_height) = Self::compute_dimension_properties(
|
|
|
|
self.position.y(),
|
|
|
|
self.margins.bottom,
|
|
|
|
self.margins.top,
|
|
|
|
self.anchors.bottom,
|
|
|
|
self.anchors.top,
|
|
|
|
parent_dimensions.y(),
|
|
|
|
);
|
|
|
|
|
2020-04-06 21:20:53 +00:00
|
|
|
self.size = glam::vec2(rect_width, rect_height);
|
2020-05-03 00:56:30 +00:00
|
|
|
rect.position = glam::vec2(rect_x, rect_y) + parent_position;
|
2020-01-13 00:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_dimension_properties(
|
|
|
|
offset: f32,
|
|
|
|
margin0: f32,
|
|
|
|
margin1: f32,
|
|
|
|
anchor0: f32,
|
|
|
|
anchor1: f32,
|
|
|
|
length: f32,
|
|
|
|
) -> (f32, f32) {
|
|
|
|
let anchor_p0 = anchor0 * length;
|
|
|
|
let anchor_p1 = anchor1 * length;
|
|
|
|
|
|
|
|
let p0_grow_direction = if anchor_p0 <= 0.5 {
|
|
|
|
GrowDirection::Positive
|
|
|
|
} else {
|
|
|
|
GrowDirection::Negative
|
|
|
|
};
|
|
|
|
let p1_grow_direction = if anchor_p1 < 0.5 {
|
|
|
|
GrowDirection::Positive
|
|
|
|
} else {
|
|
|
|
GrowDirection::Negative
|
|
|
|
};
|
|
|
|
|
|
|
|
let p0 = Self::compute_rect_position(offset, margin0, anchor_p0, p0_grow_direction);
|
|
|
|
let p1 = Self::compute_rect_position(offset, margin1, anchor_p1, p1_grow_direction);
|
|
|
|
|
|
|
|
let final_width = p1 - p0;
|
2020-01-13 06:18:17 +00:00
|
|
|
let mut p = (p0 + p1) / 2.0;
|
|
|
|
|
|
|
|
// move position to "origin" in bottom left hand corner
|
|
|
|
p = p - final_width / 2.0;
|
2020-01-13 00:51:21 +00:00
|
|
|
|
|
|
|
(p, final_width)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_rect_position(
|
|
|
|
position: f32,
|
|
|
|
margin: f32,
|
|
|
|
anchor_position: f32,
|
|
|
|
grow_direction: GrowDirection,
|
|
|
|
) -> f32 {
|
|
|
|
match grow_direction {
|
|
|
|
GrowDirection::Negative => position + anchor_position - margin,
|
|
|
|
GrowDirection::Positive => position + anchor_position + margin,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|