-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Add a default font #1790
Add a default font #1790
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use bevy_asset::Handle; | ||
use bevy_asset::{Handle, HandleUntyped}; | ||
use bevy_math::Size; | ||
use bevy_reflect::TypeUuid; | ||
use bevy_render::color::Color; | ||
use glyph_brush_layout::{HorizontalAlign, VerticalAlign}; | ||
|
||
|
@@ -91,9 +92,16 @@ pub struct TextStyle { | |
pub color: Color, | ||
} | ||
|
||
#[cfg(feature = "bevy_default_assets")] | ||
pub const DEFAULT_FONT_HANDLE: HandleUntyped = | ||
HandleUntyped::weak_from_u64(crate::font::Font::TYPE_UUID, 12210261929130131812); | ||
|
||
impl Default for TextStyle { | ||
fn default() -> Self { | ||
Self { | ||
#[cfg(feature = "bevy_default_assets")] | ||
font: DEFAULT_FONT_HANDLE.typed(), | ||
#[cfg(not(feature = "bevy_default_assets"))] | ||
font: Default::default(), | ||
font_size: 12.0, | ||
color: Color::WHITE, | ||
|
@@ -105,3 +113,22 @@ impl Default for TextStyle { | |
pub struct Text2dSize { | ||
pub size: Size, | ||
} | ||
|
||
#[cfg(feature = "bevy_default_assets")] | ||
pub(crate) mod default_font { | ||
use crate::{Font, DEFAULT_FONT_HANDLE}; | ||
use ab_glyph::{FontArc, FontRef}; | ||
use bevy_app::AppBuilder; | ||
use bevy_asset::Assets; | ||
|
||
pub(crate) fn load_default_font(app: &mut AppBuilder) { | ||
let world = app.world_mut(); | ||
let world_cell = world.cell(); | ||
let mut fonts = world_cell.get_resource_mut::<Assets<Font>>().unwrap(); | ||
let font_bytes = include_bytes!("assets/fonts/FiraSans-Bold.ttf"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This adds ~500KB to the size of the In theory, we should have this in a different crate, however because the amount of metadata needed for creating a crate anyway, it might not be worthwhile, if 95% of people leave the default font in. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe a default assets crate? if other fonts / binary assets get added, could go in the same one? |
||
let font = FontRef::try_from_slice(font_bytes).unwrap(); | ||
let font = FontArc::new(font); | ||
let font = Font { font }; | ||
fonts.set_untracked(DEFAULT_FONT_HANDLE, font); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
use bevy::text::DEFAULT_FONT_HANDLE; | ||
use bevy::{ | ||
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin}, | ||
prelude::*, | ||
|
@@ -19,8 +20,8 @@ fn main() { | |
|
||
struct TextChanges; | ||
|
||
fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) { | ||
let font = asset_server.load("fonts/FiraSans-Bold.ttf"); | ||
fn infotext_system(mut commands: Commands) { | ||
let font = DEFAULT_FONT_HANDLE.typed(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this variable is no longer needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you want to change something back to that font youd need it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can replace its There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i guess? i don't really see why it matters / quad_handle works the same way, why not just leave it exposed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it's just that the mechanism is weird, especially the I'm not saying not to leave it exposed, I'm more suggesting not to use it in the example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, fair point |
||
commands.spawn_bundle(UiCameraBundle::default()); | ||
commands.spawn_bundle(TextBundle { | ||
style: Style { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this not a startup system?