···
144
144
}
145
145
}
146
146
147
147
+
pub fn objects_with_tag(
148
148
+
&mut self,
149
149
+
tag: impl Display,
150
150
+
) -> impl Iterator<Item = (&String, &mut ColoredObject)> {
151
151
+
let tag_str = format!("{}", tag);
152
152
+
self.objects
153
153
+
.iter_mut()
154
154
+
.filter(move |(_, obj)| obj.has_tag(&tag_str))
155
155
+
}
156
156
+
157
157
+
pub fn tag_objects(
158
158
+
&mut self,
159
159
+
tag: impl Display,
160
160
+
objects: impl Fn(&String, &ColoredObject) -> bool,
161
161
+
) {
162
162
+
let tag_str = format!("{}", tag);
163
163
+
for (_, obj) in self.objects.iter_mut().filter(|(id, obj)| objects(id, obj)) {
164
164
+
obj.tag(&tag_str);
165
165
+
}
166
166
+
}
167
167
+
147
168
/// Returns the effective region the layer occupies, by merging all its objects' regions.
148
169
pub fn region(&self) -> Region {
149
170
self.objects
···
1
1
use crate::{Fill, Filter, Point, Region, Transformation};
2
2
+
use std::fmt::Display;
3
3
+
4
4
+
use itertools::Itertools;
2
5
#[cfg(feature = "web")]
3
6
use wasm_bindgen::prelude::*;
4
7
···
53
56
pub fill: Option<Fill>,
54
57
pub filters: Vec<Filter>,
55
58
pub transformations: Vec<Transformation>,
59
59
+
pub tags: Vec<String>,
56
60
}
57
61
58
62
impl ColoredObject {
···
102
106
pub fn region(&self) -> Region {
103
107
self.object.region()
104
108
}
109
109
+
110
110
+
pub fn tag(&mut self, tag: impl Display) {
111
111
+
self.tags.push(format!("{tag}"));
112
112
+
}
113
113
+
114
114
+
pub fn remove_tag(&mut self, tag: impl Display) {
115
115
+
let tag_str = format!("{tag}");
116
116
+
self.tags.retain(|t| t != &tag_str);
117
117
+
}
118
118
+
119
119
+
pub fn tagged(mut self, tag: impl Display) -> Self {
120
120
+
self.tags.push(format!("{tag}"));
121
121
+
self
122
122
+
}
123
123
+
124
124
+
pub fn has_tag(&self, tag: impl Display) -> bool {
125
125
+
let tag_str = format!("{tag}");
126
126
+
self.tags.iter().any(|t| t == &tag_str)
127
127
+
}
105
128
}
106
129
107
130
impl std::fmt::Display for ColoredObject {
···
111
134
fill,
112
135
filters,
113
136
transformations,
137
137
+
tags,
114
138
} = self;
115
139
116
140
if fill.is_some() {
···
127
151
write!(f, " with transformations {:?}", transformations)?;
128
152
}
129
153
154
154
+
if !tags.is_empty() {
155
155
+
write!(f, "{}", tags.iter().map(|t| format!("#{t}")).join(" "))?;
156
156
+
}
157
157
+
130
158
Ok(())
131
159
}
132
160
}
···
138
166
fill: None,
139
167
filters: vec![],
140
168
transformations: vec![],
169
169
+
tags: vec![],
141
170
}
142
171
}
143
172
}
···
149
178
fill,
150
179
filters: vec![],
151
180
transformations: vec![],
181
181
+
tags: vec![],
152
182
}
153
183
}
154
184
}