forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_cursor_image.rs
227 lines (204 loc) · 7.03 KB
/
custom_cursor_image.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//! Illustrates how to use a custom cursor image with a texture atlas and
//! animation.
use std::time::Duration;
use bevy::{
prelude::*,
winit::cursor::{CursorIcon, CustomCursor, CustomCursorImage},
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(
Startup,
(setup_cursor_icon, setup_camera, setup_instructions),
)
.add_systems(
Update,
(
execute_animation,
toggle_texture_atlas,
toggle_flip_x,
toggle_flip_y,
cycle_rect,
),
)
.run();
}
fn setup_cursor_icon(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
window: Single<Entity, With<Window>>,
) {
let layout =
TextureAtlasLayout::from_grid(UVec2::splat(64), 20, 10, Some(UVec2::splat(5)), None);
let texture_atlas_layout = texture_atlas_layouts.add(layout);
let animation_config = AnimationConfig::new(0, 199, 1, 4);
commands.entity(*window).insert((
CursorIcon::Custom(CustomCursor::Image(CustomCursorImage {
// Image to use as the cursor.
handle: asset_server
.load("cursors/kenney_crosshairPack/Tilesheet/crosshairs_tilesheet_white.png"),
// Optional texture atlas allows you to pick a section of the image
// and animate it.
texture_atlas: Some(TextureAtlas {
layout: texture_atlas_layout.clone(),
index: animation_config.first_sprite_index,
}),
flip_x: false,
flip_y: false,
// Optional section of the image to use as the cursor.
rect: None,
// The hotspot is the point in the cursor image that will be
// positioned at the mouse cursor's position.
hotspot: (0, 0),
})),
animation_config,
));
}
fn setup_camera(mut commands: Commands) {
commands.spawn(Camera3d::default());
}
fn setup_instructions(mut commands: Commands) {
commands.spawn((
Text::new(
"Press T to toggle the cursor's `texture_atlas`.\n
Press X to toggle the cursor's `flip_x` setting.\n
Press Y to toggle the cursor's `flip_y` setting.\n
Press C to cycle through the sections of the cursor's image using `rect`.",
),
Node {
position_type: PositionType::Absolute,
bottom: Val::Px(12.0),
left: Val::Px(12.0),
..default()
},
));
}
#[derive(Component)]
struct AnimationConfig {
first_sprite_index: usize,
last_sprite_index: usize,
increment: usize,
fps: u8,
frame_timer: Timer,
}
impl AnimationConfig {
fn new(first: usize, last: usize, increment: usize, fps: u8) -> Self {
Self {
first_sprite_index: first,
last_sprite_index: last,
increment,
fps,
frame_timer: Self::timer_from_fps(fps),
}
}
fn timer_from_fps(fps: u8) -> Timer {
Timer::new(Duration::from_secs_f32(1.0 / (fps as f32)), TimerMode::Once)
}
}
/// This system loops through all the sprites in the [`CursorIcon`]'s
/// [`TextureAtlas`], from [`AnimationConfig`]'s `first_sprite_index` to
/// `last_sprite_index`.
fn execute_animation(time: Res<Time>, mut query: Query<(&mut AnimationConfig, &mut CursorIcon)>) {
for (mut config, mut cursor_icon) in &mut query {
if let CursorIcon::Custom(CustomCursor::Image(ref mut image)) = *cursor_icon {
config.frame_timer.tick(time.delta());
if config.frame_timer.finished() {
if let Some(atlas) = image.texture_atlas.as_mut() {
atlas.index += config.increment;
if atlas.index > config.last_sprite_index {
atlas.index = config.first_sprite_index;
}
config.frame_timer = AnimationConfig::timer_from_fps(config.fps);
}
}
}
}
}
fn toggle_texture_atlas(
input: Res<ButtonInput<KeyCode>>,
mut query: Query<&mut CursorIcon, With<Window>>,
mut cached_atlas: Local<Option<TextureAtlas>>, // this lets us restore the previous value
) {
if input.just_pressed(KeyCode::KeyT) {
for mut cursor_icon in &mut query {
if let CursorIcon::Custom(CustomCursor::Image(ref mut image)) = *cursor_icon {
match image.texture_atlas.take() {
Some(a) => {
// Save the current texture atlas.
*cached_atlas = Some(a.clone());
}
None => {
// Restore the cached texture atlas.
if let Some(cached_a) = cached_atlas.take() {
image.texture_atlas = Some(cached_a);
}
}
}
}
}
}
}
fn toggle_flip_x(
input: Res<ButtonInput<KeyCode>>,
mut query: Query<&mut CursorIcon, With<Window>>,
) {
if input.just_pressed(KeyCode::KeyX) {
for mut cursor_icon in &mut query {
if let CursorIcon::Custom(CustomCursor::Image(ref mut image)) = *cursor_icon {
image.flip_x = !image.flip_x;
}
}
}
}
fn toggle_flip_y(
input: Res<ButtonInput<KeyCode>>,
mut query: Query<&mut CursorIcon, With<Window>>,
) {
if input.just_pressed(KeyCode::KeyY) {
for mut cursor_icon in &mut query {
if let CursorIcon::Custom(CustomCursor::Image(ref mut image)) = *cursor_icon {
image.flip_y = !image.flip_y;
}
}
}
}
/// This system alternates the [`CursorIcon`]'s `rect` field between `None` and
/// 4 sections/rectangles of the cursor's image.
fn cycle_rect(input: Res<ButtonInput<KeyCode>>, mut query: Query<&mut CursorIcon, With<Window>>) {
if !input.just_pressed(KeyCode::KeyC) {
return;
}
const RECT_SIZE: u32 = 32; // half the size of a tile in the texture atlas
const SECTIONS: [Option<URect>; 5] = [
Some(URect {
min: UVec2::ZERO,
max: UVec2::splat(RECT_SIZE),
}),
Some(URect {
min: UVec2::new(RECT_SIZE, 0),
max: UVec2::new(2 * RECT_SIZE, RECT_SIZE),
}),
Some(URect {
min: UVec2::new(0, RECT_SIZE),
max: UVec2::new(RECT_SIZE, 2 * RECT_SIZE),
}),
Some(URect {
min: UVec2::new(RECT_SIZE, RECT_SIZE),
max: UVec2::splat(2 * RECT_SIZE),
}),
None, // reset to None
];
for mut cursor_icon in &mut query {
if let CursorIcon::Custom(CustomCursor::Image(ref mut image)) = *cursor_icon {
let next_rect = SECTIONS
.iter()
.cycle()
.skip_while(|&&corner| corner != image.rect)
.nth(1) // move to the next element
.unwrap_or(&None);
image.rect = *next_rect;
}
}
}