Skip to content
Snippets Groups Projects
basic_ui.rs 2.74 KiB
Newer Older
use bevy::{core_pipeline::clear_color::ClearColorConfig, prelude::*, window::PrimaryWindow};
use bevy_cosmic_edit::*;
StaffEngineer's avatar
StaffEngineer committed

fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) {
StaffEngineer's avatar
StaffEngineer committed
    let primary_window = windows.single();
    let camera_bundle = Camera2dBundle {
        camera_2d: Camera2d {
            clear_color: ClearColorConfig::Custom(Color::WHITE),
        },
        ..default()
StaffEngineer's avatar
StaffEngineer committed
    };
    commands.spawn(camera_bundle);

    let mut attrs = Attrs::new();
    attrs = attrs.family(Family::Name("Victor Mono"));
    attrs = attrs.color(CosmicColor::rgb(0x94, 0x00, 0xD3));

    let scale_factor = primary_window.scale_factor() as f32;

    let cosmic_edit = commands
        .spawn(CosmicEditBundle {
            metrics: CosmicMetrics {
                font_size: 14.,
                line_height: 18.,
                scale_factor,
            },
            text_position: CosmicTextPosition::Center,
            attrs: CosmicAttrs(AttrsOwned::new(attrs)),
            text_setter: CosmicText::OneStyle("😀😀😀 x => y".to_string()),
            ..default()
        })
        .id();

    commands
        .spawn(
            // Use buttonbundle for layout
            // Includes Interaction and UiImage which are used by the plugin.
            ButtonBundle {
                style: Style {
                    width: Val::Percent(100.),
                    height: Val::Percent(100.),
                    ..default()
                },
                // Needs to be set to prevent a bug where nothing is displayed
                background_color: Color::WHITE.into(),
        )
        // point editor at this entity.
        // Plugin looks for UiImage and sets it's
        // texture to the editor's rendered image
        .insert(CosmicSource(cosmic_edit));
StaffEngineer's avatar
StaffEngineer committed
    commands.insert_resource(Focus(Some(cosmic_edit)));
StaffEngineer's avatar
StaffEngineer committed
}

fn print_text(
    text_inputs_q: Query<&CosmicEditor, With<CosmicEditor>>,
    mut previous_value: Local<String>,
) {
    for text_input in text_inputs_q.iter() {
        let current_text = text_input.get_text();
        if current_text == *previous_value {
            return;
        }
        *previous_value = current_text.clone();
        info!("Widget text: {}", current_text);
    }
}

StaffEngineer's avatar
StaffEngineer committed
fn main() {
    let font_bytes: &[u8] = include_bytes!("../assets/fonts/VictorMono-Regular.ttf");
    let font_config = CosmicFontConfig {
        fonts_dir_path: None,
        font_bytes: Some(vec![font_bytes]),
        load_system_fonts: true,
    };

StaffEngineer's avatar
StaffEngineer committed
    App::new()
        .add_plugins(DefaultPlugins)
sam's avatar
sam committed
        .add_plugins(CosmicEditPlugin {
            font_config,
            ..default()
        })
StaffEngineer's avatar
StaffEngineer committed
        .add_systems(Startup, setup)
        .add_systems(Update, print_text)
StaffEngineer's avatar
StaffEngineer committed
        .run();
}