TextPlugin + FontLoader
This commit is contained in:
parent
ad66f87ff6
commit
5d0d3d28c7
@ -7,7 +7,10 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
bevy_app = { path = "../bevy_app" }
|
||||
bevy_asset = { path = "../bevy_asset" }
|
||||
bevy_render = { path = "../bevy_render" }
|
||||
skribo = "0.1.0"
|
||||
font-kit = "0.6"
|
||||
pathfinder_geometry = "0.5"
|
||||
pathfinder_geometry = "0.5"
|
||||
anyhow = "1.0"
|
||||
@ -9,6 +9,9 @@ pub struct Font {
|
||||
pub metrics: Metrics,
|
||||
}
|
||||
|
||||
unsafe impl Send for Font {}
|
||||
unsafe impl Sync for Font {}
|
||||
|
||||
impl Font {
|
||||
pub fn try_from_bytes(font_data: Vec<u8>) -> Result<Self, FontLoadingError> {
|
||||
let font = font_kit::font::Font::from_bytes(Arc::new(font_data), 0)?;
|
||||
|
||||
16
crates/bevy_text/src/font_loader.rs
Normal file
16
crates/bevy_text/src/font_loader.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use crate::Font;
|
||||
use bevy_asset::{AssetLoader, AssetPath};
|
||||
use anyhow::Result;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FontLoader;
|
||||
|
||||
impl AssetLoader<Font> for FontLoader {
|
||||
fn from_bytes(&self, _asset_path: &AssetPath, bytes: Vec<u8>) -> Result<Font> {
|
||||
Ok(Font::try_from_bytes(bytes)?)
|
||||
}
|
||||
fn extensions(&self) -> &[&str] {
|
||||
static EXTENSIONS: &[&str] = &["ttf"];
|
||||
EXTENSIONS
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,20 @@
|
||||
mod font;
|
||||
mod font_loader;
|
||||
mod render;
|
||||
|
||||
pub use font::*;
|
||||
pub use font_loader::*;
|
||||
|
||||
use bevy_app::{AppBuilder, AppPlugin};
|
||||
use bevy_asset::AddAsset;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TextPlugin;
|
||||
|
||||
impl AppPlugin for TextPlugin {
|
||||
fn build(&self, app: &mut AppBuilder) {
|
||||
app
|
||||
.add_asset::<Font>()
|
||||
.add_asset_loader(FontLoader);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
use bevy::prelude::*;
|
||||
use std::{fs::File, io::Read};
|
||||
|
||||
fn main() {
|
||||
App::build()
|
||||
@ -10,13 +9,13 @@ fn main() {
|
||||
|
||||
fn setup(
|
||||
command_buffer: &mut CommandBuffer,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut fonts: ResMut<Assets<Font>>,
|
||||
mut textures: ResMut<Assets<Texture>>,
|
||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||
) {
|
||||
let mut font_file = File::open("assets/fonts/FiraSans-Bold.ttf").unwrap();
|
||||
let mut buffer = Vec::new();
|
||||
font_file.read_to_end(&mut buffer).unwrap();
|
||||
let font = Font::try_from_bytes(buffer).unwrap();
|
||||
let font_handle = asset_server.load_sync(&mut fonts, "assets/fonts/FiraSans-Bold.ttf").unwrap();
|
||||
let font = fonts.get(&font_handle).unwrap();
|
||||
|
||||
let texture = font.render_text("Hello from Bevy!", Color::rgba(0.9, 0.9, 0.9, 1.0), 500, 60);
|
||||
let half_width = texture.width as f32 / 2.0;
|
||||
|
||||
@ -33,6 +33,9 @@ impl AddDefaultPlugins for AppBuilder {
|
||||
#[cfg(feature = "gltf")]
|
||||
self.add_plugin(bevy_gltf::GltfPlugin::default());
|
||||
|
||||
#[cfg(feature = "text")]
|
||||
self.add_plugin(bevy_text::TextPlugin::default());
|
||||
|
||||
#[cfg(feature = "winit")]
|
||||
self.add_plugin(bevy_winit::WinitPlugin::default());
|
||||
#[cfg(not(feature = "winit"))]
|
||||
|
||||
Loading…
Reference in New Issue
Block a user