Skip to content

Commit

Permalink
Rebasing on next to match bevy 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
AlisCode committed Nov 6, 2020
1 parent 7d6b66c commit e32dd40
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 29 deletions.
15 changes: 10 additions & 5 deletions crates/bevy_text/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy_render::{
draw::{Draw, DrawContext, DrawError, Drawable},
mesh,
pipeline::{PipelineSpecialization, VertexBufferDescriptor},
prelude::{Msaa, Texture},
prelude::Msaa,
renderer::{
AssetRenderResourceBindings, BindGroup, BufferUsage, RenderResourceBindings,
RenderResourceId,
Expand Down Expand Up @@ -63,16 +63,21 @@ impl<'a> Drawable for DrawableText<'a> {
&bevy_sprite::SPRITE_SHEET_PIPELINE_HANDLE,
&PipelineSpecialization {
sample_count: self.msaa.samples,
vertex_buffer_descriptor: self.font_quad_vertex_descriptor.clone(),
..Default::default()
},
)?;

let render_resource_context = &**context.render_resource_context;
if let Some(RenderResourceId::Buffer(quad_vertex_buffer)) = render_resource_context
.get_asset_resource(&bevy_sprite::QUAD_HANDLE, mesh::VERTEX_BUFFER_ASSET_INDEX)

if let Some(RenderResourceId::Buffer(vertex_attribute_buffer_id)) = render_resource_context
.get_asset_resource(&bevy_sprite::QUAD_HANDLE, mesh::VERTEX_ATTRIBUTE_BUFFER_ID)
{
draw.set_vertex_buffer(0, quad_vertex_buffer, 0);
draw.set_vertex_buffer(0, vertex_attribute_buffer_id, 0);
} else {
println!("could not find vertex buffer for bevy_sprite::QUAD_HANDLE")
}

let mut indices = 0..0;
if let Some(RenderResourceId::Buffer(quad_index_buffer)) = render_resource_context
.get_asset_resource(&bevy_sprite::QUAD_HANDLE, mesh::INDEX_BUFFER_ASSET_INDEX)
Expand All @@ -88,7 +93,7 @@ impl<'a> Drawable for DrawableText<'a> {
// set global bindings
context.set_bind_groups_from_bindings(draw, &mut [self.render_resource_bindings])?;

for tv in self.text_vertices.borrow() {
for tv in &**self.text_vertices {
let atlas_render_resource_bindings = self
.asset_render_resource_bindings
.get_mut(&tv.atlas_info.texture_atlas)
Expand Down
12 changes: 8 additions & 4 deletions crates/bevy_text/src/glyph_brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ impl GlyphBrush {
let bounds = outlined_glyph.px_bounds();
let handle_font_atlas: Handle<FontAtlasSet> = handle.as_weak();
let font_atlas_set = font_atlas_set_storage
.get_or_insert_with(handle_font_atlas, || FontAtlasSet::default());
.get_or_insert_with(handle_font_atlas, FontAtlasSet::default);

let atlas_info = font_atlas_set
.get_glyph_atlas_info(font_size, glyph_id)
.map(|gaf| Ok(gaf))
.map(Ok)
.unwrap_or_else(|| {
font_atlas_set.add_glyph_to_atlas(
texture_atlases,
Expand Down Expand Up @@ -163,11 +163,15 @@ pub struct TextVertex {
#[derive(Debug, Default, Clone)]
pub struct TextVertices(Vec<TextVertex>);

impl TextVertices {
pub fn borrow(&self) -> &Vec<TextVertex> {
impl std::ops::Deref for TextVertices {
type Target = Vec<TextVertex>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl TextVertices {
pub fn set(&mut self, vertices: Vec<TextVertex>) {
self.0 = vertices;
}
Expand Down
12 changes: 7 additions & 5 deletions crates/bevy_text/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ impl TextPipeline {
.compute_glyphs(&[section], bounds, text_alignment)?;

if section_glyphs.is_empty() {
return Ok(Size::new(0.,0.));
return Ok(Size::new(0., 0.));
}
let first_glyph = section_glyphs.first().unwrap();
let mut min_x: f32 = first_glyph.glyph.position.x - scaled_font.h_side_bearing(first_glyph.glyph.id);
let mut min_x: f32 =
first_glyph.glyph.position.x - scaled_font.h_side_bearing(first_glyph.glyph.id);
let mut min_y: f32 = first_glyph.glyph.position.y - scaled_font.ascent();
let mut max_x: f32 = first_glyph.glyph.position.x + scaled_font.h_advance(first_glyph.glyph.id);
let mut max_x: f32 =
first_glyph.glyph.position.x + scaled_font.h_advance(first_glyph.glyph.id);
let mut max_y: f32 = first_glyph.glyph.position.y - scaled_font.descent();
for section_glyph in section_glyphs.iter() {
let glyph = &section_glyph.glyph;
Expand All @@ -74,10 +76,10 @@ impl TextPipeline {

pub fn get_or_insert_font_id(&mut self, handle: Handle<Font>, font: &Font) -> FontId {
let brush = &mut self.brush;
self.map_font_id
*self
.map_font_id
.entry(handle.id)
.or_insert_with(|| brush.add_font(handle.clone(), font.font.clone()))
.clone()
}

pub fn queue_text(
Expand Down
26 changes: 14 additions & 12 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{CalculatedSize, Node, Style, Val};
use bevy_asset::{Assets, Handle};
use bevy_ecs::{Changed, Entity, Local, Or, Query, QuerySet, Res, ResMut, Resource};
use bevy_math::{Size, Vec2};
use bevy_ecs::{Changed, Entity, Or, Query, Res, ResMut};
use bevy_math::Size;
use bevy_render::{
draw::{Draw, DrawContext, Drawable},
mesh::Mesh,
Expand All @@ -25,8 +25,8 @@ pub struct Text {
pub style: TextStyle,
}

/// Defines how min_size, size, and max_size affects the bounds of a text
/// block.
/// Defines how min_size, size, and max_size affects the bounds of a text
/// block.
pub fn text_constraint(min_size: Val, size: Val, max_size: Val) -> f32 {
// Needs support for percentages
match (min_size, size, max_size) {
Expand All @@ -52,10 +52,14 @@ pub fn text_system(
&mut CalculatedSize,
)>,
) {
for ((text, style), mut vertices, mut calculated_size) in &mut text_query.iter() {
for ((text, style), mut vertices, mut calculated_size) in text_query.iter_mut() {
let node_size = Size::new(
text_constraint(style.min_size.width, style.size.width, style.max_size.width),
text_constraint(style.min_size.height, style.size.height, style.max_size.height),
text_constraint(style.min_size.width, style.size.width, style.max_size.width),
text_constraint(
style.min_size.height,
style.size.height,
style.max_size.height,
),
);

if let Err(e) = text_pipeline.queue_text(
Expand All @@ -77,7 +81,6 @@ pub fn text_system(
) {
calculated_size.size = new_size;
}


match text_pipeline.process_queued(
&fonts,
Expand All @@ -95,8 +98,6 @@ pub fn text_system(
pub fn draw_text_system(
mut context: DrawContext,
msaa: Res<Msaa>,
font_atlas_sets: Res<Assets<FontAtlasSet>>,
texture_atlases: Res<Assets<TextureAtlas>>,
meshes: Res<Assets<Mesh>>,
mut render_resource_bindings: ResMut<RenderResourceBindings>,
mut asset_render_resource_bindings: ResMut<AssetRenderResourceBindings>,
Expand All @@ -105,7 +106,7 @@ pub fn draw_text_system(
let font_quad = meshes.get(&QUAD_HANDLE).unwrap();
let vertex_buffer_descriptor = font_quad.get_vertex_buffer_descriptor();

for (mut draw, text, text_vertices, node, global_transform) in &mut query.iter() {
for (mut draw, text, text_vertices, node, global_transform) in query.iter_mut() {
let position = global_transform.translation - (node.size / 2.0).extend(0.0);

let mut drawable_text = DrawableText {
Expand All @@ -114,9 +115,10 @@ pub fn draw_text_system(
position,
msaa: &msaa,
text_vertices,
font_quad_vertex_descriptor: vertex_buffer_descriptor,
font_quad_vertex_descriptor: &vertex_buffer_descriptor,
style: &text.style,
};

drawable_text.draw(&mut draw, &mut context).unwrap();
}
}
6 changes: 3 additions & 3 deletions examples/ui/text_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate rand;
/// This example is for debugging text layout
fn main() {
App::build()
.add_default_plugins()
.add_plugins(DefaultPlugins)
.add_startup_system(infotext_system.system())
.add_system(change_text_system.system())
.run();
Expand Down Expand Up @@ -57,7 +57,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
},
text: Text {
value:
"This is very long text with limited width in the top right and is also pink"
"This is very long text with limited width in the top right and is also pink"
.to_string(),
font: font.clone(),
style: TextStyle {
Expand All @@ -66,7 +66,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
alignment: TextAlignment {
horizontal: HorizontalAlign::Center,
vertical: VerticalAlign::Center,
}
},
},
},
..Default::default()
Expand Down

0 comments on commit e32dd40

Please sign in to comment.