1use std::collections::VecDeque;
23#[derive(Clone, Debug, PartialEq)]
4#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
5pub struct Settings {
6/// Maximum number of undos.
7 /// If your state is resource intensive, you should keep this low.
8 ///
9 /// Default: `100`
10pub max_undos: usize,
1112/// When that state hasn't changed for this many seconds,
13 /// create a new undo point (if one is needed).
14 ///
15 /// Default value: `1.0` seconds.
16pub stable_time: f32,
1718/// If the state is changing so often that we never get to `stable_time`,
19 /// then still create a save point every `auto_save_interval` seconds,
20 /// so we have something to undo to.
21 ///
22 /// Default value: `30` seconds.
23pub auto_save_interval: f32,
24}
2526impl Default for Settings {
27fn default() -> Self {
28Self {
29 max_undos: 100,
30 stable_time: 1.0,
31 auto_save_interval: 30.0,
32 }
33 }
34}
3536/// Automatic undo system.
37///
38/// Every frame you feed it the most recent state.
39/// The [`Undoer`] compares it with the latest undo point
40/// and if there is a change it may create a new undo point.
41///
42/// [`Undoer`] follows two simple rules:
43///
44/// 1) If the state has changed since the latest undo point, but has
45/// remained stable for `stable_time` seconds, an new undo point is created.
46/// 2) If the state does not stabilize within `auto_save_interval` seconds, an undo point is created.
47///
48/// Rule 1) will make sure an undo point is not created until you _stop_ dragging that slider.
49/// Rule 2) will make sure that you will get some undo points even if you are constantly changing the state.
50#[derive(Clone)]
51#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
52pub struct Undoer<State> {
53 settings: Settings,
5455/// New undoes are added to the back.
56 /// Two adjacent undo points are never equal.
57 /// The latest undo point may (often) be the current state.
58undos: VecDeque<State>,
5960/// Stores redos immediately after a sequence of undos.
61 /// Gets cleared every time the state changes.
62 /// Does not need to be a deque, because there can only be up to `undos.len()` redos,
63 /// which is already limited to `settings.max_undos`.
64redos: Vec<State>,
6566#[cfg_attr(feature = "serde", serde(skip))]
67flux: Option<Flux<State>>,
68}
6970impl<State> std::fmt::Debug for Undoer<State> {
71fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72let Self { undos, redos, .. } = self;
73 f.debug_struct("Undoer")
74 .field("undo count", &undos.len())
75 .field("redo count", &redos.len())
76 .finish()
77 }
78}
7980impl<State> Default for Undoer<State>
81where
82State: Clone + PartialEq,
83{
84#[inline]
85fn default() -> Self {
86Self {
87 settings: Settings::default(),
88 undos: VecDeque::new(),
89 redos: Vec::new(),
90 flux: None,
91 }
92 }
93}
9495/// Represents how the current state is changing
96#[derive(Clone)]
97struct Flux<State> {
98 start_time: f64,
99 latest_change_time: f64,
100 latest_state: State,
101}
102103impl<State> Undoer<State>
104where
105State: Clone + PartialEq,
106{
107/// Create a new [`Undoer`] with the given [`Settings`].
108pub fn with_settings(settings: Settings) -> Self {
109Self {
110 settings,
111 ..Default::default()
112 }
113 }
114115/// Do we have an undo point different from the given state?
116pub fn has_undo(&self, current_state: &State) -> bool {
117match self.undos.len() {
1180 => false,
1191 => self.undos.back() != Some(current_state),
120_ => true,
121 }
122 }
123124pub fn has_redo(&self, current_state: &State) -> bool {
125 !self.redos.is_empty() && self.undos.back() == Some(current_state)
126 }
127128/// Return true if the state is currently changing
129pub fn is_in_flux(&self) -> bool {
130self.flux.is_some()
131 }
132133pub fn undo(&mut self, current_state: &State) -> Option<&State> {
134if self.has_undo(current_state) {
135self.flux = None;
136137if self.undos.back() == Some(current_state) {
138self.redos.push(self.undos.pop_back().unwrap());
139 } else {
140self.redos.push(current_state.clone());
141 }
142143// Note: we keep the undo point intact.
144self.undos.back()
145 } else {
146None
147}
148 }
149150pub fn redo(&mut self, current_state: &State) -> Option<&State> {
151if !self.undos.is_empty() && self.undos.back() != Some(current_state) {
152// state changed since the last undo, redos should be cleared.
153self.redos.clear();
154None
155} else if let Some(state) = self.redos.pop() {
156self.undos.push_back(state);
157self.undos.back()
158 } else {
159None
160}
161 }
162163/// Add an undo point if, and only if, there has been a change since the latest undo point.
164pub fn add_undo(&mut self, current_state: &State) {
165if self.undos.back() != Some(current_state) {
166self.undos.push_back(current_state.clone());
167 }
168while self.undos.len() > self.settings.max_undos {
169self.undos.pop_front();
170 }
171self.flux = None;
172 }
173174/// Call this as often as you want (e.g. every frame)
175 /// and [`Undoer`] will determine if a new undo point should be created.
176 ///
177 /// * `current_time`: current time in seconds.
178pub fn feed_state(&mut self, current_time: f64, current_state: &State) {
179match self.undos.back() {
180None => {
181// First time feed_state is called.
182 // always create an undo point:
183self.add_undo(current_state);
184 }
185Some(latest_undo) => {
186if latest_undo == current_state {
187self.flux = None;
188 } else {
189self.redos.clear();
190191match self.flux.as_mut() {
192None => {
193self.flux = Some(Flux {
194 start_time: current_time,
195 latest_change_time: current_time,
196 latest_state: current_state.clone(),
197 });
198 }
199Some(flux) => {
200if &flux.latest_state == current_state {
201let time_since_latest_change =
202 (current_time - flux.latest_change_time) as f32;
203if time_since_latest_change >= self.settings.stable_time {
204self.add_undo(current_state);
205 }
206 } else {
207let time_since_flux_start = (current_time - flux.start_time) as f32;
208if time_since_flux_start >= self.settings.auto_save_interval {
209self.add_undo(current_state);
210 } else {
211 flux.latest_change_time = current_time;
212 flux.latest_state = current_state.clone();
213 }
214 }
215 }
216 }
217 }
218 }
219 }
220 }
221}