diff --git a/Cargo.toml b/Cargo.toml index 3353a5a09e8bf..ec1822877df9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ members = ["crates/*", "examples/ios", "tools/ci"] [features] default = [ "bevy_audio", + "bevy_default_assets", "bevy_dynamic_plugin", "bevy_gilrs", "bevy_gltf", @@ -48,6 +49,9 @@ bevy_gltf = ["bevy_internal/bevy_gltf"] bevy_wgpu = ["bevy_internal/bevy_wgpu"] bevy_winit = ["bevy_internal/bevy_winit"] +# Include default bevy assets +bevy_default_assets = ["render", "bevy_internal/bevy_default_assets"] + trace_chrome = ["bevy_internal/trace_chrome"] trace = ["bevy_internal/trace"] wgpu_trace = ["bevy_internal/wgpu_trace"] diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index d4556746d3508..52d055e092c97 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -41,6 +41,9 @@ x11 = ["bevy_winit/x11"] # enable rendering of font glyphs using subpixel accuracy subpixel_glyph_atlas = ["bevy_text/subpixel_glyph_atlas"] +# include default bevy assets +bevy_default_assets = ["bevy_text/bevy_default_assets"] + [dependencies] # bevy bevy_app = { path = "../bevy_app", version = "0.4.0" } diff --git a/crates/bevy_text/Cargo.toml b/crates/bevy_text/Cargo.toml index 855f70398b6db..3ffdf8e850d1a 100644 --- a/crates/bevy_text/Cargo.toml +++ b/crates/bevy_text/Cargo.toml @@ -14,6 +14,7 @@ keywords = ["bevy"] [features] subpixel_glyph_atlas = [] +bevy_default_assets = [] [dependencies] # bevy diff --git a/assets/fonts/FiraSans-Bold.ttf b/crates/bevy_text/src/assets/fonts/FiraSans-Bold.ttf old mode 100755 new mode 100644 similarity index 100% rename from assets/fonts/FiraSans-Bold.ttf rename to crates/bevy_text/src/assets/fonts/FiraSans-Bold.ttf diff --git a/crates/bevy_text/src/lib.rs b/crates/bevy_text/src/lib.rs index a9ffce5f6ddbf..e7911de94b493 100644 --- a/crates/bevy_text/src/lib.rs +++ b/crates/bevy_text/src/lib.rs @@ -43,5 +43,8 @@ impl Plugin for TextPlugin { .insert_resource(DefaultTextPipeline::default()) .add_system_to_stage(CoreStage::PostUpdate, text2d_system.system()) .add_system_to_stage(RenderStage::Draw, text2d::draw_text2d_system.system()); + + #[cfg(feature = "bevy_default_assets")] + text::default_font::load_default_font(app); } } diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index b077c2821574c..d04daeeca8c00 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -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::>().unwrap(); + let font_bytes = include_bytes!("assets/fonts/FiraSans-Bold.ttf"); + 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); + } +} diff --git a/crates/bevy_ui/Cargo.toml b/crates/bevy_ui/Cargo.toml index b0d5921d9e479..6e553a12d4566 100644 --- a/crates/bevy_ui/Cargo.toml +++ b/crates/bevy_ui/Cargo.toml @@ -33,4 +33,4 @@ bevy_utils = { path = "../bevy_utils", version = "0.4.0" } # other stretch = "0.3" serde = {version = "1", features = ["derive"]} -smallvec = "1.4" \ No newline at end of file +smallvec = "1.4" diff --git a/examples/2d/contributors.rs b/examples/2d/contributors.rs index 74db49dc9bda8..cb802f3f467dd 100644 --- a/examples/2d/contributors.rs +++ b/examples/2d/contributors.rs @@ -123,17 +123,17 @@ fn setup( TextSection { value: "Contributor showcase".to_string(), style: TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 60.0, color: Color::WHITE, + ..Default::default() }, }, TextSection { value: "".to_string(), style: TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 60.0, color: Color::WHITE, + ..Default::default() }, }, ], diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index a8cbf3c38a2c9..ee15de6742c35 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -8,16 +8,16 @@ fn main() { .run(); } -fn setup(mut commands: Commands, asset_server: Res) { +fn setup(mut commands: Commands) { // 2d camera commands.spawn_bundle(OrthographicCameraBundle::new_2d()); commands.spawn_bundle(Text2dBundle { text: Text::with_section( "This text is in the 2D scene.", TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 60.0, color: Color::WHITE, + ..Default::default() }, TextAlignment { vertical: VerticalAlign::Center, diff --git a/examples/ecs/state.rs b/examples/ecs/state.rs index eb130b3125b80..44b9b387e8e6d 100644 --- a/examples/ecs/state.rs +++ b/examples/ecs/state.rs @@ -29,11 +29,7 @@ struct MenuData { button_entity: Entity, } -fn setup_menu( - mut commands: Commands, - asset_server: Res, - button_materials: Res, -) { +fn setup_menu(mut commands: Commands, button_materials: Res) { // ui camera commands.spawn_bundle(UiCameraBundle::default()); let button_entity = commands @@ -56,9 +52,9 @@ fn setup_menu( text: Text::with_section( "Play", TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.9, 0.9, 0.9), + ..Default::default() }, Default::default(), ), diff --git a/examples/game/alien_cake_addict.rs b/examples/game/alien_cake_addict.rs index e0f574713e547..a79d3158f51a8 100644 --- a/examples/game/alien_cake_addict.rs +++ b/examples/game/alien_cake_addict.rs @@ -159,9 +159,9 @@ fn setup(mut commands: Commands, asset_server: Res, mut game: ResMu text: Text::with_section( "Score:", TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.5, 0.5, 1.0), + ..Default::default() }, Default::default(), ), @@ -365,7 +365,6 @@ fn gameover_keyboard(mut state: ResMut>, keyboard_input: Res, game: Res, mut materials: ResMut>, ) { @@ -385,9 +384,9 @@ fn display_score( text: Text::with_section( format!("Cake eaten: {}", game.cake_eaten), TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 80.0, color: Color::rgb(0.5, 0.5, 1.0), + ..Default::default() }, Default::default(), ), diff --git a/examples/game/breakout.rs b/examples/game/breakout.rs index 23169838dc33b..82904281d8a11 100644 --- a/examples/game/breakout.rs +++ b/examples/game/breakout.rs @@ -74,9 +74,9 @@ fn setup( TextSection { value: "Score: ".to_string(), style: TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.5, 0.5, 1.0), + ..Default::default() }, }, TextSection { diff --git a/examples/scene/scene.rs b/examples/scene/scene.rs index 6fc809ed2da5a..3b920a9798480 100644 --- a/examples/scene/scene.rs +++ b/examples/scene/scene.rs @@ -102,7 +102,7 @@ fn save_scene_system(world: &mut World) { // This is only necessary for the info message in the UI. See examples/ui/text.rs for a standalone // text example. -fn infotext_system(mut commands: Commands, asset_server: Res) { +fn infotext_system(mut commands: Commands) { commands.spawn_bundle(UiCameraBundle::default()); commands.spawn_bundle(TextBundle { style: Style { @@ -112,9 +112,9 @@ fn infotext_system(mut commands: Commands, asset_server: Res) { text: Text::with_section( "Nothing to see in this window! Check the console output!", TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 50.0, color: Color::WHITE, + ..Default::default() }, Default::default(), ), diff --git a/examples/tools/bevymark.rs b/examples/tools/bevymark.rs index 258c4715408e7..5feb71013972f 100644 --- a/examples/tools/bevymark.rs +++ b/examples/tools/bevymark.rs @@ -52,7 +52,7 @@ fn main() { .run(); } -fn setup(mut commands: Commands, asset_server: Res) { +fn setup(mut commands: Commands) { commands.spawn_bundle(OrthographicCameraBundle::new_2d()); commands.spawn_bundle(UiCameraBundle::default()); commands.spawn_bundle(TextBundle { @@ -61,33 +61,33 @@ fn setup(mut commands: Commands, asset_server: Res) { TextSection { value: "Bird Count: ".to_string(), style: TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.0, 1.0, 0.0), + ..Default::default() }, }, TextSection { value: "".to_string(), style: TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.0, 1.0, 1.0), + ..Default::default() }, }, TextSection { value: "\nAverage FPS: ".to_string(), style: TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.0, 1.0, 0.0), + ..Default::default() }, }, TextSection { value: "".to_string(), style: TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.0, 1.0, 1.0), + ..Default::default() }, }, ], diff --git a/examples/ui/button.rs b/examples/ui/button.rs index 6fc8e7f7a9483..635985e288e65 100644 --- a/examples/ui/button.rs +++ b/examples/ui/button.rs @@ -55,11 +55,7 @@ fn button_system( } } -fn setup( - mut commands: Commands, - asset_server: Res, - button_materials: Res, -) { +fn setup(mut commands: Commands, button_materials: Res) { // ui camera commands.spawn_bundle(UiCameraBundle::default()); commands @@ -82,9 +78,9 @@ fn setup( text: Text::with_section( "Button", TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.9, 0.9, 0.9), + ..Default::default() }, Default::default(), ), diff --git a/examples/ui/font_atlas_debug.rs b/examples/ui/font_atlas_debug.rs index ce4f81d4279a1..f96b81062fd14 100644 --- a/examples/ui/font_atlas_debug.rs +++ b/examples/ui/font_atlas_debug.rs @@ -1,3 +1,4 @@ +use bevy::text::DEFAULT_FONT_HANDLE; use bevy::{prelude::*, text::FontAtlasSet}; // TODO: This is now broken. See #1243 @@ -77,17 +78,16 @@ fn text_update_system(mut state: ResMut, time: Res