fyrox_animation/machine/event.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//! State machine could produces a fixed set of events during its work, this module contains all the stuff
22//! needed to works with such events.
23
24use crate::{
25 core::pool::Handle,
26 machine::{State, Transition},
27 EntityId,
28};
29use std::collections::VecDeque;
30
31/// Specific state machine event.
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub enum Event<T: EntityId> {
34 /// Occurs when enter some state. See module docs for example.
35 StateEnter(Handle<State<T>>),
36
37 /// Occurs when leaving some state. See module docs for example.
38 StateLeave(Handle<State<T>>),
39
40 /// Occurs when a transition is done and a new active state was set.
41 ActiveStateChanged {
42 /// Previously active state.
43 prev: Handle<State<T>>,
44
45 /// New active state.
46 new: Handle<State<T>>,
47 },
48
49 /// Occurs when active transition was changed.
50 ActiveTransitionChanged(Handle<Transition<T>>),
51}
52
53/// A simple event queue with fixed capacity. It is used to store a fixed amount of events and discard any
54/// events when the queue is full.
55#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct FixedEventQueue<T: EntityId> {
57 queue: VecDeque<Event<T>>,
58 limit: u32,
59}
60
61impl<T: EntityId> Default for FixedEventQueue<T> {
62 fn default() -> Self {
63 Self {
64 queue: Default::default(),
65 limit: u32::MAX,
66 }
67 }
68}
69
70impl<T: EntityId> FixedEventQueue<T> {
71 /// Creates a new queue with given limit.
72 pub fn new(limit: u32) -> Self {
73 Self {
74 queue: VecDeque::with_capacity(limit as usize),
75 limit,
76 }
77 }
78
79 /// Pushes an event to the queue.
80 pub fn push(&mut self, event: Event<T>) {
81 if self.queue.len() < (self.limit as usize) {
82 self.queue.push_back(event);
83 }
84 }
85
86 /// Pops an event from the queue.
87 pub fn pop(&mut self) -> Option<Event<T>> {
88 self.queue.pop_front()
89 }
90}