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
use bevy::prelude::*;
use bevy::window::PrimaryWindow;
use bevy_c3d::prelude::*;
use bevy_egui::EguiContext;
use c3dio::c3d;
use egui::widgets::Button;
use egui::DragValue;
use egui::RichText;
use egui::Slider;
use egui::TopBottomPanel;
pub struct BottomMenuPlugin;
impl Plugin for BottomMenuPlugin {
fn build(&self, app: &mut App) {
app.add_systems(PreUpdate, update_frame)
.add_systems(Update, bottom_menu_system)
.add_systems(Last, update_c3d_frame)
.init_resource::<PlayerControl>();
}
}
#[derive(Resource)]
pub struct PlayerControl {
pub is_playing: bool,
pub loop_playback: bool,
pub playback_speed: f32,
}
impl Default for PlayerControl {
fn default() -> Self {
Self {
is_playing: false,
loop_playback: false,
playback_speed: 1.,
}
}
}
#[derive(Resource, Default, Debug)]
pub struct C3dFrame {
frame: f32,
updated_frame: bool,
new_frame: f32,
}
impl C3dFrame {
pub fn update_frame(&mut self, frame: f32) {
self.new_frame = frame;
}
pub fn frame(&self) -> f32 {
self.frame
}
pub fn updated(&self) -> bool {
self.updated_frame
}
}
pub fn update_c3d_frame(mut c3d_frame: ResMut<C3dFrame>) {
c3d_frame.updated_frame = c3d_frame.new_frame != c3d_frame.frame;
if c3d_frame.updated_frame {
c3d_frame.frame = c3d_frame.new_frame;
}
}
pub fn update_frame(
mut player_control: ResMut<PlayerControl>,
mut c3d_frame: ResMut<C3dFrame>,
c3d_state: Res<C3dState>,
c3d_assets: Res<Assets<C3dAsset>>,
time: Res<Time>,
) {
if !c3d_state.loaded {
return;
}
let asset = c3d_assets.get(&c3d_state.handle);
match asset {
Some(asset) => {
if player_control.is_playing {
c3d_frame.frame = c3d_frame.frame
+ (time.delta().as_secs_f32()
* asset.c3d.points.frame_rate
* player_control.playback_speed) as f32;
c3d_frame.new_frame = c3d_frame.frame;
c3d_frame.updated_frame = true;
}
if c3d_frame.frame() >= asset.c3d.points.rows() as f32 {
if player_control.loop_playback {
c3d_frame.frame = 0.;
c3d_frame.new_frame = c3d_frame.frame;
c3d_frame.updated_frame = true;
} else {
c3d_frame.frame = (asset.c3d.points.rows() - 1) as f32;
c3d_frame.new_frame = c3d_frame.frame;
c3d_frame.updated_frame = true;
player_control.is_playing = false;
}
}
}
None => {}
}
}
pub fn bottom_menu_system(world: &mut World) {
let Ok(egui_context) = world
.query_filtered::<&mut EguiContext, With<PrimaryWindow>>()
.get_single_mut(world)
else {
return;
};
let mut ctx = egui_context.clone();
world.resource_scope::<PlayerControl, _>(|world, mut player_control| {
world.resource_scope::<C3dFrame, _>(|world, mut c3d_frame| {
world.resource_scope::<C3dState, _>(|world, c3d_state| {
let c3d_asset = world.get_resource::<Assets<C3dAsset>>();
let max_frames = match c3d_asset {
Some(c3d_asset) => {
let c3d_asset = c3d_asset.get(&c3d_state.handle);
match c3d_asset {
Some(c3d_asset) => c3d_asset.c3d.points.rows() as f32,
None => 0.,
}
}
None => 0.,
};
TopBottomPanel::bottom("bottom_panel")
.exact_height(48.)
.show(ctx.get_mut(), |ui| {
ui.horizontal_centered(|ui| {
// ui.button(RichText::new("⏮").size(24.))
// .on_hover_text("First frame");
if ui
.button(RichText::new("⏪").size(24.))
.on_hover_text("Previous frame")
.clicked()
{
if c3d_frame.frame() > 0. {
let frame = c3d_frame.frame();
c3d_frame.update_frame(frame - 1.);
if player_control.is_playing {
player_control.is_playing = false;
}
}
}
if ui
.add(
Button::new(RichText::new("▶").size(24.))
.selected(player_control.is_playing),
)
.on_hover_text("Play/Pause")
.clicked()
{
player_control.is_playing = !player_control.is_playing;
}
if ui
.button(RichText::new("⏩").size(24.))
.on_hover_text("Next frame")
.clicked()
{
let frame = c3d_frame.frame();
c3d_frame.update_frame(frame + 1.);
if player_control.is_playing {
player_control.is_playing = false;
}
}
if ui
.add(
Button::new(RichText::new("🔁").size(24.))
.selected(player_control.loop_playback),
)
.on_hover_text("Loop playback")
.clicked()
{
player_control.loop_playback = !player_control.loop_playback;
}
// ui.button(RichText::new("⏭").size(24.))
// .on_hover_text("Last frame");
ui.separator();
ui.label("Speed:");
ui.add(
DragValue::new(&mut player_control.playback_speed)
.speed(0.025)
.clamp_range(0.05..=1.),
);
ui.separator();
ui.label("Frame:");
//https://github.com/emilk/egui/discussions/3908
ui.spacing_mut().slider_width = 300.0;
ui.add(
Slider::from_get_set(0.0..=max_frames as f64, |x| {
match x {
Some(x) => {
c3d_frame.update_frame(x as f32);
}
None => {}
}
c3d_frame.frame as f64
})
.clamp_to_range(true)
.fixed_decimals(0)
.trailing_fill(true)
.handle_shape(egui::style::HandleShape::Rect {
aspect_ratio: 0.5,
}),
);
});
});
});
});
});
}