2d_text_gizmos/
2d_text_gizmos.rs1use bevy::color::palettes::css::{BLUE, GREEN, RED, YELLOW};
4use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
5use bevy::math::Isometry2d;
6use bevy::prelude::*;
7use bevy::window::{PresentMode, WindowResolution};
8use bevy::winit::WinitSettings;
9
10const TEXT_COUNT: usize = 50;
11const START_X: f32 = -800.0;
12const START_Y: f32 = 200.0;
13const X_STEP: f32 = 250.0;
14const Y_STEP: f32 = 50.0;
15
16fn main() {
17 App::new()
18 .insert_resource(WinitSettings::continuous())
19 .add_plugins((
20 DefaultPlugins.set(WindowPlugin {
21 primary_window: Some(Window {
22 present_mode: PresentMode::AutoNoVsync,
23 resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),
24 ..default()
25 }),
26 ..default()
27 }),
28 LogDiagnosticsPlugin::default(),
29 FrameTimeDiagnosticsPlugin::default(),
30 ))
31 .add_systems(Startup, setup)
32 .add_systems(Update, (draw_labels, draw_all_glyphs))
33 .run();
34}
35
36fn setup(mut commands: Commands, mut gizmo_config_store: ResMut<GizmoConfigStore>) {
37 commands.spawn(Camera2d);
38
39 let (config, _) = gizmo_config_store.config_mut::<DefaultGizmoConfigGroup>();
40
41 config.line.width = 1.;
42}
43
44fn draw_labels(mut text_gizmos: Gizmos, diagnostic: Res<DiagnosticsStore>) {
45 let colors = [RED, GREEN, BLUE, YELLOW];
46 for i in 0..TEXT_COUNT {
47 let row = i / 5;
48 let col = i % 5;
49 let color = colors[i % 4];
50 text_gizmos.text_2d(
51 Isometry2d {
52 translation: Vec2::new(
53 START_X + col as f32 * X_STEP,
54 START_Y - row as f32 * Y_STEP,
55 ),
56 rotation: Rot2::degrees(2.),
57 },
58 &format!("label {i}"),
59 25.,
60 Vec2::ZERO,
61 color,
62 );
63 }
64
65 if let Some(fps) = diagnostic.get(&FrameTimeDiagnosticsPlugin::FPS)
66 && let Some(fps_smoothed) = fps.smoothed()
67 {
68 text_gizmos.text_2d(
69 Isometry2d::from_translation(Vec2::new(600., START_Y + 150.)),
70 &format!("fps: {:.1}", fps_smoothed),
71 25.,
72 Vec2::ZERO,
73 Color::WHITE,
74 );
75 }
76
77 text_gizmos.text_2d(
78 Isometry2d::from_translation(Vec2::new(-300., START_Y + 200.)),
79 "lxgh",
80 150.,
81 Vec2::ZERO,
82 Color::WHITE,
83 );
84}
85
86const ALL_GLYPHS: &str = " !\"#$%&'()*\n\
87+,-./012345\n\
886789:;<=>?@\n\
89ABCDEFGHIJK\n\
90LMNOPQRSTUV\n\
91WXYZ[\\]^_`a\n\
92bcdefghijkl\n\
93mnopqrstuvw\n\
94xyz{|}~";
95
96fn draw_all_glyphs(mut text_gizmos: Gizmos) {
97 text_gizmos.text_2d(
98 Isometry2d::from_xy(600., 0.),
99 ALL_GLYPHS,
100 30.0,
101 Vec2::ZERO,
102 Color::WHITE,
103 );
104}