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.preferences.RenderPreferences;
15 import net.mograsim.logic.model.serializing.IdentifyParams;
16 import net.mograsim.logic.model.serializing.JSONSerializable;
17 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
18 import net.mograsim.logic.model.snippets.highlevelstatehandlers.DefaultHighLevelStateHandler;
21 * The base class for all model components.<br>
22 * A <code>ModelComponent</code> has a reference to the LogicModel it belongs to.<br>
23 * A <code>ModelComponent</code> has a name. This name is unique in the model the <code>ModelComponent</code> belongs to.<br>
24 * A <code>ModelComponent</code> has a position and size. The size can only be modified by subclasses.
26 * @author Daniel Kirschten
28 public abstract class ModelComponent implements JSONSerializable
31 * The model this component is a part of.
33 protected final LogicModelModifiable model;
35 * The name of this component. Is unique for all components in its model.<br>
36 * Does never change, but can't be final since it is set in {@link #init()}.
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 ModelComponent>> componentMovedListeners;
50 private final List<Consumer<? super ModelComponent>> 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 ModelComponent(LogicModelModifiable model, String name)
60 this(model, name, true);
64 * Creates a new {@link ModelComponent} and, if <code>callInit</code>, initializes the component (See {@link #init()}).<br>
65 * If <code>callInit==false</code>, make sure to call {@link #init()}!
67 * @author Daniel Kirschten
69 protected ModelComponent(LogicModelModifiable model, String name, boolean callInit)
73 this.bounds = new Rectangle(0, 0, 0, 0);
74 this.pinsByName = new HashMap<>();
75 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
77 this.componentMovedListeners = new ArrayList<>();
78 this.componentResizedListeners = new ArrayList<>();
79 this.pinAddedListeners = new ArrayList<>();
80 this.pinRemovedListeners = new ArrayList<>();
82 this.highLevelStateHandler = new DefaultHighLevelStateHandler();
89 * Initializes this component. This method should be called exactly once in this component's constructor.<br>
91 * <li>If <code>{@link #name}==null</code>, sets {@link #name} to {@link LogicModelModifiable#getDefaultComponentName(ModelComponent)}.
92 * <li>Registers this component in the model.
98 name = model.getDefaultComponentName(this);
99 model.componentCreated(this, this::destroyed);
103 * Destroys this component. This method is called from {@link LogicModelModifiable#componentDestroyed(ModelComponent)
104 * destroyComponent()} of the model this component is a part of.<br>
105 * When overriding, make sure to also call the original implementation.
107 * @author Daniel Kirschten
109 protected void destroyed()
111 pinsByName.values().forEach(this::removePinWithoutRedraw);
116 public String getName()
124 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
126 * @throws IllegalArgumentException if the pin doesn't belong to this component
127 * @throws IllegalArgumentException if there already is a pin with the given name
129 * @author Daniel Kirschten
131 protected void addPin(Pin pin)
133 if (pin.component != this)
134 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
135 if (pinsByName.containsKey(pin.name))
136 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
137 pinsByName.put(pin.name, pin);
138 callPinAddedListeners(pin);
139 model.requestRedraw();
143 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
145 * @throws NullPointerException if there was no pin with this name
147 * @author Daniel Kirschten
149 protected void removePin(String name)
151 removePinWithoutRedraw(pinsByName.remove(name));
152 model.requestRedraw();
155 private void removePinWithoutRedraw(Pin pin)
158 callPinRemovedListeners(pin);
162 * Returns a collection of pins of this component.
164 * @author Daniel Kirschten
166 public Map<String, Pin> getPins()
168 return pinsUnmodifiable;
172 * Returns the pin with the given name of this component.
174 * @throws IllegalArgumentException if there is no pin with the given name
175 * @see #getPinOrNull(String)
177 * @author Daniel Kirschten
179 public Pin getPin(String name)
181 Pin pin = getPinOrNull(name);
183 throw new IllegalArgumentException("No pin with the name " + name);
188 * Returns the pin with the given name of this component, or <code>null</code> if there is no such pin.
190 * @see #getPin(String)
192 * @author Daniel Kirschten
194 public Pin getPinOrNull(String name)
196 return pinsByName.get(name);
202 * @author Daniel Kirschten
204 protected void setHighLevelStateHandler(HighLevelStateHandler highLevelStateHandler)
206 this.highLevelStateHandler = highLevelStateHandler;
209 public HighLevelStateHandler getHighLevelStateHandler()
211 return highLevelStateHandler;
215 * Gets the current value of the given high-level state. <br>
216 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
218 * @see #setHighLevelState(String, Object)
219 * @see HighLevelStateHandler#get(String)
221 * @author Daniel Kirschten
223 public final Object getHighLevelState(String stateID)
225 return highLevelStateHandler.get(stateID);
229 * Sets the given high-level state to the given value. <br>
230 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
232 * @see #getHighLevelState(String)
233 * @see HighLevelStateHandler#set(String, Object)
235 * @author Daniel Kirschten
237 public final void setHighLevelState(String stateID, Object newState)
239 highLevelStateHandler.set(stateID, newState);
242 public final void addHighLevelStateListener(String stateID, Consumer<Object> stateChanged)
244 highLevelStateHandler.addListener(stateID, stateChanged);
247 public final void removeHighLevelStateListener(String stateID, Consumer<Object> stateChanged)
249 highLevelStateHandler.removeListener(stateID, stateChanged);
252 // "graphical" operations
255 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
257 * @author Daniel Kirschten
259 public void moveTo(double x, double y)
263 callComponentMovedListeners();
264 model.requestRedraw();
268 * Sets the size of this component and calls redrawListeners.
270 * @author Daniel Kirschten
272 protected void setSize(double width, double height)
274 bounds.width = width;
275 bounds.height = height;
276 callComponentResizedListener();
277 model.requestRedraw();
281 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
282 * {@link #getHeight}, because new objects are created.
284 * @author Daniel Kirschten
286 public final Rectangle getBounds()
288 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
292 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
294 * @author Daniel Kirschten
296 public double getPosX()
302 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
304 * @author Daniel Kirschten
306 public double getPosY()
312 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
314 * @author Daniel Kirschten
316 public double getWidth()
322 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
324 * @author Daniel Kirschten
326 public double getHeight()
328 return bounds.height;
332 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
335 * @author Daniel Kirschten
337 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
338 public boolean clicked(double x, double y)
344 * Render this component to the given gc, in absoulute coordinates.
346 * @author Daniel Kirschten
348 public abstract void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion);
353 public Object getParamsForSerializing(IdentifyParams idParams)
361 public void addComponentMovedListener (Consumer<? super ModelComponent> listener) {componentMovedListeners .add (listener);}
362 public void addComponentResizedListener (Consumer<? super ModelComponent> listener) {componentResizedListeners.add (listener);}
363 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
364 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
366 public void removeComponentMovedListener (Consumer<? super ModelComponent> listener) {componentMovedListeners .remove(listener);}
367 public void removeComponentResizedListener (Consumer<? super ModelComponent> listener) {componentResizedListeners.remove(listener);}
368 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
369 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
371 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
372 private void callComponentResizedListener( ) {componentResizedListeners.forEach(l -> l.accept(this));}
373 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
374 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}