Skip to content

Commit

Permalink
Reflect and register types
Browse files Browse the repository at this point in the history
  • Loading branch information
Davier committed Nov 25, 2021
1 parent c8c02cc commit cc0d79c
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 27 deletions.
7 changes: 2 additions & 5 deletions examples/2d/text2d_pipelined.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down
6 changes: 6 additions & 0 deletions pipelined/bevy_render2/src/camera/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ impl Plugin for CameraPlugin {
app.register_type::<Camera>()
.register_type::<Visibility>()
.register_type::<ComputedVisibility>()
.register_type::<OrthographicProjection>()
.register_type::<PerspectiveProjection>()
.register_type::<VisibleEntities>()
.register_type::<WindowOrigin>()
.register_type::<ScalingMode>()
.register_type::<DepthCalculation>()
.register_type::<Aabb>()
.insert_resource(active_cameras)
.add_system_to_stage(CoreStage::PostUpdate, crate::camera::active_cameras_system)
Expand Down
4 changes: 3 additions & 1 deletion pipelined/bevy_render2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub use once_cell;

use crate::{
camera::CameraPlugin,
color::Color,
mesh::MeshPlugin,
render_graph::RenderGraph,
render_resource::{RenderPipelineCache, Shader, ShaderLoader},
Expand Down Expand Up @@ -128,7 +129,8 @@ impl Plugin for RenderPlugin {
.insert_resource(queue.clone())
.add_asset::<Shader>()
.init_asset_loader::<ShaderLoader>()
.init_resource::<ScratchRenderWorld>();
.init_resource::<ScratchRenderWorld>()
.register_type::<Color>();
let render_pipeline_cache = RenderPipelineCache::new(device.clone());
let asset_server = app.world.get_resource::<AssetServer>().unwrap().clone();

Expand Down
1 change: 1 addition & 0 deletions pipelined/bevy_text2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
4 changes: 2 additions & 2 deletions pipelined/bevy_text2/src/glyph_brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 8 additions & 3 deletions pipelined/bevy_text2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -40,6 +41,10 @@ impl Plugin for TextPlugin {
fn build(&self, app: &mut App) {
app.add_asset::<Font>()
.add_asset::<FontAtlasSet>()
// TODO: uncomment when #2215 is fixed
// .register_type::<Text>()
.register_type::<VerticalAlign>()
.register_type::<HorizontalAlign>()
.init_asset_loader::<FontLoader>()
.insert_resource(DefaultTextPipeline::default())
.add_system_to_stage(CoreStage::PostUpdate, text2d_system);
Expand Down
65 changes: 58 additions & 7 deletions pipelined/bevy_text2/src/text.rs
Original file line number Diff line number Diff line change
@@ -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<TextSection>,
pub alignment: TextAlignment,
Expand Down Expand Up @@ -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,
Expand All @@ -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.<br/>
/// Bounds start from the render position and advance rightwards.
Left,
/// Leftmost & rightmost characters are equidistant to the render position.<br/>
/// Bounds start from the render position and advance equally left & right.
Center,
/// Rightmost character is immetiately to the left of the render position.<br/>
/// Bounds start from the render position and advance leftwards.
Right,
}

impl From<HorizontalAlign> 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<VerticalAlign> 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<Font>,
pub font_size: f32,
Expand All @@ -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,
}
6 changes: 4 additions & 2 deletions pipelined/bevy_text2/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pipelined/bevy_ui2/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 5 additions & 0 deletions pipelined/bevy_ui2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -55,6 +56,10 @@ impl Plugin for UiPlugin {
.register_type::<Image>()
.register_type::<JustifyContent>()
.register_type::<Node>()
// NOTE: used by Style::aspect_ratio
.register_type::<Option<f32>>()
// NOTE: used by Image
.register_type::<Option<Handle<bevy_render2::texture::Image>>>()
.register_type::<PositionType>()
.register_type::<Size<f32>>()
.register_type::<Size<Val>>()
Expand Down
2 changes: 1 addition & 1 deletion pipelined/bevy_ui2/src/widget/button.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
9 changes: 5 additions & 4 deletions pipelined/bevy_ui2/src/widget/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit cc0d79c

Please sign in to comment.