1use indexmap::IndexMap;
4#[cfg(feature = "serialization")]
5use serde::{Deserialize, Serialize};
6
7use crate::parse_ctx::{FromBlock, ParseCtx, impl_try_from_block};
8use crate::utils::{take_key, take_multi_parse, take_opt_parse};
9use crate::{Extras, VmfBlock, VmfSerializable, join_repeated_key, utils::To01String};
10
11#[derive(Debug, Clone, PartialEq, Eq)]
13#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
14pub struct Editor {
15 pub color: String,
17 #[cfg_attr(
19 feature = "serialization",
20 serde(default, skip_serializing_if = "Vec::is_empty")
21 )]
22 pub visgroup_ids: Vec<i32>,
23 #[cfg_attr(
25 feature = "serialization",
26 serde(default, skip_serializing_if = "Option::is_none")
27 )]
28 pub group_id: Option<i32>,
29 pub visgroup_shown: bool,
31 pub visgroup_auto_shown: bool,
33 #[cfg_attr(
35 feature = "serialization",
36 serde(default, skip_serializing_if = "Option::is_none")
37 )]
38 pub comments: Option<String>,
39 #[cfg_attr(
41 feature = "serialization",
42 serde(default, skip_serializing_if = "Option::is_none")
43 )]
44 pub logical_pos: Option<String>,
45 #[cfg_attr(feature = "serialization", serde(default, skip_serializing_if = "Extras::is_empty"))]
47 pub extra: Extras,
48}
49
50impl Default for Editor {
51 fn default() -> Self {
52 Self {
53 color: "255 255 255".to_string(),
54 visgroup_ids: Vec::new(),
55 group_id: None,
56 visgroup_shown: true,
57 visgroup_auto_shown: true,
58 comments: None,
59 logical_pos: None,
60 extra: Extras::default(),
61 }
62 }
63}
64
65impl Editor {
66 pub fn join_visgroup(&mut self, id: i32) -> bool {
68 if self.visgroup_ids.contains(&id) {
69 return false;
70 }
71 self.visgroup_ids.push(id);
72 true
73 }
74
75 pub fn leave_visgroup(&mut self, id: i32) -> bool {
77 let before = self.visgroup_ids.len();
78 self.visgroup_ids.retain(|&member| member != id);
79 before != self.visgroup_ids.len()
80 }
81}
82
83impl FromBlock for Editor {
84 fn from_block(mut block: VmfBlock, ctx: &mut ParseCtx) -> Self {
85 let blocks = std::mem::take(&mut block.blocks);
86 let kv = &mut block.key_values;
87
88 Self {
89 color: take_key(kv, "color").unwrap_or_else(|| "255 255 255".to_string()),
90 visgroup_ids: take_multi_parse::<i32>(kv, "visgroupid", ctx),
91 group_id: take_opt_parse::<i32>(kv, "groupid", ctx),
92 visgroup_shown: take_key(kv, "visgroupshown").is_some_and(|v| v == "1"),
93 visgroup_auto_shown: take_key(kv, "visgroupautoshown").is_some_and(|v| v == "1"),
94 comments: take_key(kv, "comments"),
95 logical_pos: take_key(kv, "logicalpos"),
96 extra: Extras {
97 key_values: std::mem::take(kv),
98 blocks,
99 },
100 }
101 }
102}
103
104impl_try_from_block!(Editor);
105
106impl From<Editor> for VmfBlock {
107 fn from(val: Editor) -> VmfBlock {
108 let mut key_values = IndexMap::new();
109 key_values.insert("color".to_string(), val.color);
110 if !val.visgroup_ids.is_empty() {
111 key_values.insert("visgroupid".to_string(), join_repeated_key(val.visgroup_ids));
112 }
113 if let Some(group_id) = val.group_id {
114 key_values.insert("groupid".to_string(), group_id.to_string());
115 }
116 key_values.insert(
117 "visgroupshown".to_string(),
118 val.visgroup_shown.to_01_string(),
119 );
120 key_values.insert(
121 "visgroupautoshown".to_string(),
122 val.visgroup_auto_shown.to_01_string(),
123 );
124 if let Some(comments) = val.comments {
125 key_values.insert("comments".to_string(), comments);
126 }
127 if let Some(logical_pos) = val.logical_pos {
128 key_values.insert("logicalpos".to_string(), logical_pos);
129 }
130 key_values.extend(val.extra.key_values);
131
132 VmfBlock {
133 name: "editor".to_string(),
134 key_values,
135 blocks: val.extra.blocks,
136 }
137 }
138}
139
140impl VmfSerializable for Editor {
141 fn to_vmf_string(&self, indent_level: usize) -> String {
142 let indent = "\t".repeat(indent_level);
143 let mut output = String::with_capacity(128);
144
145 output.push_str(&format!("{0}editor\n{0}{{\n", indent));
146 output.push_str(&format!("{}\t\"color\" \"{}\"\n", indent, self.color));
147 for visgroup_id in &self.visgroup_ids {
148 output.push_str(&format!("{}\t\"visgroupid\" \"{}\"\n", indent, visgroup_id));
149 }
150 if let Some(group_id) = self.group_id {
151 output.push_str(&format!("{}\t\"groupid\" \"{}\"\n", indent, group_id));
152 }
153 output.push_str(&format!(
154 "{}\t\"visgroupshown\" \"{}\"\n",
155 indent,
156 self.visgroup_shown.to_01_string()
157 ));
158 output.push_str(&format!(
159 "{}\t\"visgroupautoshown\" \"{}\"\n",
160 indent,
161 self.visgroup_auto_shown.to_01_string()
162 ));
163 if let Some(comments) = &self.comments {
164 output.push_str(&format!("{}\t\"comments\" \"{}\"\n", indent, comments));
165 }
166 if let Some(logical_pos) = &self.logical_pos {
167 output.push_str(&format!("{}\t\"logicalpos\" \"{}\"\n", indent, logical_pos));
168 }
169 self.extra.write_key_values(&mut output, indent_level + 1);
170 self.extra.write_blocks(&mut output, indent_level + 1);
171
172 output.push_str(&format!("{}}}\n", indent));
173 output
174 }
175}