| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#[cfg(feature = "custom_cursor")] |
| 5 |
use bevy::window::{CustomCursor, CustomCursorImage}; |
| 6 |
use bevy::{ |
| 7 |
diagnostic::{FrameCount, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, |
| 8 |
prelude::*, |
| 9 |
window::{ |
| 10 |
CursorGrabMode, CursorIcon, CursorOptions, PresentMode, SystemCursorIcon, WindowLevel, |
| 11 |
WindowTheme, |
| 12 |
}, |
| 13 |
}; |
| 14 |
|
| 15 |
fn main() { |
| 16 |
App::new() |
| 17 |
.add_plugins(( |
| 18 |
DefaultPlugins.set(WindowPlugin { |
| 19 |
primary_window: Some(Window { |
| 20 |
title: "I am a window!".into(), |
| 21 |
name: Some("bevy.app".into()), |
| 22 |
resolution: (500, 300).into(), |
| 23 |
present_mode: PresentMode::AutoVsync, |
| 24 |
|
| 25 |
fit_canvas_to_parent: true, |
| 26 |
|
| 27 |
prevent_default_event_handling: false, |
| 28 |
window_theme: Some(WindowTheme::Dark), |
| 29 |
enabled_buttons: bevy::window::EnabledButtons { |
| 30 |
maximize: false, |
| 31 |
..Default::default() |
| 32 |
}, |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
visible: false, |
| 37 |
..default() |
| 38 |
}), |
| 39 |
..default() |
| 40 |
}), |
| 41 |
LogDiagnosticsPlugin::default(), |
| 42 |
FrameTimeDiagnosticsPlugin::default(), |
| 43 |
)) |
| 44 |
.add_systems(Startup, init_cursor_icons) |
| 45 |
.add_systems( |
| 46 |
Update, |
| 47 |
( |
| 48 |
change_title, |
| 49 |
toggle_theme, |
| 50 |
toggle_cursor, |
| 51 |
toggle_vsync, |
| 52 |
toggle_window_controls, |
| 53 |
cycle_cursor_icon, |
| 54 |
switch_level, |
| 55 |
make_visible, |
| 56 |
), |
| 57 |
) |
| 58 |
.run(); |
| 59 |
} |
| 60 |
|
| 61 |
fn make_visible(mut window: Single<&mut Window>, frames: Res<FrameCount>) { |
| 62 |
|
| 63 |
if frames.0 == 3 { |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
window.visible = true; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
fn toggle_vsync(input: Res<ButtonInput<KeyCode>>, mut window: Single<&mut Window>) { |
| 74 |
if input.just_pressed(KeyCode::KeyV) { |
| 75 |
window.present_mode = if matches!(window.present_mode, PresentMode::AutoVsync) { |
| 76 |
PresentMode::AutoNoVsync |
| 77 |
} else { |
| 78 |
PresentMode::AutoVsync |
| 79 |
}; |
| 80 |
info!("PRESENT_MODE: {:?}", window.present_mode); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
fn switch_level(input: Res<ButtonInput<KeyCode>>, mut window: Single<&mut Window>) { |
| 92 |
if input.just_pressed(KeyCode::KeyT) { |
| 93 |
window.window_level = match window.window_level { |
| 94 |
WindowLevel::AlwaysOnBottom => WindowLevel::Normal, |
| 95 |
WindowLevel::Normal => WindowLevel::AlwaysOnTop, |
| 96 |
WindowLevel::AlwaysOnTop => WindowLevel::AlwaysOnBottom, |
| 97 |
}; |
| 98 |
info!("WINDOW_LEVEL: {:?}", window.window_level); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
fn toggle_window_controls(input: Res<ButtonInput<KeyCode>>, mut window: Single<&mut Window>) { |
| 108 |
let toggle_minimize = input.just_pressed(KeyCode::Digit1); |
| 109 |
let toggle_maximize = input.just_pressed(KeyCode::Digit2); |
| 110 |
let toggle_close = input.just_pressed(KeyCode::Digit3); |
| 111 |
|
| 112 |
if toggle_minimize || toggle_maximize || toggle_close { |
| 113 |
if toggle_minimize { |
| 114 |
window.enabled_buttons.minimize = !window.enabled_buttons.minimize; |
| 115 |
} |
| 116 |
if toggle_maximize { |
| 117 |
window.enabled_buttons.maximize = !window.enabled_buttons.maximize; |
| 118 |
} |
| 119 |
if toggle_close { |
| 120 |
window.enabled_buttons.close = !window.enabled_buttons.close; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
fn change_title(mut window: Single<&mut Window>, time: Res<Time>) { |
| 127 |
window.title = format!( |
| 128 |
"Seconds since startup: {}", |
| 129 |
time.elapsed().as_secs_f32().round() |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
fn toggle_cursor(mut cursor_options: Single<&mut CursorOptions>, input: Res<ButtonInput<KeyCode>>) { |
| 134 |
if input.just_pressed(KeyCode::Space) { |
| 135 |
cursor_options.visible = !cursor_options.visible; |
| 136 |
cursor_options.grab_mode = match cursor_options.grab_mode { |
| 137 |
CursorGrabMode::None => CursorGrabMode::Locked, |
| 138 |
CursorGrabMode::Locked | CursorGrabMode::Confined => CursorGrabMode::None, |
| 139 |
}; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
fn toggle_theme(mut window: Single<&mut Window>, input: Res<ButtonInput<KeyCode>>) { |
| 145 |
if input.just_pressed(KeyCode::KeyF) |
| 146 |
&& let Some(current_theme) = window.window_theme |
| 147 |
{ |
| 148 |
window.window_theme = match current_theme { |
| 149 |
WindowTheme::Light => Some(WindowTheme::Dark), |
| 150 |
WindowTheme::Dark => Some(WindowTheme::Light), |
| 151 |
}; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
#[derive(Resource)] |
| 157 |
struct CursorIcons(Vec<CursorIcon>); |
| 158 |
|
| 159 |
fn init_cursor_icons( |
| 160 |
mut commands: Commands, |
| 161 |
window: Single<Entity, With<Window>>, |
| 162 |
#[cfg(feature = "custom_cursor")] asset_server: Res<AssetServer>, |
| 163 |
) { |
| 164 |
let cursor_icons = CursorIcons(vec![ |
| 165 |
SystemCursorIcon::Default.into(), |
| 166 |
SystemCursorIcon::Pointer.into(), |
| 167 |
SystemCursorIcon::Wait.into(), |
| 168 |
SystemCursorIcon::Text.into(), |
| 169 |
#[cfg(feature = "custom_cursor")] |
| 170 |
CustomCursor::Image(CustomCursorImage { |
| 171 |
handle: asset_server.load("branding/icon.png"), |
| 172 |
hotspot: (128, 128), |
| 173 |
..Default::default() |
| 174 |
}) |
| 175 |
.into(), |
| 176 |
]); |
| 177 |
|
| 178 |
commands.entity(*window).insert(cursor_icons.0[0].clone()); |
| 179 |
commands.insert_resource(cursor_icons); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
fn cycle_cursor_icon( |
| 184 |
mut cursor: Single<&mut CursorIcon>, |
| 185 |
input: Res<ButtonInput<MouseButton>>, |
| 186 |
mut index: Local<usize>, |
| 187 |
cursor_icons: Res<CursorIcons>, |
| 188 |
) { |
| 189 |
if input.just_pressed(MouseButton::Left) { |
| 190 |
*index = (*index + 1) % cursor_icons.0.len(); |
| 191 |
**cursor = cursor_icons.0[*index].clone(); |
| 192 |
} else if input.just_pressed(MouseButton::Right) { |
| 193 |
*index = if *index == 0 { |
| 194 |
cursor_icons.0.len() - 1 |
| 195 |
} else { |
| 196 |
*index - 1 |
| 197 |
}; |
| 198 |
**cursor = cursor_icons.0[*index].clone(); |
| 199 |
} |
| 200 |
} |