Skip to content
Snippets Groups Projects
readonly.rs 2.37 KiB
Newer Older
StaffEngineer's avatar
StaffEngineer committed
use bevy::{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();
    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));
    let cosmic_edit = (
        CosmicEditBundle {
            attrs: CosmicAttrs(AttrsOwned::new(attrs)),
            text_position: CosmicTextPosition::Center,
            metrics: CosmicMetrics {
                font_size: 14.,
                line_height: 18.,
                scale_factor: primary_window.scale_factor() as f32,
            },
            text_setter: CosmicText::OneStyle("😀😀😀 x => y\nRead only widget".to_string()),
            ..default()
        },
        ButtonBundle {
            style: Style {
                width: Val::Percent(100.),
                height: Val::Percent(100.),
            background_color: BackgroundColor(Color::WHITE),
StaffEngineer's avatar
StaffEngineer committed
        },

    let mut id = None;
sam edelsten's avatar
sam edelsten committed
    // Spawn the CosmicEditUiBundle as a child of root
    commands.entity(root).with_children(|parent| {
        id = Some(parent.spawn(cosmic_edit).insert(ReadOnly).id());
StaffEngineer's avatar
StaffEngineer committed
    });
StaffEngineer's avatar
StaffEngineer committed
    commands.insert_resource(Focus(id));
StaffEngineer's avatar
StaffEngineer committed
}

pub 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,
    )
}

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)
        .run();
}