add text label to ui example. fix 0x0 label textures
This commit is contained in:
parent
159acf52af
commit
f1d58609d5
@ -32,12 +32,16 @@ impl Label {
|
|||||||
rect: Com<Rect>,
|
rect: Com<Rect>,
|
||||||
color_material_handle: Com<Handle<ColorMaterial>>,
|
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) {
|
if let Some(font) = fonts.get(&label.font) {
|
||||||
let texture = font.render_text(
|
let texture = font.render_text(
|
||||||
&label.text,
|
&label.text,
|
||||||
label.color,
|
label.color,
|
||||||
rect.size.x() as usize,
|
width as usize,
|
||||||
rect.size.y() as usize,
|
height as usize,
|
||||||
);
|
);
|
||||||
|
|
||||||
let material = color_materials.get_or_insert_with(*color_material_handle, || ColorMaterial::from(Handle::<Texture>::new()));
|
let material = color_materials.get_or_insert_with(*color_material_handle, || ColorMaterial::from(Handle::<Texture>::new()));
|
||||||
|
|||||||
@ -9,6 +9,7 @@ fn main() {
|
|||||||
|
|
||||||
fn setup(
|
fn setup(
|
||||||
command_buffer: &mut CommandBuffer,
|
command_buffer: &mut CommandBuffer,
|
||||||
|
asset_server: Res<AssetServer>,
|
||||||
mut textures: ResMut<Assets<Texture>>,
|
mut textures: ResMut<Assets<Texture>>,
|
||||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||||
) {
|
) {
|
||||||
@ -26,6 +27,8 @@ fn setup(
|
|||||||
let texture = Texture::load(TextureType::Png(
|
let texture = Texture::load(TextureType::Png(
|
||||||
"assets/branding/bevy_logo_dark_big.png".to_string(),
|
"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 aspect = texture.aspect();
|
||||||
let texture_handle = textures.add(texture);
|
let texture_handle = textures.add(texture);
|
||||||
|
|
||||||
@ -66,6 +69,21 @@ fn setup(
|
|||||||
material: materials.add(Color::rgb(0.02, 0.02, 0.02).into()),
|
material: materials.add(Color::rgb(0.02, 0.02, 0.02).into()),
|
||||||
..Default::default()
|
..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
|
// right vertical fill
|
||||||
.add_entity(UiEntity {
|
.add_entity(UiEntity {
|
||||||
node: Node::new(
|
node: Node::new(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user