2022-05-16 13:53:20 +00:00
|
|
|
//! Illustrates how to change window settings and shows how to affect
|
|
|
|
//! the mouse pointer in various ways.
|
|
|
|
|
2022-09-30 13:25:27 +00:00
|
|
|
use bevy::{
|
|
|
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
|
|
|
prelude::*,
|
|
|
|
window::PresentMode,
|
|
|
|
};
|
2020-07-20 09:05:56 +00:00
|
|
|
|
|
|
|
fn main() {
|
2021-07-27 20:21:06 +00:00
|
|
|
App::new()
|
2021-01-30 20:55:13 +00:00
|
|
|
.insert_resource(WindowDescriptor {
|
2020-07-20 09:05:56 +00:00
|
|
|
title: "I am a window!".to_string(),
|
2020-12-13 23:05:56 +00:00
|
|
|
width: 500.,
|
|
|
|
height: 300.,
|
2022-07-14 21:17:16 +00:00
|
|
|
present_mode: PresentMode::AutoVsync,
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-20 09:05:56 +00:00
|
|
|
})
|
2020-11-03 03:01:17 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2022-09-21 22:35:15 +00:00
|
|
|
.add_plugin(LogDiagnosticsPlugin::default())
|
|
|
|
.add_plugin(FrameTimeDiagnosticsPlugin)
|
2021-07-27 23:42:36 +00:00
|
|
|
.add_system(change_title)
|
|
|
|
.add_system(toggle_cursor)
|
2022-09-21 22:35:15 +00:00
|
|
|
.add_system(toggle_vsync)
|
2021-12-20 22:04:45 +00:00
|
|
|
.add_system(cycle_cursor_icon)
|
2020-07-20 09:05:56 +00:00
|
|
|
.run();
|
|
|
|
}
|
2020-10-15 18:42:19 +00:00
|
|
|
|
2022-09-21 22:35:15 +00:00
|
|
|
/// 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<Input<KeyCode>>, mut windows: ResMut<Windows>) {
|
|
|
|
if input.just_pressed(KeyCode::V) {
|
|
|
|
let window = windows.primary_mut();
|
|
|
|
|
|
|
|
window.set_present_mode(if matches!(window.present_mode(), PresentMode::AutoVsync) {
|
|
|
|
PresentMode::AutoNoVsync
|
|
|
|
} else {
|
|
|
|
PresentMode::AutoVsync
|
|
|
|
});
|
|
|
|
info!("PRESENT_MODE: {:?}", window.present_mode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 18:42:19 +00:00
|
|
|
/// This system will then change the title during execution
|
|
|
|
fn change_title(time: Res<Time>, mut windows: ResMut<Windows>) {
|
2022-03-08 00:46:04 +00:00
|
|
|
let window = windows.primary_mut();
|
2020-10-15 18:42:19 +00:00
|
|
|
window.set_title(format!(
|
|
|
|
"Seconds since startup: {}",
|
2020-11-28 21:08:31 +00:00
|
|
|
time.seconds_since_startup().round()
|
2020-10-15 18:42:19 +00:00
|
|
|
));
|
|
|
|
}
|
2020-10-16 21:07:01 +00:00
|
|
|
|
|
|
|
/// This system toggles the cursor's visibility when the space bar is pressed
|
|
|
|
fn toggle_cursor(input: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
|
2022-03-08 00:46:04 +00:00
|
|
|
let window = windows.primary_mut();
|
2020-10-16 21:07:01 +00:00
|
|
|
if input.just_pressed(KeyCode::Space) {
|
|
|
|
window.set_cursor_lock_mode(!window.cursor_locked());
|
|
|
|
window.set_cursor_visibility(!window.cursor_visible());
|
|
|
|
}
|
|
|
|
}
|
2021-12-20 22:04:45 +00:00
|
|
|
|
|
|
|
/// This system cycles the cursor's icon through a small set of icons when clicking
|
|
|
|
fn cycle_cursor_icon(
|
|
|
|
input: Res<Input<MouseButton>>,
|
|
|
|
mut windows: ResMut<Windows>,
|
|
|
|
mut index: Local<usize>,
|
|
|
|
) {
|
|
|
|
const ICONS: &[CursorIcon] = &[
|
|
|
|
CursorIcon::Default,
|
|
|
|
CursorIcon::Hand,
|
|
|
|
CursorIcon::Wait,
|
|
|
|
CursorIcon::Text,
|
|
|
|
CursorIcon::Copy,
|
|
|
|
];
|
2022-03-08 00:46:04 +00:00
|
|
|
let window = windows.primary_mut();
|
2021-12-20 22:04:45 +00:00
|
|
|
if input.just_pressed(MouseButton::Left) {
|
|
|
|
*index = (*index + 1) % ICONS.len();
|
|
|
|
window.set_cursor_icon(ICONS[*index]);
|
|
|
|
} else if input.just_pressed(MouseButton::Right) {
|
|
|
|
*index = if *index == 0 {
|
|
|
|
ICONS.len() - 1
|
|
|
|
} else {
|
|
|
|
*index - 1
|
|
|
|
};
|
|
|
|
window.set_cursor_icon(ICONS[*index]);
|
|
|
|
}
|
|
|
|
}
|