Newer
Older
use bevy::{prelude::*, window::PrimaryWindow};
#[derive(Resource)]
struct TextChangeTimer(pub Timer);
fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) {
commands.spawn(Camera2dBundle::default());
let attrs =
AttrsOwned::new(Attrs::new().color(bevy_color_to_cosmic(Color::rgb(0.27, 0.27, 0.27))));
let primary_window = windows.single();
let editor = commands
text_position: CosmicTextPosition::default(),
fill_color: FillColor::default(),
background_image: CosmicBackground::default(),
attrs: CosmicAttrs(attrs.clone()),
metrics: CosmicMetrics {
font_size: 16.,
line_height: 16.,
scale_factor: primary_window.scale_factor() as f32,
},
max_chars: CosmicMaxChars(15),
max_lines: CosmicMaxLines(1),
text_setter: CosmicText::OneStyle("BANANA IS THE CODEWORD!".into()),
// CosmicEdit draws to this spritebundle
sprite_bundle: SpriteBundle {
sprite: Sprite {
// when using another target like a UI element, this is overridden
custom_size: Some(Vec2::ONE * 128.0),
..default()
},
// this is the default behaviour for targeting UI elements.
// If wanting a sprite, define your own SpriteBundle and
// leave the visibility on. See examples/basic_sprite.rs
visibility: Visibility::Hidden,
..default()
},
// Computed fields
padding: Default::default(),
widget_size: Default::default(),
.insert(CosmicEditPlaceholderBundle {
text_setter: PlaceholderText(CosmicText::OneStyle("Placeholder".into())),
attrs: PlaceholderAttrs(AttrsOwned::new(
Attrs::new().color(CosmicColor::rgb(88, 88, 88)),
)),
})
.id();
commands
.spawn(ButtonBundle {
border_color: Color::LIME_GREEN.into(),
style: Style {
// Size and position of text box
border: UiRect::all(Val::Px(4.)),
width: Val::Percent(20.),
height: Val::Px(50.),
left: Val::Percent(40.),
top: Val::Px(100.),
commands.insert_resource(TextChangeTimer(Timer::from_seconds(
1.,
TimerMode::Repeating,
)));
}
pub fn bevy_color_to_cosmic(color: bevy::prelude::Color) -> CosmicColor {
(color.r() * 255.) as u8,
(color.g() * 255.) as u8,
(color.b() * 255.) as u8,
(color.a() * 255.) as u8,
)
}
// Test for update_buffer_text
fn text_swapper(
mut timer: ResMut<TextChangeTimer>,
time: Res<Time>,
mut cosmic_q: Query<&mut CosmicText>,
mut count: Local<usize>,
) {
timer.0.tick(time.delta());
if !timer.0.just_finished() {
return;
}
*count += 1;
for mut text in cosmic_q.iter_mut() {
text.set_if_neq(CosmicText::OneStyle(format!("TIMER {}", *count)));
}
println!("X OFFSET: {}", get_x_offset_center(50., editor.0.buffer()));