1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::collections::HashMap;
use std::io::Write;
use enum_map::Enum;
use strum::EnumProperty;
use xml::attribute::OwnedAttribute;
use xml::writer::events::StartElementBuilder;
use xml::writer::XmlEvent as XmlWriterEvent;
use xml::EventWriter;
use crate::components::colours::{ColourMap, ColourState};
use crate::components::mute::MuteFunction;
use crate::components::mute_chat::CoughToggle::Hold;
#[derive(thiserror::Error, Debug)]
#[allow(clippy::enum_variant_names)]
pub enum ParseError {
#[error("Expected int: {0}")]
ExpectedInt(#[from] std::num::ParseIntError),
#[error("Expected float: {0}")]
ExpectedFloat(#[from] std::num::ParseFloatError),
#[error("Expected enum: {0}")]
ExpectedEnum(#[from] strum::ParseError),
#[error("Invalid colours: {0}")]
InvalidColours(#[from] crate::components::colours::ParseError),
}
use std::str::FromStr;
#[derive(Debug)]
pub struct MuteChat {
element_name: String,
colour_map: ColourMap,
mic_fader_id: u8,
blink: ColourState,
cough_behaviour: CoughToggle,
cough_mute_source: MuteFunction,
cough_button_on: bool,
}
impl MuteChat {
pub fn new(element_name: String) -> Self {
let colour_map = element_name.clone();
Self {
element_name,
colour_map: ColourMap::new(colour_map),
mic_fader_id: 4,
blink: ColourState::Off,
cough_behaviour: CoughToggle::Hold,
cough_mute_source: MuteFunction::All,
cough_button_on: false,
}
}
pub fn parse_mute_chat(&mut self, attributes: &[OwnedAttribute]) -> Result<(), ParseError> {
for attr in attributes {
if attr.name.local_name == "micIsAnActiveFader" {
self.mic_fader_id = attr.value.parse()?;
continue;
}
if attr.name.local_name == "coughButtonToggleSetting" {
self.cough_behaviour = if attr.value == "0" {
CoughToggle::Hold
} else {
CoughToggle::Toggle
};
continue;
}
if attr.name.local_name == "coughButtonMuteSourceSelection" {
self.cough_mute_source = MuteFunction::from_usize(attr.value.parse()?);
continue;
}
if attr.name.local_name == "coughButtonIsOn" {
self.cough_button_on = attr.value != "0";
continue;
}
if attr.name.local_name == "blink" {
self.blink = ColourState::from_str(&attr.value)?;
continue;
}
if !self.colour_map.read_colours(attr)? {
println!("[{}] Unparsed Attribute: {}", self.element_name, attr.name);
}
}
Ok(())
}
pub fn write_mute_chat<W: Write>(
&self,
writer: &mut EventWriter<&mut W>,
) -> Result<(), xml::writer::Error> {
let mut element: StartElementBuilder =
XmlWriterEvent::start_element(self.element_name.as_str());
let mut attributes: HashMap<String, String> = HashMap::default();
attributes.insert(
"micIsAnActiveFader".to_string(),
format!("{}", self.mic_fader_id),
);
attributes.insert(
"coughButtonToggleSetting".to_string(),
if self.cough_behaviour == Hold {
"0".to_string()
} else {
"1".to_string()
},
);
attributes.insert(
"coughButtonMuteSourceSelection".to_string(),
self.cough_mute_source
.get_str("uiIndex")
.unwrap()
.to_string(),
);
attributes.insert(
"coughButtonIsOn".to_string(),
if self.cough_button_on {
"1".to_string()
} else {
"0".to_string()
},
);
attributes.insert("blink".to_string(), self.blink.to_string());
self.colour_map.write_colours(&mut attributes);
for (key, value) in &attributes {
element = element.attr(key.as_str(), value.as_str());
}
writer.write(element)?;
writer.write(XmlWriterEvent::end_element())?;
Ok(())
}
pub fn colour_map(&self) -> &ColourMap {
&self.colour_map
}
pub fn colour_map_mut(&mut self) -> &mut ColourMap {
&mut self.colour_map
}
pub fn is_cough_toggle(&self) -> bool {
self.cough_behaviour == CoughToggle::Toggle
}
pub fn mic_fader_id(&self) -> u8 {
self.mic_fader_id
}
pub fn blink(&self) -> &ColourState {
&self.blink
}
pub fn cough_behaviour(&self) -> &CoughToggle {
&self.cough_behaviour
}
pub fn cough_mute_source(&self) -> &MuteFunction {
&self.cough_mute_source
}
pub fn cough_button_on(&self) -> bool {
self.cough_button_on
}
pub fn set_blink(&mut self, blink: ColourState) {
self.blink = blink;
}
pub fn set_blink_on(&mut self, blink: bool) {
if blink {
self.blink = ColourState::On;
} else {
self.blink = ColourState::Off;
}
}
pub fn get_blink_on(&self) -> bool {
return self.blink == ColourState::On;
}
pub fn set_cough_mute_source(&mut self, cough_mute_source: MuteFunction) {
self.cough_mute_source = cough_mute_source;
}
pub fn set_cough_button_on(&mut self, cough_button_on: bool) {
self.cough_button_on = cough_button_on;
}
pub fn get_cough_button_on(&self) -> bool {
self.cough_button_on
}
pub fn set_mic_fader_id(&mut self, mic_fader_id: u8) {
self.mic_fader_id = mic_fader_id;
}
}
#[derive(PartialEq, Debug)]
pub enum CoughToggle {
Hold,
Toggle,
}