forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_settings.rs
187 lines (167 loc) · 6.72 KB
/
window_settings.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! Illustrates how to change window settings and shows how to affect
//! the mouse pointer in various ways.
use bevy::{
core::FrameCount,
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
window::{CursorGrabMode, PresentMode, WindowLevel, WindowTheme},
};
fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "I am a window!".into(),
name: Some("bevy.app".into()),
resolution: (500., 300.).into(),
present_mode: PresentMode::AutoVsync,
// Tells wasm not to override default event handling, like F5, Ctrl+R etc.
prevent_default_event_handling: false,
window_theme: Some(WindowTheme::Dark),
enabled_buttons: bevy::window::EnabledButtons {
maximize: false,
..Default::default()
},
// This will spawn an invisible window
// The window will be made visible in the make_visible() system after 3 frames.
// This is useful when you want to avoid the white window that shows up before the GPU is ready to render the app.
visible: false,
..default()
}),
..default()
}),
LogDiagnosticsPlugin::default(),
FrameTimeDiagnosticsPlugin,
))
.add_systems(
Update,
(
change_title,
toggle_theme,
toggle_cursor,
toggle_vsync,
toggle_window_controls,
cycle_cursor_icon,
switch_level,
make_visible,
),
)
.run();
}
fn make_visible(mut window: Query<&mut Window>, frames: Res<FrameCount>) {
// The delay may be different for your app or system.
if frames.0 == 3 {
// At this point the gpu is ready to show the app so we can make the window visible.
// Alternatively, you could toggle the visibility in Startup.
// It will work, but it will have one white frame before it starts rendering
window.single_mut().visible = true;
}
}
/// This system toggles the vsync mode when pressing the button V.
/// You'll see fps increase displayed in the console.
fn toggle_vsync(input: Res<ButtonInput<KeyCode>>, mut windows: Query<&mut Window>) {
if input.just_pressed(KeyCode::KeyV) {
let mut window = windows.single_mut();
window.present_mode = if matches!(window.present_mode, PresentMode::AutoVsync) {
PresentMode::AutoNoVsync
} else {
PresentMode::AutoVsync
};
info!("PRESENT_MODE: {:?}", window.present_mode);
}
}
/// This system switches the window level when pressing the T button
/// You'll notice it won't be covered by other windows, or will be covered by all the other
/// windows depending on the level.
///
/// This feature only works on some platforms. Please check the
/// [documentation](https://docs.rs/bevy/latest/bevy/prelude/struct.Window.html#structfield.window_level)
/// for more details.
fn switch_level(input: Res<ButtonInput<KeyCode>>, mut windows: Query<&mut Window>) {
if input.just_pressed(KeyCode::KeyT) {
let mut window = windows.single_mut();
window.window_level = match window.window_level {
WindowLevel::AlwaysOnBottom => WindowLevel::Normal,
WindowLevel::Normal => WindowLevel::AlwaysOnTop,
WindowLevel::AlwaysOnTop => WindowLevel::AlwaysOnBottom,
};
info!("WINDOW_LEVEL: {:?}", window.window_level);
}
}
/// This system toggles the window controls when pressing buttons 1, 2 and 3
///
/// This feature only works on some platforms. Please check the
/// [documentation](https://docs.rs/bevy/latest/bevy/prelude/struct.Window.html#structfield.enabled_buttons)
/// for more details.
fn toggle_window_controls(input: Res<ButtonInput<KeyCode>>, mut windows: Query<&mut Window>) {
let toggle_minimize = input.just_pressed(KeyCode::Digit1);
let toggle_maximize = input.just_pressed(KeyCode::Digit2);
let toggle_close = input.just_pressed(KeyCode::Digit3);
if toggle_minimize || toggle_maximize || toggle_close {
let mut window = windows.single_mut();
if toggle_minimize {
window.enabled_buttons.minimize = !window.enabled_buttons.minimize;
}
if toggle_maximize {
window.enabled_buttons.maximize = !window.enabled_buttons.maximize;
}
if toggle_close {
window.enabled_buttons.close = !window.enabled_buttons.close;
}
}
}
/// This system will then change the title during execution
fn change_title(mut windows: Query<&mut Window>, time: Res<Time>) {
let mut window = windows.single_mut();
window.title = format!(
"Seconds since startup: {}",
time.elapsed().as_secs_f32().round()
);
}
fn toggle_cursor(mut windows: Query<&mut Window>, input: Res<ButtonInput<KeyCode>>) {
if input.just_pressed(KeyCode::Space) {
let mut window = windows.single_mut();
window.cursor.visible = !window.cursor.visible;
window.cursor.grab_mode = match window.cursor.grab_mode {
CursorGrabMode::None => CursorGrabMode::Locked,
CursorGrabMode::Locked | CursorGrabMode::Confined => CursorGrabMode::None,
};
}
}
// This system will toggle the color theme used by the window
fn toggle_theme(mut windows: Query<&mut Window>, input: Res<ButtonInput<KeyCode>>) {
if input.just_pressed(KeyCode::KeyF) {
let mut window = windows.single_mut();
if let Some(current_theme) = window.window_theme {
window.window_theme = match current_theme {
WindowTheme::Light => Some(WindowTheme::Dark),
WindowTheme::Dark => Some(WindowTheme::Light),
};
}
}
}
/// This system cycles the cursor's icon through a small set of icons when clicking
fn cycle_cursor_icon(
mut windows: Query<&mut Window>,
input: Res<ButtonInput<MouseButton>>,
mut index: Local<usize>,
) {
let mut window = windows.single_mut();
const ICONS: &[CursorIcon] = &[
CursorIcon::Default,
CursorIcon::Pointer,
CursorIcon::Wait,
CursorIcon::Text,
CursorIcon::Copy,
];
if input.just_pressed(MouseButton::Left) {
*index = (*index + 1) % ICONS.len();
} else if input.just_pressed(MouseButton::Right) {
*index = if *index == 0 {
ICONS.len() - 1
} else {
*index - 1
};
}
window.cursor.icon = ICONS[*index];
}