Skip to content

Commit

Permalink
Merge pull request #42 from kahboon0425/origin/feature/play&pause
Browse files Browse the repository at this point in the history
Play Pause button & Slider
  • Loading branch information
kahboon0425 authored Jul 21, 2024
2 parents 8aceb7c + 1c4a716 commit 52108f4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/bvh/bvh_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::{
};
use bevy_bvh_anim::prelude::*;

use crate::scene_loader::MainScene;
use crate::{scene_loader::MainScene, ui::PlaybackState};

pub struct BvhPlayerPlugin;

Expand Down Expand Up @@ -167,13 +167,15 @@ fn generate_bone_map(
}
}

#[allow(clippy::too_many_arguments)]
fn bvh_player(
mut q_transforms: Query<&mut Transform, Without<MainScene>>,
mut q_scene: Query<(&mut Transform, &BoneMap), With<MainScene>>,
mut event_reader: EventReader<TargetTimeEvent>,
time: Res<Time>,
selected_bvh_asset: Res<SelectedBvhAsset>,
bvh_asset: Res<Assets<BvhAsset>>,
mut playback_state: ResMut<PlaybackState>,
mut local_time: Local<f32>,
) {
let Some(bvh) = bvh_asset.get(selected_bvh_asset.0) else {
Expand Down Expand Up @@ -250,7 +252,13 @@ fn bvh_player(
}
}

*local_time += time.delta_seconds();
// Should not do anything is current_time has not been mutated anywhere else,
// otherwise, local_time will be set to the mutated value.
*local_time = playback_state.current_time;
if playback_state.is_playing {
*local_time += time.delta_seconds();
playback_state.current_time = *local_time % playback_state.duration
}
}

pub fn get_pose(local_time: f32, bvh_data: &Bvh) -> (usize, f32) {
Expand Down
50 changes: 45 additions & 5 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl Plugin for UiPlugin {
app.init_resource::<MouseInUi>()
.init_resource::<ShowDrawArrow>()
.init_resource::<BuildConfig>()
.init_resource::<PlaybackState>()
.add_systems(PreUpdate, reset_mouse_in_ui)
.add_systems(Update, right_panel.in_set(UiSystemSet));
}
Expand Down Expand Up @@ -66,15 +67,23 @@ pub struct BuildConfig {
pub bvh_assets: HashSet<AssetId<BvhAsset>>,
}

#[derive(Resource, Default)]
pub struct PlaybackState {
pub is_playing: bool,
pub current_time: f32,
pub duration: f32,
}

fn reset_mouse_in_ui(mut mouse_in_ui: ResMut<MouseInUi>) {
mouse_in_ui.0 = false;
}

fn bvh_selection_menu(
fn bvh_playback_menu(
ui: &mut egui::Ui,
asset_server: &AssetServer,
bvh_assets: &Assets<BvhAsset>,
selected_bvh_asset: &mut SelectedBvhAsset,
playback_state: &mut PlaybackState,
) {
ui.horizontal(|ui| {
ui.label("Choose Bvh File:");
Expand All @@ -93,10 +102,33 @@ fn bvh_selection_menu(
};
if ui.selectable_label(false, bvh_name.to_string()).clicked() {
selected_bvh_asset.0 = id;
if let Some(bvh) = bvh_assets.get(id).map(|asset| asset.get()) {
playback_state.duration =
bvh.frame_time().as_secs_f32() * bvh.num_frames() as f32;
}
}
}
});
});

ui.add_space(10.0);
ui.label("Playback State");
ui.horizontal(|ui| {
let button_icon = match playback_state.is_playing {
true => "Pause",
false => "Play",
};

if ui.button(button_icon).clicked() {
playback_state.is_playing = !playback_state.is_playing;
}

let playback_duration = playback_state.duration;
ui.add(egui::Slider::new(
&mut playback_state.current_time,
0.0..=playback_duration,
));
});
}

fn arrow_checkbox(ui: &mut egui::Ui, show_draw_arrow: &mut ShowDrawArrow) {
Expand All @@ -112,7 +144,7 @@ fn bvh_map_label(ui: &mut egui::Ui, bvh_library: &Res<BvhLibrary>) {
});
}

fn bvh_map_config(ui: &mut egui::Ui, bvh_library: &Res<BvhLibrary>, bvh_asset: &Assets<BvhAsset>) {
fn _bvh_map_config(ui: &mut egui::Ui, bvh_library: &Res<BvhLibrary>, bvh_asset: &Assets<BvhAsset>) {
ui.vertical(|ui| {
let Some(asset) = bvh_library.get_map().and_then(|id| bvh_asset.get(id)) else {
return;
Expand Down Expand Up @@ -203,6 +235,7 @@ fn right_panel(
asset_server: Res<AssetServer>,
bvh_assets: Res<Assets<BvhAsset>>,
bvh_library: Res<BvhLibrary>,
mut playback_state: ResMut<PlaybackState>,
mut page: Local<RightPanelPage>,
mut mouse_in_ui: ResMut<MouseInUi>,
) {
Expand Down Expand Up @@ -233,11 +266,18 @@ fn right_panel(
ui.heading("Configurations");
ui.add_space(10.0);
bvh_map_label(ui, &bvh_library);
bvh_selection_menu(ui, &asset_server, &bvh_assets, &mut selected_bvh_asset);
bvh_map_config(ui, &bvh_library, &bvh_assets);
bvh_playback_menu(
ui,
&asset_server,
&bvh_assets,
&mut selected_bvh_asset,
&mut playback_state,
);
ui.add_space(10.0);
// bvh_map_config(ui, &bvh_library, &bvh_assets);
}
RightPanelPage::Builder => {
ui.heading("Buidler");
ui.heading("Builder");
ui.add_space(10.0);
bvh_buider_menu(ui, &asset_server, &bvh_assets, &mut build_configs);
ui.add_space(10.0);
Expand Down

0 comments on commit 52108f4

Please sign in to comment.