Embed font at compile-time

This commit is contained in:
Tobias Berger 2022-09-19 12:48:13 +02:00
parent 81df0cd02f
commit 8fa5393a41
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
4 changed files with 86 additions and 649146 deletions

649067
planet.ron

File diff suppressed because one or more lines are too long

View file

@ -1,14 +1,15 @@
#[cfg(feature = "render")]
use crate::TerrainCell;
#[cfg(all(feature = "debug", feature = "render"))] #[cfg(all(feature = "debug", feature = "render"))]
use bevy::log::debug; use bevy::log::debug;
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
use bevy::utils::default; use bevy::utils::default;
#[cfg(feature = "render")] #[cfg(feature = "render")]
use bevy::{ use {
asset::{Assets, HandleId}, crate::TerrainCell,
render::render_resource::Extent3d, bevy::{
render::{color::Color, texture::Image}, asset::{Assets, HandleId},
render::render_resource::Extent3d,
render::{color::Color, texture::Image},
},
}; };
use { use {
crate::{Biome, World, WorldGenError}, crate::{Biome, World, WorldGenError},

View file

@ -47,62 +47,62 @@ use bevy::{
render::mesh::{shape::Icosphere, Mesh}, render::mesh::{shape::Icosphere, Mesh},
transform::components::Transform, transform::components::Transform,
}; };
#[cfg(feature = "render")]
use bevy::{
asset::{AssetServer, Assets},
core_pipeline::core_2d::{Camera2d, Camera2dBundle},
ecs::{
change_detection::ResMut,
query::{Changed, With},
system::{Commands, Query, Res},
},
hierarchy::BuildChildren,
prelude::Vec2,
render::{
camera::{Camera, RenderTarget},
color::Color,
render_resource::{
Extent3d,
TextureDescriptor,
TextureDimension,
TextureFormat,
TextureUsages,
},
texture::{Image, ImageSettings},
},
sprite::{Sprite, SpriteBundle},
text::Text,
transform::components::GlobalTransform,
ui::{
entity::{NodeBundle, TextBundle},
AlignSelf,
FocusPolicy,
Interaction,
JustifyContent,
PositionType,
Size,
Style,
UiColor,
UiRect,
Val,
},
window::{CursorIcon, WindowDescriptor, Windows},
winit::WinitSettings,
};
#[cfg(all(feature = "debug", feature = "render"))] #[cfg(all(feature = "debug", feature = "render"))]
use bevy::{ use bevy::{
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin}, diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
log::debug, log::debug,
}; };
#[cfg(feature = "render")] #[cfg(feature = "render")]
use components::{ use {
markers::{InfoPanel, ToolbarButton}, bevy::text::Font,
third_party::PanCam, bevy::{
asset::Assets,
core_pipeline::core_2d::{Camera2d, Camera2dBundle},
ecs::{
change_detection::ResMut,
query::{Changed, With},
system::{Commands, Query, Res},
},
hierarchy::BuildChildren,
prelude::Vec2,
render::{
camera::{Camera, RenderTarget},
color::Color,
render_resource::{
Extent3d,
TextureDescriptor,
TextureDimension,
TextureFormat,
TextureUsages,
},
texture::{Image, ImageSettings},
},
sprite::{Sprite, SpriteBundle},
text::Text,
transform::components::GlobalTransform,
ui::{
entity::{NodeBundle, TextBundle},
AlignSelf,
FocusPolicy,
Interaction,
JustifyContent,
PositionType,
Size,
Style,
UiColor,
UiRect,
Val,
},
window::{CursorIcon, WindowDescriptor, Windows},
winit::WinitSettings,
},
components::{
markers::{InfoPanel, ToolbarButton},
third_party::PanCam,
},
resources::CursorMapPosition,
ui_helpers::{toolbar_button, toolbar_button_text},
}; };
#[cfg(feature = "render")]
use resources::CursorMapPosition;
#[cfg(feature = "render")]
use ui_helpers::{toolbar_button, toolbar_button_text};
use { use {
bevy::{ bevy::{
app::App, app::App,
@ -337,12 +337,16 @@ fn update_info_panel(
#[cfg(feature = "render")] #[cfg(feature = "render")]
fn generate_graphics( fn generate_graphics(
mut commands: Commands<'_, '_>, mut commands: Commands<'_, '_>,
mut world_manager: ResMut<'_, WorldManager>,
mut images: ResMut<'_, Assets<Image>>, mut images: ResMut<'_, Assets<Image>>,
mut fonts: ResMut<'_, Assets<Font>>,
#[cfg(feature = "planet_view")] mut materials: ResMut<'_, Assets<StandardMaterial>>, #[cfg(feature = "planet_view")] mut materials: ResMut<'_, Assets<StandardMaterial>>,
#[cfg(feature = "planet_view")] mut meshes: ResMut<'_, Assets<Mesh>>, #[cfg(feature = "planet_view")] mut meshes: ResMut<'_, Assets<Mesh>>,
mut world_manager: ResMut<'_, WorldManager>,
asset_server: Res<'_, AssetServer>,
) { ) {
let julia_mono_handle = fonts.add(
Font::try_from_bytes(include_bytes!("../assets/JuliaMono.ttf").to_vec())
.expect("Failed to create JuliaMono font!"),
);
let world = world_manager.world(); let world = world_manager.world();
let custom_sprite_size = Vec2 { let custom_sprite_size = Vec2 {
x: (WORLD_SCALE * world.width as i32) as f32, x: (WORLD_SCALE * world.width as i32) as f32,
@ -417,6 +421,7 @@ fn generate_graphics(
}, },
..default() ..default()
}); });
_ = commands _ = commands
.spawn_bundle(NodeBundle { .spawn_bundle(NodeBundle {
style: Style { style: Style {
@ -444,7 +449,7 @@ fn generate_graphics(
text: Text::from_section( text: Text::from_section(
"Info Panel", "Info Panel",
bevy::text::TextStyle { bevy::text::TextStyle {
font: asset_server.load("JuliaMono.ttf"), font: julia_mono_handle.clone(),
font_size: 15.0, font_size: 15.0,
color: Color::WHITE, color: Color::WHITE,
}, },
@ -471,8 +476,10 @@ fn generate_graphics(
_ = button_box _ = button_box
.spawn_bundle(toolbar_button()) .spawn_bundle(toolbar_button())
.with_children(|button| { .with_children(|button| {
_ = button _ = button.spawn_bundle(toolbar_button_text(
.spawn_bundle(toolbar_button_text(&asset_server, button_type)); julia_mono_handle.clone(),
button_type,
));
}) })
.insert(button_type) .insert(button_type)
}); });

View file

@ -1,19 +1,19 @@
#[cfg(feature = "render")] #[cfg(feature = "render")]
use crate::{components::markers::ToolbarButton, NORMAL_BUTTON}; use {
#[cfg(feature = "render")] crate::{components::markers::ToolbarButton, NORMAL_BUTTON},
use bevy::{ bevy::{
asset::AssetServer, asset::Handle,
ecs::system::Res, render::color::Color,
render::color::Color, text::{Font, Text, TextStyle},
text::{Text, TextStyle}, ui::{
ui::{ entity::{ButtonBundle, TextBundle},
entity::{ButtonBundle, TextBundle}, widget::Button,
widget::Button, AlignItems,
AlignItems, JustifyContent,
JustifyContent, Style,
Style, },
utils::default,
}, },
utils::default,
}; };
#[cfg(feature = "render")] #[cfg(feature = "render")]
@ -31,17 +31,14 @@ pub(crate) fn toolbar_button() -> ButtonBundle {
} }
#[cfg(feature = "render")] #[cfg(feature = "render")]
pub(crate) fn toolbar_button_text( pub(crate) fn toolbar_button_text(font: Handle<Font>, which: ToolbarButton) -> TextBundle {
asset_server: &Res<'_, AssetServer>,
which: ToolbarButton,
) -> TextBundle {
TextBundle { TextBundle {
text: Text::from_section( text: Text::from_section(
which, which,
TextStyle { TextStyle {
font: asset_server.load("JuliaMono.ttf"), font,
font_size: 20.0, font_size: 20.0,
color: Color::WHITE, color: Color::WHITE,
}, },
), ),
..default() ..default()