|
| 1 | +use bevy_asset::Assets; |
| 2 | +use bevy_ecs::{Bundle, Changed, Entity, Local, Query, QuerySet, Res, ResMut, With}; |
| 3 | +use bevy_math::{Size, Vec3}; |
| 4 | +use bevy_render::{ |
| 5 | + draw::{DrawContext, Drawable}, |
| 6 | + mesh::Mesh, |
| 7 | + prelude::{Draw, Msaa, Texture, Visible}, |
| 8 | + render_graph::base::MainPass, |
| 9 | + renderer::RenderResourceBindings, |
| 10 | +}; |
| 11 | +use bevy_sprite::{TextureAtlas, QUAD_HANDLE}; |
| 12 | +use bevy_transform::prelude::{GlobalTransform, Transform}; |
| 13 | +use glyph_brush_layout::{HorizontalAlign, VerticalAlign}; |
| 14 | + |
| 15 | +use crate::{ |
| 16 | + CalculatedSize, DefaultTextPipeline, DrawableText, Font, FontAtlasSet, Text, TextError, |
| 17 | +}; |
| 18 | + |
| 19 | +/// The bundle of components needed to draw text in a 2D scene via the Camera2dBundle. |
| 20 | +#[derive(Bundle, Clone, Debug)] |
| 21 | +pub struct Text2dBundle { |
| 22 | + pub draw: Draw, |
| 23 | + pub visible: Visible, |
| 24 | + pub text: Text, |
| 25 | + pub transform: Transform, |
| 26 | + pub global_transform: GlobalTransform, |
| 27 | + pub main_pass: MainPass, |
| 28 | + pub calculated_size: CalculatedSize, |
| 29 | +} |
| 30 | + |
| 31 | +impl Default for Text2dBundle { |
| 32 | + fn default() -> Self { |
| 33 | + Self { |
| 34 | + draw: Draw { |
| 35 | + ..Default::default() |
| 36 | + }, |
| 37 | + visible: Visible { |
| 38 | + is_transparent: true, |
| 39 | + ..Default::default() |
| 40 | + }, |
| 41 | + text: Default::default(), |
| 42 | + transform: Default::default(), |
| 43 | + global_transform: Default::default(), |
| 44 | + main_pass: MainPass {}, |
| 45 | + calculated_size: CalculatedSize { |
| 46 | + size: Size::default(), |
| 47 | + }, |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// System for drawing text in a 2D scene via the Camera2dBundle. Included in the default |
| 53 | +/// `TextPlugin`. Position is determined by the `Transform`'s translation, though scale and rotation |
| 54 | +/// are ignored. |
| 55 | +pub fn draw_text2d_system( |
| 56 | + mut context: DrawContext, |
| 57 | + msaa: Res<Msaa>, |
| 58 | + meshes: Res<Assets<Mesh>>, |
| 59 | + mut render_resource_bindings: ResMut<RenderResourceBindings>, |
| 60 | + text_pipeline: Res<DefaultTextPipeline>, |
| 61 | + mut query: Query< |
| 62 | + ( |
| 63 | + Entity, |
| 64 | + &mut Draw, |
| 65 | + &Visible, |
| 66 | + &Text, |
| 67 | + &GlobalTransform, |
| 68 | + &CalculatedSize, |
| 69 | + ), |
| 70 | + With<MainPass>, |
| 71 | + >, |
| 72 | +) { |
| 73 | + let font_quad = meshes.get(&QUAD_HANDLE).unwrap(); |
| 74 | + let vertex_buffer_descriptor = font_quad.get_vertex_buffer_descriptor(); |
| 75 | + |
| 76 | + for (entity, mut draw, visible, text, global_transform, calculated_size) in query.iter_mut() { |
| 77 | + if !visible.is_visible { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + let (width, height) = (calculated_size.size.width, calculated_size.size.height); |
| 82 | + |
| 83 | + if let Some(text_glyphs) = text_pipeline.get_glyphs(&entity) { |
| 84 | + let position = global_transform.translation |
| 85 | + + match text.style.alignment.vertical { |
| 86 | + VerticalAlign::Top => Vec3::zero(), |
| 87 | + VerticalAlign::Center => Vec3::new(0.0, -height * 0.5, 0.0), |
| 88 | + VerticalAlign::Bottom => Vec3::new(0.0, -height, 0.0), |
| 89 | + } |
| 90 | + + match text.style.alignment.horizontal { |
| 91 | + HorizontalAlign::Left => Vec3::new(-width, 0.0, 0.0), |
| 92 | + HorizontalAlign::Center => Vec3::new(-width * 0.5, 0.0, 0.0), |
| 93 | + HorizontalAlign::Right => Vec3::zero(), |
| 94 | + }; |
| 95 | + |
| 96 | + let mut drawable_text = DrawableText { |
| 97 | + render_resource_bindings: &mut render_resource_bindings, |
| 98 | + position, |
| 99 | + msaa: &msaa, |
| 100 | + text_glyphs: &text_glyphs.glyphs, |
| 101 | + font_quad_vertex_descriptor: &vertex_buffer_descriptor, |
| 102 | + style: &text.style, |
| 103 | + }; |
| 104 | + |
| 105 | + drawable_text.draw(&mut draw, &mut context).unwrap(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +#[derive(Debug, Default)] |
| 111 | +pub struct QueuedText2d { |
| 112 | + entities: Vec<Entity>, |
| 113 | +} |
| 114 | + |
| 115 | +/// Updates the TextGlyphs with the new computed glyphs from the layout |
| 116 | +pub fn text2d_system( |
| 117 | + mut queued_text: Local<QueuedText2d>, |
| 118 | + mut textures: ResMut<Assets<Texture>>, |
| 119 | + fonts: Res<Assets<Font>>, |
| 120 | + mut texture_atlases: ResMut<Assets<TextureAtlas>>, |
| 121 | + mut font_atlas_set_storage: ResMut<Assets<FontAtlasSet>>, |
| 122 | + mut text_pipeline: ResMut<DefaultTextPipeline>, |
| 123 | + mut text_queries: QuerySet<( |
| 124 | + Query<Entity, Changed<Text>>, |
| 125 | + Query<(&Text, &mut CalculatedSize)>, |
| 126 | + )>, |
| 127 | +) { |
| 128 | + // Adds all entities where the text or the style has changed to the local queue |
| 129 | + for entity in text_queries.q0_mut().iter_mut() { |
| 130 | + queued_text.entities.push(entity); |
| 131 | + } |
| 132 | + |
| 133 | + if queued_text.entities.is_empty() { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + // Computes all text in the local queue |
| 138 | + let mut new_queue = Vec::new(); |
| 139 | + let query = text_queries.q1_mut(); |
| 140 | + for entity in queued_text.entities.drain(..) { |
| 141 | + if let Ok((text, mut calculated_size)) = query.get_mut(entity) { |
| 142 | + match text_pipeline.queue_text( |
| 143 | + entity, |
| 144 | + text.font.clone(), |
| 145 | + &fonts, |
| 146 | + &text.value, |
| 147 | + text.style.font_size, |
| 148 | + text.style.alignment, |
| 149 | + Size::new(f32::MAX, f32::MAX), |
| 150 | + &mut *font_atlas_set_storage, |
| 151 | + &mut *texture_atlases, |
| 152 | + &mut *textures, |
| 153 | + ) { |
| 154 | + Err(TextError::NoSuchFont) => { |
| 155 | + // There was an error processing the text layout, let's add this entity to the queue for further processing |
| 156 | + new_queue.push(entity); |
| 157 | + } |
| 158 | + Err(e @ TextError::FailedToAddGlyph(_)) => { |
| 159 | + panic!("Fatal error when processing text: {}.", e); |
| 160 | + } |
| 161 | + Ok(()) => { |
| 162 | + let text_layout_info = text_pipeline.get_glyphs(&entity).expect( |
| 163 | + "Failed to get glyphs from the pipeline that have just been computed", |
| 164 | + ); |
| 165 | + calculated_size.size = text_layout_info.size; |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + queued_text.entities = new_queue; |
| 172 | +} |
0 commit comments