Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rich text #1245

Merged
merged 18 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updating examples in progress
  • Loading branch information
tigregalis committed Jan 24, 2021
commit efa2dcfa5735b0f59a7712677373a5f436ab6c72
6 changes: 3 additions & 3 deletions crates/bevy_text/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
let mut max_x: f32 = std::f32::MIN;
let mut max_y: f32 = std::f32::MIN;

for section_glyph in section_glyphs.iter() {
let scaled_font = scaled_fonts[section_glyph.section_index];
let glyph = &section_glyph.glyph;
for sg in section_glyphs.iter() {
let scaled_font = scaled_fonts[sg.section_index];
let glyph = &sg.glyph;
min_x = min_x.min(glyph.position.x);
min_y = min_y.min(glyph.position.y - scaled_font.ascent());
max_x = max_x.max(glyph.position.x + scaled_font.h_advance(glyph.id));
Expand Down
43 changes: 29 additions & 14 deletions examples/2d/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const GRAVITY: f32 = -9.821 * 100.0;
const SPRITE_SIZE: f32 = 75.0;

const COL_DESELECTED: Color = Color::rgb_linear(0.03, 0.03, 0.03);
const COL_SELECTED: Color = Color::rgb_linear(5.0, 5.0, 5.0);
const COL_SELECTED: Color = Color::WHITE;

const SHOWCASE_TIMER_SECS: f32 = 3.0;

Expand Down Expand Up @@ -113,13 +113,27 @@ fn setup(
..Default::default()
},
text: Text {
value: "Contributor showcase".to_string(),
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
style: TextStyle {
font_size: 60.0,
color: Color::WHITE,
..Default::default()
},
sections: vec![
TextSection {
value: "Contributor showcase".to_string(),
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
style: TextStyle {
font_size: 60.0,
color: Color::WHITE,
..Default::default()
},
},
TextSection {
value: "".to_string(),
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
style: TextStyle {
font_size: 60.0,
color: Color::WHITE,
..Default::default()
},
},
],
..Default::default()
},
..Default::default()
});
Expand Down Expand Up @@ -195,7 +209,9 @@ fn select(

trans.translation.z = 100.0;

text.value = format!("Contributor: {}", name);
text.sections[0].value = "Contributor: ".to_string();
text.sections[1].value = name.to_string();
text.sections[1].style.color = mat.color;

Some(())
}
Expand Down Expand Up @@ -312,9 +328,8 @@ fn contributors() -> Contributors {
/// Because there is no `Mul<Color> for Color` instead `[f32; 3]` is
/// used.
fn gen_color(rng: &mut impl Rng) -> [f32; 3] {
let r = rng.gen_range(0.2..1.0);
let g = rng.gen_range(0.2..1.0);
let b = rng.gen_range(0.2..1.0);
let v = Vec3::new(r, g, b);
v.normalize().into()
let r = rng.gen_range(0.3..1.0);
let g = rng.gen_range(0.3..1.0);
let b = rng.gen_range(0.3..1.0);
[r, g, b]
}
16 changes: 9 additions & 7 deletions examples/ecs/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ fn setup_menu(
.with_children(|parent| {
parent.spawn(TextBundle {
text: Text {
value: "Play".to_string(),
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
style: TextStyle {
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
..Default::default()
},
sections: vec![TextSection {
value: "Play".to_string(),
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
style: TextStyle {
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
}],
..Default::default()
},
..Default::default()
});
Expand Down
16 changes: 9 additions & 7 deletions examples/scene/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ fn infotext_system(commands: &mut Commands, asset_server: Res<AssetServer>) {
..Default::default()
},
text: Text {
value: "Nothing to see in this window! Check the console output!".to_string(),
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
style: TextStyle {
font_size: 50.0,
color: Color::WHITE,
..Default::default()
},
sections: vec![TextSection {
value: "Nothing to see in this window! Check the console output!".to_string(),
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
style: TextStyle {
font_size: 50.0,
color: Color::WHITE,
},
}],
..Default::default()
},
..Default::default()
});
Expand Down
45 changes: 37 additions & 8 deletions examples/tools/bevymark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,41 @@ fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
.spawn(CameraUiBundle::default())
.spawn(TextBundle {
text: Text {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
value: "Bird Count:".to_string(),
style: TextStyle {
color: Color::rgb(0.0, 1.0, 0.0),
font_size: 40.0,
..Default::default()
},
sections: vec![
TextSection {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
value: "Bird Count: ".to_string(),
style: TextStyle {
color: Color::rgb(0.0, 1.0, 0.0),
font_size: 40.0,
},
},
TextSection {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
value: "".to_string(),
style: TextStyle {
color: Color::rgb(0.0, 1.0, 1.0),
font_size: 40.0,
},
},
TextSection {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
value: "\nAverage FPS: ".to_string(),
style: TextStyle {
color: Color::rgb(0.0, 1.0, 0.0),
font_size: 40.0,
},
},
TextSection {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
value: "".to_string(),
style: TextStyle {
color: Color::rgb(0.0, 1.0, 1.0),
font_size: 40.0,
},
},
],
..Default::default()
},
style: Style {
position_type: PositionType::Absolute,
Expand Down Expand Up @@ -150,7 +178,8 @@ fn counter_system(
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
if let Some(average) = fps.average() {
for mut text in query.iter_mut() {
text.value = format!("Bird Count: {}\nAverage FPS: {:.2}", counter.count, average);
text.sections[1].value = format!("{}", counter.count);
text.sections[3].value = format!("{:.2}", average);
}
}
};
Expand Down
22 changes: 12 additions & 10 deletions examples/ui/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ fn button_system(
let mut text = text_query.get_mut(children[0]).unwrap();
match *interaction {
Interaction::Clicked => {
text.value = "Press".to_string();
text.sections[0].value = "Press".to_string();
*material = button_materials.pressed.clone();
}
Interaction::Hovered => {
text.value = "Hover".to_string();
text.sections[0].value = "Hover".to_string();
*material = button_materials.hovered.clone();
}
Interaction::None => {
text.value = "Button".to_string();
text.sections[0].value = "Button".to_string();
*material = button_materials.normal.clone();
}
}
Expand Down Expand Up @@ -79,13 +79,15 @@ fn setup(
.with_children(|parent| {
parent.spawn(TextBundle {
text: Text {
value: "Button".to_string(),
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
style: TextStyle {
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
..Default::default()
},
sections: vec![TextSection {
value: "Button".to_string(),
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
style: TextStyle {
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
}],
..Default::default()
},
..Default::default()
});
Expand Down
23 changes: 14 additions & 9 deletions examples/ui/font_atlas_debug.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::{prelude::*, text::FontAtlasSet};

// TODO: This is now broken.
/// This example illustrates how FontAtlases are populated. Bevy uses FontAtlases under the hood to optimize text rendering.
fn main() {
App::build()
Expand Down Expand Up @@ -34,12 +35,14 @@ fn atlas_render_system(
font_atlas_sets: Res<Assets<FontAtlasSet>>,
texture_atlases: Res<Assets<TextureAtlas>>,
) {
let count = font_atlas_sets.iter().count();
if let Some(set) = font_atlas_sets.get(&state.handle.as_weak::<FontAtlasSet>()) {
if let Some((_size, font_atlas)) = set.iter().next() {
let x_offset = state.atlas_count as f32;
if state.atlas_count == font_atlas.len() as u32 {
return;
}
dbg!(count);
let texture_atlas = texture_atlases
.get(&font_atlas[state.atlas_count as usize].texture_atlas)
.unwrap();
Expand All @@ -65,8 +68,8 @@ fn text_update_system(mut state: ResMut<State>, time: Res<Time>, mut query: Quer
if state.timer.tick(time.delta_seconds()).finished() {
for mut text in query.iter_mut() {
let c = rand::random::<u8>() as char;
if !text.value.contains(c) {
text.value = format!("{}{}", text.value, c);
if !text.sections[0].value.contains(c) {
text.sections[0].value.push(c);
}
}

Expand All @@ -79,13 +82,15 @@ fn setup(commands: &mut Commands, asset_server: Res<AssetServer>, mut state: Res
state.handle = font_handle.clone();
commands.spawn(CameraUiBundle::default()).spawn(TextBundle {
text: Text {
value: "a".to_string(),
font: font_handle,
style: TextStyle {
font_size: 60.0,
color: Color::WHITE,
..Default::default()
},
sections: vec![TextSection {
value: "a".to_string(),
font: font_handle,
style: TextStyle {
font_size: 60.0,
color: Color::MIDNIGHT_BLUE,
},
}],
..Default::default()
},
..Default::default()
});
Expand Down