-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
EntityGroup.java
349 lines (304 loc) · 10.6 KB
/
EntityGroup.java
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
Copyright 2021 Will Winder
This file is part of Universal Gcode Sender (UGS).
UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.ugs.nbp.designer.entities;
import com.willwinder.ugs.nbp.designer.Utils;
import com.willwinder.ugs.nbp.designer.gui.Drawing;
import com.willwinder.ugs.nbp.designer.model.Size;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Stream;
/**
* @author Joacim Breiler
*/
public class EntityGroup extends AbstractEntity implements EntityListener {
private final List<Entity> children;
private double groupRotation = 0;
private Rectangle2D cachedBounds = new Rectangle2D.Double(0, 0, 0, 0);
public EntityGroup() {
super();
children = new CopyOnWriteArrayList<>();
setName("Group");
}
@Override
public void render(Graphics2D graphics, Drawing drawing) {
children.forEach(node -> node.render(graphics, drawing));
}
@Override
public void setSize(Size size) {
if(size.getWidth() == 0 || size.getHeight() == 0) {
return;
}
Size originalSize = getSize();
scale(size.getWidth() / originalSize.getWidth(), size.getHeight() / originalSize.getHeight());
}
@Override
public void rotate(double angle) {
try {
groupRotation += angle;
Point2D center = getCenter();
getAllChildren().forEach(entity -> entity.rotate(center, angle));
invalidateBounds();
notifyEvent(new EntityEvent(this, EventType.ROTATED));
} catch (Exception e) {
throw new EntityException("Couldn't set the rotation", e);
}
}
@Override
public void rotate(Point2D center, double angle) {
try {
groupRotation += angle;
getAllChildren().forEach(entity -> entity.rotate(center, angle));
invalidateBounds();
notifyEvent(new EntityEvent(this, EventType.ROTATED));
} catch (Exception e) {
throw new EntityException("Couldn't set the rotation", e);
}
}
@Override
public Shape getShape() {
return getBounds();
}
@Override
public Size getSize() {
Rectangle2D bounds = getBounds();
return new Size(bounds.getWidth(), bounds.getHeight());
}
@Override
public Rectangle2D getBounds() {
if (cachedBounds != null) {
return cachedBounds;
}
cachedBounds = getAllChildren().stream()
.map(Entity::getBounds)
.collect(BoundsCollector.toBounds());
return cachedBounds;
}
@Override
public Shape getRelativeShape() {
try {
return getTransform().createInverse().createTransformedShape(getShape());
} catch (NoninvertibleTransformException e) {
throw new EntityException("Could not create inverse transformer");
}
}
public void addChild(Entity entity) {
if (!containsChild(entity)) {
children.add(entity);
entity.addListener(this);
invalidateBounds();
}
}
public void addChild(Entity entity, int index) {
if (children.size() < index) {
index = children.size();
}
children.add(index, entity);
entity.addListener(this);
invalidateBounds();
}
private void invalidateBounds() {
cachedBounds = null;
}
@Override
public Point2D getCenter() {
Rectangle2D bounds = getBounds();
return new Point2D.Double(bounds.getCenterX(), bounds.getCenterY());
}
public void addAll(List<Entity> entities) {
entities.forEach(entity -> {
if (!containsChild(entity)) {
children.add(entity);
entity.addListener(this);
}
});
invalidateBounds();
}
@Override
public void applyTransform(AffineTransform transform) {
if (children != null) {
children.forEach(c -> c.applyTransform(transform));
}
invalidateBounds();
}
@Override
public void move(Point2D deltaMovement) {
try {
applyTransform(AffineTransform.getTranslateInstance(deltaMovement.getX(), deltaMovement.getY()));
invalidateBounds();
notifyEvent(new EntityEvent(this, EventType.MOVED));
} catch (Exception e) {
throw new EntityException("Could not make inverse transform of point", e);
}
}
@Override
public void setTransform(AffineTransform transform) {
children.forEach(c -> c.setTransform(transform));
invalidateBounds();
}
/**
* Returns true if the entity is a direct child or a grand child.
*
* @param entity the entity to check
* @return true if a child or grand child
*/
public boolean containsChild(Entity entity) {
// Find any direct children
if (children.contains(entity)) {
return true;
}
// Find grand children
return children.stream()
.filter(EntityGroup.class::isInstance)
.map(EntityGroup.class::cast)
.anyMatch(e -> e.containsChild(entity));
}
/**
* Attempts to find the parent that contains the given entity.
*
* @param entity the child entity to find the parent for
* @return an optional with its parent if found
*/
public Optional<EntityGroup> findParentFor(Entity entity) {
if (children.contains(entity)) {
return Optional.of(this);
}
return children.stream()
.filter(EntityGroup.class::isInstance)
.map(EntityGroup.class::cast)
.map(e -> e.findParentFor(entity))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
}
/**
* Removes a direct child entity from the group. If it exists as a grand child it will be left alone.
*
* @param entity the entity to remove
*/
public void removeChild(Entity entity) {
entity.removeListener(this);
children.remove(entity);
invalidateBounds();
}
@Override
public void destroy() {
super.destroy();
children.forEach(Entity::destroy);
}
public void removeAll() {
this.groupRotation = 0;
this.children.forEach(entity -> entity.removeListener(this));
this.children.clear();
invalidateBounds();
}
public List<Entity> getChildrenAt(Point2D p) {
return this.children
.stream()
.flatMap(s -> {
if (s instanceof EntityGroup entityGroup) {
return entityGroup.getChildrenAt(p).stream();
} else if (s.isWithin(p)) {
return Stream.of(s);
} else {
return Stream.empty();
}
}).toList();
}
public List<Entity> getChildrenIntersecting(Shape shape) {
return this.children
.stream()
.flatMap(s -> {
if (s instanceof EntityGroup entityGroup) {
return entityGroup.getChildrenIntersecting(shape).stream();
} else if (s.isIntersecting(shape)) {
return Stream.of(s);
} else {
return Stream.empty();
}
}).toList();
}
/**
* Get a list of all direct children of this group
*
* @return a list of children entities
*/
public final List<Entity> getChildren() {
return Collections.unmodifiableList(this.children);
}
@Override
public double getRotation() {
if (children.size() == 1) {
groupRotation = children.get(0).getRotation();
}
return Utils.normalizeRotation(groupRotation);
}
@Override
public void setRotation(double rotation) {
Point2D center = getCenter();
double deltaRotation = rotation - getRotation();
if (deltaRotation != 0) {
children.forEach(entity -> entity.rotate(center, deltaRotation));
}
groupRotation += deltaRotation;
notifyEvent(new EntityEvent(this, EventType.ROTATED));
invalidateBounds();
}
public final List<Entity> getAllChildren() {
if (this.children == null) {
return Collections.emptyList();
}
return this.children
.stream()
.flatMap(s -> {
if (s instanceof EntityGroup entityGroup) {
return entityGroup.getAllChildren().stream();
} else {
return Stream.of(s);
}
}).toList();
}
@Override
public void scale(double sx, double sy) {
Point2D originalPosition = getPosition();
this.children.forEach(child -> {
Point2D childOriginalPosition = child.getPosition();
Point2D relativePosition = new Point2D.Double(childOriginalPosition.getX() - originalPosition.getX(), childOriginalPosition.getY() - originalPosition.getY());
child.scale(sx, sy);
child.setPosition(new Point2D.Double(originalPosition.getX() + (relativePosition.getX() * sx), originalPosition.getY() + (relativePosition.getY() * sy)));
});
invalidateBounds();
notifyEvent(new EntityEvent(this, EventType.RESIZED));
}
@Override
public void onEvent(EntityEvent entityEvent) {
invalidateBounds();
notifyEvent(entityEvent);
}
@Override
public Entity copy() {
EntityGroup group = new EntityGroup();
copyPropertiesTo(group);
getChildren().stream().map(Entity::copy).forEach(group::addChild);
return group;
}
}