Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/bevy_sprite/src/render/sprite_sheet.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

layout(location = 0) in vec2 v_Uv;
layout(location = 1) in vec4 v_Color;
layout(location = 2) in vec4 v_Bounds;

layout(location = 0) out vec4 o_Target;

layout(set = 1, binding = 2) uniform texture2D TextureAtlas_texture;
layout(set = 1, binding = 3) uniform sampler TextureAtlas_texture_sampler;

void main() {
if(gl_FragCoord.x >= v_Bounds.x || gl_FragCoord.x <= v_Bounds.z ||
gl_FragCoord.y >= v_Bounds.y || gl_FragCoord.y <= v_Bounds.w) {
discard;
}
o_Target = v_Color * texture(
sampler2D(TextureAtlas_texture, TextureAtlas_texture_sampler),
v_Uv);
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_sprite/src/render/sprite_sheet.vert
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ layout(location = 2) in vec2 Vertex_Uv;

layout(location = 0) out vec2 v_Uv;
layout(location = 1) out vec4 v_Color;
layout(location = 2) out vec4 v_Bounds;

layout(set = 0, binding = 0) uniform CameraViewProj {
mat4 ViewProj;
Expand Down Expand Up @@ -34,6 +35,7 @@ layout(set = 2, binding = 1) uniform TextureAtlasSprite {
vec4 color;
uint index;
uint flip;
vec4 bounds;
};

void main() {
Expand Down Expand Up @@ -82,4 +84,5 @@ void main() {

v_Color = color;
gl_Position = ViewProj * SpriteTransform * vec4(vertex_position, 1.0);
v_Bounds = bounds;
}
9 changes: 7 additions & 2 deletions crates/bevy_sprite/src/texture_atlas.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Rect;
use bevy_asset::Handle;
use bevy_core::Bytes;
use bevy_math::Vec2;
use bevy_math::{Vec2, Vec4};
use bevy_reflect::TypeUuid;
use bevy_render::{
color::Color,
Expand Down Expand Up @@ -33,6 +33,7 @@ pub struct TextureAtlasSprite {
pub index: u32,
pub flip_x: bool,
pub flip_y: bool,
pub bounds: Vec4,
}

impl RenderResource for TextureAtlasSprite {
Expand All @@ -50,14 +51,17 @@ impl RenderResource for TextureAtlasSprite {
self.color.write_bytes(color_buf);

// Write the index buffer
let (index_buf, flip_buf) = rest.split_at_mut(4);
let (index_buf, rest) = rest.split_at_mut(4);
let (flip_buf, bounds_buf) = rest.split_at_mut(4);
self.index.write_bytes(index_buf);

// First bit means flip x, second bit means flip y
flip_buf[0] = if self.flip_x { 0b01 } else { 0 } | if self.flip_y { 0b10 } else { 0 };
flip_buf[1] = 0;
flip_buf[2] = 0;
flip_buf[3] = 0;

self.bounds.write_bytes(bounds_buf);
}

fn texture(&self) -> Option<&Handle<Texture>> {
Expand All @@ -72,6 +76,7 @@ impl Default for TextureAtlasSprite {
color: Color::WHITE,
flip_x: false,
flip_y: false,
bounds: Default::default(),
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_text/src/draw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_math::{Mat4, Vec3};
use bevy_math::{Mat4, Vec3, Vec4};
use bevy_render::{
draw::{Draw, DrawContext, DrawError, Drawable},
mesh,
Expand All @@ -21,6 +21,7 @@ pub struct DrawableText<'a> {
pub text_glyphs: &'a Vec<PositionedGlyph>,
pub msaa: &'a Msaa,
pub font_quad_vertex_layout: &'a VertexBufferLayout,
pub bounds: Vec4,
}

impl<'a> Drawable for DrawableText<'a> {
Expand Down Expand Up @@ -74,6 +75,7 @@ impl<'a> Drawable for DrawableText<'a> {
color: self.sections[tv.section_index].style.color,
flip_x: false,
flip_y: false,
bounds: self.bounds,
};

// To get the rendering right for non-one scaling factors, we need
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub fn draw_text2d_system(
font_quad_vertex_layout: &font_quad_vertex_layout,
scale_factor,
sections: &text.sections,
bounds: Default::default(),
};

drawable_text.draw(&mut draw, &mut context).unwrap();
Expand Down
54 changes: 51 additions & 3 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
mod convert;

use crate::{CalculatedSize, Node, Style};
use crate::{CalculatedSize, Node, Overflow, Style};
use bevy_app::EventReader;
use bevy_ecs::system::QuerySet;
use bevy_ecs::{
entity::Entity,
query::{Changed, FilterFetch, With, Without, WorldQuery},
system::{Query, Res, ResMut},
};
use bevy_log::warn;
use bevy_math::Vec2;
use bevy_transform::prelude::{Children, Parent, Transform};
use bevy_math::{Vec2, Vec3, Vec4};
use bevy_transform::prelude::{Children, GlobalTransform, Parent, Transform};
use bevy_utils::HashMap;
use bevy_window::{Window, WindowId, WindowScaleFactorChanged, Windows};
use std::fmt;
Expand Down Expand Up @@ -187,6 +188,53 @@ pub enum FlexError {
unsafe impl Send for FlexSurface {}
unsafe impl Sync for FlexSurface {}

pub fn bounds_node_system(
windows: Res<Windows>,
mut query_set: QuerySet<(
Query<(Option<&Parent>, &Style, &Node, &GlobalTransform)>,
Query<&mut Node>,
)>,
query: Query<Entity, With<Node>>,
) {
// y gets flipped somewhere..
let window = windows.get_primary().unwrap();
let window_height = window.height();
let get_bounds = |position: Vec3, size: Vec2| -> Vec4 {
Vec4::new(
position.x - size.x / 2.0,
window_height - (position.y + size.y / 2.0),
position.x + size.x / 2.0,
window_height - (position.y - size.y / 2.0),
)
};
for entity in query.iter() {
let mut current_entity = entity;
let mut bounds = Default::default();
loop {
if let Ok((parent, style, node, global_transform)) = query_set.q0().get(current_entity)
{
if current_entity == entity {
bounds = get_bounds(global_transform.translation, node.size);
}
if style.overflow == Overflow::Hidden {
bounds = get_bounds(global_transform.translation, node.size);
break;
}
if let Some(parent) = parent {
current_entity = parent.0;
} else {
break;
}
} else {
break;
}
}
if let Ok(mut node) = query_set.q1_mut().get_mut(entity) {
node.bounds = bounds;
}
}
}

#[allow(clippy::too_many_arguments)]
pub fn flex_node_system(
windows: Res<Windows>,
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl Plugin for UiPlugin {
.register_type::<Rect<Val>>()
.register_type::<Style>()
.register_type::<Val>()
.register_type::<Overflow>()
.add_system_to_stage(
CoreStage::PreUpdate,
ui_focus_system.system().after(InputSystem),
Expand All @@ -78,6 +79,12 @@ impl Plugin for UiPlugin {
.label(UiSystem::Flex)
.before(TransformSystem::TransformPropagate),
)
.add_system_to_stage(
CoreStage::PostUpdate,
bounds_node_system
.system()
.after(TransformSystem::TransformPropagate),
)
.add_system_to_stage(
CoreStage::PostUpdate,
ui_z_system
Expand Down
11 changes: 8 additions & 3 deletions crates/bevy_ui/src/render/ui.frag
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
#version 450

layout(location = 0) in vec2 v_Uv;

layout(location = 1) in vec4 v_Bounds;
layout(location = 0) out vec4 o_Target;

layout(set = 2, binding = 0) uniform ColorMaterial_color {
vec4 Color;
};

# ifdef COLORMATERIAL_TEXTURE
# ifdef COLORMATERIAL_TEXTURE
layout(set = 2, binding = 1) uniform texture2D ColorMaterial_texture;
layout(set = 2, binding = 2) uniform sampler ColorMaterial_texture_sampler;
# endif

void main() {
vec4 color = Color;
if(gl_FragCoord.x <= v_Bounds.x || gl_FragCoord.x >= v_Bounds.z ||
gl_FragCoord.y <= v_Bounds.y || gl_FragCoord.y >= v_Bounds.w)
{
discard;
}
vec4 color = Color;
# ifdef COLORMATERIAL_TEXTURE
color *= texture(
sampler2D(ColorMaterial_texture, ColorMaterial_texture_sampler),
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_ui/src/render/ui.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ layout(location = 1) in vec3 Vertex_Normal;
layout(location = 2) in vec2 Vertex_Uv;

layout(location = 0) out vec2 v_Uv;
layout(location = 1) out vec4 v_Bounds;

layout(set = 0, binding = 0) uniform CameraViewProj {
mat4 ViewProj;
Expand All @@ -17,8 +18,14 @@ layout(set = 1, binding = 1) uniform Node_size {
vec2 NodeSize;
};

layout(set = 1, binding = 2) uniform Node_bounds {
vec4 Bounds;
};


void main() {
v_Uv = Vertex_Uv;
v_Bounds = Bounds;
vec3 position = Vertex_Position * vec3(NodeSize, 0.0);
gl_Position = ViewProj * Object * vec4(position, 1.0);
}
18 changes: 17 additions & 1 deletion crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy_ecs::reflect::ReflectComponent;
use bevy_math::{Rect, Size, Vec2};
use bevy_math::{Rect, Size, Vec2, Vec4};
use bevy_reflect::{Reflect, ReflectDeserialize};
use bevy_render::renderer::RenderResources;
use serde::{Deserialize, Serialize};
Expand All @@ -9,6 +9,7 @@ use std::ops::{Add, AddAssign};
#[reflect(Component)]
pub struct Node {
pub size: Vec2,
pub bounds: Vec4,
}

#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
Expand Down Expand Up @@ -72,6 +73,7 @@ pub struct Style {
pub min_size: Size<Val>,
pub max_size: Size<Val>,
pub aspect_ratio: Option<f32>,
pub overflow: Overflow,
}

impl Default for Style {
Expand All @@ -97,6 +99,7 @@ impl Default for Style {
min_size: Size::new(Val::Auto, Val::Auto),
max_size: Size::new(Val::Auto, Val::Auto),
aspect_ratio: Default::default(),
overflow: Default::default(),
}
}
}
Expand Down Expand Up @@ -251,6 +254,19 @@ impl Default for FlexWrap {
}
}

#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)]
pub enum Overflow {
Visible,
Hidden,
}

impl Default for Overflow {
fn default() -> Overflow {
Overflow::Visible
}
}

#[derive(Default, Copy, Clone, Debug)]
pub struct CalculatedSize {
pub size: Size,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ pub fn draw_text_system(
text_glyphs: &text_glyphs.glyphs,
font_quad_vertex_layout: &vertex_buffer_layout,
sections: &text.sections,
bounds: node.bounds,
};

drawable_text.draw(&mut draw, &mut context).unwrap();
Expand Down
23 changes: 23 additions & 0 deletions examples/ui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,35 @@ fn setup(
.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
overflow: Overflow::Hidden,
..Default::default()
},
material: materials.add(Color::rgb(1.0, 0.0, 0.0).into()),
..Default::default()
})
.with_children(|parent| {
parent.spawn_bundle(TextBundle {
style: Style {
margin: Rect::all(Val::Px(5.0)),
position_type: PositionType::Absolute,
position: Rect {
left: Val::Px(20.0),
bottom: Val::Px(-20.0),
..Default::default()
},
..Default::default()
},
text: Text::with_section(
"Text Example",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 30.0,
color: Color::WHITE,
},
Default::default(),
),
..Default::default()
});
parent.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
Expand Down