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.ViewModelModifiable;
13 import net.mograsim.logic.model.model.wires.Pin;
14 import net.mograsim.logic.model.serializing.IdentifierGetter;
15 import net.mograsim.logic.model.serializing.JSONSerializable;
16 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
19 * The base class for all GUI components.<br>
20 * A <code>GUIComponent</code> has a reference to the ViewModel it belongs to.<br>
21 * A <code>GUIComponent</code> has a name. This name is unique in the model the <code>GUIComponent</code> belongs to.<br>
22 * A <code>GUIComponent</code> has a position and size. The size can only be modified by subclasses.
24 * @author Daniel Kirschten
26 public abstract class GUIComponent implements JSONSerializable
29 * The model this component is a part of.
31 protected final ViewModelModifiable 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 GUIComponent>> componentMovedListeners;
47 private final List<Consumer<? super GUIComponent>> 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 GUIComponent(ViewModelModifiable model, String name)
58 this.name = name == null ? model.getDefaultComponentName(this) : name;
59 this.bounds = new Rectangle(0, 0, 0, 0);
60 this.pinsByName = new HashMap<>();
61 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
63 this.componentMovedListeners = new ArrayList<>();
64 this.componentResizedListeners = new ArrayList<>();
65 this.pinAddedListeners = new ArrayList<>();
66 this.pinRemovedListeners = new ArrayList<>();
68 // TODO this will crash the high level state debug shell because submodel is not yet set.
69 // The same problem exists in ViewModelModifiable.getDefaultComponentName; see there
70 model.componentCreated(this, this::destroyed);
74 * Destroys this component. This method is called from {@link ViewModelModifiable#componentDestroyed(GUIComponent) destroyComponent()}
75 * of the model this component is a part of.<br>
76 * When overriding, make sure to also call the original implementation.
78 * @author Daniel Kirschten
80 protected void destroyed()
82 pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
88 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
90 * @throws IllegalArgumentException if the pin doesn't belong to this component
91 * @throws IllegalArgumentException if there already is a pin with the given name
93 * @author Daniel Kirschten
95 protected void addPin(Pin pin)
97 if (pin.component != this)
98 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
99 if (pinsByName.containsKey(pin.name))
100 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
101 pinsByName.put(pin.name, pin);
102 callPinAddedListeners(pin);
103 model.requestRedraw();
107 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
109 * @throws NullPointerException if there was no pin with this name
111 * @author Daniel Kirschten
113 protected void removePin(String name)
115 Pin pin = pinsByName.remove(name);
116 callPinRemovedListeners(pin);
117 model.requestRedraw();
121 * Returns a collection of pins of this component.
123 * @author Daniel Kirschten
125 public Map<String, Pin> getPins()
127 return pinsUnmodifiable;
131 * Returns the pin with the given name of this component.
133 * @throws IllegalArgumentException if there is no pin with the given name
135 * @author Daniel Kirschten
137 public Pin getPin(String name)
139 Pin pin = pinsByName.get(name);
141 throw new IllegalArgumentException("No pin with the name " + name);
148 * @author Daniel Kirschten
150 protected void setHighLevelStateHandler(HighLevelStateHandler highLevelStateHandler)
152 this.highLevelStateHandler = highLevelStateHandler;
155 public HighLevelStateHandler getHighLevelStateHandler()
157 return highLevelStateHandler;
161 * Gets the current value of the given high-level state. <br>
162 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
164 * @see #setHighLevelState(String, Object)
165 * @see HighLevelStateHandler#getHighLevelState(String)
167 * @author Daniel Kirschten
169 public Object getHighLevelState(String stateID)
171 return highLevelStateHandler.getHighLevelState(stateID);
175 * Sets the given high-level state to the given value. <br>
176 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
178 * @see #getHighLevelState(String)
179 * @see HighLevelStateHandler#setHighLevelState(String, Object)
181 * @author Daniel Kirschten
183 public void setHighLevelState(String stateID, Object newState)
185 highLevelStateHandler.setHighLevelState(stateID, newState);
188 // "graphical" operations
191 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
193 * @author Daniel Kirschten
195 public void moveTo(double x, double y)
199 callComponentMovedListeners();
200 model.requestRedraw();
204 * Sets the size of this component and calls redrawListeners.
206 * @author Daniel Kirschten
208 protected void setSize(double width, double height)
210 bounds.width = width;
211 bounds.height = height;
212 callComponentResizedListener();
213 model.requestRedraw();
217 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
218 * {@link #getHeight}, because new objects are created.
220 * @author Daniel Kirschten
222 public final Rectangle getBounds()
224 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
228 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
230 * @author Daniel Kirschten
232 public double getPosX()
238 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
240 * @author Daniel Kirschten
242 public double getPosY()
248 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
250 * @author Daniel Kirschten
252 public double getWidth()
258 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
260 * @author Daniel Kirschten
262 public double getHeight()
264 return bounds.height;
268 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
271 * @author Daniel Kirschten
273 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
274 public boolean clicked(double x, double y)
280 * Render this component to the given gc, in absoulute coordinates.
282 * @author Daniel Kirschten
284 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
289 public Object getParamsForSerializing(IdentifierGetter idGetter)
297 public void addComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .add (listener);}
298 public void addComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.add (listener);}
299 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
300 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
302 public void removeComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
303 public void removeComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.remove(listener);}
304 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
305 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
307 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
308 private void callComponentResizedListener( ) {componentResizedListeners.forEach(l -> l.accept(this));}
309 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
310 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}