-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Replace the f32 font_size value of TextStyle with an enum value
#9524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| use bevy_asset::Handle; | ||
| use bevy_ecs::{prelude::Component, reflect::ReflectComponent}; | ||
| use bevy_ecs::{prelude::Component, reflect::ReflectComponent, system::Resource}; | ||
| use bevy_reflect::prelude::*; | ||
| use bevy_render::color::Color; | ||
| use bevy_utils::default; | ||
|
|
@@ -174,20 +174,69 @@ pub struct TextStyle { | |
| /// | ||
| /// A new font atlas is generated for every combination of font handle and scaled font size | ||
| /// which can have a strong performance impact. | ||
| pub font_size: f32, | ||
| pub font_size: FontSize, | ||
| pub color: Color, | ||
| } | ||
|
|
||
| impl Default for TextStyle { | ||
| fn default() -> Self { | ||
| Self { | ||
| font: DEFAULT_FONT_HANDLE.typed(), | ||
| font_size: 12.0, | ||
| font_size: FontSize::Default, | ||
| color: Color::WHITE, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// The default font size. | ||
| /// Used when the size field of TextStyle is set to Default. | ||
| #[derive(Resource, Clone, Copy, Debug)] | ||
| pub enum DefaultFontSize { | ||
| /// Default font size in logical pixels | ||
| Px(f32), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there be any notes here about reasonable values? e.g., negative values?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's implicit that negative values would be nonsense but we could add some sort of error message. In main atm if |
||
| /// Default font size expressed as a percentage of the height of the viewport. | ||
| Vh(f32), | ||
| } | ||
|
|
||
| impl Default for DefaultFontSize { | ||
| fn default() -> Self { | ||
| DefaultFontSize::Px(12.) | ||
| } | ||
| } | ||
|
|
||
| /// The size of a font | ||
| #[derive(Default, Copy, Clone, Debug, Reflect)] | ||
| pub enum FontSize { | ||
| /// The size of the font in logical pixels | ||
| Px(f32), | ||
| /// The size of the font expressed as a percentage of the height of the viewport. | ||
| Vh(f32), | ||
| /// Use the size from the `DefaultFontSize` resource | ||
| #[default] | ||
| Default, | ||
| } | ||
|
|
||
| impl From<DefaultFontSize> for FontSize { | ||
| fn from(value: DefaultFontSize) -> Self { | ||
| match value { | ||
| DefaultFontSize::Px(px) => FontSize::Px(px), | ||
| DefaultFontSize::Vh(vh) => FontSize::Vh(vh), | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| impl FontSize { | ||
| /// Resolve the `FontSize` to a numeric value in logical pixels. | ||
| pub fn resolve(self, default_font_size: DefaultFontSize, viewport_height: f32, scale_factor: f64) -> f32 { | ||
| match self { | ||
| FontSize::Px(px) => (px as f64 * scale_factor) as f32, | ||
| FontSize::Vh(vh) => viewport_height * vh / 100., | ||
| FontSize::Default => Self::from(default_font_size).resolve(default_font_size, viewport_height, scale_factor), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Determines how lines will be broken when preventing text from running out of bounds. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)] | ||
| #[reflect(Serialize, Deserialize)] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this instead be a new-type over
FontSize, with error handling to warn about a recursive definition? It would remove a few lines worth of repetition.