bevy/examples/window/window_settings.rs
MrGVSV 5479047aa2 Added set_cursor_icon(...) to Window (#3395)
# Objective

The window's cursor should be settable without having to implement a custom cursor icon solution. This will especially be helpful when creating user-interfaces that might like to use the cursor to denote some meaning (e.g., _clickable_, _resizable_, etc.).

## Solution

Added a `CursorIcon` enum that maps one-to-one to winit's `CursorIcon` enum, as well as a method to set/get it for the given `Window`.
2021-12-20 22:04:45 +00:00

63 lines
1.9 KiB
Rust

use bevy::prelude::*;
/// This example illustrates how to customize the default window settings
fn main() {
App::new()
.insert_resource(WindowDescriptor {
title: "I am a window!".to_string(),
width: 500.,
height: 300.,
vsync: true,
..Default::default()
})
.add_plugins(DefaultPlugins)
.add_system(change_title)
.add_system(toggle_cursor)
.add_system(cycle_cursor_icon)
.run();
}
/// This system will then change the title during execution
fn change_title(time: Res<Time>, mut windows: ResMut<Windows>) {
let window = windows.get_primary_mut().unwrap();
window.set_title(format!(
"Seconds since startup: {}",
time.seconds_since_startup().round()
));
}
/// This system toggles the cursor's visibility when the space bar is pressed
fn toggle_cursor(input: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
let window = windows.get_primary_mut().unwrap();
if input.just_pressed(KeyCode::Space) {
window.set_cursor_lock_mode(!window.cursor_locked());
window.set_cursor_visibility(!window.cursor_visible());
}
}
/// 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,
];
let window = windows.get_primary_mut().unwrap();
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]);
}
}