The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / Selection.java
1 package net.mograsim.logic.model.editor;
2
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.Iterator;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.function.DoubleBinaryOperator;
10
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13 import net.mograsim.logic.model.editor.handles.Handle;
14
15 public class Selection implements Iterable<Handle>
16 {
17         private Set<Handle> selection = new HashSet<>();
18
19         public Map<Handle, Point> calculateOffsets()
20         {
21                 Map<Handle, Point> offsets = new HashMap<>();
22                 Point ref = getTopLeft();
23                 selection.forEach(h -> offsets.put(h, new Point(h.getPosX() - ref.x, h.getPosY() - ref.y)));
24                 return offsets;
25         }
26
27         public Rectangle getBounds()
28         {
29                 Point pos1 = getTopLeft();
30                 Point pos2 = getBottomRight();
31                 return new Rectangle(pos1.x, pos1.y, pos2.x - pos1.x, pos2.y - pos1.y);
32         }
33
34         public double getWidth()
35         {// TODO: Compute this more efficiently
36                 return getTopRight().x - getTopLeft().x;
37         }
38
39         public double getHeight()
40         {
41                 return getBottomLeft().y - getTopLeft().y;
42         }
43
44         public Point getTopLeft()
45         {
46                 return getCorner(Double.MAX_VALUE, Double::min, r -> 0, Double.MAX_VALUE, Double::min, r -> 0);
47         }
48
49         public Point getTopRight()
50         {
51                 return getCorner(-Double.MAX_VALUE, Double::max, r -> r.width, Double.MAX_VALUE, Double::min, r -> 0);
52         }
53
54         public Point getBottomLeft()
55         {
56                 return getCorner(Double.MAX_VALUE, Double::min, r -> 0, -Double.MAX_VALUE, Double::max, r -> r.height);
57         }
58
59         public Point getBottomRight()
60         {
61                 return getCorner(-Double.MAX_VALUE, Double::max, r -> r.width, -Double.MAX_VALUE, Double::max, r -> r.height);
62         }
63
64         public Point getCorner(double xIdentity, DoubleBinaryOperator xOp, Offset xOffset, double yIdentity, DoubleBinaryOperator yOp,
65                         Offset yOffset)
66         {
67                 double x = xIdentity, y = yIdentity;
68                 for (Handle c : selection)
69                 {
70                         Rectangle bounds = c.getBounds();
71                         x = xOp.applyAsDouble(x, bounds.x + xOffset.computeOffset(bounds));
72                         y = yOp.applyAsDouble(y, bounds.y + yOffset.computeOffset(bounds));
73                 }
74                 return new Point(x, y);
75         }
76
77         private static interface Offset
78         {
79                 public double computeOffset(Rectangle bounds);
80         }
81
82         public void add(Handle h)
83         {
84                 selection.add(h);
85                 h.onSelect();
86         }
87
88         public void remove(Handle h)
89         {
90                 selection.remove(h);
91                 h.onDeselect();
92         }
93
94         public void clear()
95         {
96                 selection.forEach(h -> h.onDeselect());
97                 selection.clear();
98         }
99
100         public int size()
101         {
102                 return selection.size();
103         }
104
105         public boolean contains(Handle h)
106         {
107                 return selection.contains(h);
108         }
109
110         public void addAll(Collection<Handle> handles)
111         {
112                 handles.forEach(h -> h.onSelect());
113                 selection.addAll(handles);
114         }
115
116         @Override
117         public Iterator<Handle> iterator()
118         {
119                 return selection.iterator();
120         }
121
122         @Override
123         public String toString()
124         {
125                 return selection.toString();
126         }
127
128         public boolean isEmpty()
129         {
130                 return selection.isEmpty();
131         }
132 }