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 com.google.gson.JsonElement;
11 import com.google.gson.JsonNull;
13 import net.haspamelodica.swt.helper.gcs.GeneralGC;
14 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
15 import net.mograsim.logic.model.model.ViewModelModifiable;
16 import net.mograsim.logic.model.model.wires.Pin;
17 import net.mograsim.logic.model.serializing.IdentifierGetter;
18 import net.mograsim.logic.model.serializing.JSONSerializable;
19 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
22 * The base class for all GUI components.<br>
23 * A <code>GUIComponent</code> has a reference to the ViewModel it belongs to.<br>
24 * A <code>GUIComponent</code> has a name. This name is unique in the model the <code>GUIComponent</code> belongs to.<br>
25 * A <code>GUIComponent</code> has a position and size. The size can only be modified by subclasses.
27 * @author Daniel Kirschten
29 public abstract class GUIComponent implements JSONSerializable
32 * The model this component is a part of.
34 protected final ViewModelModifiable model;
36 * The name of this component. Is unique for all components in its model.
38 public final String name;
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 GUIComponent>> componentMovedListeners;
50 private final List<Consumer<? super GUIComponent>> componentResizedListeners;
51 private final List<Consumer<? super Pin>> pinAddedListeners;
52 private final List<Consumer<? super Pin>> pinRemovedListeners;
53 private final List<Runnable> redrawListeners;
55 private final Runnable redrawListenerForSubcomponents;
57 private HighLevelStateHandler highLevelStateHandler;
59 // creation and destruction
61 public GUIComponent(ViewModelModifiable model, String name)
64 this.name = name == null ? model.getDefaultComponentName(this) : name;
65 this.bounds = new Rectangle(0, 0, 0, 0);
66 this.pinsByName = new HashMap<>();
67 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
69 this.componentMovedListeners = new ArrayList<>();
70 this.componentResizedListeners = new ArrayList<>();
71 this.pinAddedListeners = new ArrayList<>();
72 this.pinRemovedListeners = new ArrayList<>();
73 this.redrawListeners = new ArrayList<>();
75 redrawListenerForSubcomponents = this::requestRedraw;
77 // TODO this will crash the high level state debug shell because submodel is not yet set.
78 // The same problem exists in ViewModelModifiable.getDefaultComponentName; see there
79 model.componentCreated(this);
83 * Destroys this component. This method implicitly calls {@link ViewModelModifiable#componentDestroyed(GUIComponent)
84 * componentDestroyed()} for the model this component is a part of.
86 * @author Daniel Kirschten
90 pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
91 model.componentDestroyed(this);
97 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
99 * @throws IllegalArgumentException if the pin doesn't belong to this component
100 * @throws IllegalArgumentException if there already is a pin with the given name
102 * @author Daniel Kirschten
104 protected void addPin(Pin pin)
106 if (pin.component != this)
107 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
108 if (pinsByName.containsKey(pin.name))
109 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
110 pinsByName.put(pin.name, pin);
111 callPinAddedListeners(pin);
112 pin.addRedrawListener(redrawListenerForSubcomponents);
117 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
119 * @throws NullPointerException if there was no pin with this name
121 * @author Daniel Kirschten
123 protected void removePin(String name)
125 Pin pin = pinsByName.remove(name);
126 callPinRemovedListeners(pin);
127 pin.removeRedrawListener(redrawListenerForSubcomponents);
132 * Returns a collection of pins of this component.
134 * @author Daniel Kirschten
136 public Map<String, Pin> getPins()
138 return pinsUnmodifiable;
142 * Returns the pin with the given name of this component.
144 * @throws IllegalArgumentException if there is no pin with the given name
146 * @author Daniel Kirschten
148 public Pin getPin(String name)
150 Pin pin = pinsByName.get(name);
152 throw new IllegalArgumentException("No pin with the name " + name);
159 * @author Daniel Kirschten
161 protected void setHighLevelStateHandler(HighLevelStateHandler highLevelStateHandler)
163 this.highLevelStateHandler = highLevelStateHandler;
166 public HighLevelStateHandler getHighLevelStateHandler()
168 return highLevelStateHandler;
172 * Gets the current value of the given high-level state. <br>
173 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
175 * @see #setHighLevelState(String, Object)
176 * @see HighLevelStateHandler#getHighLevelState(String)
178 * @author Daniel Kirschten
180 public Object getHighLevelState(String stateID)
182 return highLevelStateHandler.getHighLevelState(stateID);
186 * Sets the given high-level state to the given value. <br>
187 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
189 * @see #getHighLevelState(String)
190 * @see HighLevelStateHandler#setHighLevelState(String, Object)
192 * @author Daniel Kirschten
194 public void setHighLevelState(String stateID, Object newState)
196 highLevelStateHandler.setHighLevelState(stateID, newState);
199 // "graphical" operations
202 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
204 * @author Daniel Kirschten
206 public void moveTo(double x, double y)
210 callComponentMovedListeners();
215 * Sets the size of this component and calls redrawListeners.
217 * @author Daniel Kirschten
219 protected void setSize(double width, double height)
221 bounds.width = width;
222 bounds.height = height;
223 callComponentResizedListener();
228 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
229 * {@link #getHeight}, because new objects are created.
231 * @author Daniel Kirschten
233 public final Rectangle getBounds()
235 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
239 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
241 * @author Daniel Kirschten
243 public double getPosX()
249 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
251 * @author Daniel Kirschten
253 public double getPosY()
259 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
261 * @author Daniel Kirschten
263 public double getWidth()
269 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
271 * @author Daniel Kirschten
273 public double getHeight()
275 return bounds.height;
279 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
282 * @author Daniel Kirschten
284 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
285 public boolean clicked(double x, double y)
291 * Render this component to the given gc, in absoulute coordinates.
293 * @author Daniel Kirschten
295 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
300 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
302 return JsonNull.INSTANCE;
308 * Calls redraw listeners.
310 * @author Daniel Kirschten
312 protected void requestRedraw()
314 callRedrawListeners();
318 public void addComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .add (listener);}
319 public void addComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.add (listener);}
320 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
321 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
322 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
324 public void removeComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
325 public void removeComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.remove(listener);}
326 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
327 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
328 public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);}
330 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
331 private void callComponentResizedListener( ) {componentResizedListeners.forEach(l -> l.accept(this));}
332 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
333 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}
334 private void callRedrawListeners ( ) {redrawListeners .forEach(l -> l.run( ));}