Skip to content

Commit

Permalink
Merge pull request #701 from FyroxEngine/animation_resource
Browse files Browse the repository at this point in the history
Animation tracks data resource
  • Loading branch information
mrDIMAS authored Nov 8, 2024
2 parents 01ac019 + 91dd22c commit 558f74f
Show file tree
Hide file tree
Showing 16 changed files with 601 additions and 285 deletions.
4 changes: 2 additions & 2 deletions editor/src/absm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ impl AbsmEditor {

let mut animation_targets = FxHashSet::default();
for animation in animations.iter_mut() {
for track in animation.tracks() {
animation_targets.insert(track.target());
for track_binding in animation.track_bindings().values() {
animation_targets.insert(track_binding.target());
}
}

Expand Down
4 changes: 2 additions & 2 deletions editor/src/absm/toolbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ impl Toolbar {
animation_container_ref(graph, selection.absm_node_handle)
{
for animation in animations.iter() {
for track in animation.tracks() {
unique_nodes.insert(track.target());
for track_binding in animation.track_bindings().values() {
unique_nodes.insert(track_binding.target());
}
}
}
Expand Down
71 changes: 46 additions & 25 deletions editor/src/animation/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::{
scene::{commands::GameSceneContext, Selection},
ui_scene::commands::UiSceneContext,
};
use fyrox::generic_animation::track::TrackBinding;
use std::{
fmt::Debug,
ops::{IndexMut, Range},
Expand Down Expand Up @@ -73,19 +74,22 @@ pub fn fetch_animations_container<N: Debug + 'static>(
pub struct AddTrackCommand<N: Debug + 'static> {
animation_player: Handle<N>,
animation: Handle<Animation<Handle<N>>>,
track: Option<Track<Handle<N>>>,
track: Option<Track>,
binding: TrackBinding<Handle<N>>,
}

impl<N: Debug + 'static> AddTrackCommand<N> {
pub fn new(
animation_player: Handle<N>,
animation: Handle<Animation<Handle<N>>>,
track: Track<Handle<N>>,
track: Track,
binding: TrackBinding<Handle<N>>,
) -> Self {
Self {
animation_player,
animation,
track: Some(track),
binding,
}
}
}
Expand All @@ -97,12 +101,16 @@ impl<N: Debug + 'static> CommandTrait for AddTrackCommand<N> {

fn execute(&mut self, context: &mut dyn CommandContext) {
fetch_animations_container(self.animation_player, context)[self.animation]
.add_track(self.track.take().unwrap());
.add_track_with_binding(self.binding.clone(), self.track.take().unwrap());
}

fn revert(&mut self, context: &mut dyn CommandContext) {
self.track =
fetch_animations_container(self.animation_player, context)[self.animation].pop_track();
let (binding, track) = fetch_animations_container(self.animation_player, context)
[self.animation]
.pop_track_with_binding()
.unwrap();
self.binding = binding;
self.track = Some(track);
}
}

Expand All @@ -111,7 +119,8 @@ pub struct RemoveTrackCommand<N: Debug + 'static> {
animation_player: Handle<N>,
animation: Handle<Animation<Handle<N>>>,
id: Uuid,
track: Option<(usize, Track<Handle<N>>)>,
#[allow(clippy::type_complexity)]
track: Option<(usize, (TrackBinding<Handle<N>>, Track))>,
}

impl<N: Debug + 'static> RemoveTrackCommand<N> {
Expand All @@ -138,17 +147,22 @@ impl<N: Debug + 'static> CommandTrait for RemoveTrackCommand<N> {
let animation =
&mut fetch_animations_container(self.animation_player, context)[self.animation];
let index = animation
.tracks_data()
.state()
.data()
.unwrap()
.tracks_mut()
.iter()
.position(|t| t.id() == self.id)
.unwrap();
self.track = Some((index, animation.remove_track(index)));
self.track = Some((index, animation.remove_track_with_binding(index).unwrap()));
}

fn revert(&mut self, context: &mut dyn CommandContext) {
let (index, track) = self.track.take().unwrap();
fetch_animations_container(self.animation_player, context)[self.animation]
.insert_track(index, track);
let (index, (binding, track)) = self.track.take().unwrap();
let animation =
&mut fetch_animations_container(self.animation_player, context)[self.animation];
animation.insert_track_with_binding(index, binding, track);
}
}

Expand All @@ -161,9 +175,13 @@ pub struct ReplaceTrackCurveCommand<N: Debug + 'static> {

