2020-12-27 19:19:03 +00:00
|
|
|
use crate::{Node, Style, Val};
|
|
|
|
use bevy_asset::Assets;
|
2020-11-13 00:21:48 +00:00
|
|
|
use bevy_ecs::{Changed, Entity, Local, Or, Query, QuerySet, Res, ResMut};
|
2020-09-15 01:20:20 +00:00
|
|
|
use bevy_math::Size;
|
2020-07-20 03:33:55 +00:00
|
|
|
use bevy_render::{
|
|
|
|
draw::{Draw, DrawContext, Drawable},
|
2020-10-31 02:21:53 +00:00
|
|
|
mesh::Mesh,
|
2020-12-09 21:38:48 +00:00
|
|
|
prelude::{Msaa, Visible},
|
2020-12-03 20:39:29 +00:00
|
|
|
renderer::RenderResourceBindings,
|
2020-08-16 03:27:41 +00:00
|
|
|
texture::Texture,
|
2020-07-20 03:33:55 +00:00
|
|
|
};
|
2020-10-31 02:21:53 +00:00
|
|
|
use bevy_sprite::{TextureAtlas, QUAD_HANDLE};
|
2020-12-27 19:19:03 +00:00
|
|
|
use bevy_text::{
|
|
|
|
CalculatedSize, DefaultTextPipeline, DrawableText, Font, FontAtlasSet, Text, TextError,
|
|
|
|
};
|
2020-09-14 21:00:32 +00:00
|
|
|
use bevy_transform::prelude::GlobalTransform;
|
2020-12-30 22:40:50 +00:00
|
|
|
use bevy_window::Windows;
|
2020-09-19 21:22:01 +00:00
|
|
|
|
2020-10-08 18:43:01 +00:00
|
|
|
#[derive(Debug, Default)]
|
2020-09-22 03:23:09 +00:00
|
|
|
pub struct QueuedText {
|
|
|
|
entities: Vec<Entity>,
|
2020-09-19 21:22:01 +00:00
|
|
|
}
|
2020-07-20 03:33:55 +00:00
|
|
|
|
2020-12-30 22:40:50 +00:00
|
|
|
fn scale_value(value: f32, factor: f64) -> f32 {
|
|
|
|
(value as f64 * factor) as f32
|
|
|
|
}
|
|
|
|
|
2020-11-13 00:21:48 +00:00
|
|
|
/// Defines how min_size, size, and max_size affects the bounds of a text
|
|
|
|
/// block.
|
2020-12-30 22:40:50 +00:00
|
|
|
pub fn text_constraint(min_size: Val, size: Val, max_size: Val, scale_factor: f64) -> f32 {
|
2020-11-13 00:21:48 +00:00
|
|
|
// Needs support for percentages
|
|
|
|
match (min_size, size, max_size) {
|
2020-12-30 22:40:50 +00:00
|
|
|
(_, _, Val::Px(max)) => scale_value(max, scale_factor),
|
|
|
|
(Val::Px(min), _, _) => scale_value(min, scale_factor),
|
|
|
|
(Val::Undefined, Val::Px(size), Val::Undefined) => scale_value(size, scale_factor),
|
|
|
|
(Val::Auto, Val::Px(size), Val::Auto) => scale_value(size, scale_factor),
|
2020-11-13 00:21:48 +00:00
|
|
|
_ => f32::MAX,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the size of a text block and updates the TextGlyphs with the
|
|
|
|
/// new computed glyphs from the layout
|
2020-12-30 22:40:50 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-07-20 03:33:55 +00:00
|
|
|
pub fn text_system(
|
2020-09-22 03:23:09 +00:00
|
|
|
mut queued_text: Local<QueuedText>,
|
2020-07-20 03:33:55 +00:00
|
|
|
mut textures: ResMut<Assets<Texture>>,
|
|
|
|
fonts: Res<Assets<Font>>,
|
2020-12-30 22:40:50 +00:00
|
|
|
windows: Res<Windows>,
|
2020-07-20 03:33:55 +00:00
|
|
|
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
|
2020-11-13 00:21:48 +00:00
|
|
|
mut font_atlas_set_storage: ResMut<Assets<FontAtlasSet>>,
|
|
|
|
mut text_pipeline: ResMut<DefaultTextPipeline>,
|
|
|
|
mut text_queries: QuerySet<(
|
|
|
|
Query<Entity, Or<(Changed<Text>, Changed<Style>)>>,
|
|
|
|
Query<(&Text, &Style, &mut CalculatedSize)>,
|
2020-10-30 06:39:55 +00:00
|
|
|
)>,
|
2020-07-20 03:33:55 +00:00
|
|
|
) {
|
2020-12-30 22:40:50 +00:00
|
|
|
let scale_factor = if let Some(window) = windows.get_primary() {
|
|
|
|
window.scale_factor()
|
|
|
|
} else {
|
|
|
|
1.
|
|
|
|
};
|
|
|
|
|
|
|
|
let inv_scale_factor = 1. / scale_factor;
|
|
|
|
|
2020-11-13 00:21:48 +00:00
|
|
|
// Adds all entities where the text or the style has changed to the local queue
|
|
|
|
for entity in text_queries.q0_mut().iter_mut() {
|
|
|
|
queued_text.entities.push(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
if queued_text.entities.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Computes all text in the local queue
|
|
|
|
let mut new_queue = Vec::new();
|
|
|
|
let query = text_queries.q1_mut();
|
2020-09-22 03:23:09 +00:00
|
|
|
for entity in queued_text.entities.drain(..) {
|
2020-11-13 00:21:48 +00:00
|
|
|
if let Ok((text, style, mut calculated_size)) = query.get_mut(entity) {
|
2020-12-27 19:19:03 +00:00
|
|
|
let node_size = Size::new(
|
2020-12-30 22:40:50 +00:00
|
|
|
text_constraint(
|
|
|
|
style.min_size.width,
|
|
|
|
style.size.width,
|
|
|
|
style.max_size.width,
|
|
|
|
scale_factor,
|
|
|
|
),
|
2020-12-27 19:19:03 +00:00
|
|
|
text_constraint(
|
|
|
|
style.min_size.height,
|
|
|
|
style.size.height,
|
|
|
|
style.max_size.height,
|
2020-12-30 22:40:50 +00:00
|
|
|
scale_factor,
|
2020-12-27 19:19:03 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
match text_pipeline.queue_text(
|
2020-11-13 00:21:48 +00:00
|
|
|
entity,
|
2020-12-27 19:19:03 +00:00
|
|
|
&fonts,
|
2021-01-25 01:07:43 +00:00
|
|
|
&text.sections,
|
|
|
|
scale_factor,
|
|
|
|
text.alignment,
|
2020-12-27 19:19:03 +00:00
|
|
|
node_size,
|
2020-11-13 00:21:48 +00:00
|
|
|
&mut *font_atlas_set_storage,
|
2020-12-27 19:19:03 +00:00
|
|
|
&mut *texture_atlases,
|
|
|
|
&mut *textures,
|
2020-10-30 06:39:55 +00:00
|
|
|
) {
|
2020-12-27 19:19:03 +00:00
|
|
|
Err(TextError::NoSuchFont) => {
|
|
|
|
// There was an error processing the text layout, let's add this entity to the queue for further processing
|
|
|
|
new_queue.push(entity);
|
|
|
|
}
|
|
|
|
Err(e @ TextError::FailedToAddGlyph(_)) => {
|
|
|
|
panic!("Fatal error when processing text: {}.", e);
|
|
|
|
}
|
|
|
|
Ok(()) => {
|
2020-11-13 00:21:48 +00:00
|
|
|
let text_layout_info = text_pipeline.get_glyphs(&entity).expect(
|
|
|
|
"Failed to get glyphs from the pipeline that have just been computed",
|
|
|
|
);
|
2020-12-30 22:40:50 +00:00
|
|
|
calculated_size.size = Size {
|
|
|
|
width: scale_value(text_layout_info.size.width, inv_scale_factor),
|
|
|
|
height: scale_value(text_layout_info.size.height, inv_scale_factor),
|
|
|
|
};
|
2020-11-13 00:21:48 +00:00
|
|
|
}
|
2020-09-19 21:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 00:21:48 +00:00
|
|
|
queued_text.entities = new_queue;
|
|
|
|
}
|
2020-09-22 03:23:09 +00:00
|
|
|
|
2020-08-16 07:30:04 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-07-20 03:33:55 +00:00
|
|
|
pub fn draw_text_system(
|
2020-11-13 00:21:48 +00:00
|
|
|
mut context: DrawContext,
|
2020-07-30 01:15:15 +00:00
|
|
|
msaa: Res<Msaa>,
|
2020-12-30 22:40:50 +00:00
|
|
|
windows: Res<Windows>,
|
2020-10-31 02:21:53 +00:00
|
|
|
meshes: Res<Assets<Mesh>>,
|
2020-07-20 03:33:55 +00:00
|
|
|
mut render_resource_bindings: ResMut<RenderResourceBindings>,
|
2020-11-13 00:21:48 +00:00
|
|
|
text_pipeline: Res<DefaultTextPipeline>,
|
2020-12-09 21:38:48 +00:00
|
|
|
mut query: Query<(Entity, &mut Draw, &Visible, &Text, &Node, &GlobalTransform)>,
|
2020-07-20 03:33:55 +00:00
|
|
|
) {
|
2020-12-30 22:40:50 +00:00
|
|
|
let scale_factor = if let Some(window) = windows.get_primary() {
|
|
|
|
window.scale_factor()
|
|
|
|
} else {
|
|
|
|
1.
|
|
|
|
};
|
|
|
|
|
2020-11-02 21:15:07 +00:00
|
|
|
let font_quad = meshes.get(&QUAD_HANDLE).unwrap();
|
2021-02-01 04:06:42 +00:00
|
|
|
let vertex_buffer_layout = font_quad.get_vertex_buffer_layout();
|
2020-10-31 02:21:53 +00:00
|
|
|
|
2020-12-09 21:38:48 +00:00
|
|
|
for (entity, mut draw, visible, text, node, global_transform) in query.iter_mut() {
|
|
|
|
if !visible.is_visible {
|
2020-11-22 19:57:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-13 00:21:48 +00:00
|
|
|
if let Some(text_glyphs) = text_pipeline.get_glyphs(&entity) {
|
2020-10-18 20:03:16 +00:00
|
|
|
let position = global_transform.translation - (node.size / 2.0).extend(0.0);
|
2020-11-13 00:21:48 +00:00
|
|
|
|
2020-09-19 19:57:52 +00:00
|
|
|
let mut drawable_text = DrawableText {
|
|
|
|
render_resource_bindings: &mut render_resource_bindings,
|
|
|
|
position,
|
2020-12-30 22:40:50 +00:00
|
|
|
scale_factor: scale_factor as f32,
|
2020-09-19 19:57:52 +00:00
|
|
|
msaa: &msaa,
|
2020-11-13 00:21:48 +00:00
|
|
|
text_glyphs: &text_glyphs.glyphs,
|
2021-02-01 04:06:42 +00:00
|
|
|
font_quad_vertex_layout: &vertex_buffer_layout,
|
2021-01-25 01:07:43 +00:00
|
|
|
sections: &text.sections,
|
2020-09-19 19:57:52 +00:00
|
|
|
};
|
2020-11-13 00:21:48 +00:00
|
|
|
|
|
|
|
drawable_text.draw(&mut draw, &mut context).unwrap();
|
2020-09-19 19:57:52 +00:00
|
|
|
}
|
2020-07-20 03:33:55 +00:00
|
|
|
}
|
2020-07-26 19:10:18 +00:00
|
|
|
}
|