Skip to content

Commit

Permalink
RectTransform
Browse files Browse the repository at this point in the history
  • Loading branch information
james7132 committed Mar 18, 2022
1 parent de677db commit 9e0a8ec
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 104 deletions.
55 changes: 40 additions & 15 deletions crates/bevy_ui/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,45 @@

use crate::{
widget::{Button, ImageMode},
CalculatedSize, FocusPolicy, Interaction, Node, Style, UiColor, UiImage,
FocusPolicy, Interaction, Node, Style, UiColor, UiImage,
};
use bevy_ecs::{bundle::Bundle, prelude::Component};
use bevy_transform::prelude::{Transform, GlobalTransform};
use bevy_render::{
camera::{Camera, DepthCalculation, OrthographicProjection, WindowOrigin},
view::{Visibility, VisibleEntities},
};
use bevy_text::Text;
use bevy_transform::prelude::{GlobalTransform, Transform};
use bevy_math::{Vec2, Size};

#[derive(Component, Clone, Copy, Debug, Default)]
pub struct RectTransform {
pub position: Vec2,
pub size: Size<f32>,
}

#[derive(Component, Clone, Copy, Debug, Default)]
pub struct GlobalRectTransform {
pub position: Vec2,
pub size: Size<f32>,
pub depth: u32,
}

impl GlobalRectTransform {
pub fn canvas_depth(&self) -> u16 {
(self.depth >> 16) as u16
}

pub fn entity_depth(&self) -> u16 {
(self.depth & 0xFFFF) as u16
}

pub fn inherit(&mut self, parent: &Self, source: RectTransform) {
self.position = parent.position + source.position;
self.size = source.size;
self.depth = parent.depth + 1;
}
}