impl<N: Debug + 'static> ReplaceTrackCurveCommand<N> {
fn swap(&mut self, context: &mut dyn CommandContext) {
for track in
fetch_animations_container(self.animation_player, context)[self.animation].tracks_mut()
{
let animation =
&mut fetch_animations_container(self.animation_player, context)[self.animation];

let mut tracks_data_state = animation.tracks_data().state();
let tracks_data = tracks_data_state.data().unwrap();

for track in tracks_data.tracks_mut() {
for curve in track.data_container_mut().curves_mut() {
if curve.id() == self.curve.id() {
std::mem::swap(&mut self.curve, curve);
Expand Down Expand Up @@ -601,9 +619,8 @@ pub struct SetTrackEnabledCommand<N: Debug + 'static> {
impl<N: Debug + 'static> SetTrackEnabledCommand<N> {
fn swap(&mut self, context: &mut dyn CommandContext) {
let track = fetch_animation(self.animation_player_handle, self.animation_handle, context)
.tracks_mut()
.iter_mut()
.find(|t| t.id() == self.track)
.track_bindings_mut()
.get_mut(&self.track)
.unwrap();

let old = track.is_enabled();
Expand Down Expand Up @@ -637,9 +654,8 @@ pub struct SetTrackTargetCommand<N: Debug + 'static> {
impl<N: Debug + 'static> SetTrackTargetCommand<N> {
fn swap(&mut self, context: &mut dyn CommandContext) {
let track = fetch_animation(self.animation_player_handle, self.animation_handle, context)
.tracks_mut()
.iter_mut()
.find(|t| t.id() == self.track)
.track_bindings_mut()
.get_mut(&self.track)
.unwrap();

let old = track.target();
Expand All @@ -663,28 +679,33 @@ impl<N: Debug + 'static> CommandTrait for SetTrackTargetCommand<N> {
}

#[derive(Debug)]
pub struct SetTrackBindingCommand<N: Debug + 'static> {
pub struct SetTrackValueBindingCommand<N: Debug + 'static> {
pub animation_player_handle: Handle<N>,
pub animation_handle: Handle<Animation<Handle<N>>>,
pub track: Uuid,
pub binding: ValueBinding,
}

impl<N: Debug + 'static> SetTrackBindingCommand<N> {
impl<N: Debug + 'static> SetTrackValueBindingCommand<N> {
fn swap(&mut self, context: &mut dyn CommandContext) {
let track = fetch_animation(self.animation_player_handle, self.animation_handle, context)
let animation =
fetch_animation(self.animation_player_handle, self.animation_handle, context);
let mut tracks_data_state = animation.tracks_data().state();
let tracks_data = tracks_data_state.data().unwrap();

let track = tracks_data
.tracks_mut()
.iter_mut()
.find(|t| t.id() == self.track)
.unwrap();

let old = track.binding().clone();
track.set_binding(self.binding.clone());
let old = track.value_binding().clone();
track.set_value_binding(self.binding.clone());
self.binding = old;
}
}

impl<N: Debug + 'static> CommandTrait for SetTrackBindingCommand<N> {
impl<N: Debug + 'static> CommandTrait for SetTrackValueBindingCommand<N> {
fn name(&mut self, _context: &dyn CommandContext) -> String {
"Set Track Binding".to_string()
}
Expand Down
18 changes: 12 additions & 6 deletions editor/src/animation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,9 @@ impl AnimationEditor {
animation.rewind();

let animation_targets = animation
.tracks()
.iter()
.map(|t| t.target())
.track_bindings()
.values()
.map(|binding| binding.target())
.collect::<FxHashSet<_>>();

self.enter_preview_mode(
Expand Down Expand Up @@ -689,6 +689,11 @@ impl AnimationEditor {
self.track_list
.sync_to_model(animation, graph, &selection, ui);

let animation_tracks_data_state = animation.tracks_data().state();
let Some(animation_tracks_data) = animation_tracks_data_state.data_ref() else {
return;
};

send_sync_message(
ui,
CurveEditorMessage::hightlight_zones(
Expand Down Expand Up @@ -729,7 +734,8 @@ impl AnimationEditor {
SelectedEntity::Track(track_id) => {
// If a track is selected, show all its curves at once. This way it will
// be easier to edit complex values, such as Vector2/3/4.
if let Some(track) = animation

if let Some(track) = animation_tracks_data
.tracks()
.iter()
.find(|track| &track.id() == track_id)
Expand All @@ -748,7 +754,7 @@ impl AnimationEditor {
}
SelectedEntity::Curve(curve_id) => {
if let Some((index, selected_curve)) =
animation.tracks().iter().find_map(|t| {
animation_tracks_data.tracks().iter().find_map(|t| {
t.data_container().curves_ref().iter().enumerate().find_map(
|(i, c)| {
if &c.id() == curve_id {
Expand All @@ -772,7 +778,7 @@ impl AnimationEditor {
}
}
let mut background_curves = Vec::<Curve>::new();
for track in animation.tracks() {
for track in animation_tracks_data.tracks() {
for curve in track.data_container().curves_ref() {
if !selected_curves.iter().any(|(_, c)| c.id == curve.id) {
background_curves.push(curve.clone());
Expand Down
Loading

0 comments on commit 558f74f

Please sign in to comment.