Skip to content
Snippets Groups Projects
basic_sprite.rs 1.98 KiB
Newer Older
sam edelsten's avatar
sam edelsten committed
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_cosmic_edit::{focus::FocusedWidget, *};
sam edelsten's avatar
sam edelsten committed
use util::{change_active_editor_sprite, deselect_editor_on_esc, print_editor_text};
StaffEngineer's avatar
StaffEngineer committed

sam edelsten's avatar
sam edelsten committed
fn setup(
    mut commands: Commands,
    windows: Query<&Window, With<PrimaryWindow>>,
    mut font_system: ResMut<CosmicFontSystem>,
) {
StaffEngineer's avatar
StaffEngineer committed
    let primary_window = windows.single();
    let camera_bundle = Camera2dBundle {
sam edelsten's avatar
sam edelsten committed
        camera: Camera {
StaffEngineer's avatar
StaffEngineer committed
            clear_color: ClearColorConfig::Custom(Color::WHITE),
sam edelsten's avatar
sam edelsten committed
            ..default()
StaffEngineer's avatar
StaffEngineer committed
        },
        ..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 cosmic_edit = (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",
            attrs,
        ),
        sprite_bundle: SpriteBundle {
            sprite: Sprite {
                custom_size: Some(Vec2::new(primary_window.width(), primary_window.height())),
                ..default()
            },
            ..default()
StaffEngineer's avatar
StaffEngineer committed
        },
        ..default()
    },);

    let cosmic_edit = commands.spawn(cosmic_edit).id();

sam edelsten's avatar
sam edelsten committed
    commands.insert_resource(FocusedWidget(Some(cosmic_edit)));
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,
            (
                print_editor_text,
                change_active_editor_sprite,
                deselect_editor_on_esc,
            ),
        )
StaffEngineer's avatar
StaffEngineer committed
        .run();
}