Skip to content
Snippets Groups Projects
readonly.rs 2 KiB
Newer Older
sam edelsten's avatar
sam edelsten committed
use bevy::prelude::*;
use bevy_cosmic_edit::*;
StaffEngineer's avatar
StaffEngineer committed

sam edelsten's avatar
sam edelsten committed
fn setup(mut commands: Commands, mut font_system: ResMut<CosmicFontSystem>) {
StaffEngineer's avatar
StaffEngineer committed
    commands.spawn(Camera2dBundle::default());
    let root = commands
        .spawn(NodeBundle {
            style: Style {
                display: Display::Flex,
                width: Val::Percent(100.),
                height: Val::Percent(100.),
                ..default()
            },
            ..default()
        })
        .id();

    let mut attrs = Attrs::new();
    attrs = attrs.family(Family::Name("Victor Mono"));
    attrs = attrs.color(bevy_color_to_cosmic(Color::PURPLE));
    // spawn editor
    let cosmic_edit = commands
        .spawn(CosmicEditBundle {
sam edelsten's avatar
sam edelsten committed
            buffer: CosmicBuffer::new(&mut font_system, Metrics::new(14., 18.)).with_text(
                &mut font_system,
                "😀😀😀 x => y\nRead only widget",
                attrs,
            ),
            ..default()
        })
        .insert(ReadOnly)
        .id();
    // Spawn the ButtonBundle as a child of root
    commands.entity(root).with_children(|parent| {
        parent
            .spawn(ButtonBundle {
                style: Style {
                    width: Val::Percent(100.),
                    height: Val::Percent(100.),
                    ..default()
                },
                background_color: BackgroundColor(Color::WHITE),
                ..default()
            })
            // add cosmic source
            .insert(CosmicSource(cosmic_edit));
StaffEngineer's avatar
StaffEngineer committed
    });
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)
sam edelsten's avatar
sam edelsten committed
        .add_systems(Update, change_active_editor_ui)
StaffEngineer's avatar
StaffEngineer committed
        .run();
}