-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathani.rs
150 lines (127 loc) · 4.06 KB
/
ani.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
use std::time::Duration;
use bevy::{
prelude::*,
winit::cursor::{CursorIcon, CustomCursor},
};
use bevy_cursor_kit::{ani::animation::AnimationDuration, prelude::*};
use flip::FlipPlugin;
use ui::UiPlugin;
#[path = "./helpers/flip.rs"]
mod flip;
#[path = "./helpers/ui.rs"]
mod ui;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(CursorAssetPlugin)
.add_plugins(UiPlugin)
.add_plugins(FlipPlugin)
.add_systems(Startup, (setup_cursor, setup_instructions))
.add_systems(Update, (insert_cursor, animate_cursor))
.run();
}
#[derive(Debug, Resource, Reflect)]
#[reflect(Debug, Resource)]
struct Cursors {
cursor: Handle<AnimatedCursor>,
}
fn setup_cursor(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.insert_resource(Cursors {
cursor: asset_server.load("Master Sword-Fairy.ANI"),
});
}
fn insert_cursor(
mut commands: Commands,
animated_cursors: Res<Assets<AnimatedCursor>>,
cursors: Res<Cursors>,
window: Single<Entity, With<Window>>,
mut setup: Local<bool>,
) {
if *setup {
return;
}
let Some(c) = animated_cursors.get(&cursors.cursor.clone()) else {
return;
};
commands.entity(*window).insert((
CursorIcon::Custom(CustomCursorImageBuilder::from_animated_cursor(c, None).build()),
c.hotspots.clone(),
AnimationConfig::new(
0,
c.animation.clips[0].atlas_indices.len() - 1,
match c.animation.clips[0].duration {
AnimationDuration::PerFrame(millis) => Duration::from_millis(millis as u64),
AnimationDuration::PerRepetition(_) => panic!("PerRepetition not supported"),
},
),
));
*setup = true;
}
#[derive(Component, Debug, Reflect)]
#[reflect(Debug, Component)]
struct AnimationConfig {
first_sprite_index: usize,
last_sprite_index: usize,
duration_per_frame: Duration,
frame_timer: Timer,
}
impl AnimationConfig {
fn new(first: usize, last: usize, duration_per_frame: Duration) -> Self {
Self {
first_sprite_index: first,
last_sprite_index: last,
duration_per_frame,
frame_timer: Timer::new(duration_per_frame, 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 animate_cursor(
time: Res<Time>,
mut query: Query<(&mut CursorIcon, &CursorHotspots, &mut AnimationConfig)>,
) {
for (mut cursor_icon, hotspots, mut config) in &mut query {
let CursorIcon::Custom(CustomCursor::Image(ref mut image)) = *cursor_icon else {
continue;
};
config.frame_timer.tick(time.delta());
if !config.frame_timer.finished() {
continue;
}
let Some(atlas) = image.texture_atlas.as_mut() else {
continue;
};
let mut new_index = atlas.index + 1;
if new_index > config.last_sprite_index {
new_index = config.first_sprite_index;
}
if new_index != atlas.index {
atlas.index = new_index;
info!("Changed to sprite index {}", atlas.index);
}
config.frame_timer = Timer::new(config.duration_per_frame, TimerMode::Once);
// Animation frames may have different hotspots, so we need to update
// the hotspot for each frame.
let new_hotspot = hotspots.get_or_default(atlas.index);
if new_hotspot != image.hotspot {
image.hotspot = new_hotspot;
info!("Changed to hotspot {:?}", image.hotspot);
}
}
}
fn setup_instructions(mut commands: Commands) {
commands.spawn((
Text::new(
"Press X to toggle the cursor's `flip_x` setting\n
Press Y to toggle the cursor's `flip_y` setting",
),
Node {
position_type: PositionType::Absolute,
bottom: Val::Px(12.0),
left: Val::Px(12.0),
..default()
},
));
}