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 net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12 import net.mograsim.logic.model.model.LogicModelModifiable;
13 import net.mograsim.logic.model.model.wires.Pin;
14 import net.mograsim.logic.model.serializing.IdentifyParams;
15 import net.mograsim.logic.model.serializing.JSONSerializable;
16 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
19 * The base class for all model components.<br>
20 * A <code>ModelComponent</code> has a reference to the LogicModel it belongs to.<br>
21 * A <code>ModelComponent</code> has a name. This name is unique in the model the <code>ModelComponent</code> belongs to.<br>
22 * A <code>ModelComponent</code> has a position and size. The size can only be modified by subclasses.
24 * @author Daniel Kirschten
26 public abstract class ModelComponent implements JSONSerializable
29 * The model this component is a part of.
31 protected final LogicModelModifiable model;
33 * The name of this component. Is unique for all components in its model.<br>
34 * Does never change, but can't be final since it is set in {@link #init()}.
37 private final Rectangle bounds;
39 * The list of all pins of this component by name.
41 private final Map<String, Pin> pinsByName;
43 * An unmodifiable view of {@link #pinsByName}.
45 protected final Map<String, Pin> pinsUnmodifiable;
47 private final List<Consumer<? super ModelComponent>> componentMovedListeners;
48 private final List<Consumer<? super ModelComponent>> componentResizedListeners;
49 private final List<Consumer<? super Pin>> pinAddedListeners;
50 private final List<Consumer<? super Pin>> pinRemovedListeners;
52 private HighLevelStateHandler highLevelStateHandler;
54 // creation and destruction
56 public ModelComponent(LogicModelModifiable model, String name)
58 this(model, name, true);
62 * Creates a new {@link ModelComponent} and, if <code>callInit</code>, initializes the component (See {@link #init()}).<br>
63 * If <code>callInit==false</code>, make sure to call {@link #init()}!
65 * @author Daniel Kirschten
67 protected ModelComponent(LogicModelModifiable model, String name, boolean callInit)
71 this.bounds = new Rectangle(0, 0, 0, 0);
72 this.pinsByName = new HashMap<>();
73 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
75 this.componentMovedListeners = new ArrayList<>();
76 this.componentResizedListeners = new ArrayList<>();
77 this.pinAddedListeners = new ArrayList<>();
78 this.pinRemovedListeners = new ArrayList<>();
85 * Initializes this component. This method should be called exactly once in this component's constructor.<br>
87 * <li>If <code>{@link #name}==null</code>, sets {@link #name} to {@link LogicModelModifiable#getDefaultComponentName(ModelComponent)}.
88 * <li>Registers this component in the model.
94 name = model.getDefaultComponentName(this);
95 model.componentCreated(this, this::destroyed);
100 public String getName()
106 * Destroys this component. This method is called from {@link LogicModelModifiable#componentDestroyed(ModelComponent)
107 * destroyComponent()} of the model this component is a part of.<br>
108 * When overriding, make sure to also call the original implementation.
110 * @author Daniel Kirschten
112 protected void destroyed()
114 pinsByName.values().forEach(this::removePinWithoutRedraw);
120 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
122 * @throws IllegalArgumentException if the pin doesn't belong to this component
123 * @throws IllegalArgumentException if there already is a pin with the given name
125 * @author Daniel Kirschten
127 protected void addPin(Pin pin)
129 if (pin.component != this)
130 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
131 if (pinsByName.containsKey(pin.name))
132 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
133 pinsByName.put(pin.name, pin);
134 callPinAddedListeners(pin);
135 model.requestRedraw();
139 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
141 * @throws NullPointerException if there was no pin with this name
143 * @author Daniel Kirschten
145 protected void removePin(String name)
147 removePinWithoutRedraw(pinsByName.remove(name));
148 model.requestRedraw();
151 private void removePinWithoutRedraw(Pin pin)
154 callPinRemovedListeners(pin);
158 * Returns a collection of pins of this component.
160 * @author Daniel Kirschten
162 public Map<String, Pin> getPins()
164 return pinsUnmodifiable;
168 * Returns the pin with the given name of this component.
170 * @throws IllegalArgumentException if there is no pin with the given name
171 * @see #getPinOrNull(String)
173 * @author Daniel Kirschten
175 public Pin getPin(String name)
177 Pin pin = getPinOrNull(name);
179 throw new IllegalArgumentException("No pin with the name " + name);
184 * Returns the pin with the given name of this component, or <code>null</code> if there is no such pin.
186 * @see #getPin(String)
188 * @author Daniel Kirschten
190 public Pin getPinOrNull(String name)
192 return pinsByName.get(name);
198 * @author Daniel Kirschten
200 protected void setHighLevelStateHandler(HighLevelStateHandler highLevelStateHandler)
202 this.highLevelStateHandler = highLevelStateHandler;
205 public HighLevelStateHandler getHighLevelStateHandler()
207 return highLevelStateHandler;
211 * Gets the current value of the given high-level state. <br>
212 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
214 * @see #setHighLevelState(String, Object)
215 * @see HighLevelStateHandler#getHighLevelState(String)
217 * @author Daniel Kirschten
219 public Object getHighLevelState(String stateID)
221 return highLevelStateHandler.getHighLevelState(stateID);
225 * Sets the given high-level state to the given value. <br>
226 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
228 * @see #getHighLevelState(String)
229 * @see HighLevelStateHandler#setHighLevelState(String, Object)
231 * @author Daniel Kirschten
233 public void setHighLevelState(String stateID, Object newState)
235 highLevelStateHandler.setHighLevelState(stateID, newState);
238 // "graphical" operations
241 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
243 * @author Daniel Kirschten
245 public void moveTo(double x, double y)
249 callComponentMovedListeners();
250 model.requestRedraw();
254 * Sets the size of this component and calls redrawListeners.
256 * @author Daniel Kirschten
258 protected void setSize(double width, double height)
260 bounds.width = width;
261 bounds.height = height;
262 callComponentResizedListener();
263 model.requestRedraw();
267 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
268 * {@link #getHeight}, because new objects are created.
270 * @author Daniel Kirschten
272 public final Rectangle getBounds()
274 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
278 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
280 * @author Daniel Kirschten
282 public double getPosX()
288 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
290 * @author Daniel Kirschten
292 public double getPosY()
298 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
300 * @author Daniel Kirschten
302 public double getWidth()
308 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
310 * @author Daniel Kirschten
312 public double getHeight()
314 return bounds.height;
318 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
321 * @author Daniel Kirschten
323 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
324 public boolean clicked(double x, double y)
330 * Render this component to the given gc, in absoulute coordinates.
332 * @author Daniel Kirschten
334 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
339 public Object getParamsForSerializing(IdentifyParams idParams)
347 public void addComponentMovedListener (Consumer<? super ModelComponent> listener) {componentMovedListeners .add (listener);}
348 public void addComponentResizedListener (Consumer<? super ModelComponent> listener) {componentResizedListeners.add (listener);}
349 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
350 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
352 public void removeComponentMovedListener (Consumer<? super ModelComponent> listener) {componentMovedListeners .remove(listener);}
353 public void removeComponentResizedListener (Consumer<? super ModelComponent> listener) {componentResizedListeners.remove(listener);}
354 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
355 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
357 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
358 private void callComponentResizedListener( ) {componentResizedListeners.forEach(l -> l.accept(this));}
359 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
360 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}