1 package net.mograsim.logic.ui.model.components;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
8 import java.util.TreeMap;
9 import java.util.function.Consumer;
10 import java.util.function.Supplier;
12 import net.haspamelodica.swt.helper.gcs.GeneralGC;
13 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
14 import net.mograsim.logic.ui.model.ViewModelModifiable;
15 import net.mograsim.logic.ui.model.wires.Pin;
18 * The base class for all GUI components.<br>
19 * A <code>GUIComponent</code> has a position and size. The size can only be modified by subclasses.<br>
21 * @author Daniel Kirschten
23 public abstract class GUIComponent
26 * The model this component is a part of.
28 protected final ViewModelModifiable model;
29 private final Rectangle bounds;
31 * The list of all pins of this component by name.
33 private final Map<String, Pin> pinsByName;
35 * An unmodifiable view of {@link #pinsByName}.
37 protected final Map<String, Pin> pinsUnmodifiable;
39 private final List<Consumer<? super GUIComponent>> componentMovedListeners;
40 private final List<Consumer<? super Pin>> pinAddedListeners;
41 private final List<Consumer<? super Pin>> pinRemovedListeners;
42 private final List<Runnable> redrawListeners;
44 private final Runnable redrawListenerForSubcomponents;
45 // Defines how the GUIComponent is referenced in SubmodelComponentParams
46 protected Supplier<String> identifierDelegate = () -> getClass().getSimpleName();
48 // creation and destruction
50 public GUIComponent(ViewModelModifiable model)
53 this.bounds = new Rectangle(0, 0, 0, 0);
54 this.pinsByName = new HashMap<>();
55 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
57 this.componentMovedListeners = new ArrayList<>();
58 this.pinAddedListeners = new ArrayList<>();
59 this.pinRemovedListeners = new ArrayList<>();
60 this.redrawListeners = new ArrayList<>();
62 redrawListenerForSubcomponents = this::requestRedraw;
64 model.componentCreated(this);
68 * Destroys this component. This method implicitly calls {@link ViewModelModifiable#componentDestroyed(GUIComponent)
69 * componentDestroyed()} for the model this component is a part of.
71 * @author Daniel Kirschten
75 pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
76 model.componentDestroyed(this);
82 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
84 * @throws IllegalArgumentException if the pin doesn't belong to this component
85 * @throws IllegalArgumentException if there already is a pin with the given name
87 * @author Daniel Kirschten
89 protected void addPin(Pin pin)
91 if (pin.component != this)
92 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
93 if (pinsByName.containsKey(pin.name))
94 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
95 pinsByName.put(pin.name, pin);
96 callPinAddedListeners(pin);
97 pin.addRedrawListener(redrawListenerForSubcomponents);
102 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
104 * @throws NullPointerException if there was no pin with this name
106 * @author Daniel Kirschten
108 protected void removePin(String name)
110 Pin pin = pinsByName.remove(name);
111 callPinRemovedListeners(pin);
112 pin.removeRedrawListener(redrawListenerForSubcomponents);
117 * Returns a collection of pins of this component.
119 * @author Daniel Kirschten
121 public Map<String, Pin> getPins()
123 return pinsUnmodifiable;
127 * Returns the pin with the given name of this component.
129 * @throws IllegalArgumentException if there is no pin with the given name
131 * @author Daniel Kirschten
133 public Pin getPin(String name)
135 Pin pin = pinsByName.get(name);
137 throw new IllegalArgumentException("No pin with the name " + name);
144 * Sets the given high-level state to the given value. <br>
145 * TODO more documentation!
147 * @author Daniel Kirschten
149 @SuppressWarnings({ "static-method", "unused" }) // this method is intended to be overridden
150 public void setHighLevelState(String stateID, Object newState)
152 throw new IllegalArgumentException("No high level state with ID " + stateID);
156 * Gets the current value of the given high-level state. <br>
157 * TODO more documentation!
159 * @author Daniel Kirschten
161 @SuppressWarnings("static-method") // this method is intended to be overridden
162 public Object getHighLevelState(String stateID)
164 throw new IllegalArgumentException("No high level state with ID " + stateID);
167 // "graphical" operations
170 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
172 * @author Daniel Kirschten
174 public void moveTo(double x, double y)
178 callComponentMovedListeners();
183 * Sets the size of this component and calls redrawListeners.
185 * @author Daniel Kirschten
187 protected void setSize(double width, double height)
189 bounds.width = width;
190 bounds.height = height;
195 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
196 * {@link #getHeight}, because new objects are created.
198 * @author Daniel Kirschten
200 public Rectangle getBounds()
202 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
206 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
208 * @author Daniel Kirschten
210 public double getPosX()
216 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
218 * @author Daniel Kirschten
220 public double getPosY()
226 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
228 * @author Daniel Kirschten
230 public double getWidth()
236 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
238 * @author Daniel Kirschten
240 public double getHeight()
242 return bounds.height;
246 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
249 * @author Daniel Kirschten
251 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
252 public boolean clicked(double x, double y)
258 * Render this component to the given gc, in absoulute coordinates.
260 * @author Daniel Kirschten
262 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
267 * @return an identifier used to reference this GUIComponent inside of {@link SubmodelComponentParams}
269 public String getIdentifier()
271 return identifierDelegate.get();
274 @SuppressWarnings("static-method")
275 public Map<String, Object> getInstantiationParameters()
277 return new TreeMap<>();
283 * Calls redraw listeners.
285 * @author Daniel Kirschten
287 protected void requestRedraw()
289 callRedrawListeners();
293 public void addComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners.add (listener);}
294 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
295 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
296 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
298 public void removeComponentMovedListener(Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
299 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
300 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
301 public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);}
303 private void callComponentMovedListeners( ) {componentMovedListeners.forEach(l -> l.accept(this));}
304 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
305 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}
306 private void callRedrawListeners ( ) {redrawListeners .forEach(l -> l.run( ));}