text_wrap_debug/
text_wrap_debug.rs1use argh::FromArgs;
4use bevy::{prelude::*, text::LineBreak, window::WindowResolution, winit::WinitSettings};
5
6#[derive(FromArgs, Resource)]
7struct Args {
9 #[argh(option)]
10 scale_factor: Option<f32>,
12
13 #[argh(option, default = "1.")]
14 ui_scale: f32,
16}
17
18fn main() {
19 #[cfg(not(target_arch = "wasm32"))]
21 let args: Args = argh::from_env();
22 #[cfg(target_arch = "wasm32")]
23 let args = Args::from_args(&[], &[]).unwrap();
24
25 let window = if let Some(scale_factor) = args.scale_factor {
26 Window {
27 resolution: WindowResolution::default().with_scale_factor_override(scale_factor),
28 ..Default::default()
29 }
30 } else {
31 Window::default()
32 };
33
34 App::new()
35 .add_plugins(DefaultPlugins.set(WindowPlugin {
36 primary_window: Some(window),
37 ..Default::default()
38 }))
39 .insert_resource(WinitSettings::desktop_app())
40 .insert_resource(UiScale(args.ui_scale))
41 .add_systems(Startup, spawn)
42 .run();
43}
44
45fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
46 commands.spawn(Camera2d);
47
48 let text_font = TextFont {
49 font: asset_server.load("fonts/FiraSans-Bold.ttf"),
50 font_size: 12.0,
51 ..default()
52 };
53
54 let root = commands
55 .spawn((
56 Node {
57 width: Val::Percent(100.),
58 height: Val::Percent(100.),
59 flex_direction: FlexDirection::Column,
60 ..default()
61 },
62 BackgroundColor(Color::BLACK),
63 ))
64 .id();
65
66 for linebreak in [
67 LineBreak::AnyCharacter,
68 LineBreak::WordBoundary,
69 LineBreak::WordOrCharacter,
70 LineBreak::NoWrap,
71 ] {
72 let row_id = commands
73 .spawn(Node {
74 flex_direction: FlexDirection::Row,
75 justify_content: JustifyContent::SpaceAround,
76 align_items: AlignItems::Center,
77 width: Val::Percent(100.),
78 height: Val::Percent(50.),
79 ..default()
80 })
81 .id();
82
83 let justifications = vec![
84 JustifyContent::Center,
85 JustifyContent::FlexStart,
86 JustifyContent::FlexEnd,
87 JustifyContent::SpaceAround,
88 JustifyContent::SpaceBetween,
89 JustifyContent::SpaceEvenly,
90 ];
91
92 for (i, justification) in justifications.into_iter().enumerate() {
93 let c = 0.3 + i as f32 * 0.1;
94 let column_id = commands
95 .spawn((
96 Node {
97 justify_content: justification,
98 flex_direction: FlexDirection::Column,
99 width: Val::Percent(16.),
100 height: Val::Percent(95.),
101 overflow: Overflow::clip_x(),
102 ..default()
103 },
104 BackgroundColor(Color::srgb(0.5, c, 1.0 - c)),
105 ))
106 .id();
107
108 let messages = [
109 format!("JustifyContent::{justification:?}"),
110 format!("LineBreakOn::{linebreak:?}"),
111 "Line 1\nLine 2".to_string(),
112 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas auctor, nunc ac faucibus fringilla.".to_string(),
113 "pneumonoultramicroscopicsilicovolcanoconiosis".to_string()
114 ];
115
116 for (j, message) in messages.into_iter().enumerate() {
117 commands.entity(column_id).with_child((
118 Text(message.clone()),
119 text_font.clone(),
120 TextLayout::new(JustifyText::Left, linebreak),
121 BackgroundColor(Color::srgb(0.8 - j as f32 * 0.2, 0., 0.)),
122 ));
123 }
124 commands.entity(row_id).add_child(column_id);
125 }
126 commands.entity(root).add_child(row_id);
127 }
128}