window_fallthrough/window_fallthrough.rs
1//! This example illustrates how have a mouse's clicks/wheel/movement etc fall through the spawned transparent window to a window below.
2//! If you build this, and hit 'P' it should toggle on/off the mouse's passthrough.
3//! Note: this example will not work on following platforms: iOS / Android / Web / X11. Window fall through is not supported there.
4
5use bevy::{prelude::*, window::CursorOptions};
6
7fn main() {
8 App::new()
9 .insert_resource(ClearColor(Color::NONE)) // Use a transparent window, to make effects obvious.
10 .add_plugins(DefaultPlugins.set(WindowPlugin {
11 primary_window: Some(Window {
12 // Set the window's parameters, note we're setting the window to always be on top.
13 transparent: true,
14 decorations: true,
15 window_level: bevy::window::WindowLevel::AlwaysOnTop,
16 ..default()
17 }),
18 ..default()
19 }))
20 .add_systems(Startup, setup)
21 .add_systems(Update, toggle_mouse_passthrough) // This allows us to hit 'P' to toggle on/off the mouse's passthrough
22 .run();
23}
24
25fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
26 // UI camera
27 commands.spawn(Camera2d);
28 // Text with one span
29 commands.spawn((
30 // Accepts a `String` or any type that converts into a `String`, such as `&str`
31 Text::new("Hit 'P' then scroll/click around!"),
32 TextFont {
33 font: asset_server.load("fonts/FiraSans-Bold.ttf"),
34 font_size: 83.0, // Nice and big so you can see it!
35 ..default()
36 },
37 // Set the style of the TextBundle itself.
38 Node {
39 position_type: PositionType::Absolute,
40 bottom: px(5),
41 right: px(10),
42 ..default()
43 },
44 ));
45}
46// A simple system to handle some keyboard input and toggle on/off the hit test.
47fn toggle_mouse_passthrough(
48 keyboard_input: Res<ButtonInput<KeyCode>>,
49 mut cursor_options: Single<&mut CursorOptions>,
50) {
51 if keyboard_input.just_pressed(KeyCode::KeyP) {
52 cursor_options.hit_test = !cursor_options.hit_test;
53 }
54}