Stopped creation of unneccessary Rectangle instances
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / GUIComponent.java
1 package net.mograsim.logic.ui.model.components;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.TreeMap;
10 import java.util.function.Consumer;
11 import java.util.function.Supplier;
12
13 import net.haspamelodica.swt.helper.gcs.GeneralGC;
14 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
15 import net.mograsim.logic.ui.model.ViewModelModifiable;
16 import net.mograsim.logic.ui.model.wires.Pin;
17
18 public abstract class GUIComponent
19 {
20         protected final ViewModelModifiable model;
21         private final Rectangle bounds;
22         private final Map<String, Pin> pinsByName;
23         protected final Collection<Pin> pinsUnmodifiable;
24
25         private final List<Consumer<? super GUIComponent>> componentMovedListeners;
26         private final List<Consumer<? super Pin>> pinAddedListeners;
27         private final List<Consumer<? super Pin>> pinRemovedListeners;
28         private final List<Runnable> redrawListeners;
29
30         private final Runnable redrawListenerForSubcomponents;
31         // Defines how the GUIComponent is referenced in SubmodelComponentParams
32         protected Supplier<String> identifierDelegate = () -> "class:".concat(getClass().getCanonicalName());
33
34         public GUIComponent(ViewModelModifiable model)
35         {
36                 this.model = model;
37                 this.bounds = new Rectangle(0, 0, 0, 0);
38                 this.pinsByName = new HashMap<>();
39                 this.pinsUnmodifiable = Collections.unmodifiableCollection(pinsByName.values());
40
41                 this.componentMovedListeners = new ArrayList<>();
42                 this.pinAddedListeners = new ArrayList<>();
43                 this.pinRemovedListeners = new ArrayList<>();
44                 this.redrawListeners = new ArrayList<>();
45
46                 redrawListenerForSubcomponents = this::callRedrawListeners;
47
48                 model.componentCreated(this);
49         }
50
51         public void destroy()
52         {
53                 pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
54                 model.componentDestroyed(this);
55         }
56
57         public void moveTo(double x, double y)
58         {
59                 bounds.x = x;
60                 bounds.y = y;
61                 callComponentMovedListeners();
62         }
63
64         /**
65          * Returns the bounds of this component. Used for calculating which component is clicked.
66          */
67         public Rectangle getBounds()
68         {
69                 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
70         }
71
72         public double getPosX()
73         {
74                 return bounds.x;
75         }
76
77         public double getPosY()
78         {
79                 return bounds.y;
80         }
81
82         public double getWidth()
83         {
84                 return bounds.width;
85         }
86
87         public double getHeight()
88         {
89                 return bounds.height;
90         }
91
92         /**
93          * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
94          * click.
95          */
96         @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
97         public boolean clicked(double x, double y)
98         {
99                 return false;
100         }
101
102         /**
103          * Returns a collection of pins of this component.
104          */
105         public Collection<Pin> getPins()
106         {
107                 return pinsUnmodifiable;
108         }
109
110         public Pin getPin(String name)
111         {
112                 Pin pin = pinsByName.get(name);
113                 if (pin == null)
114                         throw new IllegalArgumentException("No pin with the name " + name);
115                 return pin;
116         }
117
118         // @formatter:off
119         public void addComponentMovedListener   (Consumer<? super GUIComponent> listener) {componentMovedListeners.add   (listener);}
120         public void addPinAddedListener         (Consumer<? super Pin         > listener) {pinAddedListeners      .add   (listener);}
121         public void addPinRemovedListener       (Consumer<? super Pin         > listener) {pinRemovedListeners    .add   (listener);}
122         public void addRedrawListener           (Runnable                       listener) {redrawListeners        .add   (listener);}
123
124         public void removeComponentMovedListener(Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
125         public void removePinAddedListener      (Consumer<? super Pin         > listener) {pinAddedListeners       .remove(listener);}
126         public void removePinRemovedListener    (Consumer<? super Pin         > listener) {pinRemovedListeners     .remove(listener);}
127         public void removeRedrawListener        (Runnable                       listener) {redrawListeners         .remove(listener);}
128
129         private void callComponentMovedListeners(     ) {componentMovedListeners.forEach(l -> l.accept(this));}
130         private void callPinAddedListeners      (Pin p) {pinAddedListeners      .forEach(l -> l.accept(p   ));}
131         private void callPinRemovedListeners    (Pin p) {pinRemovedListeners    .forEach(l -> l.accept(p   ));}
132         private void callRedrawListeners        (     ) {redrawListeners        .forEach(l -> l.run(       ));}
133         // @formatter:on
134
135         /**
136          * Render this component to the given gc.
137          */
138         public abstract void render(GeneralGC gc, Rectangle visibleRegion);
139
140         protected void requestRedraw()
141         {
142                 callRedrawListeners();
143         }
144
145         protected void setSize(double width, double height)
146         {
147                 bounds.width = width;
148                 bounds.height = height;
149                 callRedrawListeners();
150         }
151
152         protected void addPin(Pin pin)
153         {
154                 if (pinsByName.containsKey(pin.name))
155                         throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
156                 pinsByName.put(pin.name, pin);
157                 callPinAddedListeners(pin);
158                 pin.addRedrawListener(redrawListenerForSubcomponents);
159                 callRedrawListeners();
160         }
161
162         protected void removePin(String name)
163         {
164                 Pin pin = pinsByName.remove(name);
165                 callPinRemovedListeners(pin);
166                 pin.removeRedrawListener(redrawListenerForSubcomponents);
167                 callRedrawListeners();
168         }
169
170         /**
171          * @return an identifier used to reference this GUIComponent inside of {@link SubmodelComponentParams}
172          */
173         public String getIdentifier()
174         {
175                 return identifierDelegate.get();
176         }
177
178         @SuppressWarnings("static-method")
179         public Map<String, Object> getInstantiationParameters()
180         {
181                 return new TreeMap<>();
182         }
183 }