From cc0d79c402dd999c931f27efcf4c64628274862a Mon Sep 17 00:00:00 2001 From: Brice DAVIER Date: Thu, 25 Nov 2021 21:24:03 +0100 Subject: [PATCH] Reflect and register types --- examples/2d/text2d_pipelined.rs | 7 +-- pipelined/bevy_render2/src/camera/mod.rs | 6 +++ pipelined/bevy_render2/src/lib.rs | 4 +- pipelined/bevy_text2/Cargo.toml | 1 + pipelined/bevy_text2/src/glyph_brush.rs | 4 +- pipelined/bevy_text2/src/lib.rs | 11 ++-- pipelined/bevy_text2/src/text.rs | 65 +++++++++++++++++++++--- pipelined/bevy_text2/src/text2d.rs | 6 ++- pipelined/bevy_ui2/src/focus.rs | 4 +- pipelined/bevy_ui2/src/lib.rs | 5 ++ pipelined/bevy_ui2/src/widget/button.rs | 2 +- pipelined/bevy_ui2/src/widget/image.rs | 9 ++-- 12 files changed, 97 insertions(+), 27 deletions(-) diff --git a/examples/2d/text2d_pipelined.rs b/examples/2d/text2d_pipelined.rs index ad7f8b8fdeafa3..dc6ffdca3165c4 100644 --- a/examples/2d/text2d_pipelined.rs +++ b/examples/2d/text2d_pipelined.rs @@ -1,12 +1,9 @@ use bevy::{ core::Time, math::{Quat, Vec3}, - prelude::{ - App, AssetServer, Commands, Component, HorizontalAlign, Query, Res, Transform, - VerticalAlign, With, - }, + prelude::{App, AssetServer, Commands, Component, Query, Res, Transform, With}, render2::{camera::OrthographicCameraBundle, color::Color}, - text2::{Text, Text2dBundle, TextAlignment, TextStyle}, + text2::{HorizontalAlign, Text, Text2dBundle, TextAlignment, TextStyle, VerticalAlign}, PipelinedDefaultPlugins, }; diff --git a/pipelined/bevy_render2/src/camera/mod.rs b/pipelined/bevy_render2/src/camera/mod.rs index 4e2c146ea76fe7..d775c03cff3eaa 100644 --- a/pipelined/bevy_render2/src/camera/mod.rs +++ b/pipelined/bevy_render2/src/camera/mod.rs @@ -36,6 +36,12 @@ impl Plugin for CameraPlugin { app.register_type::() .register_type::() .register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::() .register_type::() .insert_resource(active_cameras) .add_system_to_stage(CoreStage::PostUpdate, crate::camera::active_cameras_system) diff --git a/pipelined/bevy_render2/src/lib.rs b/pipelined/bevy_render2/src/lib.rs index 062812f4a7d22f..494d453fd4d45c 100644 --- a/pipelined/bevy_render2/src/lib.rs +++ b/pipelined/bevy_render2/src/lib.rs @@ -15,6 +15,7 @@ pub use once_cell; use crate::{ camera::CameraPlugin, + color::Color, mesh::MeshPlugin, render_graph::RenderGraph, render_resource::{RenderPipelineCache, Shader, ShaderLoader}, @@ -128,7 +129,8 @@ impl Plugin for RenderPlugin { .insert_resource(queue.clone()) .add_asset::() .init_asset_loader::() - .init_resource::(); + .init_resource::() + .register_type::(); let render_pipeline_cache = RenderPipelineCache::new(device.clone()); let asset_server = app.world.get_resource::().unwrap().clone(); diff --git a/pipelined/bevy_text2/Cargo.toml b/pipelined/bevy_text2/Cargo.toml index f7152821e8e8b1..1a3fc09eee9d1b 100644 --- a/pipelined/bevy_text2/Cargo.toml +++ b/pipelined/bevy_text2/Cargo.toml @@ -30,3 +30,4 @@ anyhow = "1.0.4" ab_glyph = "0.2.6" glyph_brush_layout = "0.2.1" thiserror = "1.0" +serde = {version = "1", features = ["derive"]} \ No newline at end of file diff --git a/pipelined/bevy_text2/src/glyph_brush.rs b/pipelined/bevy_text2/src/glyph_brush.rs index 5b144cc64a5e4c..f3cf9a99d80e81 100644 --- a/pipelined/bevy_text2/src/glyph_brush.rs +++ b/pipelined/bevy_text2/src/glyph_brush.rs @@ -37,8 +37,8 @@ impl GlyphBrush { ..Default::default() }; let section_glyphs = Layout::default() - .h_align(text_alignment.horizontal) - .v_align(text_alignment.vertical) + .h_align(text_alignment.horizontal.into()) + .v_align(text_alignment.vertical.into()) .calculate_glyphs(&self.fonts, &geom, sections); Ok(section_glyphs) } diff --git a/pipelined/bevy_text2/src/lib.rs b/pipelined/bevy_text2/src/lib.rs index 9efabeebd4ea53..5f6feb234b9b3c 100644 --- a/pipelined/bevy_text2/src/lib.rs +++ b/pipelined/bevy_text2/src/lib.rs @@ -20,9 +20,10 @@ pub use text2d::*; pub mod prelude { #[doc(hidden)] - pub use crate::{Font, Text, Text2dBundle, TextAlignment, TextError, TextSection, TextStyle}; - #[doc(hidden)] - pub use glyph_brush_layout::{HorizontalAlign, VerticalAlign}; + pub use crate::{ + Font, HorizontalAlign, Text, Text2dBundle, TextAlignment, TextError, TextSection, + TextStyle, VerticalAlign, + }; } use bevy_app::prelude::*; @@ -40,6 +41,10 @@ impl Plugin for TextPlugin { fn build(&self, app: &mut App) { app.add_asset::() .add_asset::() + // TODO: uncomment when #2215 is fixed + // .register_type::() + .register_type::() + .register_type::() .init_asset_loader::() .insert_resource(DefaultTextPipeline::default()) .add_system_to_stage(CoreStage::PostUpdate, text2d_system); diff --git a/pipelined/bevy_text2/src/text.rs b/pipelined/bevy_text2/src/text.rs index 2b2c46cc44f274..d9f32a436d49e1 100644 --- a/pipelined/bevy_text2/src/text.rs +++ b/pipelined/bevy_text2/src/text.rs @@ -1,12 +1,14 @@ use bevy_asset::Handle; -use bevy_ecs::prelude::Component; +use bevy_ecs::{prelude::Component, reflect::ReflectComponent}; use bevy_math::Size; +use bevy_reflect::{Reflect, ReflectDeserialize}; use bevy_render2::color::Color; -use glyph_brush_layout::{HorizontalAlign, VerticalAlign}; +use serde::{Deserialize, Serialize}; use crate::Font; -#[derive(Component, Debug, Default, Clone)] +#[derive(Component, Debug, Default, Clone, Reflect)] +#[reflect(Component)] pub struct Text { pub sections: Vec, pub alignment: TextAlignment, @@ -64,13 +66,13 @@ impl Text { } } -#[derive(Debug, Default, Clone)] +#[derive(Debug, Default, Clone, Reflect)] pub struct TextSection { pub value: String, pub style: TextStyle, } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, Reflect)] pub struct TextAlignment { pub vertical: VerticalAlign, pub horizontal: HorizontalAlign, @@ -85,7 +87,55 @@ impl Default for TextAlignment { } } -#[derive(Clone, Debug)] +/// Describes horizontal alignment preference for positioning & bounds. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)] +#[reflect_value(Serialize, Deserialize)] +pub enum HorizontalAlign { + /// Leftmost character is immediately to the right of the render position.
+ /// Bounds start from the render position and advance rightwards. + Left, + /// Leftmost & rightmost characters are equidistant to the render position.
+ /// Bounds start from the render position and advance equally left & right. + Center, + /// Rightmost character is immetiately to the left of the render position.
+ /// Bounds start from the render position and advance leftwards. + Right, +} + +impl From for glyph_brush_layout::HorizontalAlign { + fn from(val: HorizontalAlign) -> Self { + match val { + HorizontalAlign::Left => glyph_brush_layout::HorizontalAlign::Left, + HorizontalAlign::Center => glyph_brush_layout::HorizontalAlign::Center, + HorizontalAlign::Right => glyph_brush_layout::HorizontalAlign::Right, + } + } +} + +/// Describes vertical alignment preference for positioning & bounds. Currently a placeholder +/// for future functionality. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)] +#[reflect_value(Serialize, Deserialize)] +pub enum VerticalAlign { + /// Characters/bounds start underneath the render position and progress downwards. + Top, + /// Characters/bounds center at the render position and progress outward equally. + Center, + /// Characters/bounds start above the render position and progress upward. + Bottom, +} + +impl From for glyph_brush_layout::VerticalAlign { + fn from(val: VerticalAlign) -> Self { + match val { + VerticalAlign::Top => glyph_brush_layout::VerticalAlign::Top, + VerticalAlign::Center => glyph_brush_layout::VerticalAlign::Center, + VerticalAlign::Bottom => glyph_brush_layout::VerticalAlign::Bottom, + } + } +} + +#[derive(Clone, Debug, Reflect)] pub struct TextStyle { pub font: Handle, pub font_size: f32, @@ -102,7 +152,8 @@ impl Default for TextStyle { } } -#[derive(Component, Default, Copy, Clone, Debug)] +#[derive(Component, Default, Copy, Clone, Debug, Reflect)] +#[reflect(Component)] pub struct Text2dSize { pub size: Size, } diff --git a/pipelined/bevy_text2/src/text2d.rs b/pipelined/bevy_text2/src/text2d.rs index 83eca361897f63..785a98a8e9cb85 100644 --- a/pipelined/bevy_text2/src/text2d.rs +++ b/pipelined/bevy_text2/src/text2d.rs @@ -10,9 +10,11 @@ use bevy_render2::{texture::Image, RenderWorld}; use bevy_sprite2::{ExtractedSprite, ExtractedSprites, TextureAtlas}; use bevy_transform::prelude::{GlobalTransform, Transform}; use bevy_window::Windows; -use glyph_brush_layout::{HorizontalAlign, VerticalAlign}; -use crate::{DefaultTextPipeline, Font, FontAtlasSet, Text, Text2dSize, TextError}; +use crate::{ + DefaultTextPipeline, Font, FontAtlasSet, HorizontalAlign, Text, Text2dSize, TextError, + VerticalAlign, +}; /// The bundle of components needed to draw text in a 2D scene via a 2D `OrthographicCameraBundle`. /// [Example usage.](https://github.com/bevyengine/bevy/blob/latest/examples/2d/text2d.rs) diff --git a/pipelined/bevy_ui2/src/focus.rs b/pipelined/bevy_ui2/src/focus.rs index 232f981071f463..14340f5ed144d0 100644 --- a/pipelined/bevy_ui2/src/focus.rs +++ b/pipelined/bevy_ui2/src/focus.rs @@ -3,14 +3,14 @@ use bevy_core::FloatOrd; use bevy_ecs::{ entity::Entity, prelude::Component, + reflect::ReflectComponent, system::{Local, Query, Res}, - reflect::ReflectComponent }; use bevy_input::{mouse::MouseButton, touch::Touches, Input}; use bevy_reflect::{Reflect, ReflectDeserialize}; use bevy_transform::components::GlobalTransform; use bevy_window::Windows; -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; use smallvec::SmallVec; #[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)] diff --git a/pipelined/bevy_ui2/src/lib.rs b/pipelined/bevy_ui2/src/lib.rs index 7a8a3cfe6fc7c9..e4739c805845c2 100644 --- a/pipelined/bevy_ui2/src/lib.rs +++ b/pipelined/bevy_ui2/src/lib.rs @@ -22,6 +22,7 @@ pub mod prelude { } use bevy_app::prelude::*; +use bevy_asset::Handle; use bevy_ecs::schedule::{ParallelSystemDescriptorCoercion, SystemLabel}; use bevy_input::InputSystem; use bevy_math::{Rect, Size}; @@ -55,6 +56,10 @@ impl Plugin for UiPlugin { .register_type::() .register_type::() .register_type::() + // NOTE: used by Style::aspect_ratio + .register_type::>() + // NOTE: used by Image + .register_type::>>() .register_type::() .register_type::>() .register_type::>() diff --git a/pipelined/bevy_ui2/src/widget/button.rs b/pipelined/bevy_ui2/src/widget/button.rs index d5bb0222072383..bcbcc1c997d9e2 100644 --- a/pipelined/bevy_ui2/src/widget/button.rs +++ b/pipelined/bevy_ui2/src/widget/button.rs @@ -1,6 +1,6 @@ use bevy_ecs::prelude::Component; -use bevy_reflect::Reflect; use bevy_ecs::reflect::ReflectComponent; +use bevy_reflect::Reflect; #[derive(Component, Debug, Default, Clone, Copy, Reflect)] #[reflect(Component)] diff --git a/pipelined/bevy_ui2/src/widget/image.rs b/pipelined/bevy_ui2/src/widget/image.rs index c09a560d6da544..2897c9b4bdb805 100644 --- a/pipelined/bevy_ui2/src/widget/image.rs +++ b/pipelined/bevy_ui2/src/widget/image.rs @@ -3,14 +3,15 @@ use bevy_asset::Assets; use bevy_ecs::{ component::Component, query::With, + reflect::ReflectComponent, system::{Query, Res}, - reflect::ReflectComponent }; use bevy_math::Size; -use bevy_reflect::Reflect; +use bevy_reflect::{Reflect, ReflectDeserialize}; +use serde::{Deserialize, Serialize}; -#[derive(Component, Debug, Clone, Reflect)] -#[reflect(Component)] +#[derive(Component, Debug, Clone, Reflect, Serialize, Deserialize)] +#[reflect_value(Component, Serialize, Deserialize)] pub enum ImageMode { KeepAspect, }