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.
35 public final String name;
36 private final Rectangle bounds;
38 * The list of all pins of this component by name.
40 private final Map<String, Pin> pinsByName;
42 * An unmodifiable view of {@link #pinsByName}.
44 protected final Map<String, Pin> pinsUnmodifiable;
46 private final List<Consumer<? super ModelComponent>> componentMovedListeners;
47 private final List<Consumer<? super ModelComponent>> componentResizedListeners;
48 private final List<Consumer<? super Pin>> pinAddedListeners;
49 private final List<Consumer<? super Pin>> pinRemovedListeners;
51 private HighLevelStateHandler highLevelStateHandler;
53 // creation and destruction
55 public ModelComponent(LogicModelModifiable model, String name)
57 this(model, name, true);
61 * Creates a new {@link ModelComponent} and, if <code>callInit</code>, initializes the component (See {@link #init()}).<br>
62 * If <code>callInit==false</code>, make sure to call {@link #init()}!
64 * @author Daniel Kirschten
66 protected ModelComponent(LogicModelModifiable model, String name, boolean callInit)
69 this.name = name == null ? model.getDefaultComponentName(this) : name;
70 this.bounds = new Rectangle(0, 0, 0, 0);
71 this.pinsByName = new HashMap<>();
72 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
74 this.componentMovedListeners = new ArrayList<>();
75 this.componentResizedListeners = new ArrayList<>();
76 this.pinAddedListeners = new ArrayList<>();
77 this.pinRemovedListeners = new ArrayList<>();
84 * Initializes this component. This method should be called exactly once in this component's constructor.<br>
85 * Currently, this method only registers this component in the model.
89 model.componentCreated(this, this::destroyed);
93 * Destroys this component. This method is called from {@link LogicModelModifiable#componentDestroyed(ModelComponent)
94 * destroyComponent()} of the model this component is a part of.<br>
95 * When overriding, make sure to also call the original implementation.
97 * @author Daniel Kirschten
99 protected void destroyed()
101 pinsByName.values().forEach(this::removePinWithoutRedraw);
107 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
109 * @throws IllegalArgumentException if the pin doesn't belong to this component
110 * @throws IllegalArgumentException if there already is a pin with the given name
112 * @author Daniel Kirschten
114 protected void addPin(Pin pin)
116 if (pin.component != this)
117 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
118 if (pinsByName.containsKey(pin.name))
119 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
120 pinsByName.put(pin.name, pin);
121 callPinAddedListeners(pin);
122 model.requestRedraw();
126 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
128 * @throws NullPointerException if there was no pin with this name
130 * @author Daniel Kirschten
132 protected void removePin(String name)
134 removePinWithoutRedraw(pinsByName.remove(name));
135 model.requestRedraw();
138 private void removePinWithoutRedraw(Pin pin)
141 callPinRemovedListeners(pin);
145 * Returns a collection of pins of this component.
147 * @author Daniel Kirschten
149 public Map<String, Pin> getPins()
151 return pinsUnmodifiable;
155 * Returns the pin with the given name of this component.
157 * @throws IllegalArgumentException if there is no pin with the given name
159 * @author Daniel Kirschten
161 public Pin getPin(String name)
163 Pin pin = pinsByName.get(name);
165 throw new IllegalArgumentException("No pin with the name " + name);
172 * @author Daniel Kirschten
174 protected void setHighLevelStateHandler(HighLevelStateHandler highLevelStateHandler)
176 this.highLevelStateHandler = highLevelStateHandler;
179 public HighLevelStateHandler getHighLevelStateHandler()
181 return highLevelStateHandler;
185 * Gets the current value of the given high-level state. <br>
186 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
188 * @see #setHighLevelState(String, Object)
189 * @see HighLevelStateHandler#getHighLevelState(String)
191 * @author Daniel Kirschten
193 public Object getHighLevelState(String stateID)
195 return highLevelStateHandler.getHighLevelState(stateID);
199 * Sets the given high-level state to the given value. <br>
200 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
202 * @see #getHighLevelState(String)
203 * @see HighLevelStateHandler#setHighLevelState(String, Object)
205 * @author Daniel Kirschten
207 public void setHighLevelState(String stateID, Object newState)
209 highLevelStateHandler.setHighLevelState(stateID, newState);
212 // "graphical" operations
215 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
217 * @author Daniel Kirschten
219 public void moveTo(double x, double y)
223 callComponentMovedListeners();
224 model.requestRedraw();
228 * Sets the size of this component and calls redrawListeners.
230 * @author Daniel Kirschten
232 protected void setSize(double width, double height)
234 bounds.width = width;
235 bounds.height = height;
236 callComponentResizedListener();
237 model.requestRedraw();
241 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
242 * {@link #getHeight}, because new objects are created.
244 * @author Daniel Kirschten
246 public final Rectangle getBounds()
248 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
252 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
254 * @author Daniel Kirschten
256 public double getPosX()
262 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
264 * @author Daniel Kirschten
266 public double getPosY()
272 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
274 * @author Daniel Kirschten
276 public double getWidth()
282 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
284 * @author Daniel Kirschten
286 public double getHeight()
288 return bounds.height;
292 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
295 * @author Daniel Kirschten
297 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
298 public boolean clicked(double x, double y)
304 * Render this component to the given gc, in absoulute coordinates.
306 * @author Daniel Kirschten
308 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
313 public Object getParamsForSerializing(IdentifyParams idParams)
321 public void addComponentMovedListener (Consumer<? super ModelComponent> listener) {componentMovedListeners .add (listener);}
322 public void addComponentResizedListener (Consumer<? super ModelComponent> listener) {componentResizedListeners.add (listener);}
323 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
324 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
326 public void removeComponentMovedListener (Consumer<? super ModelComponent> listener) {componentMovedListeners .remove(listener);}
327 public void removeComponentResizedListener (Consumer<? super ModelComponent> listener) {componentResizedListeners.remove(listener);}
328 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
329 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
331 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
332 private void callComponentResizedListener( ) {componentResizedListeners.forEach(l -> l.accept(this));}
333 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
334 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}