Skip to content

Commit

Permalink
Merge branch 'feature/zindex-and-uistack' into feature/ui-roots-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
oceantume committed Sep 14, 2022
2 parents 37984cb + e1d58fe commit 5bedfe7
Show file tree
Hide file tree
Showing 24 changed files with 776 additions and 491 deletions.
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,16 @@ description = "Demonstrates transparency for UI"
category = "UI (User Interface)"
wasm = true

[[example]]
name = "z_index"
path = "examples/ui/z_index.rs"

[package.metadata.example.z_index]
name = "UI Z-Index"
description = "Demonstrates z-index for UI"
category = "UI (User Interface)"
wasm = true

[[example]]
name = "ui"
path = "examples/ui/ui.rs"
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ bevy_derive = { path = "../bevy_derive", version = "0.9.0-dev" }
bevy_ecs = { path = "../bevy_ecs", version = "0.9.0-dev", default-features = false }
bevy_reflect = { path = "../bevy_reflect", version = "0.9.0-dev", optional = true }
bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" }
bevy_tasks = { path = "../bevy_tasks", version = "0.9.0-dev" }

# other
serde = { version = "1.0", features = ["derive"], optional = true }
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w> {
) -> bool;
}

/// A helper trait for [`WorldQuery`] that works around Rust's lack of Generic Associated Types
/// A helper trait for [`WorldQuery`] that works around Rust's lack of Generic Associated Types.
///
/// **Note**: Consider using the type aliases [`QueryItem`] and [`QueryFetch`] when using `Item` or `Fetch`.
pub trait WorldQueryGats<'world> {
type Item;
type Fetch;
Expand Down
10 changes: 6 additions & 4 deletions crates/bevy_input/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy_ecs::system::Resource;
use bevy_reflect::Reflect;
use bevy_utils::HashSet;
use std::hash::Hash;

