leptos_bevy_canvas/
lib.rs

1//! Embed an idiomatic Bevy app inside your Leptos app.
2//!
3//! [Send and Receive Messages ![Messages Demo](https://media.githubusercontent.com/media/Synphonyte/leptos-bevy-canvas/refs/heads/main/docs/unidir_messages.webp)](https://github.com/Synphonyte/leptos-bevy-canvas/tree/main/examples/unidir_messages)
4//!
5//! [Sync Bevy Queries ![Query Sync Demo](https://media.githubusercontent.com/media/Synphonyte/leptos-bevy-canvas/refs/heads/main/docs/synced_bevy_query.webp)](https://github.com/Synphonyte/leptos-bevy-canvas/tree/main/examples/synced_bevy_query)
6//!
7//! # Features
8//!
9//! - **Easy to use** - Simply embed your Bevy app inside your Leptos app with the
10//!   [`BevyCanvas`](fn@crate::prelude::BevyCanvas) component.
11//! - **Idiomatic** - This crate doesn't want you to do anything differently in the way you write
12//!   your Bevy app or your Leptos app. It just gives you the tools for them to communicate.
13//! - **Messages** - Send messages in either or both directions between your Bevy app and your Leptos app.
14//! - **Resource signals** - Synchronize Bevy `Resource`s with `RwSignal`s in your Leptos app.
15//! - **Query signals** - Synchronize Bevy `Query`s with `RwSignal`s in your Leptos app.
16//! - **State signals** - Synchronize Bevy `State`s with `RwSignal`s in your Leptos app.
17//!
18//! # Example
19//!
20//! ```
21//! use bevy::prelude::*;
22//! use leptos::prelude::*;
23//! use leptos_bevy_canvas::prelude::*;
24//!
25//! #[derive(Message)]
26//! pub struct TextMessage {
27//!     pub text: String,
28//! }
29//!
30//! #[component]
31//! pub fn App() -> impl IntoView {
32//!     // This initializes a sender for the Leptos app and
33//!     // a receiver for the Bevy app
34//!     let (text_message_sender, bevy_text_receiver) = message_l2b::<TextMessage>();
35//!
36//!     let on_input = move |evt| {
37//!         // send the message over to Bevy
38//!         text_message_sender
39//!             .send(TextMessage { text: event_target_value(&evt) })
40//!             .ok();
41//!     };
42//!
43//!     view! {
44//!         <input type="text" on:input=on_input />
45//!
46//!         <BevyCanvas
47//!             init=move || {
48//!                 // Pass the receiver into the Bevy app initialization
49//!                 init_bevy_app(bevy_text_receiver)
50//!             }
51//!
52//!             {..}
53//!             width="300"
54//!             height="500"
55//!         />
56//!     }
57//! }
58//!
59//! // In Bevy it ends up just as a normal message
60//! pub fn set_text(
61//!     mut message_reader: MessageReader<TextMessage>,
62//! ) {
63//!     for message in message_reader.read() {
64//!         // do something with the message
65//!     }
66//! }
67//!
68//! // This initializes a normal Bevy app
69//! fn init_bevy_app( text_receiver: BevyMessageReceiver<TextMessage>) -> App {
70//!     let mut app = App::new();
71//!     app
72//!         .add_plugins(
73//!             DefaultPlugins.set(WindowPlugin {
74//!                 primary_window: Some(Window {
75//!                     // "#bevy_canvas" is the default and can be
76//!                     // changed in the <BevyCanvas> component
77//!                     canvas: Some("#bevy_canvas".into()),
78//!                     ..default()
79//!                 }),
80//!                 ..default()
81//!             }),
82//!         )
83//!         // import the message here into Bevy
84//!         .import_message_from_leptos(text_receiver)
85//!         .add_systems(Update, set_text);
86//!
87//!     app
88//! }
89//! ```
90//!
91//! Please check the examples to see how to synchronize a `Resource` or a `Query`.
92//!
93//! # Compatibility
94//!
95//! | Crate version | Compatible Leptos version | Compatible Bevy version |
96//! |---------------|---------------------------|-------------------------|
97//! | 0.4           | 0.8                       | 0.17                    |
98//! | 0.3           | 0.8                       | 0.16                    |
99//! | 0.1, 0.2      | 0.7                       | 0.15                    |
100
101mod app_extension;
102mod leptos_component;
103mod messages;
104mod plugin;
105mod queries;
106mod signal_synced;
107pub mod systems;
108pub mod traits;
109mod utils;
110
111pub mod prelude {
112    pub use crate::app_extension::*;
113    pub use crate::leptos_component::*;
114    pub use crate::messages::*;
115    pub use crate::queries::*;
116    pub use crate::signal_synced::*;
117}