ui is back!
This commit is contained in:
		
							parent
							
								
									05dbf31fd1
								
							
						
					
					
						commit
						d50313c285
					
				@ -9,6 +9,7 @@ pub struct ActiveCamera;
 | 
			
		||||
#[derive(Default)]
 | 
			
		||||
pub struct ActiveCamera2d;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct OrthographicCamera {
 | 
			
		||||
    pub left: f32,
 | 
			
		||||
    pub right: f32,
 | 
			
		||||
@ -45,6 +46,7 @@ impl Default for OrthographicCamera {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct PerspectiveCamera {
 | 
			
		||||
    pub fov: f32,
 | 
			
		||||
    pub aspect_ratio: f32,
 | 
			
		||||
@ -70,6 +72,7 @@ impl Default for PerspectiveCamera {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub enum CameraType {
 | 
			
		||||
    Perspective(PerspectiveCamera),
 | 
			
		||||
    Orthographic(OrthographicCamera),
 | 
			
		||||
@ -91,7 +94,7 @@ impl Default for CameraType {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Default)]
 | 
			
		||||
#[derive(Default, Debug)]
 | 
			
		||||
pub struct Camera {
 | 
			
		||||
    pub view_matrix: Mat4,
 | 
			
		||||
    pub camera_type: CameraType,
 | 
			
		||||
 | 
			
		||||
@ -80,6 +80,7 @@ impl DrawTarget for AssignedMeshesDrawTarget {
 | 
			
		||||
                    pipeline_descriptor,
 | 
			
		||||
                    &renderable.render_resource_assignments,
 | 
			
		||||
                );
 | 
			
		||||
 | 
			
		||||
                render_pass.draw_indexed(0..current_mesh_index_len, 0, 0..1);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -8,7 +8,6 @@ use crate::{
 | 
			
		||||
    renderer::{GlobalRenderResourceContext, RenderContext},
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
use bevy_transform::components::LocalToWorld;
 | 
			
		||||
use legion::prelude::*;
 | 
			
		||||
use zerocopy::AsBytes;
 | 
			
		||||
 | 
			
		||||
@ -41,7 +40,7 @@ impl SystemNode for Camera2dNode {
 | 
			
		||||
            // TODO: this write on RenderResourceAssignments will prevent this system from running in parallel with other systems that do the same
 | 
			
		||||
            .write_resource::<RenderResourceAssignments>()
 | 
			
		||||
            .read_resource::<Events<WindowResized>>()
 | 
			
		||||
            .with_query(<(Read<Camera>, Read<LocalToWorld>, Read<ActiveCamera2d>)>::query())
 | 
			
		||||
            .with_query(<(Read<Camera>, Read<ActiveCamera2d>)>::query())
 | 
			
		||||
            .build(
 | 
			
		||||
                move |_,
 | 
			
		||||
                      world,
 | 
			
		||||
@ -66,9 +65,9 @@ impl SystemNode for Camera2dNode {
 | 
			
		||||
                        .find_latest(&window_resized_events, |event| event.is_primary);
 | 
			
		||||
                    if let Some(_) = primary_window_resized_event {
 | 
			
		||||
                        let matrix_size = std::mem::size_of::<[[f32; 4]; 4]>();
 | 
			
		||||
                        for (camera, local_to_world, _) in query.iter(world) {
 | 
			
		||||
                        for (camera, _) in query.iter(world) {
 | 
			
		||||
                            let camera_matrix: [[f32; 4]; 4] =
 | 
			
		||||
                                (camera.view_matrix * local_to_world.0).to_cols_array_2d();
 | 
			
		||||
                                camera.view_matrix.to_cols_array_2d();
 | 
			
		||||
 | 
			
		||||
                            if let Some(old_tmp_buffer) = tmp_buffer {
 | 
			
		||||
                                render_resources.remove_buffer(old_tmp_buffer);
 | 
			
		||||
 | 
			
		||||
@ -10,7 +10,7 @@ pub struct UiEntity {
 | 
			
		||||
    pub node: Node,
 | 
			
		||||
    pub rect: Rect,
 | 
			
		||||
    pub mesh: Handle<Mesh>, // TODO: maybe abstract this out
 | 
			
		||||
    // pub renderable: Renderable,
 | 
			
		||||
    pub renderable: Renderable,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Default for UiEntity {
 | 
			
		||||
@ -19,12 +19,12 @@ impl Default for UiEntity {
 | 
			
		||||
            node: Default::default(),
 | 
			
		||||
            rect: Default::default(),
 | 
			
		||||
            mesh: QUAD_HANDLE,
 | 
			
		||||
            // renderable: Renderable {
 | 
			
		||||
            //     pipelines: vec![
 | 
			
		||||
            //         UI_PIPELINE_HANDLE
 | 
			
		||||
            //     ],
 | 
			
		||||
            //     ..Default::default()
 | 
			
		||||
            // }
 | 
			
		||||
            renderable: Renderable {
 | 
			
		||||
                pipelines: vec![
 | 
			
		||||
                    UI_PIPELINE_HANDLE
 | 
			
		||||
                ],
 | 
			
		||||
                ..Default::default()
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,7 +1,6 @@
 | 
			
		||||
use super::{Anchors, Margins};
 | 
			
		||||
use bevy_render::Color;
 | 
			
		||||
use glam::{self, Vec2};
 | 
			
		||||
use crate::Rect;
 | 
			
		||||
use glam::Vec2;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Clone)]
 | 
			
		||||
enum GrowDirection {
 | 
			
		||||
@ -12,36 +11,30 @@ enum GrowDirection {
 | 
			
		||||
#[derive(Debug, Clone)]
 | 
			
		||||
pub struct Node {
 | 
			
		||||
    pub position: Vec2,
 | 
			
		||||
    pub size: Vec2,
 | 
			
		||||
    pub anchors: Anchors,
 | 
			
		||||
    pub margins: Margins,
 | 
			
		||||
    pub color: Color,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Default for Node {
 | 
			
		||||
    fn default() -> Self {
 | 
			
		||||
        Node {
 | 
			
		||||
            position: Vec2::default(),
 | 
			
		||||
            size: Vec2::default(),
 | 
			
		||||
            anchors: Anchors::default(),
 | 
			
		||||
            margins: Margins::default(),
 | 
			
		||||
            color: Color::rgb(0.0, 0.0, 0.0),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Node {
 | 
			
		||||
    pub fn new(position: Vec2, anchors: Anchors, margins: Margins, color: Color) -> Self {
 | 
			
		||||
    pub fn new(position: Vec2, anchors: Anchors, margins: Margins) -> Self {
 | 
			
		||||
        Node {
 | 
			
		||||
            position,
 | 
			
		||||
            size: Vec2::default(),
 | 
			
		||||
            anchors,
 | 
			
		||||
            margins,
 | 
			
		||||
            color,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn update(&mut self, rect: &mut Rect, parent_dimensions: Vec2, parent_position: Vec2) {
 | 
			
		||||
    pub fn update(&mut self, rect: &mut Rect, parent_dimensions: Vec2, parent_position: Vec2, z_index: f32) {
 | 
			
		||||
        let (rect_x, rect_width) = Self::compute_dimension_properties(
 | 
			
		||||
            self.position.x(),
 | 
			
		||||
            self.margins.left,
 | 
			
		||||
@ -59,8 +52,9 @@ impl Node {
 | 
			
		||||
            parent_dimensions.y(),
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        self.size = glam::vec2(rect_width, rect_height);
 | 
			
		||||
        rect.position = glam::vec2(rect_x, rect_y) + parent_position;
 | 
			
		||||
        rect.size = Vec2::new(rect_width, rect_height);
 | 
			
		||||
        rect.position = Vec2::new(rect_x, rect_y) + parent_position;
 | 
			
		||||
        rect.z_index = z_index;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn compute_dimension_properties(
 | 
			
		||||
 | 
			
		||||
@ -25,8 +25,8 @@ pub fn build_ui_pipeline(shaders: &mut AssetStorage<Shader>) -> PipelineDescript
 | 
			
		||||
        }),
 | 
			
		||||
        depth_stencil_state: Some(DepthStencilStateDescriptor {
 | 
			
		||||
            format: TextureFormat::Depth32Float,
 | 
			
		||||
            depth_write_enabled: true,
 | 
			
		||||
            depth_compare: CompareFunction::Less,
 | 
			
		||||
            depth_write_enabled: false,
 | 
			
		||||
            depth_compare: CompareFunction::Always,
 | 
			
		||||
            stencil_front: StencilStateFaceDescriptor::IGNORE,
 | 
			
		||||
            stencil_back: StencilStateFaceDescriptor::IGNORE,
 | 
			
		||||
            stencil_read_mask: 0,
 | 
			
		||||
 | 
			
		||||
@ -9,7 +9,7 @@ use legion::{prelude::*, systems::SubWorld};
 | 
			
		||||
pub fn ui_update_system() -> Box<dyn Schedulable> {
 | 
			
		||||
    SystemBuilder::new("ui_update")
 | 
			
		||||
        .read_resource::<Windows>()
 | 
			
		||||
        .with_query(<(Write<Node>,)>::query().filter(!component::<Parent>()))
 | 
			
		||||
        .with_query(<Read<Node>>::query().filter(!component::<Parent>()))
 | 
			
		||||
        .write_component::<Node>()
 | 
			
		||||
        .write_component::<Rect>()
 | 
			
		||||
        .read_component::<Children>()
 | 
			
		||||
@ -17,11 +17,15 @@ pub fn ui_update_system() -> Box<dyn Schedulable> {
 | 
			
		||||
            if let Some(window) = windows.get_primary() {
 | 
			
		||||
                let parent_size = glam::vec2(window.width as f32, window.height as f32);
 | 
			
		||||
                let parent_position = glam::vec2(0.0, 0.0);
 | 
			
		||||
                for (entity, _) in node_query.iter_entities_mut(world) {
 | 
			
		||||
                for entity in node_query
 | 
			
		||||
                    .iter_entities(world)
 | 
			
		||||
                    .map(|(e, _)| e)
 | 
			
		||||
                    .collect::<Vec<Entity>>()
 | 
			
		||||
                {
 | 
			
		||||
                    run_on_hierarchy_subworld_mut(
 | 
			
		||||
                        world,
 | 
			
		||||
                        entity,
 | 
			
		||||
                        (parent_size, parent_position),
 | 
			
		||||
                        (parent_size, parent_position, 0.9999),
 | 
			
		||||
                        &mut update_node_entity,
 | 
			
		||||
                    );
 | 
			
		||||
                }
 | 
			
		||||
@ -32,15 +36,15 @@ pub fn ui_update_system() -> Box<dyn Schedulable> {
 | 
			
		||||
fn update_node_entity(
 | 
			
		||||
    world: &mut SubWorld,
 | 
			
		||||
    entity: Entity,
 | 
			
		||||
    parent_properties: (Vec2, Vec2),
 | 
			
		||||
) -> Option<(Vec2, Vec2)> {
 | 
			
		||||
    let (parent_size, parent_position) = parent_properties;
 | 
			
		||||
    parent_properties: (Vec2, Vec2, f32),
 | 
			
		||||
) -> Option<(Vec2, Vec2, f32)> {
 | 
			
		||||
    let (parent_size, parent_position, z_index) = parent_properties;
 | 
			
		||||
    // TODO: Somehow remove this unsafe
 | 
			
		||||
    unsafe {
 | 
			
		||||
        if let Some(mut node) = world.get_component_mut_unchecked::<Node>(entity) {
 | 
			
		||||
            if let Some(mut rect) = world.get_component_mut_unchecked::<Rect>(entity) {
 | 
			
		||||
                node.update(&mut rect, parent_size, parent_position);
 | 
			
		||||
                return Some((node.size, rect.position));
 | 
			
		||||
                node.update(&mut rect, parent_size, parent_position, z_index);
 | 
			
		||||
                return Some((rect.size, rect.position, z_index - 0.0001));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
use bevy::prelude::*;
 | 
			
		||||
use bevy_ui::Rect;
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    App::build()
 | 
			
		||||
@ -51,8 +52,11 @@ fn setup(world: &mut World, resources: &mut Resources) {
 | 
			
		||||
                math::vec2(0.0, 0.0),
 | 
			
		||||
                Anchors::new(0.0, 0.0, 0.0, 1.0),
 | 
			
		||||
                Margins::new(10.0, 200.0, 10.0, 10.0),
 | 
			
		||||
                Color::rgb(0.1, 0.1, 0.1),
 | 
			
		||||
            ),
 | 
			
		||||
            rect: Rect {
 | 
			
		||||
                color: Color::rgb(0.02, 0.02, 0.02),
 | 
			
		||||
                ..Default::default()
 | 
			
		||||
            },
 | 
			
		||||
            ..Default::default()
 | 
			
		||||
        })
 | 
			
		||||
        // top right anchor with vertical fill
 | 
			
		||||
@ -61,8 +65,11 @@ fn setup(world: &mut World, resources: &mut Resources) {
 | 
			
		||||
                math::vec2(0.0, 0.0),
 | 
			
		||||
                Anchors::new(1.0, 1.0, 0.0, 1.0),
 | 
			
		||||
                Margins::new(10.0, 100.0, 50.0, 100.0),
 | 
			
		||||
                Color::rgb(0.1, 0.1, 0.1),
 | 
			
		||||
            ),
 | 
			
		||||
            rect: Rect {
 | 
			
		||||
                color: Color::rgb(0.02, 0.02, 0.02),
 | 
			
		||||
                ..Default::default()
 | 
			
		||||
            },
 | 
			
		||||
            ..Default::default()
 | 
			
		||||
        })
 | 
			
		||||
        // render order test: reddest in the back, whitest in the front
 | 
			
		||||
@ -71,8 +78,11 @@ fn setup(world: &mut World, resources: &mut Resources) {
 | 
			
		||||
                math::vec2(75.0, 75.0),
 | 
			
		||||
                Anchors::new(0.5, 0.5, 0.5, 0.5),
 | 
			
		||||
                Margins::new(0.0, 100.0, 0.0, 100.0),
 | 
			
		||||
                Color::rgb(1.0, 0.1, 0.1),
 | 
			
		||||
            ),
 | 
			
		||||
            rect: Rect {
 | 
			
		||||
                color: Color::rgb(1.0, 0.00, 0.0),
 | 
			
		||||
                ..Default::default()
 | 
			
		||||
            },
 | 
			
		||||
            ..Default::default()
 | 
			
		||||
        })
 | 
			
		||||
        .add_entity(UiEntity {
 | 
			
		||||
@ -80,8 +90,11 @@ fn setup(world: &mut World, resources: &mut Resources) {
 | 
			
		||||
                math::vec2(50.0, 50.0),
 | 
			
		||||
                Anchors::new(0.5, 0.5, 0.5, 0.5),
 | 
			
		||||
                Margins::new(0.0, 100.0, 0.0, 100.0),
 | 
			
		||||
                Color::rgb(1.0, 0.3, 0.3),
 | 
			
		||||
            ),
 | 
			
		||||
            rect: Rect {
 | 
			
		||||
                color: Color::rgb(1.0, 0.3, 0.3),
 | 
			
		||||
                ..Default::default()
 | 
			
		||||
            },
 | 
			
		||||
            ..Default::default()
 | 
			
		||||
        })
 | 
			
		||||
        .add_entity(UiEntity {
 | 
			
		||||
@ -89,8 +102,11 @@ fn setup(world: &mut World, resources: &mut Resources) {
 | 
			
		||||
                math::vec2(100.0, 100.0),
 | 
			
		||||
                Anchors::new(0.5, 0.5, 0.5, 0.5),
 | 
			
		||||
                Margins::new(0.0, 100.0, 0.0, 100.0),
 | 
			
		||||
                Color::rgb(1.0, 0.5, 0.5),
 | 
			
		||||
            ),
 | 
			
		||||
            rect: Rect {
 | 
			
		||||
                color: Color::rgb(1.0, 0.5, 0.5),
 | 
			
		||||
                ..Default::default()
 | 
			
		||||
            },
 | 
			
		||||
            ..Default::default()
 | 
			
		||||
        })
 | 
			
		||||
        .add_entity(UiEntity {
 | 
			
		||||
@ -98,8 +114,11 @@ fn setup(world: &mut World, resources: &mut Resources) {
 | 
			
		||||
                math::vec2(150.0, 150.0),
 | 
			
		||||
                Anchors::new(0.5, 0.5, 0.5, 0.5),
 | 
			
		||||
                Margins::new(0.0, 100.0, 0.0, 100.0),
 | 
			
		||||
                Color::rgb(1.0, 0.7, 0.7),
 | 
			
		||||
            ),
 | 
			
		||||
            rect: Rect {
 | 
			
		||||
                color: Color::rgb(1.0, 0.7, 0.7),
 | 
			
		||||
                ..Default::default()
 | 
			
		||||
            },
 | 
			
		||||
            ..Default::default()
 | 
			
		||||
        })
 | 
			
		||||
        // parenting
 | 
			
		||||
@ -108,8 +127,11 @@ fn setup(world: &mut World, resources: &mut Resources) {
 | 
			
		||||
                math::vec2(300.0, 300.0),
 | 
			
		||||
                Anchors::new(0.0, 0.0, 0.0, 0.0),
 | 
			
		||||
                Margins::new(0.0, 200.0, 0.0, 200.0),
 | 
			
		||||
                Color::rgb(0.1, 0.1, 1.0),
 | 
			
		||||
            ),
 | 
			
		||||
            rect: Rect {
 | 
			
		||||
                color: Color::rgb(0.1, 0.1, 1.0),
 | 
			
		||||
                ..Default::default()
 | 
			
		||||
            },
 | 
			
		||||
            ..Default::default()
 | 
			
		||||
        })
 | 
			
		||||
        .add_children(|builder| {
 | 
			
		||||
@ -118,8 +140,11 @@ fn setup(world: &mut World, resources: &mut Resources) {
 | 
			
		||||
                    math::vec2(0.0, 0.0),
 | 
			
		||||
                    Anchors::new(0.0, 1.0, 0.0, 1.0),
 | 
			
		||||
                    Margins::new(20.0, 20.0, 20.0, 20.0),
 | 
			
		||||
                    Color::rgb(0.6, 0.6, 1.0),
 | 
			
		||||
                ),
 | 
			
		||||
                rect: Rect {
 | 
			
		||||
                    color: Color::rgb(0.6, 0.6, 1.0),
 | 
			
		||||
                    ..Default::default()
 | 
			
		||||
                },
 | 
			
		||||
                ..Default::default()
 | 
			
		||||
            });
 | 
			
		||||
        })
 | 
			
		||||
@ -129,8 +154,11 @@ fn setup(world: &mut World, resources: &mut Resources) {
 | 
			
		||||
                math::vec2(200.0, 200.0),
 | 
			
		||||
                Anchors::new(0.5, 0.5, 0.5, 0.5),
 | 
			
		||||
                Margins::new(0.0, 100.0, 0.0, 100.0),
 | 
			
		||||
                Color::rgba(1.0, 0.9, 0.9, 0.4),
 | 
			
		||||
            ),
 | 
			
		||||
            rect: Rect {
 | 
			
		||||
                color: Color::rgba(1.0, 0.9, 0.9, 0.4),
 | 
			
		||||
                ..Default::default()
 | 
			
		||||
            },
 | 
			
		||||
            ..Default::default()
 | 
			
		||||
        });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user