Expand Down Expand Up @@ -33,8 +34,9 @@ use bevy_ecs::schedule::State;
/// * Call the [`Input::press`] method for each press event.
/// * Call the [`Input::release`] method for each release event.
/// * Call the [`Input::clear`] method at each frame start, before processing events.
#[derive(Debug, Clone, Resource)]
pub struct Input<T: Eq + Hash> {
#[derive(Debug, Clone, Resource, Reflect)]
#[reflect_value]
pub struct Input<T: Copy + Eq + Hash + Send + Sync + 'static> {
/// A collection of every button that is currently being pressed.
pressed: HashSet<T>,
/// A collection of every button that has just been pressed.
Expand All @@ -43,7 +45,7 @@ pub struct Input<T: Eq + Hash> {
just_released: HashSet<T>,
}

impl<T: Eq + Hash> Default for Input<T> {
impl<T: Copy + Eq + Hash + Send + Sync + 'static> Default for Input<T> {
fn default() -> Self {
Self {
pressed: Default::default(),
Expand All @@ -55,7 +57,7 @@ impl<T: Eq + Hash> Default for Input<T> {

impl<T> Input<T>
where
T: Copy + Eq + Hash,
T: Copy + Eq + Hash + Send + Sync + 'static,
{
/// Registers a press for the given `input`.
pub fn press(&mut self, input: T) {
Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_input/src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{ButtonState, Input};
use bevy_ecs::{event::EventReader, system::ResMut};
use bevy_reflect::{FromReflect, Reflect};

/// A keyboard input event.
///
Expand Down Expand Up @@ -60,8 +61,9 @@ pub fn keyboard_input_system(
/// ## Updating
///
/// The resource is updated inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system).
#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)]
#[derive(Reflect, FromReflect, Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[reflect(Hash, PartialEq)]
#[repr(u32)]
pub enum KeyCode {
/// The `1` key over the letters.
Expand Down Expand Up @@ -422,6 +424,7 @@ pub enum KeyCode {
/// ## Updating
///
/// The resource is updated inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system).
#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)]
#[derive(Reflect, FromReflect, Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)]
#[reflect(Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct ScanCode(pub u32);
17 changes: 8 additions & 9 deletions crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,18 @@ impl<'a> ReflectDerive<'a> {
let mut force_reflect_value = false;

for attribute in input.attrs.iter().filter_map(|attr| attr.parse_meta().ok()) {
let meta_list = if let Meta::List(meta_list) = attribute {
meta_list
} else {
continue;
};

if let Some(ident) = meta_list.path.get_ident() {
if ident == REFLECT_ATTRIBUTE_NAME {
match attribute {
Meta::List(meta_list) if meta_list.path.is_ident(REFLECT_ATTRIBUTE_NAME) => {
traits = ReflectTraits::from_nested_metas(&meta_list.nested);
} else if ident == REFLECT_VALUE_ATTRIBUTE_NAME {
}
Meta::List(meta_list) if meta_list.path.is_ident(REFLECT_VALUE_ATTRIBUTE_NAME) => {
force_reflect_value = true;
traits = ReflectTraits::from_nested_metas(&meta_list.nested);
}
Meta::Path(path) if path.is_ident(REFLECT_VALUE_ATTRIBUTE_NAME) => {
force_reflect_value = true;
}
_ => continue,
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> TokenStream {

#[inline]
fn clone_value(&self) -> Box<dyn #bevy_reflect_path::Reflect> {
Box::new(self.clone())
Box::new(std::clone::Clone::clone(self))
}

#[inline]
fn apply(&mut self, value: &dyn #bevy_reflect_path::Reflect) {
let value = value.as_any();
if let Some(value) = value.downcast_ref::<Self>() {
*self = value.clone();
*self = std::clone::Clone::clone(value);
} else {
panic!("Value is not {}.", std::any::type_name::<Self>());
}
Expand Down
6 changes: 2 additions & 4 deletions crates/bevy_text/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ pub mod prelude {

use bevy_app::prelude::*;
use bevy_asset::AddAsset;
use bevy_ecs::{entity::Entity, schedule::ParallelSystemDescriptorCoercion};
use bevy_ecs::schedule::ParallelSystemDescriptorCoercion;
use bevy_render::{RenderApp, RenderStage};
use bevy_sprite::SpriteSystem;
use bevy_window::ModifiesWindows;

pub type DefaultTextPipeline = TextPipeline<Entity>;

#[derive(Default)]
pub struct TextPlugin;

Expand All @@ -48,7 +46,7 @@ impl Plugin for TextPlugin {
.register_type::<VerticalAlign>()
.register_type::<HorizontalAlign>()
.init_asset_loader::<FontLoader>()
.insert_resource(DefaultTextPipeline::default())
.insert_resource(TextPipeline::default())
.add_system_to_stage(
CoreStage::PostUpdate,
update_text2d_layout.after(ModifiesWindows),
Expand Down
44 changes: 11 additions & 33 deletions crates/bevy_text/src/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::hash::Hash;

use ab_glyph::{PxScale, ScaleFont};
use bevy_asset::{Assets, Handle, HandleId};
use bevy_ecs::component::Component;
use bevy_ecs::system::Resource;
use bevy_math::Vec2;
use bevy_render::texture::Image;
Expand All @@ -15,29 +14,22 @@ use crate::{
TextAlignment, TextSection,
};

#[derive(Resource)]
pub struct TextPipeline<ID> {
#[derive(Default, Resource)]
pub struct TextPipeline {
brush: GlyphBrush,
glyph_map: HashMap<ID, TextLayoutInfo>,
map_font_id: HashMap<HandleId, FontId>,
}

impl<ID> Default for TextPipeline<ID> {
fn default() -> Self {
TextPipeline {
brush: GlyphBrush::default(),
glyph_map: Default::default(),
map_font_id: Default::default(),
}
}
}

/// Render information for a corresponding [`Text`](crate::Text) component.
///
/// Contains scaled glyphs and their size. Generated via [`TextPipeline::queue_text`].
#[derive(Component, Clone, Default, Debug)]
pub struct TextLayoutInfo {
pub glyphs: Vec<PositionedGlyph>,
pub size: Vec2,
}

impl<ID: Hash + Eq> TextPipeline<ID> {
impl TextPipeline {
pub fn get_or_insert_font_id(&mut self, handle: &Handle<Font>, font: &Font) -> FontId {
let brush = &mut self.brush;
*self
Expand All @@ -46,14 +38,9 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
.or_insert_with(|| brush.add_font(handle.clone(), font.font.clone()))
}

pub fn get_glyphs(&self, id: &ID) -> Option<&TextLayoutInfo> {
self.glyph_map.get(id)
}

#[allow(clippy::too_many_arguments)]
pub fn queue_text(
&mut self,
id: ID,
fonts: &Assets<Font>,
sections: &[TextSection],
scale_factor: f64,
Expand All @@ -62,7 +49,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
font_atlas_set_storage: &mut Assets<FontAtlasSet>,
texture_atlases: &mut Assets<TextureAtlas>,
textures: &mut Assets<Image>,
) -> Result<(), TextError> {
) -> Result<TextLayoutInfo, TextError> {
let mut scaled_fonts = Vec::new();
let sections = sections
.iter()
Expand Down Expand Up @@ -90,14 +77,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
.compute_glyphs(&sections, bounds, text_alignment)?;

if section_glyphs.is_empty() {
self.glyph_map.insert(
id,
TextLayoutInfo {
glyphs: Vec::new(),
size: Vec2::ZERO,
},
);
return Ok(());
return Ok(TextLayoutInfo::default());
}

let mut min_x: f32 = std::f32::MAX;
Expand Down Expand Up @@ -125,8 +105,6 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
textures,
)?;

self.glyph_map.insert(id, TextLayoutInfo { glyphs, size });

Ok(())
Ok(TextLayoutInfo { glyphs, size })
}
}
Loading

0 comments on commit 5bedfe7

Please sign in to comment.