### Build Example for Web Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/README.md Command to build a specific example ('simple') for the WebAssembly target. This is used for testing Bevy Egui in a web environment. ```bash cargo build --target wasm32-unknown-unknown --example simple ``` -------------------------------- ### Run Example Binaries Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/README.md Commands to run the example binaries for Bevy Egui. These examples showcase different functionalities of the library. ```bash cargo run --example simple cargo run --example ui cargo run --example two_windows ``` -------------------------------- ### EguiPlugin Configuration Example Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/configuration.md Example of how to add and configure the EguiPlugin in a Bevy application. This example sets the UI render order and a custom bindless mode array size. ```rust fn main() { App::new() .add_plugins(DefaultPlugins) .add_plugins(EguiPlugin { ui_render_order: UiRenderOrder::BevyUiAboveEgui, // Game UI on top bindless_mode_array_size: std::num::NonZero::new(32), // Larger array ..default() }) .run(); } ``` -------------------------------- ### Basic Egui Plugin Setup Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-plugin.md Demonstrates the basic setup for using the EguiPlugin in a Bevy application. UI systems should be added to the EguiPrimaryContextPass schedule. ```rust use bevy::prelude::*; use bevy_egui::{EguiPlugin, EguiPrimaryContextPass, EguiContexts, egui}; fn main() { App::new() .add_plugins(DefaultPlugins) .add_plugins(EguiPlugin::default()) .add_systems(Startup, setup_camera) .add_systems(EguiPrimaryContextPass, ui_system) .run(); } fn setup_camera(mut commands: Commands) { commands.spawn(Camera2d); } fn ui_system(mut contexts: EguiContexts) -> Result<(), QuerySingleError> { egui::Window::new("Example").show(contexts.ctx_mut()?, |ui| { ui.label("Hello, world!"); }); Ok(()) } ``` -------------------------------- ### Run a Bevy Egui Example Source: https://github.com/vladbat00/bevy_egui/blob/main/README.md Execute a specific example from the bevy_egui crate using Cargo. Replace 'ui' with the desired example name. ```bash cargo run --example ui ``` -------------------------------- ### Minimal Bevy Egui Setup Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/INDEX.md This snippet shows the basic setup required to integrate Bevy Egui into a Bevy application. It includes adding the EguiPlugin and defining startup and UI systems. ```rust app.add_plugins(EguiPlugin::default()) .add_systems(Startup, setup) .add_systems(EguiPrimaryContextPass, ui_system) fn setup(mut commands: Commands) { commands.spawn(Camera2d); } fn ui_system(mut ctx: EguiContexts) -> Result<(), _> { /* UI */ } ``` -------------------------------- ### Minimal Bevy Egui Usage Example Source: https://github.com/vladbat00/bevy_egui/blob/main/README.md A basic Rust example demonstrating how to set up a Bevy application with Egui integration using the multi-pass mode. ```rust use bevy::prelude::*; use bevy_egui::{egui, EguiContexts, EguiPlugin, EguiPrimaryContextPass}; fn main() { App::new() .add_plugins(DefaultPlugins) .add_plugins(EguiPlugin::default()) .add_systems(Startup, setup_camera_system) .add_systems(EguiPrimaryContextPass, ui_example_system) .run(); } fn setup_camera_system(mut commands: Commands) { commands.spawn(Camera2d); } fn ui_example_system(mut contexts: EguiContexts) -> Result { egui::Window::new("Hello").show(contexts.ctx_mut()?, |ui| { ui.label("world"); }); Ok(()) } ``` -------------------------------- ### Example Usage of EguiGlobalSettings Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/configuration.md Demonstrates how to modify global Egui settings at runtime. This example shows disabling auto-context creation, specific input systems, and cursor icon updates. ```rust fn setup_global_config(mut settings: ResMut) { // Disable auto-creation for manual context management settings.auto_create_primary_context = false; // Disable keyboard input systems for performance settings.input_system_settings.run_write_keyboard_input_messages_system = false; // Let game handle cursor icons settings.enable_cursor_icon_updates = false; } ``` -------------------------------- ### Install XCB Dependencies on Debian Source: https://github.com/vladbat00/bevy_egui/blob/main/README.md On Debian-based systems, install the necessary XCB libraries for Egui support using this command. ```bash sudo apt install libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev ``` -------------------------------- ### FocusedNonWindowEguiContext Resource Access Example Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/input-systems.md Resource tracking which non-window context receives keyboard input. This example demonstrates how to access the `FocusedNonWindowEguiContext` to determine where keyboard input is directed. ```rust fn keyboard_system(focused: Option>) { if let Some(focused) = focused { // Keyboard input goes to this context println!("Focused context: {:?}", focused.0); } } ``` -------------------------------- ### Minimal Bevy Egui Dependency Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/getting-started.md Use this configuration for a minimal setup, disabling default features. ```toml # Minimal [dependencies] bevy_egui = { version = "0.40", default-features = false } ``` -------------------------------- ### Custom Context with Manual Pass Control Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/configuration.md Example of spawning a new Egui context with manual control enabled and pointer input capture disabled. This setup requires explicit calls to begin_pass and end_pass. ```rust // Custom context with manual pass control fn setup_manual_context(mut commands: Commands) { commands.spawn(( Camera2d::default(), EguiContext::default(), EguiContextSettings { run_manually: true, // Must call begin_pass/end_pass manually capture_pointer_input: false, // Don't suppress Bevy Picking enable_cursor_icon_updates: false, // Custom cursor handling ..default() }, )); } ``` -------------------------------- ### Performance Optimization Tips Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/output-handling.md Provides code examples for optimizing Egui rendering by disabling cursor updates and IME if not needed, and checking if output is empty before processing. ```rust // Disable cursor updates if not needed global.enable_cursor_icon_updates = false; // Disable IME for non-text-input apps global.enable_ime = false; // Check if output is empty before processing if !output.is_empty() { // Process only if there's actual content } ``` -------------------------------- ### Clipboard Integration Example Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/output-handling.md When text is selected and Ctrl+C is pressed in Egui, the text is written to `EguiOutput.platform_output.copied_text`. The `process_output_system` then reads this and calls `clipboard.set_text()`. Requires the `manage_clipboard` feature. ```rust // In Egui, when user presses Ctrl+C with selected text: // The text is written to EguiOutput.platform_output.copied_text // process_output_system reads it and calls clipboard.set_text() ``` -------------------------------- ### Game UI with Egui Configuration Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/configuration.md Configure EguiPlugin for a game UI where Bevy UI is rendered above Egui. This setup allows for full input handling. ```rust .add_plugins(EguiPlugin { ui_render_order: UiRenderOrder::BevyUiAboveEgui, // Bevy UI on top ..default() }) .add_systems(Startup, |mut global: ResMut| { // Full input handling }) ``` -------------------------------- ### Custom Mouse Input System Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/input-systems.md An example of a Bevy system that reads mouse button input events and processes them using the Egui context. Ensure EguiContexts and WindowToEguiContextMap are available. ```rust fn custom_input_system( mut mouse_events: EventReader, mut contexts: EguiContexts, window_map: Res, ) -> Result<(), bevy_ecs::query::QuerySingleError> { for event in mouse_events.read() { let ctx = contexts.ctx_mut()?; // Process custom mouse handling } Ok(()) } ``` -------------------------------- ### HoveredNonWindowEguiContext Resource Example Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/input-systems.md Resource indicating which non-window Egui context is currently hovered. This example shows how to update the hovered context based on pointer position, typically used with bevy_picking for world-space UI. ```rust fn update_hover_system( pointers: Query<&PointerLocation>, mut hovered: ResMut, spatial_query: SpatialQuery, ) { // Update hovered context based on pointer position // Typically used with bevy_picking for world-space UI } ``` -------------------------------- ### Secondary Context with Disabled Input Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/configuration.md Example of setting up a secondary Egui context, such as for split-screen, with input systems disabled. This prevents the context from processing keyboard or pointer input. ```rust // Secondary context for split-screen with disabled input fn setup_secondary_context(mut commands: Commands) { commands.spawn(( Camera3d::default(), EguiContext::default(), EguiContextSettings { input_system_settings: EguiInputSystemSettings { run_write_keyboard_input_messages_system: false, run_write_pointer_button_messages_system: false, ..default() }, ..default() }, )); } ``` -------------------------------- ### Disabling Keyboard Input System Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/input-systems.md Example demonstrating how to disable the keyboard input system by setting a specific flag in EguiGlobalSettings. ```rust fn disable_some_input(mut global: ResMut) { global.input_system_settings.run_write_keyboard_input_messages_system = false; // Keyboard input won't be processed } ``` -------------------------------- ### Accessing and Showing a Window with Primary EguiContext Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-contexts.md Use `ctx_mut` to get a mutable reference to the primary Egui context and display a window. Ensure `EguiContexts` is added as a parameter to your system. ```rust use bevy::prelude::*; use bevy_egui::{EguiContexts, egui}; fn my_system(mut contexts: EguiContexts) -> Result<(), bevy_ecs::query::QuerySingleError> { let ctx = contexts.ctx_mut()?; egui::Window::new("Debug").show(ctx, |ui| { ui.label("FPS: 60"); }); Ok(()) } ``` -------------------------------- ### Processing Mouse Events with EguiContextMessageReader Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/input-systems.md Example of how to use EguiContextMessageReader to process mouse events and identify the target Egui context entity. ```rust fn process_mouse_events( mut reader: EguiContextMessageReader, ) { for (mouse_event, context_entity) in reader.read(|msg| msg.window) { println!("Mouse event for context: {:?}", context_entity); match mouse_event.button { MouseButton::Left => { /* ... */ } _ => {} } } } ``` -------------------------------- ### Create a Simple Egui Window Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/README.md Demonstrates how to create a basic Egui window with a label. This function should be called within a system that has access to `EguiContexts`. ```rust fn ui(mut contexts: EguiContexts) -> Result<(), _> { egui::Window::new("Hello").show(contexts.ctx_mut()?, |ui| { ui.label("World"); }); Ok(()) } ``` -------------------------------- ### Create Multiple Egui Windows Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/README.md Shows how to manage and display multiple Egui windows simultaneously. This requires obtaining separate contexts for each window using `contexts.ctx_for_entities_mut`. ```rust fn multi_window(mut contexts: EguiContexts, entities: Res) -> Result<(), _> { let [ctx1, ctx2] = contexts.ctx_for_entities_mut([entities.0, entities.1])?; egui::Window::new("W1").show(ctx1, |ui| { ui.label("Window 1"); }); egui::Window::new("W2").show(ctx2, |ui| { ui.label("Window 2"); }); Ok(()) } ``` -------------------------------- ### Basic Single-Context UI with EguiContexts Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-contexts.md Demonstrates how to create a simple egui window within a single context. Requires mutable access to `EguiContexts`. ```rust fn simple_ui(mut contexts: EguiContexts) -> Result<(), bevy_ecs::query::QuerySingleError> { egui::Window::new("Controls").show(contexts.ctx_mut()?, |ui| { if ui.button("Click me").clicked() { println!("Button pressed!"); } }); Ok(()) } ``` -------------------------------- ### Get Image ID from Egui Context Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-contexts.md Retrieves the Egui texture ID for a registered image without modifying registration. Use this to check if an image is loaded and get its texture ID for display. ```rust fn show_image_if_loaded( mut contexts: EguiContexts, image_handle: Res>, ) -> Result<(), bevy_ecs::query::QuerySingleError> { let ctx = contexts.ctx_mut()?; egui::Window::new("Gallery").show(ctx, |ui| { if let Some(texture_id) = contexts.image_id(image_handle.id()) { egui::Image::new(egui::ImageSource::Texture(egui::load::SizedTexture { id: texture_id, size: egui::Vec2::new(200.0, 200.0), })) .show(ui); } else { ui.label("Image not yet loaded..."); } }); Ok(()) } ``` -------------------------------- ### Setting up a Secondary Camera Context Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/getting-started.md This snippet shows how to set up a secondary camera with its own Egui context for split-screen or multi-window UIs. It involves spawning a new entity with `Camera2d`, `EguiContext`, and an optional custom schedule. ```rust fn setup_secondary_camera( mut commands: Commands, primary_query: Query>, ) { // Primary context already exists // Create secondary context commands.spawn(( Camera2d::default(), EguiContext::default(), // Optional: custom schedule for multi-pass EguiMultipassSchedule::new(SecondaryContextPass), )); } #[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)] struct SecondaryContextPass; fn secondary_ui( mut contexts: EguiContexts, ) -> Result<(), QueryEntityError> { // Get context for specific entity let secondary_entity = /* ... */; let ctx = contexts.ctx_for_entity_mut(secondary_entity)?; egui::Window::new("Secondary").show(ctx, |ui| { ui.label("This is on a different context"); }); Ok(()) } ``` -------------------------------- ### Initialize Wayland Clipboard Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/clipboard.md Initializes the Wayland clipboard using `smithay-clipboard`. This is typically done during plugin finalization and requires a Wayland display handle. ```rust // Initialized from wayland display handle egui_clipboard.wayland_clipboard = Some(Arc::new(Mutex::new( unsafe { smithay_clipboard::Clipboard::new(display.display.as_ptr()) } ))); ``` -------------------------------- ### EnableMultipassForPrimaryContext Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/types.md Resource inserted when multi-pass mode is enabled. This resource is used internally for conditional system setup. ```APIDOC ## Marker Types ### EnableMultipassForPrimaryContext Resource inserted when multi-pass mode is enabled. Used internally for conditional system setup. ``` -------------------------------- ### Multi-Window UI with EguiContexts for Multiple Cameras Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-contexts.md Shows how to render separate egui windows for different camera contexts. This is useful for split-screen or multi-view UIs. It requires querying for camera entities and obtaining contexts for specific entities. ```rust fn split_screen_ui( mut contexts: EguiContexts, cameras: Query>, ) { let camera_ids: Vec<_> = cameras.iter().collect(); if let Ok([ctx1, ctx2]) = contexts.ctx_for_entities_mut([camera_ids[0], camera_ids[1]]) { egui::Window::new("Panel 1").show(ctx1, |ui| { ui.heading("Left"); }); egui::Window::new("Panel 2").show(ctx2, |ui| { ui.heading("Right"); }); } } ``` -------------------------------- ### Get Asset ID from EguiTextureHandle Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/textures.md Retrieves the AssetId from an EguiTextureHandle. This method is available for both Strong and Weak variants. ```rust pub fn asset_id(&self) -> AssetId ``` ```rust let handle = EguiTextureHandle::Strong(image_handle); let id = handle.asset_id(); ``` -------------------------------- ### Enable Clipboard Management Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/clipboard.md To enable clipboard support, include the `manage_clipboard` feature in your `bevy_egui` dependency. ```toml [dependencies] bevy_egui = { version = "0.40", features = ["manage_clipboard"] } ``` -------------------------------- ### Access Egui Context in Systems Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-context.md Retrieves and manipulates Egui contexts within Bevy systems, for example, to request a repaint. ```rust fn ui_system(mut contexts: Query<&mut EguiContext>) { for mut context in contexts.iter_mut() { let ctx = context.get_mut(); ctx.request_repaint(); } } ``` -------------------------------- ### Copy Image to Clipboard Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/clipboard.md Demonstrates how to copy an image, represented as an egui::ColorImage, to the system clipboard. ```rust fn image_clipboard_system( mut clipboard: ResMut, screenshot: Res, ) { let color_image = egui::ColorImage { size: screenshot.dimensions, pixels: screenshot.to_egui_pixels(), }; clipboard.set_image(&color_image); println!("Screenshot copied to clipboard"); } ``` -------------------------------- ### Define EnableMultipassForPrimaryContext Resource Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/types.md Defines a resource that is inserted when multi-pass mode is enabled. This is used internally for conditional system setup. ```rust #[derive(Resource)] pub struct EnableMultipassForPrimaryContext ``` -------------------------------- ### Configure EguiPlugin in Bevy App Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/getting-started.md Set up the EguiPlugin when creating a Bevy App. Allows control over rendering order relative to Bevy UI and configuration of GPU memory for bindless textures. Use `..default()` to apply default settings for other options. ```rust fn main() { App::new() .add_plugins(EguiPlugin { // Control rendering order with Bevy UI ui_render_order: UiRenderOrder::BevyUiAboveEgui, // Configure GPU memory for textures bindless_mode_array_size: std::num::NonZero::new(32), ..default() }) .run(); } ``` -------------------------------- ### Get Mutable Egui Context Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-context.md Provides mutable access to the underlying egui::Context. Use this to modify the context, such as requesting a repaint. ```rust pub fn get_mut(&mut self) -> &mut egui::Context ``` ```rust fn modify_context(mut query: Query<&mut EguiContext>) { for mut ctx in query.iter_mut() { let context = ctx.get_mut(); context.request_repaint(); } } ``` -------------------------------- ### Copy and Paste Text with Egui Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/clipboard.md Demonstrates how to copy text to the clipboard and paste text from the clipboard using Egui widgets and the EguiClipboard resource. ```rust fn text_editor_system( mut contexts: EguiContexts, mut clipboard: ResMut, ) -> Result<(), bevy_ecs::query::QuerySingleError> { let ctx = contexts.ctx_mut()?; egui::CentralPanel::default().show(ctx, |ui| { if ui.button("Copy").clicked() { clipboard.set_text("Hello from Egui!"); } if ui.button("Paste").clicked() { if let Some(text) = clipboard.get_text() { println!("Pasted: {}", text); } } }); Ok(()) } ``` -------------------------------- ### Egui Clipboard Interaction Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/README.md Demonstrates how to interact with the system clipboard using `EguiClipboard`. This snippet shows how to set text to be copied and retrieve text from the clipboard. ```rust fn clipboard(mut contexts: EguiContexts, mut clip: ResMut) -> Result<(), _> { egui::Window::new("Text").show(contexts.ctx_mut()?, |ui| { if ui.button("Copy").clicked() { clip.set_text("Hello!"); } if ui.button("Paste").clicked() { if let Some(text) = clip.get_text() { println!("{}", text); } } }); Ok(()) } ``` -------------------------------- ### Open URL with Hyperlink Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/output-handling.md Demonstrates opening a URL in the browser when a hyperlink is clicked within the Egui UI. Requires the `open_url` feature. ```rust fn open_url_demo(mut contexts: EguiContexts) -> Result<(), _> { let ctx = contexts.ctx_mut()?; egui::CentralPanel::default().show(ctx, |ui| { ui.hyperlink("https://example.com"); // Opens when clicked }); Ok(()) } ``` -------------------------------- ### Accessing Multiple EguiContexts for Split-Screen Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-contexts.md Use `ctx_for_entities_mut` to get mutable references to multiple Egui contexts simultaneously, preventing borrow conflicts. This is ideal for split-screen or multi-view applications. ```rust fn split_screen_system( mut contexts: EguiContexts, cameras: Query>, ) -> Result<(), Box> { let camera_entities: Vec = cameras.iter().collect(); if camera_entities.len() >= 2 { let [ctx1, ctx2] = contexts.ctx_for_entities_mut([camera_entities[0], camera_entities[1]])?; egui::Window::new("Left").show(ctx1, |ui| { ui.label("Left view"); }); egui::Window::new("Right").show(ctx2, |ui| { ui.label("Right view"); }); } Ok(()) } ``` -------------------------------- ### Project Documentation Structure Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/README.md Illustrates the directory structure of the Bevy Egui project, outlining the purpose of each markdown file and directory. ```text output/ ├── README.md (this file) ├── getting-started.md (basic usage and examples) ├── configuration.md (all configuration options) ├── types.md (all type definitions) ├── api-reference/ │ ├── egui-plugin.md │ ├── egui-context.md │ ├── egui-contexts.md │ ├── input-systems.md │ ├── textures.md │ ├── clipboard.md │ ├── output-handling.md │ └── helpers.md └── (additional reference documents) ``` -------------------------------- ### Configure Egui Context Settings Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-context.md Creates an Egui context with custom settings, such as disabling manual updates or enabling input capture. ```rust fn setup_context_with_config(mut commands: Commands) { let camera = commands.spawn(Camera2d).id(); commands.entity(camera).insert(( EguiContext::default(), EguiContextSettings { run_manually: false, capture_pointer_input: true, enable_cursor_icon_updates: true, enable_ime: true, ..default() }, )); } ``` -------------------------------- ### Get Immutable Egui Context (Feature Gated) Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-context.md Provides immutable access to the underlying egui::Context. This method is only available when the 'immutable_ctx' feature is enabled. Prefer mutable access for better scheduler performance. ```rust #[cfg(feature = "immutable_ctx")] pub fn get(&self) -> &egui::Context ``` -------------------------------- ### Convert ModifierKeysState to egui::Modifiers Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/input-systems.md Converts the Bevy `ModifierKeysState` resource to Egui's `egui::Modifiers` representation. This example shows how to use the `to_egui_modifiers` method and access Egui-specific modifier flags like `command` and `mac_cmd`. ```rust fn use_modifiers(state: Res) { let egui_mods = state.to_egui_modifiers(); println!("Command key: {}", egui_mods.command); println!("Mac Cmd: {}", egui_mods.mac_cmd); } ``` -------------------------------- ### Adding UI Systems to Multi-Pass Rendering Schedule Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/README.md Illustrates how to add custom UI systems to the `EguiPrimaryContextPass` schedule for multi-pass rendering. This ensures UI systems execute at the correct stage when layout information is needed. ```rust app.add_systems(EguiPrimaryContextPass, my_ui_system); ``` -------------------------------- ### Check if TextInput is Allowed with ModifierKeysState Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/input-systems.md Determines if text input is permissible based on the current modifier key state. This example uses the `text_input_is_allowed` method to prevent text insertion when modifier combinations might interfere (e.g., Ctrl+A). ```rust fn validate_text_input(state: Res) { if state.text_input_is_allowed() { // Safe to insert text characters } } ``` -------------------------------- ### Display Images in Egui Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/README.md Illustrates how to load and display images within an Egui window. It uses `contexts.add_image` to handle texture loading and `egui::Image` to render the image. ```rust fn images(mut contexts: EguiContexts, server: Res) -> Result<(), _> { let id = contexts.add_image(EguiTextureHandle::Strong(server.load("img.png"))); egui::Window::new("Gallery").show(contexts.ctx_mut()?, |ui| { egui::Image::new(egui::ImageSource::Texture( egui::load::SizedTexture { id, size: [256.0, 256.0].into() } )).show(ui); }); Ok(()) } ``` -------------------------------- ### Minimal Bevy Egui Configuration Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/configuration.md This snippet shows the most basic configuration for Bevy Egui, enabling only the essential rendering pipeline. ```toml # Example: minimal configuration [dependencies] bevy_egui = { version = "0.40", features = [] } ``` -------------------------------- ### Get Text from Clipboard Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/clipboard.md Retrieves text from the system clipboard. It returns `Some(String)` on success, or `None` if the clipboard is unavailable, an error occurred, or it's empty. Errors are logged at the error level, except for non-availability errors which return an empty string. ```rust #[must_use] pub fn get_text(&mut self) -> Option ``` ```rust fn paste_from_clipboard_system( mut clipboard: ResMut, ) { if let Some(text) = clipboard.get_text() { println!("Clipboard contains: {}", text); } } ``` -------------------------------- ### Get Image ID from Egui Textures Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/textures.md Retrieves the `egui::TextureId` for a registered Bevy image asset without altering the registration status. The function is marked `#[must_use]`, warning if the return value is ignored, encouraging its use for conditional display or checks. ```rust fn display_if_loaded_system( mut contexts: EguiContexts, image_handle: Res>, ) -> Result<(), bevy_ecs::query::QuerySingleError> { let ctx = contexts.ctx_mut()?; egui::Window::new("Conditional Image").show(ctx, |ui| { if let Some(texture_id) = contexts.image_id(image_handle.id()) { egui::Image::new(egui::ImageSource::Texture(egui::load::SizedTexture { id: texture_id, size: egui::Vec2::new(100.0, 100.0), })) .show(ui); } else { ui.label("Loading image..."); } }); Ok(()) } ``` -------------------------------- ### Configure Egui Plugin Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/README.md Configure the `EguiPlugin` when adding it to a Bevy application. This allows customization of the UI render order and bindless mode array size. ```rust App::new() .add_plugins(EguiPlugin { ui_render_order: UiRenderOrder::EguiAboveBevyUi, bindless_mode_array_size: Some(NonZero::new(16).unwrap()), ..default() }) ``` -------------------------------- ### Adding an Image to Egui Context Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/INDEX.md Shows how to add an image to the Egui context for display. This involves creating an EguiTextureHandle and using the `add_image` method. ```rust let id = contexts.add_image(EguiTextureHandle::Strong(handle)); ``` -------------------------------- ### Configure Per-Context Settings Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/README.md Spawn an entity with `EguiContextSettings` to configure individual Egui contexts. This allows fine-grained control over settings like manual rendering, pointer input capture, and IME enablement for specific contexts. ```rust commands.spawn(( Camera2d::default(), EguiContext::default(), EguiContextSettings { run_manually: false, capture_pointer_input: true, enable_cursor_icon_updates: true, enable_ime: true, ..default() }, )); ``` -------------------------------- ### Output Processing Logic in process_output_system Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/output-handling.md Illustrates the core logic within process_output_system for handling clipboard, cursor icons, URLs, and IME based on Egui's platform output. ```rust fn process_output_system(...) { for (entity, mut ctx, mut output, camera, primary) in contexts.iter_mut() { let full_output = /* ... extract from EguiFullOutput ... */; if let Some(full_output) = full_output { output.platform_output = full_output.platform_output.clone(); output.pixels_per_point = full_output.pixels_per_point; // 1. Copy text to clipboard if !full_output.platform_output.copied_text.is_empty() { clipboard.set_text(&full_output.platform_output.copied_text); } // 2. Update cursor icon if let Some(window) = window_query.get_mut(window_entity) { window.cursor.icon = egui_to_winit_cursor_icon( full_output.platform_output.cursor_icon ).unwrap_or_default(); } // 3. Open URLs (web only) #[cfg(feature = "open_url")] if let Some(open_url) = &full_output.platform_output.open_url { webbrowser::open(&open_url.url); } // 4. Update IME handle_ime(&full_output.platform_output.ime, &mut context_ime_state); } } } ``` -------------------------------- ### Set Clipboard Text (Desktop) Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/clipboard.md Implements setting clipboard text for desktop platforms using the `arboard` crate. Requires a thread-local clipboard instance. ```rust fn set_text_impl(&mut self, contents: &str) { if let Some(mut clipboard) = self.get() { if let Err(err) = clipboard.set_text(contents.to_owned()) { log::error!("Failed to set clipboard contents: {:?}", err); } } } ``` -------------------------------- ### Default Implementation for EguiInputSystemSettings Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/configuration.md Provides the default configuration for EguiInputSystemSettings, where all input processing systems are enabled by default. ```rust impl Default for EguiInputSystemSettings { fn default() -> Self { Self { run_write_modifiers_keys_state_system: true, run_write_window_pointer_moved_messages_system: true, // ... all fields true } } } ``` -------------------------------- ### EguiInputSystemSettings Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/input-systems.md Configuration struct for enabling or disabling various input systems within Bevy Egui. All input systems are enabled by default. ```APIDOC ## EguiInputSystemSettings ### Description Configuration struct for enabling or disabling various input systems within Bevy Egui. All input systems are enabled by default. Fine-grained control can be applied to individual input processing systems. ### Usage ```rust fn disable_some_input(mut global: ResMut) { global.input_system_settings.run_write_keyboard_input_messages_system = false; // Keyboard input won't be processed } ``` ### Fields - **run_write_modifiers_keys_state_system** (bool) - Controls if the system for writing modifier keys state runs. - **run_write_window_pointer_moved_messages_system** (bool) - Controls if the system for writing window pointer moved messages runs. - **run_write_pointer_button_messages_system** (bool) - Controls if the system for writing pointer button messages runs. - **run_write_window_touch_messages_system** (bool) - Controls if the system for writing window touch messages runs. - **run_write_non_window_pointer_moved_messages_system** (bool) - Controls if the system for writing non-window pointer moved messages runs. - **run_write_mouse_wheel_messages_system** (bool) - Controls if the system for writing mouse wheel messages runs. - **run_write_pinch_gesture_messages_system** (bool) - Controls if the system for writing pinch gesture messages runs. - **run_write_non_window_touch_messages_system** (bool) - Controls if the system for writing non-window touch messages runs. - **run_write_keyboard_input_messages_system** (bool) - Controls if the system for writing keyboard input messages runs. - **run_write_ime_messages_system** (bool) - Controls if the system for writing IME messages runs. - **run_write_file_dnd_messages_system** (bool) - Controls if the system for writing file drag and drop messages runs. - **run_write_text_agent_channel_messages_system** (bool) - (wasm32 only) Controls if the system for writing text agent channel messages runs. - **run_write_web_clipboard_messages_system** (bool) - (wasm32 only, with `manage_clipboard` feature) Controls if the system for writing web clipboard messages runs. ``` -------------------------------- ### Handling Button Clicks and Keyboard Input Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/getting-started.md This snippet demonstrates how to create a button that prints a message when clicked and how to detect keyboard input, specifically the Space key. It utilizes `EguiContexts` and `ButtonInput`. ```rust fn button_ui( mut contexts: EguiContexts, keyboard: Res>, ) -> Result<(), QuerySingleError> { let ctx = contexts.ctx_mut()?; egui::Window::new("Controls").show(ctx, |ui| { if ui.button("Click Me").clicked() { println!("Button clicked!"); } if keyboard.just_pressed(KeyCode::Space) { println!("Space pressed!"); } }); Ok(()) } ``` -------------------------------- ### EguiPlugin Default Implementation Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-plugin.md Shows the default implementation for EguiPlugin, which creates a plugin with default settings. ```APIDOC ## `impl Default for EguiPlugin` ### Description Provides a default constructor for `EguiPlugin`. This creates a new instance with default settings, including multi-pass mode enabled, Egui rendering above Bevy UI, and bindless mode with an array size of 16. ### Method `Default::default()` ### Returns A new `EguiPlugin` instance with default configuration. ``` -------------------------------- ### Configure Per-Context Egui Settings Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/getting-started.md Spawn an entity with `EguiContextSettings` to manage individual Egui contexts. This allows manual control over Egui passes, pointer input capture, and cursor icon updates for specific contexts. ```rust fn setup_context(mut commands: Commands) { commands.spawn(( Camera2d::default(), EguiContext::default(), EguiContextSettings { // Manually control Egui passes run_manually: false, // Don't suppress Bevy Picking capture_pointer_input: false, // Don't update cursor enable_cursor_icon_updates: false, ..default() }, )); } ``` -------------------------------- ### Set Text to Clipboard Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/clipboard.md Places text onto the system clipboard. This method adapts its behavior based on the target platform (X11/Windows, Wayland, or Web) and logs errors if the operation fails. ```rust pub fn set_text(&mut self, contents: &str) ``` ```rust use bevy::prelude::*; use bevy_egui::EguiClipboard; fn copy_to_clipboard_system( mut clipboard: ResMut, ) { let text = "Hello, clipboard!"; clipboard.set_text(text); } ``` -------------------------------- ### Configure Global Egui Settings Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-plugin.md Set global configuration options for the Egui plugin, such as auto-creating the primary context, enabling cursor icon updates, and enabling the input method editor. ```rust fn setup(mut global_settings: ResMut) { global_settings.auto_create_primary_context = true; // Auto-create primary context global_settings.enable_cursor_icon_updates = true; // Update cursor icons global_settings.enable_ime = true; // Enable input method editor global_settings.enable_absorb_bevy_input_system = false; // Absorb Bevy input } ``` -------------------------------- ### Custom Plugin Integration with EguiPlugin Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-plugin.md Shows how to structure a custom Bevy plugin that depends on EguiPlugin. It asserts that EguiPlugin is added and adds UI systems to the EguiPrimaryContextPass. ```rust pub struct MyEguiPlugin; impl Plugin for MyEguiPlugin { fn build(&self, app: &mut App) { assert!(app.is_plugin_added::()); app.add_systems(EguiPrimaryContextPass, my_ui_system); } } fn my_ui_system(mut contexts: EguiContexts) -> Result<(), QuerySingleError> { // UI code here Ok(()) } ``` -------------------------------- ### EguiStartupSet Enum Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/types.md Defines system sets for the startup schedule, specifically for initializing Egui contexts. This enum is used to organize startup systems. ```rust #[derive(SystemSet, Clone, Hash, Debug, Eq, PartialEq)] pub enum EguiStartupSet { InitContexts, } ``` -------------------------------- ### Difference between Logical and Physical Key Conversion Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/helpers.md Illustrates the difference in conversion behavior between Bevy's logical and physical key representations. ```rust // Logical: respects keyboard layout bevy_to_egui_key(&Key::Character("é")) // Some(Key::from_name("é")) // Physical: ignores layout bevy_to_egui_physical_key(&KeyCode::KeyE) // Some(Key::E) ``` -------------------------------- ### Safe UI Rendering with EguiContexts Error Handling Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-contexts.md Illustrates how to safely access and use the egui context by handling potential errors. This pattern prevents panics if the context is unavailable. ```rust fn safe_ui_system(mut contexts: EguiContexts) { match contexts.ctx_mut() { Ok(ctx) => { egui::Window::new("Safe UI").show(ctx, |ui| { ui.label("Running successfully"); }); } Err(e) => { eprintln!("Failed to access Egui context: {:?}", e); } } } ``` -------------------------------- ### Displaying an Image in Egui Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/textures.md Loads an image using Bevy's AssetServer and displays it within an egui interface using a strong handle. Ensure the image file exists at the specified path. ```rust fn display_image( mut contexts: EguiContexts, textures: Res, ) -> Result<(), bevy_ecs::query::QuerySingleError> { let image_handle = textures.load("background.png"); let texture_id = contexts.add_image(EguiTextureHandle::Strong(image_handle))?; let ctx = contexts.ctx_mut()?; egui::CentralPanel::default().show(ctx, |ui| { egui::Image::new(egui::ImageSource::Texture(egui::load::SizedTexture { id: texture_id, size: egui::Vec2::new(1024.0, 768.0), })) .show(ui); }); Ok(()) } ``` -------------------------------- ### Web-Only Bevy Egui Configuration Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/configuration.md This configuration is tailored for web environments, enabling features like clipboard support and URL opening, while excluding features not typically needed on the web. ```toml # Example: web-only with clipboard [dependencies] bevy_egui = { version = "0.40", features = [ "render", "manage_clipboard", "open_url", "default_fonts" ] } ``` -------------------------------- ### World-Space UI Configuration (Manual Context Focus) Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/configuration.md Configure Egui for world-space UI scenarios where context updates are manually managed. Focused non-window context updates are disabled, requiring manual updates to HoveredNonWindowEguiContext and FocusedNonWindowEguiContext. ```rust .add_systems(Startup, |mut global: ResMut| { global.enable_focused_non_window_context_updates = false; // Manually update HoveredNonWindowEguiContext and FocusedNonWindowEguiContext }) ``` -------------------------------- ### Default Implementation for EguiGlobalSettings Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/configuration.md Provides the default values for `EguiGlobalSettings`. This is used when the settings are first initialized. ```rust impl Default for EguiGlobalSettings { fn default() -> Self { Self { auto_create_primary_context: true, enable_focused_non_window_context_updates: true, input_system_settings: EguiInputSystemSettings::default(), enable_absorb_bevy_input_system: false, enable_cursor_icon_updates: true, enable_ime: true, } } } ``` -------------------------------- ### Debug Overlay Only Configuration Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/configuration.md Configure EguiPlugin for a debug overlay where Egui is rendered above Bevy UI. Input handling is restricted to mouse events only, disabling keyboard input messages. ```rust .add_plugins(EguiPlugin { ui_render_order: UiRenderOrder::EguiAboveBevyUi, ..default() }) .add_systems(Startup, |mut global: ResMut| { // Only mouse, no keyboard overlay input global.input_system_settings.run_write_keyboard_input_messages_system = false; }) ``` -------------------------------- ### bevy_to_egui_key and bevy_to_egui_physical_key Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/helpers.md Helper functions to convert Bevy key inputs (logical and physical) to Egui key inputs, facilitating consistent input handling across different input methods. ```APIDOC ## bevy_to_egui_key and bevy_to_egui_physical_key ### Description Helper functions to convert Bevy key inputs (logical and physical) to Egui key inputs, facilitating consistent input handling across different input methods. ### Example ```rust use bevy::prelude::* use bevy_egui::helpers::{bevy_to_egui_key, bevy_to_egui_physical_key} use bevy_input::keyboard::{Key, KeyCode} fn route_keyboard_input( keyboard_events: EventReader, mut input_buffer: ResMut, ) { for event in keyboard_events.read() { // Try logical key first (respects layout) if let Some(egui_key) = bevy_to_egui_key(&event.logical_key) { input_buffer.push(egui_key); } // Fall back to physical key else if let Some(egui_key) = bevy_to_egui_physical_key(&event.key_code) { input_buffer.push(egui_key); } } } ``` ``` -------------------------------- ### Default Implementation for EguiPlugin Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-plugin.md Provides the default configuration for the EguiPlugin, setting multi-pass mode, Egui rendering above Bevy UI, and a default bindless mode array size. ```rust impl Default for EguiPlugin { fn default() -> Self { ... } } ``` -------------------------------- ### WindowToEguiContextMap System Integration Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/input-systems.md Demonstrates how `WindowToEguiContextMap` is automatically updated by systems during `EguiPreUpdateSet::InitContexts`. These systems ensure map consistency when Egui contexts are added or removed. ```rust impl WindowToEguiContextMap { pub fn on_egui_context_added_system(...) pub fn on_egui_context_removed_system(...) } ``` -------------------------------- ### Add Bevy and Bevy Egui Dependencies Source: https://github.com/vladbat00/bevy_egui/blob/main/README.md Add the Bevy game engine and bevy_egui crate to your project's Cargo.toml file. ```toml # Cargo.toml [dependencies] bevy = "0.19.0" bevy_egui = "0.40.0" ``` -------------------------------- ### Route Keyboard Input to Egui Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/helpers.md Routes Bevy keyboard input events to Egui, prioritizing logical keys and falling back to physical keys. Requires `bevy_input::keyboard::{Key, KeyCode}` and helper functions from `bevy_egui::helpers`. ```rust use bevy::prelude::*; use bevy_egui::helpers::{bevy_to_egui_key, bevy_to_egui_physical_key}; use bevy_input::keyboard::{Key, KeyCode}; fn route_keyboard_input( keyboard_events: EventReader, mut input_buffer: ResMut, ) { for event in keyboard_events.read() { // Try logical key first (respects layout) if let Some(egui_key) = bevy_to_egui_key(&event.logical_key) { input_buffer.push(egui_key); } // Fall back to physical key else if let Some(egui_key) = bevy_to_egui_physical_key(&event.key_code) { input_buffer.push(egui_key); } } } ``` -------------------------------- ### Create Secondary Egui Context Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/api-reference/egui-context.md Spawns a secondary Egui context for a new window. This is useful for multi-window applications. ```rust fn setup_secondary_context( mut commands: Commands, window_query: Query>, ) { for window_entity in window_query.iter() { commands.spawn(( EguiContext::default(), Camera3d::default(), Camera { target: RenderTarget::Window(WindowRef::Entity(window_entity)), ..default() }, )); } } ``` -------------------------------- ### Displaying Stats UI Source: https://github.com/vladbat00/bevy_egui/blob/main/_autodocs/getting-started.md This snippet shows how to display game statistics like elapsed time within an Egui window. It requires the `EguiContexts` and `Time` resources. ```rust fn stats_ui( mut contexts: EguiContexts, time: Res