Crate bevy_cosmic_edit

Source
Expand description

§bevy_cosmic_edit

Multiline text editor using cosmic_text for the bevy game engine!

This bevy plugin provides multiline text editing for bevy apps, thanks to cosmic_text crate! Emoji, ligatures, and other fancy stuff is supported!

bevy_cosmic_edit

§Usage

Warning: This plugin is currently in early development, and its API is subject to change.

use bevy::prelude::*;
use bevy_cosmic_edit::{
    cosmic_text::{Attrs, Family, Metrics},
    prelude::*,
};

fn setup(mut commands: Commands, mut font_system: ResMut<CosmicFontSystem>) {
    let camera_bundle = (
        Camera2d,
        Camera {
            clear_color: ClearColorConfig::Custom(bevy::color::palettes::css::PINK.into()),
            ..default()
        },
    );
    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 = commands
        .spawn((
            TextEdit,
            CosmicEditBuffer::new(&mut font_system, Metrics::new(20., 20.)).with_rich_text(
                &mut font_system,
                vec![("Banana", attrs)],
                attrs,
            ),
            Node {
                width: Val::Percent(100.),
                height: Val::Percent(100.),
                ..default()
            },
        ))
        .id();

    commands.insert_resource(FocusedWidget(Some(cosmic_edit)));
}

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,
    };

    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(CosmicEditPlugin { font_config })
        .add_systems(Startup, setup)
        .add_systems(
            Update,
            (
                print_editor_text,
                change_active_editor_ui,
                deselect_editor_on_esc,
            ),
        )
        .run();
}

Check the examples folder for much more!

Native:

$ cargo r --example font_per_widget

Wasm:

$ cargo install wasm-server-runner
$ RUSTFLAGS=--cfg=web_sys_unstable_apis cargo r --target wasm32-unknown-unknown --example basic_ui

§Compatibility

bevybevy_cosmic_edit
0.15.00.26 - latest
0.14.00.21 - 0.25
0.13.00.16 - 0.20
0.12.*0.15
0.11.*0.8 - 0.14

§Feature flags

  • multicam — Enable to avoid panicing when multiple cameras are used in the same world. Requires you to add CosmicPrimaryCamera marker component to the primary camera

§License

MIT or Apache-2.0

Re-exports§

pub use bevy::text::cosmic_text;

Modules§

prelude
render_targets
Generalizes over render target implementations.
utils

Structs§

CosmicBackgroundColor
Color to be used as a buffer’s background
CosmicBackgroundImage
Image to be used as a buffer’s background
CosmicEditBuffer
Component wrapper for Buffer
CosmicEditPlugin
Plugin struct that adds systems and initializes resources related to cosmic edit functionality.
CosmicEditor
Wrapper component for an Editor with a few helpful values for cursor blinking
CosmicFontConfig
Resource struct that holds configuration options for cosmic fonts.
CosmicFontSystem
Holds the font system used internally by cosmic_text
CosmicPrimaryCamera
Attach to primary camera, and enable the multicam feature to use multiple cameras. Will panic if no Camera’s without this component exist and the multicam feature is enabled.
CosmicTextChanged
Text change events
CursorColor
Color to be used for the text cursor. Defaults to Color::BLACK
CursorPluginDisabled
Unit resource whose existence in the world disables the cursor plugin systems.
DefaultAttrs
Default text attributes to be used on a CosmicEditBuffer
FocusedWidget
Resource struct that keeps track of the currently active editor entity.
HoverCursor
What cursor icon to show when hovering over a widget
InputSet
System set for mouse and keyboard input events. Runs in PreUpdate and Update
MaxChars
Maximum number of characters allowed in a buffer
MaxLines
Maximum number of lines allowed in a buffer
Password
Component to be added to an entity with a CosmicEditBuffer to block contents with a password blocker glyph
Placeholder
Component to be added to an entity with a CosmicEditBuffer add placeholder text
ReadOnly
Tag component to disable writing to a CosmicEditBuffer
SelectedTextColor
Color to be used for the selected text
SelectionColor
Color to be used as the selected text background. Defaults to [Color::GRAY]
TextHoverIn
For use with custom cursor control
TextHoverOut
For use with custom cursor control Event is emitted when cursor leaves a text widget
UserSelectNone
Tag component to disable user selection Like CSS user-select: none https://developer.mozilla.org/en-US/docs/Web/CSS/user-select

Enums§

CosmicTextAlign
Enum representing the text alignment in a cosmic Buffer. Defaults to CosmicTextAlign::Center
CosmicWrap
Enum representing text wrapping in a cosmic Buffer
ScrollEnabled
Should CosmicEditBuffer respond to scroll events?