 4ba09f3dd9
			
		
	
	
		4ba09f3dd9
		
			
		
	
	
	
	
		
			
			# Objective - Allow users to customize the line height of text. - Implements #16085 ## Solution - Add a `line_height` field to `TextFont` to feed into `cosmic_text`'s `Metrics`. ## Testing - Tested on my own game, and worked exactly as I wanted. - My game is only 2D, so I only tested `Text2d`. `Text` still needs tested, but I imagine it'll work fine. - An example is available [here](https://code.cartoon-aa.xyz/Cyborus/custom-line-height-example) --- ## Showcase <details> <summary>Click to view showcase</summary> With font: ```rust TextFont { font: /* unimportant */, font_size: 16.0, line_height: None, ..default() } ```  With font: ```rust TextFont { font: /* unimportant */, font_size: 16.0, line_height: Some(16.0), ..default() } ```  </details> ## Migration Guide `TextFont` now has a `line_height` field. Any instantiation of `TextFont` that doesn't have `..default()` will need to add this field.
		
			
				
	
	
		
			83 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			83 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! Showcase how to use and configure FPS overlay.
 | |
| 
 | |
| use bevy::{
 | |
|     dev_tools::fps_overlay::{FpsOverlayConfig, FpsOverlayPlugin},
 | |
|     prelude::*,
 | |
|     text::FontSmoothing,
 | |
| };
 | |
| 
 | |
| struct OverlayColor;
 | |
| 
 | |
| impl OverlayColor {
 | |
|     const RED: Color = Color::srgb(1.0, 0.0, 0.0);
 | |
|     const GREEN: Color = Color::srgb(0.0, 1.0, 0.0);
 | |
| }
 | |
| 
 | |
| fn main() {
 | |
|     App::new()
 | |
|         .add_plugins((
 | |
|             DefaultPlugins,
 | |
|             FpsOverlayPlugin {
 | |
|                 config: FpsOverlayConfig {
 | |
|                     text_config: TextFont {
 | |
|                         // Here we define size of our overlay
 | |
|                         font_size: 42.0,
 | |
|                         // If we want, we can use a custom font
 | |
|                         font: default(),
 | |
|                         // We could also disable font smoothing,
 | |
|                         font_smoothing: FontSmoothing::default(),
 | |
|                         ..default()
 | |
|                     },
 | |
|                     // We can also change color of the overlay
 | |
|                     text_color: OverlayColor::GREEN,
 | |
|                     enabled: true,
 | |
|                 },
 | |
|             },
 | |
|         ))
 | |
|         .add_systems(Startup, setup)
 | |
|         .add_systems(Update, customize_config)
 | |
|         .run();
 | |
| }
 | |
| 
 | |
| fn setup(mut commands: Commands) {
 | |
|     // We need to spawn a camera (2d or 3d) to see the overlay
 | |
|     commands.spawn(Camera2d);
 | |
| 
 | |
|     // Instruction text
 | |
| 
 | |
|     commands.spawn((
 | |
|         Text::new(concat!(
 | |
|             "Press 1 to toggle the overlay color.\n",
 | |
|             "Press 2 to decrease the overlay size.\n",
 | |
|             "Press 3 to increase the overlay size.\n",
 | |
|             "Press 4 to toggle the overlay visibility."
 | |
|         )),
 | |
|         Node {
 | |
|             position_type: PositionType::Absolute,
 | |
|             bottom: Val::Px(12.),
 | |
|             left: Val::Px(12.),
 | |
|             ..default()
 | |
|         },
 | |
|     ));
 | |
| }
 | |
| 
 | |
| fn customize_config(input: Res<ButtonInput<KeyCode>>, mut overlay: ResMut<FpsOverlayConfig>) {
 | |
|     if input.just_pressed(KeyCode::Digit1) {
 | |
|         // Changing resource will affect overlay
 | |
|         if overlay.text_color == OverlayColor::GREEN {
 | |
|             overlay.text_color = OverlayColor::RED;
 | |
|         } else {
 | |
|             overlay.text_color = OverlayColor::GREEN;
 | |
|         }
 | |
|     }
 | |
|     if input.just_pressed(KeyCode::Digit2) {
 | |
|         overlay.text_config.font_size -= 2.0;
 | |
|     }
 | |
|     if input.just_pressed(KeyCode::Digit3) {
 | |
|         overlay.text_config.font_size += 2.0;
 | |
|     }
 | |
|     if input.just_pressed(KeyCode::Digit4) {
 | |
|         overlay.enabled = !overlay.enabled;
 | |
|     }
 | |
| }
 |