1 package net.mograsim.logic.model.model.components;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
8 import java.util.function.Consumer;
10 import com.google.gson.JsonElement;
11 import com.google.gson.JsonNull;
13 import net.haspamelodica.swt.helper.gcs.GeneralGC;
14 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
15 import net.mograsim.logic.model.model.ViewModelModifiable;
16 import net.mograsim.logic.model.model.wires.Pin;
17 import net.mograsim.logic.model.serializing.IdentifierGetter;
18 import net.mograsim.logic.model.serializing.JSONSerializable;
19 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
22 * The base class for all GUI components.<br>
23 * A <code>GUIComponent</code> has a reference to the ViewModel it belongs to.<br>
24 * A <code>GUIComponent</code> has a name. This name is unique in the model the <code>GUIComponent</code> belongs to.<br>
25 * A <code>GUIComponent</code> has a position and size. The size can only be modified by subclasses.
27 * @author Daniel Kirschten
29 public abstract class GUIComponent implements JSONSerializable
32 * The model this component is a part of.
34 protected final ViewModelModifiable model;
36 * The name of this component. Is unique for all components in its model.
38 public final String name;
39 private final Rectangle bounds;
41 * The list of all pins of this component by name.
43 private final Map<String, Pin> pinsByName;
45 * An unmodifiable view of {@link #pinsByName}.
47 protected final Map<String, Pin> pinsUnmodifiable;
49 private final List<Consumer<? super GUIComponent>> componentMovedListeners;
50 private final List<Consumer<? super GUIComponent>> componentResizedListeners;
51 private final List<Consumer<? super Pin>> pinAddedListeners;
52 private final List<Consumer<? super Pin>> pinRemovedListeners;
54 private HighLevelStateHandler highLevelStateHandler;
56 // creation and destruction
58 public GUIComponent(ViewModelModifiable model, String name)
61 this.name = name == null ? model.getDefaultComponentName(this) : name;
62 this.bounds = new Rectangle(0, 0, 0, 0);
63 this.pinsByName = new HashMap<>();
64 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
66 this.componentMovedListeners = new ArrayList<>();
67 this.componentResizedListeners = new ArrayList<>();
68 this.pinAddedListeners = new ArrayList<>();
69 this.pinRemovedListeners = new ArrayList<>();
71 // TODO this will crash the high level state debug shell because submodel is not yet set.
72 // The same problem exists in ViewModelModifiable.getDefaultComponentName; see there
73 model.componentCreated(this);
77 * Destroys this component. This method implicitly calls {@link ViewModelModifiable#componentDestroyed(GUIComponent)
78 * componentDestroyed()} for the model this component is a part of.
80 * @author Daniel Kirschten
84 pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
85 model.componentDestroyed(this);
91 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
93 * @throws IllegalArgumentException if the pin doesn't belong to this component
94 * @throws IllegalArgumentException if there already is a pin with the given name
96 * @author Daniel Kirschten
98 protected void addPin(Pin pin)
100 if (pin.component != this)
101 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
102 if (pinsByName.containsKey(pin.name))
103 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
104 pinsByName.put(pin.name, pin);
105 callPinAddedListeners(pin);
106 model.requestRedraw();
110 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
112 * @throws NullPointerException if there was no pin with this name
114 * @author Daniel Kirschten
116 protected void removePin(String name)
118 Pin pin = pinsByName.remove(name);
119 callPinRemovedListeners(pin);
120 model.requestRedraw();
124 * Returns a collection of pins of this component.
126 * @author Daniel Kirschten
128 public Map<String, Pin> getPins()
130 return pinsUnmodifiable;
134 * Returns the pin with the given name of this component.
136 * @throws IllegalArgumentException if there is no pin with the given name
138 * @author Daniel Kirschten
140 public Pin getPin(String name)
142 Pin pin = pinsByName.get(name);
144 throw new IllegalArgumentException("No pin with the name " + name);
151 * @author Daniel Kirschten
153 protected void setHighLevelStateHandler(HighLevelStateHandler highLevelStateHandler)
155 this.highLevelStateHandler = highLevelStateHandler;
158 public HighLevelStateHandler getHighLevelStateHandler()
160 return highLevelStateHandler;
164 * Gets the current value of the given high-level state. <br>
165 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
167 * @see #setHighLevelState(String, Object)
168 * @see HighLevelStateHandler#getHighLevelState(String)
170 * @author Daniel Kirschten
172 public Object getHighLevelState(String stateID)
174 return highLevelStateHandler.getHighLevelState(stateID);
178 * Sets the given high-level state to the given value. <br>
179 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
181 * @see #getHighLevelState(String)
182 * @see HighLevelStateHandler#setHighLevelState(String, Object)
184 * @author Daniel Kirschten
186 public void setHighLevelState(String stateID, Object newState)
188 highLevelStateHandler.setHighLevelState(stateID, newState);
191 // "graphical" operations
194 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
196 * @author Daniel Kirschten
198 public void moveTo(double x, double y)
202 callComponentMovedListeners();
203 model.requestRedraw();
207 * Sets the size of this component and calls redrawListeners.
209 * @author Daniel Kirschten
211 protected void setSize(double width, double height)
213 bounds.width = width;
214 bounds.height = height;
215 callComponentResizedListener();
216 model.requestRedraw();
220 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
221 * {@link #getHeight}, because new objects are created.
223 * @author Daniel Kirschten
225 public final Rectangle getBounds()
227 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
231 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
233 * @author Daniel Kirschten
235 public double getPosX()
241 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
243 * @author Daniel Kirschten
245 public double getPosY()
251 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
253 * @author Daniel Kirschten
255 public double getWidth()
261 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
263 * @author Daniel Kirschten
265 public double getHeight()
267 return bounds.height;
271 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
274 * @author Daniel Kirschten
276 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
277 public boolean clicked(double x, double y)
283 * Render this component to the given gc, in absoulute coordinates.
285 * @author Daniel Kirschten
287 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
292 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
294 return JsonNull.INSTANCE;
300 public void addComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .add (listener);}
301 public void addComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.add (listener);}
302 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
303 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
305 public void removeComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
306 public void removeComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.remove(listener);}
307 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
308 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
310 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
311 private void callComponentResizedListener( ) {componentResizedListeners.forEach(l -> l.accept(this));}
312 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
313 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}