2020-04-06 21:20:53 +00:00
|
|
|
use super::{Anchors, Margins};
|
2020-06-25 17:13:00 +00:00
|
|
|
use bevy_render::render_resource::RenderResources;
|
2020-07-10 08:37:06 +00:00
|
|
|
use bevy_transform::prelude::Translation;
|
2020-06-25 17:13:00 +00:00
|
|
|
use glam::{Vec2, Vec3};
|
2020-01-13 00:51:21 +00:00
|
|
|
|
2020-03-11 08:44:46 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-05-06 20:49:07 +00:00
|
|
|
enum MarginGrowDirection {
|
2020-01-13 00:51:21 +00:00
|
|
|
Negative,
|
|
|
|
Positive,
|
|
|
|
}
|
|
|
|
|
2020-06-25 17:13:00 +00:00
|
|
|
#[derive(Debug, Clone, Default, RenderResources)]
|
2020-01-13 00:51:21 +00:00
|
|
|
pub struct Node {
|
2020-06-25 17:13:00 +00:00
|
|
|
pub size: Vec2,
|
|
|
|
#[render_resources(ignore)]
|
2020-01-13 00:51:21 +00:00
|
|
|
pub position: Vec2,
|
2020-06-25 17:13:00 +00:00
|
|
|
#[render_resources(ignore)]
|
2020-01-13 00:51:21 +00:00
|
|
|
pub anchors: Anchors,
|
2020-06-25 17:13:00 +00:00
|
|
|
#[render_resources(ignore)]
|
2020-01-13 00:51:21 +00:00
|
|
|
pub margins: Margins,
|
|
|
|
}
|
|
|
|
|
2020-06-25 17:13:00 +00:00
|
|
|
impl Node {
|
|
|
|
pub fn new(anchors: Anchors, margins: Margins) -> Self {
|
2020-01-13 00:51:21 +00:00
|
|
|
Node {
|
2020-06-25 17:13:00 +00:00
|
|
|
anchors,
|
|
|
|
margins,
|
|
|
|
..Default::default()
|
2020-01-13 00:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 17:13:00 +00:00
|
|
|
pub fn positioned(position: Vec2, anchors: Anchors, margins: Margins) -> Self {
|
2020-01-13 00:51:21 +00:00
|
|
|
Node {
|
|
|
|
position,
|
|
|
|
anchors,
|
|
|
|
margins,
|
2020-06-25 17:13:00 +00:00
|
|
|
..Default::default()
|
2020-01-13 00:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 17:13:00 +00:00
|
|
|
pub fn update(&mut self, translation: &mut Translation, z_offset: f32, parent_size: Vec2) {
|
2020-06-01 06:39:20 +00:00
|
|
|
let (quad_x, quad_width) = Self::compute_dimension_properties(
|
2020-01-13 00:51:21 +00:00
|
|
|
self.margins.left,
|
|
|
|
self.margins.right,
|
|
|
|
self.anchors.left,
|
|
|
|
self.anchors.right,
|
2020-05-06 20:49:07 +00:00
|
|
|
parent_size.x(),
|
2020-01-13 00:51:21 +00:00
|
|
|
);
|
2020-06-01 06:39:20 +00:00
|
|
|
let (quad_y, quad_height) = Self::compute_dimension_properties(
|
2020-01-13 00:51:21 +00:00
|
|
|
self.margins.bottom,
|
|
|
|
self.margins.top,
|
|
|
|
self.anchors.bottom,
|
|
|
|
self.anchors.top,
|
2020-05-06 20:49:07 +00:00
|
|
|
parent_size.y(),
|
2020-01-13 00:51:21 +00:00
|
|
|
);
|
|
|
|
|
2020-06-25 17:13:00 +00:00
|
|
|
self.size = Vec2::new(quad_width, quad_height);
|
2020-07-10 08:37:06 +00:00
|
|
|
translation.0 = self.position.extend(0.0) + Vec3::new(quad_x, quad_y, z_offset)
|
|
|
|
- (parent_size / 2.0).extend(0.0);
|
2020-01-13 00:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_dimension_properties(
|
|
|
|
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 {
|
2020-05-06 20:49:07 +00:00
|
|
|
MarginGrowDirection::Positive
|
2020-01-13 00:51:21 +00:00
|
|
|
} else {
|
2020-05-06 20:49:07 +00:00
|
|
|
MarginGrowDirection::Negative
|
2020-01-13 00:51:21 +00:00
|
|
|
};
|
2020-05-06 20:49:07 +00:00
|
|
|
let p1_grow_direction = if anchor_p1 <= 0.5 {
|
|
|
|
MarginGrowDirection::Positive
|
2020-01-13 00:51:21 +00:00
|
|
|
} else {
|
2020-05-06 20:49:07 +00:00
|
|
|
MarginGrowDirection::Negative
|
2020-01-13 00:51:21 +00:00
|
|
|
};
|
|
|
|
|
2020-06-25 17:13:00 +00:00
|
|
|
let p0 = Self::compute_anchored_position(margin0, anchor_p0, p0_grow_direction);
|
|
|
|
let p1 = Self::compute_anchored_position(margin1, anchor_p1, p1_grow_direction);
|
2020-01-13 00:51:21 +00:00
|
|
|
|
|
|
|
let final_width = p1 - p0;
|
2020-05-06 20:49:07 +00:00
|
|
|
let p = (p0 + p1) / 2.0;
|
|
|
|
(p, final_width.abs())
|
2020-01-13 00:51:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 17:13:00 +00:00
|
|
|
fn compute_anchored_position(
|
2020-01-13 00:51:21 +00:00
|
|
|
margin: f32,
|
|
|
|
anchor_position: f32,
|
2020-05-06 20:49:07 +00:00
|
|
|
grow_direction: MarginGrowDirection,
|
2020-01-13 00:51:21 +00:00
|
|
|
) -> f32 {
|
|
|
|
match grow_direction {
|
2020-06-25 17:13:00 +00:00
|
|
|
MarginGrowDirection::Negative => anchor_position - margin,
|
|
|
|
MarginGrowDirection::Positive => anchor_position + margin,
|
2020-01-13 00:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|