Newer
Older
use bevy::{
prelude::*,
window::{PrimaryWindow, WindowResolution},
};
#[derive(Component)]
struct SubmitButton;
#[derive(Component)]
struct UsernameTag;
#[derive(Component)]
struct PasswordTag;
#[derive(Component)]
struct DisplayTag;
fn setup(mut commands: Commands, window: Query<&Window, With<PrimaryWindow>>) {
let window = window.single();
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
let login_editor = commands
.spawn(CosmicEditBundle {
max_lines: CosmicMaxLines(1),
metrics: CosmicMetrics {
scale_factor: window.scale_factor() as f32,
..default()
},
sprite_bundle: SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(300.0, 50.0)),
..default()
},
visibility: Visibility::Hidden,
..default()
},
..default()
})
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Username".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::rgb_u8(128, 128, 128))),
)),
})
.insert(UsernameTag)
.id();
let password_editor = commands
.spawn(CosmicEditBundle {
max_lines: CosmicMaxLines(1),
metrics: CosmicMetrics {
scale_factor: window.scale_factor() as f32,
..default()
},
..default()
})
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Password".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::rgb_u8(128, 128, 128))),
)),
})
.insert(PasswordTag)
.insert(PasswordInput::default())
.id();
let submit_editor = commands
.spawn(CosmicEditBundle {
max_lines: CosmicMaxLines(1),
metrics: CosmicMetrics {
font_size: 25.0,
line_height: 25.0,
scale_factor: window.scale_factor() as f32,
..default()
},
attrs: CosmicAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::WHITE)),
)),
text_setter: CosmicText::OneStyle("Submit".into()),
fill_color: FillColor(Color::GREEN),
..default()
})
.insert(ReadOnly)
.id();
let display_editor = commands
.spawn(CosmicEditBundle {
metrics: CosmicMetrics {
scale_factor: window.scale_factor() as f32,
..default()
},
..default()
})
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Output".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(bevy_color_to_cosmic(Color::rgb_u8(128, 128, 128))),
)),
})
.insert((ReadOnly, DisplayTag))
.id();
commands.insert_resource(Focus(Some(login_editor)));
// Spawn UI
commands
.spawn(NodeBundle {
style: Style {
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
padding: UiRect::all(Val::Px(15.0)),
width: Val::Px(330.0),
..default()
},
..default()
})
.with_children(|root| {
style: Style {
// Size and position of text box
width: Val::Px(300.),
height: Val::Px(50.),
margin: UiRect::all(Val::Px(15.0)),
..default()
},
background_color: BackgroundColor(Color::WHITE),
..default()
})
style: Style {
// Size and position of text box
width: Val::Px(300.),
height: Val::Px(50.),
margin: UiRect::all(Val::Px(15.0)),
..default()
},
background_color: BackgroundColor(Color::WHITE),
..default()
})
.insert(CosmicSource(password_editor));
root.spawn(ButtonBundle {
style: Style {
// Size and position of text box
height: Val::Px(50.),
margin: UiRect::all(Val::Px(15.0)),
..default()
},
background_color: BackgroundColor(Color::WHITE),
style: Style {
// Size and position of text box
width: Val::Px(300.),
margin: UiRect::all(Val::Px(15.0)),
..default()
},
background_color: BackgroundColor(Color::WHITE),
..default()
})
});
}
fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor {
cosmic_text::Color::rgba(
(color.r() * 255.) as u8,
(color.g() * 255.) as u8,
(color.b() * 255.) as u8,
(color.a() * 255.) as u8,
)
}
fn change_active_editor_ui(
mut commands: Commands,
mut interaction_query: Query<
(&Interaction, &CosmicSource),
(Changed<Interaction>, Without<ReadOnly>),
for (interaction, source) in interaction_query.iter_mut() {
commands.insert_resource(Focus(Some(source.0)));
}
}
}
fn print_changed_input(mut evr_type: EventReader<CosmicTextChanged>) {
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
fn submit_button(
button_q: Query<&Interaction, With<SubmitButton>>,
username_q: Query<
&CosmicEditor,
(With<UsernameTag>, Without<PasswordTag>, Without<DisplayTag>),
>,
password_q: Query<
&CosmicEditor,
(With<PasswordTag>, Without<UsernameTag>, Without<DisplayTag>),
>,
mut display_q: Query<
(&mut CosmicEditor, &CosmicAttrs),
(With<DisplayTag>, Without<UsernameTag>, Without<PasswordTag>),
>,
mut font_system: ResMut<CosmicFontSystem>,
mut window: Query<&mut Window, With<PrimaryWindow>>,
) {
for interaction in button_q.iter() {
match interaction {
Interaction::None => {}
Interaction::Pressed => {
let u = username_q.single();
let p = password_q.single();
let (mut d, attrs) = display_q.single_mut();
let text = format!(
"Submitted!\nUsername: {}\nPassword: {}\n",
u.get_text(),
p.get_text()
);
d.set_text(
CosmicText::OneStyle(text),
attrs.0.clone(),
&mut font_system.0,
);
}
Interaction::Hovered => {
window.single_mut().cursor.icon = CursorIcon::Hand;
}
}
}
}
fn main() {
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
resolution: WindowResolution::new(330., 480.),
..default()
}),
..default()
}))
.add_plugins(CosmicEditPlugin {
change_cursor: CursorConfig::Default,
..default()
})
.add_systems(Startup, setup)
.add_systems(Update, change_active_editor_ui)