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!
§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
bevy | bevy_cosmic_edit |
---|---|
0.15.0 | 0.26 - latest |
0.14.0 | 0.21 - 0.25 |
0.13.0 | 0.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 addCosmicPrimaryCamera
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§
- Cosmic
Background Color - Color to be used as a buffer’s background
- Cosmic
Background Image - Image to be used as a buffer’s background
- Cosmic
Edit Buffer - Component wrapper for
Buffer
- Cosmic
Edit Plugin - Plugin struct that adds systems and initializes resources related to cosmic edit functionality.
- Cosmic
Editor - Wrapper component for an
Editor
with a few helpful values for cursor blinking - Cosmic
Font Config - Resource struct that holds configuration options for cosmic fonts.
- Cosmic
Font System - Holds the font system used internally by
cosmic_text
- Cosmic
Primary Camera - Attach to primary camera, and enable the
multicam
feature to use multiple cameras. Will panic if no Camera’s without this component exist and themulticam
feature is enabled. - Cosmic
Text Changed - Text change events
- Cursor
Color - Color to be used for the text cursor.
Defaults to
Color::BLACK
- Cursor
Plugin Disabled - Unit resource whose existence in the world disables the cursor plugin systems.
- Default
Attrs - Default text attributes to be used on a
CosmicEditBuffer
- Focused
Widget - Resource struct that keeps track of the currently active editor entity.
- Hover
Cursor - What cursor icon to show when hovering over a widget
- Input
Set - System set for mouse and keyboard input events. Runs in
PreUpdate
andUpdate
- 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 - Read
Only - Tag component to disable writing to a
CosmicEditBuffer
- Selected
Text Color - Color to be used for the selected text
- Selection
Color - Color to be used as the selected text background.
Defaults to [
Color::GRAY
] - Text
Hover In - For use with custom cursor control
- Text
Hover Out - For use with custom cursor control Event is emitted when cursor leaves a text widget
- User
Select None - Tag component to disable user selection
Like CSS
user-select: none
https://developer.mozilla.org/en-US/docs/Web/CSS/user-select
Enums§
- Cosmic
Text Align - Enum representing the text alignment in a cosmic
Buffer
. Defaults toCosmicTextAlign::Center
- Cosmic
Wrap - Enum representing text wrapping in a cosmic
Buffer
- Scroll
Enabled - Should
CosmicEditBuffer
respond to scroll events?