Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/gizmos/2d_text_gizmos.rs
9396 views
1
//! Example demonstrating text gizmos.
2
3
use bevy::color::palettes::css::{BLUE, GREEN, RED, YELLOW};
4
use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
5
use bevy::math::Isometry2d;
6
use bevy::prelude::*;
7
use bevy::window::{PresentMode, WindowResolution};
8
use bevy::winit::WinitSettings;
9
10
const TEXT_COUNT: usize = 50;
11
const START_X: f32 = -800.0;
12
const START_Y: f32 = 200.0;
13
const X_STEP: f32 = 250.0;
14
const Y_STEP: f32 = 50.0;
15
16
fn 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
36
fn 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
44
fn 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
86
const ALL_GLYPHS: &str = " !\"#$%&'()*\n\
87
+,-./012345\n\
88
6789:;<=>?@\n\
89
ABCDEFGHIJK\n\
90
LMNOPQRSTUV\n\
91
WXYZ[\\]^_`a\n\
92
bcdefghijkl\n\
93
mnopqrstuvw\n\
94
xyz{|}~";
95
96
fn 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
}
105
106