
# Objective - Allow other crates to use `TextureAtlas` and friends without needing to depend on `bevy_sprite`. - Specifically, this allows adding `TextureAtlas` support to custom cursors in https://github.com/bevyengine/bevy/pull/17121 by allowing `bevy_winit` to depend on `bevy_image` instead of `bevy_sprite` which is a [non-starter]. [non-starter]: https://github.com/bevyengine/bevy/pull/17121#discussion_r1904955083 ## Solution - Move `TextureAtlas`, `TextureAtlasBuilder`, `TextureAtlasSources`, `TextureAtlasLayout` and `DynamicTextureAtlasBuilder` into `bevy_image`. - Add a new plugin to `bevy_image` named `TextureAtlasPlugin` which allows us to register `TextureAtlas` and `TextureAtlasLayout` which was previously done in `SpritePlugin`. Since `SpritePlugin` did the registration previously, we just need to make it add `TextureAtlasPlugin`. ## Testing - CI builds it. - I also ran multiple examples which hopefully covered any issues: ``` $ cargo run --example sprite $ cargo run --example text $ cargo run --example ui_texture_atlas $ cargo run --example sprite_animation $ cargo run --example sprite_sheet $ cargo run --example sprite_picking ``` --- ## Migration Guide The following types have been moved from `bevy_sprite` to `bevy_image`: `TextureAtlas`, `TextureAtlasBuilder`, `TextureAtlasSources`, `TextureAtlasLayout` and `DynamicTextureAtlasBuilder`. If you are using the `bevy` crate, and were importing these types directly (e.g. before `use bevy::sprite::TextureAtlas`), be sure to update your import paths (e.g. after `use bevy::image::TextureAtlas`) If you are using the `bevy` prelude to import these types (e.g. `use bevy::prelude::*`), you don't need to change anything. If you are using the `bevy_sprite` subcrate, be sure to add `bevy_image` as a dependency if you do not already have it, and be sure to update your import paths.
74 lines
2.9 KiB
Rust
74 lines
2.9 KiB
Rust
//! This module exports types related to rendering glyphs.
|
|
|
|
use bevy_asset::Handle;
|
|
use bevy_image::prelude::*;
|
|
use bevy_math::{IVec2, Vec2};
|
|
use bevy_reflect::Reflect;
|
|
|
|
/// A glyph of a font, typically representing a single character, positioned in screen space.
|
|
///
|
|
/// Contains information about how and where to render a glyph.
|
|
///
|
|
/// Used in [`TextPipeline::queue_text`](crate::TextPipeline::queue_text) and [`crate::TextLayoutInfo`] for rendering glyphs.
|
|
#[derive(Debug, Clone, Reflect)]
|
|
pub struct PositionedGlyph {
|
|
/// The position of the glyph in the text block's bounding box.
|
|
pub position: Vec2,
|
|
/// The width and height of the glyph in logical pixels.
|
|
pub size: Vec2,
|
|
/// Information about the glyph's atlas.
|
|
pub atlas_info: GlyphAtlasInfo,
|
|
/// The index of the glyph in the [`ComputedTextBlock`](crate::ComputedTextBlock)'s tracked spans.
|
|
pub span_index: usize,
|
|
/// TODO: In order to do text editing, we need access to the size of glyphs and their index in the associated String.
|
|
/// For example, to figure out where to place the cursor in an input box from the mouse's position.
|
|
/// Without this, it's only possible in texts where each glyph is one byte. Cosmic text has methods for this
|
|
/// cosmic-texts [hit detection](https://pop-os.github.io/cosmic-text/cosmic_text/struct.Buffer.html#method.hit)
|
|
byte_index: usize,
|
|
}
|
|
|
|
impl PositionedGlyph {
|
|
/// Creates a new [`PositionedGlyph`]
|
|
pub fn new(position: Vec2, size: Vec2, atlas_info: GlyphAtlasInfo, span_index: usize) -> Self {
|
|
Self {
|
|
position,
|
|
size,
|
|
atlas_info,
|
|
span_index,
|
|
byte_index: 0,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Information about a glyph in an atlas.
|
|
///
|
|
/// Rasterized glyphs are stored as rectangles
|
|
/// in one or more [`FontAtlas`](crate::FontAtlas)es.
|
|
///
|
|
/// Used in [`PositionedGlyph`] and [`FontAtlasSet`](crate::FontAtlasSet).
|
|
#[derive(Debug, Clone, Reflect)]
|
|
pub struct GlyphAtlasInfo {
|
|
/// A handle to the [`Image`] data for the texture atlas this glyph was placed in.
|
|
///
|
|
/// A (weak) clone of the handle held by the [`FontAtlas`](crate::FontAtlas).
|
|
pub texture: Handle<Image>,
|
|
/// A handle to the [`TextureAtlasLayout`] map for the texture atlas this glyph was placed in.
|
|
///
|
|
/// A (weak) clone of the handle held by the [`FontAtlas`](crate::FontAtlas).
|
|
pub texture_atlas: Handle<TextureAtlasLayout>,
|
|
/// Location and offset of a glyph within the texture atlas.
|
|
pub location: GlyphAtlasLocation,
|
|
}
|
|
|
|
/// The location of a glyph in an atlas,
|
|
/// and how it should be positioned when placed.
|
|
///
|
|
/// Used in [`GlyphAtlasInfo`] and [`FontAtlas`](crate::FontAtlas).
|
|
#[derive(Debug, Clone, Copy, Reflect)]
|
|
pub struct GlyphAtlasLocation {
|
|
/// The index of the glyph in the atlas
|
|
pub glyph_index: usize,
|
|
/// The required offset (relative positioning) when placed
|
|
pub offset: IVec2,
|
|
}
|