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 // TODO split into "blueprint" and "used component"
29 public abstract class ModelComponent implements JSONSerializable
32 * The model this component is a part of.
34 protected final LogicModelModifiable model;
36 * The name of this component. Is unique for all components in its model.<br>
37 * Does never change, but can't be final since it is set in {@link #init()}.
40 private final Rectangle bounds;
42 * The list of all pins of this component by name.
44 private final Map<String, Pin> pinsByName;
46 * An unmodifiable view of {@link #pinsByName}.
48 protected final Map<String, Pin> pinsUnmodifiable;
50 private final List<Consumer<? super ModelComponent>> componentMovedListeners;
51 private final List<Consumer<? super ModelComponent>> componentResizedListeners;
52 private final List<Consumer<? super Pin>> pinAddedListeners;
53 private final List<Consumer<? super Pin>> pinRemovedListeners;
55 private HighLevelStateHandler highLevelStateHandler;
57 // creation and destruction
59 public ModelComponent(LogicModelModifiable model, String name)
61 this(model, name, true);
65 * Creates a new {@link ModelComponent} and, if <code>callInit</code>, initializes the component (See {@link #init()}).<br>
66 * If <code>callInit==false</code>, make sure to call {@link #init()}!
68 * @author Daniel Kirschten
70 protected ModelComponent(LogicModelModifiable model, String name, boolean callInit)
74 this.bounds = new Rectangle(0, 0, 0, 0);
75 this.pinsByName = new HashMap<>();
76 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
78 this.componentMovedListeners = new ArrayList<>();
79 this.componentResizedListeners = new ArrayList<>();
80 this.pinAddedListeners = new ArrayList<>();
81 this.pinRemovedListeners = new ArrayList<>();
83 this.highLevelStateHandler = new DefaultHighLevelStateHandler();
90 * Initializes this component. This method should be called exactly once in this component's constructor.<br>
92 * <li>If <code>{@link #name}==null</code>, sets {@link #name} to {@link LogicModelModifiable#getDefaultComponentName(ModelComponent)}.
93 * <li>Registers this component in the model.
99 name = model.getDefaultComponentName(this);
100 model.componentCreated(this, this::destroyed);
104 * Destroys this component. This method is called from {@link LogicModelModifiable#componentDestroyed(ModelComponent)
105 * destroyComponent()} of the model this component is a part of.<br>
106 * When overriding, make sure to also call the original implementation.
108 * @author Daniel Kirschten
110 protected void destroyed()
112 pinsByName.values().forEach(this::removePinWithoutRedraw);
117 public String getName()
125 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
127 * @throws IllegalArgumentException if the pin doesn't belong to this component
128 * @throws IllegalArgumentException if there already is a pin with the given name
130 * @author Daniel Kirschten
132 protected void addPin(Pin pin)
134 if (pin.component != this)
135 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
136 if (pinsByName.containsKey(pin.name))
137 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
138 pinsByName.put(pin.name, pin);
139 callPinAddedListeners(pin);
140 model.requestRedraw();
144 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
146 * @throws NullPointerException if there was no pin with this name
148 * @author Daniel Kirschten
150 protected void removePin(String name)
152 removePinWithoutRedraw(pinsByName.remove(name));
153 model.requestRedraw();
156 private void removePinWithoutRedraw(Pin pin)
159 callPinRemovedListeners(pin);
163 * Returns a collection of pins of this component.
165 * @author Daniel Kirschten
167 public Map<String, Pin> getPins()
169 return pinsUnmodifiable;
173 * Returns the pin with the given name of this component.
175 * @throws IllegalArgumentException if there is no pin with the given name
176 * @see #getPinOrNull(String)
178 * @author Daniel Kirschten
180 public Pin getPin(String name)
182 Pin pin = getPinOrNull(name);
184 throw new IllegalArgumentException("No pin with the name " + name);
189 * Returns the pin with the given name of this component, or <code>null</code> if there is no such pin.
191 * @see #getPin(String)
193 * @author Daniel Kirschten
195 public Pin getPinOrNull(String name)
197 return pinsByName.get(name);
203 * @author Daniel Kirschten
205 protected void setHighLevelStateHandler(HighLevelStateHandler highLevelStateHandler)
207 this.highLevelStateHandler = highLevelStateHandler;
210 public HighLevelStateHandler getHighLevelStateHandler()
212 return highLevelStateHandler;
216 * Gets the current value of the given high-level state. <br>
217 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
219 * @see #setHighLevelState(String, Object)
220 * @see HighLevelStateHandler#get(String)
222 * @author Daniel Kirschten
224 public final Object getHighLevelState(String stateID)
226 return highLevelStateHandler.get(stateID);
230 * Sets the given high-level state to the given value. <br>
231 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
233 * @see #getHighLevelState(String)
234 * @see HighLevelStateHandler#set(String, Object)
236 * @author Daniel Kirschten
238 public final void setHighLevelState(String stateID, Object newState)
240 highLevelStateHandler.set(stateID, newState);
243 public final void addHighLevelStateListener(String stateID, Consumer<Object> stateChanged)
245 highLevelStateHandler.addListener(stateID, stateChanged);
248 public final void removeHighLevelStateListener(String stateID, Consumer<Object> stateChanged)
250 highLevelStateHandler.removeListener(stateID, stateChanged);
253 // "graphical" operations
256 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
258 * @author Daniel Kirschten
260 public void moveTo(double x, double y)
264 callComponentMovedListeners();
265 model.requestRedraw();
269 * Sets the size of this component and calls redrawListeners.
271 * @author Daniel Kirschten
273 protected void setSize(double width, double height)
275 bounds.width = width;
276 bounds.height = height;
277 callComponentResizedListener();
278 model.requestRedraw();
282 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
283 * {@link #getHeight}, because new objects are created.
285 * @author Daniel Kirschten
287 public final Rectangle getBounds()
289 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
293 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
295 * @author Daniel Kirschten
297 public double getPosX()
303 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
305 * @author Daniel Kirschten
307 public double getPosY()
313 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
315 * @author Daniel Kirschten
317 public double getWidth()
323 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
325 * @author Daniel Kirschten
327 public double getHeight()
329 return bounds.height;
333 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
336 * @author Daniel Kirschten
338 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
339 public boolean clicked(double x, double y)
345 * Render this component to the given gc, in absoulute coordinates.
347 * @author Daniel Kirschten
349 public abstract void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion);
354 public Object getParamsForSerializing(IdentifyParams idParams)
362 public void addComponentMovedListener (Consumer<? super ModelComponent> listener) {componentMovedListeners .add (listener);}
363 public void addComponentResizedListener (Consumer<? super ModelComponent> listener) {componentResizedListeners.add (listener);}
364 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
365 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
367 public void removeComponentMovedListener (Consumer<? super ModelComponent> listener) {componentMovedListeners .remove(listener);}
368 public void removeComponentResizedListener (Consumer<? super ModelComponent> listener) {componentResizedListeners.remove(listener);}
369 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
370 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
372 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
373 private void callComponentResizedListener( ) {componentResizedListeners.forEach(l -> l.accept(this));}
374 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
375 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}