add text label to ui example. fix 0x0 label textures

This commit is contained in:
Carter Anderson 2020-05-22 17:07:14 -07:00
parent 159acf52af
commit f1d58609d5
2 changed files with 24 additions and 2 deletions

View file

@ -32,12 +32,16 @@ impl Label {
rect: Com<Rect>,
color_material_handle: Com<Handle<ColorMaterial>>,
) {
// ensure the texture is at least 1x1
let width = rect.size.x().max(1.0);
let height = rect.size.y().max(1.0);
if let Some(font) = fonts.get(&label.font) {
let texture = font.render_text(
&label.text,
label.color,
rect.size.x() as usize,
rect.size.y() as usize,
width as usize,
height as usize,
);
let material = color_materials.get_or_insert_with(*color_material_handle, || ColorMaterial::from(Handle::<Texture>::new()));

View file

@ -9,6 +9,7 @@ fn main() {
fn setup(
command_buffer: &mut CommandBuffer,
asset_server: Res<AssetServer>,
mut textures: ResMut<Assets<Texture>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
@ -26,6 +27,8 @@ fn setup(
let texture = Texture::load(TextureType::Png(
"assets/branding/bevy_logo_dark_big.png".to_string(),
));
let font_handle = asset_server.load("assets/fonts/FiraSans-Bold.ttf").unwrap();
let aspect = texture.aspect();
let texture_handle = textures.add(texture);
@ -66,6 +69,21 @@ fn setup(
material: materials.add(Color::rgb(0.02, 0.02, 0.02).into()),
..Default::default()
})
.add_children(|builder| {
builder.add_entity(LabelEntity {
node: Node::new(
math::vec2(0.0, 0.0),
Anchors::TOP_LEFT,
Margins::new(10.0, 200.0, 40.0, 10.0),
),
label: Label {
text: "Text Label".to_string(),
font: font_handle,
..Default::default()
},
..Default::default()
})
})
// right vertical fill
.add_entity(UiEntity {
node: Node::new(