1 package net.mograsim.logic.ui.model.components;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
9 import java.util.TreeMap;
10 import java.util.function.Consumer;
11 import java.util.function.Supplier;
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;
19 * The base class for all GUI components.<br>
20 * A <code>GUIComponent</code> has a position and size. The size can only be modified by subclasses.<br>
22 * @author Daniel Kirschten
24 public abstract class GUIComponent
27 * The model this component is a part of.
29 protected final ViewModelModifiable model;
30 private final Rectangle bounds;
32 * The list of all pins of this component by name.
34 private final Map<String, Pin> pinsByName;
36 * An unmodifiable view of {@link #pinsByName}<code>.</code>{@link Map#values() values()}.
38 protected final Collection<Pin> pinsUnmodifiable;
40 private final List<Consumer<? super GUIComponent>> componentMovedListeners;
41 private final List<Consumer<? super Pin>> pinAddedListeners;
42 private final List<Consumer<? super Pin>> pinRemovedListeners;
43 private final List<Runnable> redrawListeners;
45 private final Runnable redrawListenerForSubcomponents;
46 // Defines how the GUIComponent is referenced in SubmodelComponentParams
47 protected Supplier<String> identifierDelegate = () -> "class:".concat(getClass().getCanonicalName());
49 // creation and destruction
51 public GUIComponent(ViewModelModifiable model)
54 this.bounds = new Rectangle(0, 0, 0, 0);
55 this.pinsByName = new HashMap<>();
56 this.pinsUnmodifiable = Collections.unmodifiableCollection(pinsByName.values());
58 this.componentMovedListeners = new ArrayList<>();
59 this.pinAddedListeners = new ArrayList<>();
60 this.pinRemovedListeners = new ArrayList<>();
61 this.redrawListeners = new ArrayList<>();
63 redrawListenerForSubcomponents = this::requestRedraw;
65 model.componentCreated(this);
69 * Destroys this component. This method implicitly calls {@link ViewModelModifiable#componentDestroyed(GUIComponent)
70 * componentDestroyed()} for the model this component is a part of.
72 * @author Daniel Kirschten
76 pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
77 model.componentDestroyed(this);
83 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
85 protected void addPin(Pin pin)
87 if (pin.component != this)
88 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
89 if (pinsByName.containsKey(pin.name))
90 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
91 pinsByName.put(pin.name, pin);
92 callPinAddedListeners(pin);
93 pin.addRedrawListener(redrawListenerForSubcomponents);
98 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
100 * @throws NullPointerException if there was no pin with this name.
102 protected void removePin(String name)
104 Pin pin = pinsByName.remove(name);
105 callPinRemovedListeners(pin);
106 pin.removeRedrawListener(redrawListenerForSubcomponents);
111 * Returns a collection of pins of this component.
113 * @author Daniel Kirschten
115 public Collection<Pin> getPins()
117 return pinsUnmodifiable;
121 * Returns the pin with the given name of this component.
123 * @throws IllegalArgumentException if there is no pin with the given name
125 * @author Daniel Kirschten
127 public Pin getPin(String name)
129 Pin pin = pinsByName.get(name);
131 throw new IllegalArgumentException("No pin with the name " + name);
135 // "graphical" operations
138 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
140 * @author Daniel Kirschten
142 public void moveTo(double x, double y)
146 callComponentMovedListeners();
151 * Sets the size of this component and calls redrawListeners.
153 * @author Daniel Kirschten
155 protected void setSize(double width, double height)
157 bounds.width = width;
158 bounds.height = height;
163 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
164 * {@link #getHeight}, because new objects are created.
166 * @author Daniel Kirschten
168 public Rectangle getBounds()
170 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
174 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
176 * @author Daniel Kirschten
178 public double getPosX()
184 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
186 * @author Daniel Kirschten
188 public double getPosY()
194 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
196 * @author Daniel Kirschten
198 public double getWidth()
204 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
206 * @author Daniel Kirschten
208 public double getHeight()
210 return bounds.height;
214 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
217 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
218 public boolean clicked(double x, double y)
224 * Render this component to the given gc, in absoulute coordinates.
226 * @author Daniel Kirschten
228 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
233 * @return an identifier used to reference this GUIComponent inside of {@link SubmodelComponentParams}
235 public String getIdentifier()
237 return identifierDelegate.get();
240 @SuppressWarnings("static-method")
241 public Map<String, Object> getInstantiationParameters()
243 return new TreeMap<>();
249 * Calls redraw listeners.
251 * @author Daniel Kirschten
253 protected void requestRedraw()
255 callRedrawListeners();
259 public void addComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners.add (listener);}
260 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
261 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
262 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
264 public void removeComponentMovedListener(Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
265 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
266 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
267 public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);}
269 private void callComponentMovedListeners( ) {componentMovedListeners.forEach(l -> l.accept(this));}
270 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
271 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}
272 private void callRedrawListeners ( ) {redrawListeners .forEach(l -> l.run( ));}