/// The basic UI node
#[derive(Bundle, Clone, Debug, Default)]
Expand All @@ -26,9 +56,9 @@ pub struct NodeBundle {
/// Whether this node should block interaction with lower nodes
pub focus_policy: FocusPolicy,
/// The transform of the node
pub transform: Transform,
pub transform: RectTransform,
/// The global transform of the node
pub global_transform: GlobalTransform,
pub global_transform: GlobalRectTransform,
/// Describes the visibility properties of the node
pub visibility: Visibility,
}
Expand All @@ -42,18 +72,16 @@ pub struct ImageBundle {
pub style: Style,
/// Configures how the image should scale
pub image_mode: ImageMode,
/// The calculated size based on the given image
pub calculated_size: CalculatedSize,
/// The color of the node
pub color: UiColor,
/// The image of the node
pub image: UiImage,
/// Whether this node should block interaction with lower nodes
pub focus_policy: FocusPolicy,
/// The transform of the node
pub transform: Transform,
pub transform: RectTransform,
/// The global transform of the node
pub global_transform: GlobalTransform,
pub global_transform: GlobalRectTransform,
/// Describes the visibility properties of the node
pub visibility: Visibility,
}
Expand All @@ -67,14 +95,12 @@ pub struct TextBundle {
pub style: Style,
/// Contains the text of the node
pub text: Text,
/// The calculated size based on the given image
pub calculated_size: CalculatedSize,
/// Whether this node should block interaction with lower nodes
pub focus_policy: FocusPolicy,
/// The transform of the node
pub transform: Transform,
pub transform: RectTransform,
/// The global transform of the node
pub global_transform: GlobalTransform,
pub global_transform: GlobalRectTransform,
/// Describes the visibility properties of the node
pub visibility: Visibility,
}
Expand All @@ -85,7 +111,6 @@ impl Default for TextBundle {
focus_policy: FocusPolicy::Pass,
text: Default::default(),
node: Default::default(),
calculated_size: Default::default(),
style: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
Expand All @@ -112,9 +137,9 @@ pub struct ButtonBundle {
/// The image of the node
pub image: UiImage,
/// The transform of the node
pub transform: Transform,
pub transform: RectTransform,
/// The global transform of the node
pub global_transform: GlobalTransform,
pub global_transform: GlobalRectTransform,
/// Describes the visibility properties of the node
pub visibility: Visibility,
}
Expand Down
20 changes: 10 additions & 10 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod convert;

use crate::{CalculatedSize, Node, Style};
use crate::{Node, Style};
use bevy_ecs::{
entity::Entity,
event::EventReader,
Expand Down Expand Up @@ -76,7 +76,7 @@ impl FlexSurface {
&mut self,
entity: Entity,
style: &Style,
calculated_size: CalculatedSize,
calculated_size: Size<f32>,
scale_factor: f64,
) {
let stretch = &mut self.stretch;
Expand Down Expand Up @@ -204,11 +204,11 @@ pub fn flex_node_system(
mut scale_factor_events: EventReader<WindowScaleFactorChanged>,
mut flex_surface: ResMut<FlexSurface>,
root_node_query: Query<Entity, (With<Node>, Without<Parent>)>,
node_query: Query<(Entity, &Style, Option<&CalculatedSize>), (With<Node>, Changed<Style>)>,
full_node_query: Query<(Entity, &Style, Option<&CalculatedSize>), With<Node>>,
node_query: Query<(Entity, &Style, Option<&GlobalRectTransform>), (With<Node>, Changed<Style>)>,
full_node_query: Query<(Entity, &Style, Option<&GlobalRectTransform>), With<Node>>,
changed_size_query: Query<
(Entity, &Style, &CalculatedSize),
(With<Node>, Changed<CalculatedSize>),
(Entity, &Style, &GlobalRectTransform),
(With<Node>, Changed<GlobalRectTransform>),
>,
children_query: Query<(Entity, &Children), (With<Node>, Changed<Children>)>,
mut node_transform_query: Query<(Entity, &mut Node, &mut Transform, Option<&Parent>)>,
Expand All @@ -234,15 +234,15 @@ pub fn flex_node_system(
fn update_changed<F: WorldQuery>(
flex_surface: &mut FlexSurface,
scaling_factor: f64,
query: Query<(Entity, &Style, Option<&CalculatedSize>), F>,
query: Query<(Entity, &Style, Option<&GlobalRectTransform>), F>,
) where
F::Fetch: FilterFetch,
{
// update changed nodes
for (entity, style, calculated_size) in query.iter() {
for (entity, style, transform) in query.iter() {
// TODO: remove node from old hierarchy if its root has changed
if let Some(calculated_size) = calculated_size {
flex_surface.upsert_leaf(entity, style, *calculated_size, scaling_factor);
if let Some(transform) = transform {
flex_surface.upsert_leaf(entity, style, transform.size, scaling_factor);
} else {
flex_surface.upsert_node(entity, style, scaling_factor);
}
Expand Down
11 changes: 4 additions & 7 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use bevy_ecs::{
use bevy_input::{mouse::MouseButton, touch::Touches, Input};
use bevy_math::Vec2;
use bevy_reflect::{Reflect, ReflectDeserialize};
use bevy_transform::components::GlobalTransform;
use bevy_window::Windows;
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
Expand Down Expand Up @@ -66,7 +65,7 @@ pub fn ui_focus_system(
mut node_query: Query<(
Entity,
&Node,
&GlobalTransform,
&GlobalRectTransform,
Option<&mut Interaction>,
Option<&FocusPolicy>,
Option<&CalculatedClip>,
Expand Down Expand Up @@ -104,11 +103,9 @@ pub fn ui_focus_system(
.iter_mut()
.filter_map(
|(entity, node, global_transform, interaction, focus_policy, clip)| {
let position = global_transform.translation;
let ui_position = position.truncate();
let extents = node.size / 2.0;
let mut min = ui_position - extents;
let mut max = ui_position + extents;
let mut min = global_transform.position - extents;
let mut max = global_transform.position + extents;
if let Some(clip) = clip {
min = Vec2::max(min, clip.clip.min);
max = Vec2::min(max, clip.clip.max);
Expand All @@ -123,7 +120,7 @@ pub fn ui_focus_system(
};

if contains_cursor {
Some((entity, focus_policy, interaction, FloatOrd(position.z)))
Some((entity, focus_policy, interaction, global_transform.depth))
} else {
if let Some(mut interaction) = interaction {
if *interaction == Interaction::Hovered
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ impl Plugin for UiPlugin {
.register_type::<AlignContent>()
.register_type::<AlignItems>()
.register_type::<AlignSelf>()
.register_type::<CalculatedSize>()
.register_type::<Direction>()
.register_type::<Display>()
.register_type::<FlexDirection>()
Expand Down
39 changes: 19 additions & 20 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use camera::*;
pub use pipeline::*;
pub use render_pass::*;

use crate::{CalculatedClip, Node, UiColor, UiImage};
use crate::{CalculatedClip, Node, UiColor, UiImage, prelude::GlobalRectTransform};
use bevy_app::prelude::*;
use bevy_asset::{load_internal_asset, AssetEvent, Assets, Handle, HandleUntyped};
use bevy_core::FloatOrd;
Expand All @@ -26,7 +26,6 @@ use bevy_render::{
};
use bevy_sprite::{Rect, SpriteAssetEvents, TextureAtlas};
use bevy_text::{DefaultTextPipeline, Text};
use bevy_transform::components::GlobalTransform;
use bevy_utils::HashMap;
use bevy_window::{WindowId, Windows};
use bytemuck::{Pod, Zeroable};
Expand Down Expand Up @@ -113,7 +112,7 @@ pub fn build_ui_render(app: &mut App) {
}

pub struct ExtractedUiNode {
pub transform: Mat4,
pub transform: GlobalRectTransform,
pub color: Color,
pub rect: Rect,
pub image: Handle<Image>,
Expand All @@ -131,7 +130,7 @@ pub fn extract_uinodes(
images: Res<Assets<Image>>,
uinode_query: Query<(
&Node,
&GlobalTransform,
&GlobalRectTransform,
&UiColor,
&UiImage,
&Visibility,
Expand All @@ -150,7 +149,7 @@ pub fn extract_uinodes(
continue;
}
extracted_uinodes.uinodes.push(ExtractedUiNode {
transform: transform.compute_matrix(),
transform: *transform,
color: color.0,
rect: bevy_sprite::Rect {
min: Vec2::ZERO,
Expand All @@ -171,7 +170,7 @@ pub fn extract_text_uinodes(
uinode_query: Query<(
Entity,
&Node,
&GlobalTransform,
&GlobaRectlTransform,
&Text,
&Visibility,
Option<&CalculatedClip>,
Expand Down Expand Up @@ -203,15 +202,15 @@ pub fn extract_text_uinodes(
let rect = atlas.textures[index];
let atlas_size = Some(atlas.size);

let transform =
Mat4::from_rotation_translation(transform.rotation, transform.translation)
* Mat4::from_scale(transform.scale / scale_factor)
* Mat4::from_translation(
alignment_offset * scale_factor + text_glyph.position.extend(0.),
);
// let transform =
// Mat4::from_rotation_translation(transform.rotation, transform.translation)
// * Mat4::from_scale(transform.scale / scale_factor)
// * Mat4::from_translation(
// alignment_offset * scale_factor + text_glyph.position.extend(0.),
// );

extracted_uinodes.uinodes.push(ExtractedUiNode {
transform,
transform: GlobalRectTransform::default(),
color,
rect,
image: texture,
Expand Down Expand Up @@ -258,7 +257,7 @@ const QUAD_INDICES: [usize; 6] = [0, 2, 3, 0, 1, 2];
pub struct UiBatch {
pub range: Range<u32>,
pub image: Handle<Image>,
pub z: f32,
pub depth: u32,
}

pub fn prepare_uinodes(
Expand All @@ -273,19 +272,19 @@ pub fn prepare_uinodes(
// sort by increasing z for correct transparency
extracted_uinodes
.uinodes
.sort_by(|a, b| FloatOrd(a.transform.w_axis[2]).cmp(&FloatOrd(b.transform.w_axis[2])));
.sort_by(|a, b| a.transform.depth.cmp(&b.transform.depth));

let mut start = 0;
let mut end = 0;
let mut current_batch_handle = Default::default();
let mut last_z = 0.0;
let mut last_depth = 0;
for extracted_uinode in &extracted_uinodes.uinodes {
if current_batch_handle != extracted_uinode.image {
if start != end {
commands.spawn_bundle((UiBatch {
range: start..end,
image: current_batch_handle,
z: last_z,
depth: last_depth,
},));
start = end;
}
Expand Down Expand Up @@ -375,7 +374,7 @@ pub fn prepare_uinodes(
});
}

last_z = extracted_uinode.transform.w_axis[2];
last_depth = extracted_uinode.transform.depth;
end += QUAD_INDICES.len() as u32;
}

Expand All @@ -384,7 +383,7 @@ pub fn prepare_uinodes(
commands.spawn_bundle((UiBatch {
range: start..end,
image: current_batch_handle,
z: last_z,
depth: last_depth,
},));
}

Expand Down Expand Up @@ -458,7 +457,7 @@ pub fn queue_uinodes(
draw_function: draw_ui_function,
pipeline,
entity,
sort_key: FloatOrd(batch.z),
sort_key: FloatOrd(batch.depth as f32),
});
}
}
Expand Down
8 changes: 0 additions & 8 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,6 @@ impl Default for FlexWrap {
}
}

/// The calculated size of the node
#[derive(Component, Default, Copy, Clone, Debug, Reflect)]
#[reflect(Component)]
pub struct CalculatedSize {
/// The size of the node
pub size: Size,
}

/// The color of the node
#[derive(Component, Default, Copy, Clone, Debug, Reflect)]
#[reflect(Component)]
Expand Down
Loading

0 comments on commit 9e0a8ec

Please sign in to comment.