2020-07-28 04:04:04 +00:00
|
|
|
use crate::{CalculatedSize, Node};
|
2020-07-20 03:33:55 +00:00
|
|
|
use bevy_asset::{Assets, Handle};
|
2020-07-28 04:04:04 +00:00
|
|
|
use bevy_ecs::{Changed, Query, Res, ResMut};
|
2020-08-11 02:26:48 +00:00
|
|
|
use bevy_math::{Size, Vec3};
|
2020-07-20 03:33:55 +00:00
|
|
|
use bevy_render::{
|
|
|
|
draw::{Draw, DrawContext, Drawable},
|
2020-08-16 03:27:41 +00:00
|
|
|
prelude::Msaa,
|
2020-07-20 03:33:55 +00:00
|
|
|
renderer::{AssetRenderResourceBindings, RenderResourceBindings},
|
2020-08-16 03:27:41 +00:00
|
|
|
texture::Texture,
|
2020-07-20 03:33:55 +00:00
|
|
|
};
|
|
|
|
use bevy_sprite::TextureAtlas;
|
|
|
|
use bevy_text::{DrawableText, Font, FontAtlasSet, TextStyle};
|
|
|
|
use bevy_transform::prelude::Transform;
|
|
|
|
|
2020-08-29 23:49:39 +00:00
|
|
|
#[derive(Default, Clone)]
|
2020-07-20 03:33:55 +00:00
|
|
|
pub struct Text {
|
|
|
|
pub value: String,
|
|
|
|
pub font: Handle<Font>,
|
|
|
|
pub style: TextStyle,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn text_system(
|
|
|
|
mut textures: ResMut<Assets<Texture>>,
|
|
|
|
fonts: Res<Assets<Font>>,
|
|
|
|
mut font_atlas_sets: ResMut<Assets<FontAtlasSet>>,
|
|
|
|
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
|
2020-07-28 04:04:04 +00:00
|
|
|
mut query: Query<(Changed<Text>, &mut CalculatedSize)>,
|
2020-07-20 03:33:55 +00:00
|
|
|
) {
|
2020-07-28 04:04:04 +00:00
|
|
|
for (text, mut calculated_size) in &mut query.iter() {
|
2020-07-20 03:33:55 +00:00
|
|
|
let font_atlases = font_atlas_sets
|
|
|
|
.get_or_insert_with(Handle::from_id(text.font.id), || {
|
|
|
|
FontAtlasSet::new(text.font)
|
|
|
|
});
|
|
|
|
// TODO: this call results in one or more TextureAtlases, whose render resources are created in the RENDER_GRAPH_SYSTEMS
|
|
|
|
// stage. That logic runs _before_ the DRAW stage, which means we cant call add_glyphs_to_atlas in the draw stage
|
|
|
|
// without our render resources being a frame behind. Therefore glyph atlasing either needs its own system or the TextureAtlas
|
|
|
|
// resource generation needs to happen AFTER the render graph systems. maybe draw systems should execute within the
|
|
|
|
// render graph so ordering like this can be taken into account? Maybe the RENDER_GRAPH_SYSTEMS stage should be removed entirely
|
|
|
|
// in favor of node.update()? Regardless, in the immediate short term the current approach is fine.
|
2020-07-28 04:04:04 +00:00
|
|
|
let width = font_atlases.add_glyphs_to_atlas(
|
2020-07-20 03:33:55 +00:00
|
|
|
&fonts,
|
|
|
|
&mut texture_atlases,
|
|
|
|
&mut textures,
|
|
|
|
text.style.font_size,
|
|
|
|
&text.value,
|
|
|
|
);
|
2020-07-28 04:04:04 +00:00
|
|
|
|
|
|
|
calculated_size.size = Size::new(width, text.style.font_size);
|
2020-07-20 03:33:55 +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(
|
|
|
|
mut draw_context: DrawContext,
|
|
|
|
fonts: Res<Assets<Font>>,
|
2020-07-30 01:15:15 +00:00
|
|
|
msaa: Res<Msaa>,
|
2020-07-20 03:33:55 +00:00
|
|
|
font_atlas_sets: Res<Assets<FontAtlasSet>>,
|
|
|
|
texture_atlases: Res<Assets<TextureAtlas>>,
|
|
|
|
mut render_resource_bindings: ResMut<RenderResourceBindings>,
|
|
|
|
mut asset_render_resource_bindings: ResMut<AssetRenderResourceBindings>,
|
|
|
|
mut query: Query<(&mut Draw, &Text, &Node, &Transform)>,
|
|
|
|
) {
|
2020-07-22 03:12:15 +00:00
|
|
|
for (mut draw, text, node, transform) in &mut query.iter() {
|
2020-08-16 03:27:41 +00:00
|
|
|
let position =
|
|
|
|
Vec3::from(transform.value.w_axis().truncate()) - (node.size / 2.0).extend(0.0);
|
2020-07-20 03:33:55 +00:00
|
|
|
|
|
|
|
let mut drawable_text = DrawableText {
|
|
|
|
font: fonts.get(&text.font).unwrap(),
|
|
|
|
font_atlas_set: font_atlas_sets
|
|
|
|
.get(&text.font.as_handle::<FontAtlasSet>())
|
|
|
|
.unwrap(),
|
|
|
|
texture_atlases: &texture_atlases,
|
|
|
|
render_resource_bindings: &mut render_resource_bindings,
|
|
|
|
asset_render_resource_bindings: &mut asset_render_resource_bindings,
|
|
|
|
position,
|
2020-07-30 01:15:15 +00:00
|
|
|
msaa: &msaa,
|
2020-07-20 03:33:55 +00:00
|
|
|
style: &text.style,
|
|
|
|
text: &text.value,
|
|
|
|
container_size: node.size,
|
|
|
|
};
|
|
|
|
drawable_text.draw(&mut draw, &mut draw_context).unwrap();
|
|
|
|
}
|
2020-07-26 19:10:18 +00:00
|
|
|
}
|