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 {
crate::TerrainCell,
bevy::{
asset::{Assets, HandleId}, asset::{Assets, HandleId},
render::render_resource::Extent3d, render::render_resource::Extent3d,
render::{color::Color, texture::Image}, render::{color::Color, texture::Image},
},
}; };
use { use {
crate::{Biome, World, WorldGenError}, crate::{Biome, World, WorldGenError},

View file

@ -47,9 +47,16 @@ use bevy::{
render::mesh::{shape::Icosphere, Mesh}, render::mesh::{shape::Icosphere, Mesh},
transform::components::Transform, transform::components::Transform,
}; };
#[cfg(feature = "render")] #[cfg(all(feature = "debug", feature = "render"))]
use bevy::{ use bevy::{
asset::{AssetServer, Assets}, diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
log::debug,
};
#[cfg(feature = "render")]
use {
bevy::text::Font,
bevy::{
asset::Assets,
core_pipeline::core_2d::{Camera2d, Camera2dBundle}, core_pipeline::core_2d::{Camera2d, Camera2dBundle},
ecs::{ ecs::{
change_detection::ResMut, change_detection::ResMut,
@ -88,21 +95,14 @@ use bevy::{
}, },
window::{CursorIcon, WindowDescriptor, Windows}, window::{CursorIcon, WindowDescriptor, Windows},
winit::WinitSettings, winit::WinitSettings,
}; },
#[cfg(all(feature = "debug", feature = "render"))] components::{
use bevy::{
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
log::debug,
};
#[cfg(feature = "render")]
use components::{
markers::{InfoPanel, ToolbarButton}, markers::{InfoPanel, ToolbarButton},
third_party::PanCam, 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,11 +1,10 @@
#[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::{Text, TextStyle}, text::{Font, Text, TextStyle},
ui::{ ui::{
entity::{ButtonBundle, TextBundle}, entity::{ButtonBundle, TextBundle},
widget::Button, widget::Button,
@ -14,6 +13,7 @@ use bevy::{
Style, Style,
}, },
utils::default, utils::default,
},
}; };
#[cfg(feature = "render")] #[cfg(feature = "render")]
@ -31,15 +31,12 @@ 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,
}, },