livesplit_core/component/
current_comparison.rs

1//! Provides the Current Comparison Component and relevant types for using it.
2//! The Current Comparison Component is a component that shows the name of the
3//! comparison that is currently selected to be compared against.
4
5use super::key_value;
6use crate::{
7    platform::prelude::*,
8    settings::{Color, Field, Gradient, SettingsDescription, Value},
9    Timer,
10};
11use serde::{Deserialize, Serialize};
12
13/// The Current Comparison Component is a component that shows the name of the
14/// comparison that is currently selected to be compared against.
15#[derive(Default, Clone)]
16pub struct Component {
17    settings: Settings,
18}
19
20/// The Settings for this component.
21#[derive(Clone, Serialize, Deserialize)]
22#[serde(default)]
23pub struct Settings {
24    /// The background shown behind the component.
25    pub background: Gradient,
26    /// Specifies whether to display the name of the component and its value in
27    /// two separate rows.
28    pub display_two_rows: bool,
29    /// The color of the label. If `None` is specified, the color is taken from
30    /// the layout.
31    pub label_color: Option<Color>,
32    /// The color of the value. If `None` is specified, the color is taken from
33    /// the layout.
34    pub value_color: Option<Color>,
35}
36
37impl Default for Settings {
38    fn default() -> Self {
39        Self {
40            background: key_value::DEFAULT_GRADIENT,
41            display_two_rows: false,
42            label_color: None,
43            value_color: None,
44        }
45    }
46}
47
48impl Component {
49    /// Creates a new Current Comparison Component.
50    pub fn new() -> Self {
51        Default::default()
52    }
53
54    /// Creates a new Current Comparison Component with the given settings.
55    pub const fn with_settings(settings: Settings) -> Self {
56        Self { settings }
57    }
58
59    /// Accesses the settings of the component.
60    pub const fn settings(&self) -> &Settings {
61        &self.settings
62    }
63
64    /// Grants mutable access to the settings of the component.
65    pub fn settings_mut(&mut self) -> &mut Settings {
66        &mut self.settings
67    }
68
69    /// Accesses the name of the component.
70    pub const fn name(&self) -> &'static str {
71        "Current Comparison"
72    }
73
74    /// Updates the component's state based on the timer provided.
75    pub fn update_state(&self, state: &mut key_value::State, timer: &Timer) {
76        state.background = self.settings.background;
77        state.key_color = self.settings.label_color;
78        state.value_color = self.settings.value_color;
79        state.semantic_color = Default::default();
80
81        state.key.clear();
82        state.key.push_str("Comparing Against");
83
84        state.value.clear();
85        state.value.push_str(timer.current_comparison());
86
87        state.key_abbreviations.clear();
88        state.key_abbreviations.push("Comparison".into());
89
90        state.display_two_rows = self.settings.display_two_rows;
91        state.updates_frequently = false;
92    }
93
94    /// Calculates the component's state based on the timer provided.
95    pub fn state(&self, timer: &Timer) -> key_value::State {
96        let mut state = Default::default();
97        self.update_state(&mut state, timer);
98        state
99    }
100
101    /// Accesses a generic description of the settings available for this
102    /// component and their current values.
103    pub fn settings_description(&self) -> SettingsDescription {
104        SettingsDescription::with_fields(vec![
105            Field::new("Background".into(), self.settings.background.into()),
106            Field::new(
107                "Display 2 Rows".into(),
108                self.settings.display_two_rows.into(),
109            ),
110            Field::new("Label Color".into(), self.settings.label_color.into()),
111            Field::new("Value Color".into(), self.settings.value_color.into()),
112        ])
113    }
114
115    /// Sets a setting's value by its index to the given value.
116    ///
117    /// # Panics
118    ///
119    /// This panics if the type of the value to be set is not compatible with
120    /// the type of the setting's value. A panic can also occur if the index of
121    /// the setting provided is out of bounds.
122    pub fn set_value(&mut self, index: usize, value: Value) {
123        match index {
124            0 => self.settings.background = value.into(),
125            1 => self.settings.display_two_rows = value.into(),
126            2 => self.settings.label_color = value.into(),
127            3 => self.settings.value_color = value.into(),
128            _ => panic!("Unsupported Setting Index"),
129        }
130    }
131}