fyrox_animation/signal.rs
1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Signal is a named marker on specific time position on the animation timeline. See [`AnimationSignal`] docs for more info.
22
23use crate::core::{reflect::prelude::*, uuid::Uuid, visitor::prelude::*};
24use fyrox_core::NameProvider;
25
26/// An event happened in an animation.
27#[derive(Clone, PartialEq, Eq, Debug)]
28pub struct AnimationEvent {
29 /// An id of an animation event.
30 pub signal_id: Uuid,
31
32 /// Name of the signal emitted the event.
33 pub name: String,
34}
35
36/// Signal is a named marker on specific time position on the animation timeline. Signal will emit an event if the animation playback
37/// time passes signal's position from left-to-right (or vice versa depending on playback direction). Signals are usually used to
38/// attach some specific actions to a position in time. For example, you can have a walking animation and you want to emit sounds
39/// when character's feet touch ground. In this case you need to add a few signals at times when each foot touches the ground.
40/// After that all you need to do is to fetch animation events one-by-one and emit respective sounds. See [`AnimationSignal`] docs
41/// for more info and examples.
42#[derive(Clone, Debug, Visit, Reflect, PartialEq)]
43pub struct AnimationSignal {
44 /// An id of the animation signal. Any event produced by the signal will have this id.
45 pub id: Uuid,
46
47 /// Name of the animation signal. Could be used to find the signal in a container of signals.
48 pub name: String,
49
50 /// A position (in seconds) on an animation time line.
51 pub time: f32,
52
53 /// The flag defines whether the signal is enabled or not. Disabled signals won't produce any events.
54 pub enabled: bool,
55}
56
57impl NameProvider for AnimationSignal {
58 fn name(&self) -> &str {
59 &self.name
60 }
61}
62
63impl AnimationSignal {
64 /// Creates a new enabled animation signal with a given id, name and time position.
65 pub fn new(id: Uuid, name: &str, time: f32) -> Self {
66 Self {
67 id,
68 name: name.to_owned(),
69 time,
70 enabled: true,
71 }
72 }
73}
74
75impl Default for AnimationSignal {
76 fn default() -> Self {
77 Self {
78 id: Uuid::new_v4(),
79 name: Default::default(),
80 time: 0.0,
81 enabled: true,
82 }
83 }
84}