bms_rs/bms/model/
scroll.rs

1//! This module introduces struct [`ScrollObjects`], which manages definitions and events of scroll speed change.
2
3use std::collections::{BTreeMap, HashMap, btree_map::Entry};
4
5use crate::{
6    bms::{error::Result, prelude::*},
7    parse::prompt::ChannelDuplication,
8};
9
10#[derive(Debug, Default, Clone, PartialEq, Eq)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12/// This aggregate manages definition and events of scroll speed change.
13pub struct ScrollObjects {
14    /// Scroll speed change definitions, indexed by [`ObjId`]. `#SCROLL[01-ZZ]`
15    pub scroll_defs: HashMap<ObjId, Decimal>,
16    /// The scrolling factors corresponding to the id of the scroll speed change object.
17    pub scrolling_factor_changes: BTreeMap<ObjTime, ScrollingFactorObj>,
18}
19
20impl ScrollObjects {
21    /// Adds a new scrolling factor change object to the notes.
22    pub fn push_scrolling_factor_change(
23        &mut self,
24        scrolling_factor_change: ScrollingFactorObj,
25        prompt_handler: &impl Prompter,
26    ) -> Result<()> {
27        match self
28            .scrolling_factor_changes
29            .entry(scrolling_factor_change.time)
30        {
31            Entry::Vacant(entry) => {
32                entry.insert(scrolling_factor_change);
33                Ok(())
34            }
35            Entry::Occupied(mut entry) => {
36                let existing = entry.get();
37
38                prompt_handler
39                    .handle_channel_duplication(ChannelDuplication::ScrollingFactorChangeEvent {
40                        time: scrolling_factor_change.time,
41                        older: existing,
42                        newer: &scrolling_factor_change,
43                    })
44                    .apply_channel(
45                        entry.get_mut(),
46                        scrolling_factor_change.clone(),
47                        scrolling_factor_change.time,
48                        Channel::Scroll,
49                    )
50            }
51        }
52    }
53}