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 model.componentCreated(this);
81 * Destroys this component. This method implicitly calls {@link ViewModelModifiable#componentDestroyed(GUIComponent)
82 * componentDestroyed()} for the model this component is a part of.
84 * @author Daniel Kirschten
88 pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
89 model.componentDestroyed(this);
95 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
97 * @throws IllegalArgumentException if the pin doesn't belong to this component
98 * @throws IllegalArgumentException if there already is a pin with the given name
100 * @author Daniel Kirschten
102 protected void addPin(Pin pin)
104 if (pin.component != this)
105 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
106 if (pinsByName.containsKey(pin.name))
107 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
108 pinsByName.put(pin.name, pin);
109 callPinAddedListeners(pin);
110 pin.addRedrawListener(redrawListenerForSubcomponents);
115 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
117 * @throws NullPointerException if there was no pin with this name
119 * @author Daniel Kirschten
121 protected void removePin(String name)
123 Pin pin = pinsByName.remove(name);
124 callPinRemovedListeners(pin);
125 pin.removeRedrawListener(redrawListenerForSubcomponents);
130 * Returns a collection of pins of this component.
132 * @author Daniel Kirschten
134 public Map<String, Pin> getPins()
136 return pinsUnmodifiable;
140 * Returns the pin with the given name of this component.
142 * @throws IllegalArgumentException if there is no pin with the given name
144 * @author Daniel Kirschten
146 public Pin getPin(String name)
148 Pin pin = pinsByName.get(name);
150 throw new IllegalArgumentException("No pin with the name " + name);
157 * @author Daniel Kirschten
159 protected void setHighLevelStateHandler(HighLevelStateHandler highLevelStateHandler)
161 this.highLevelStateHandler = highLevelStateHandler;
164 public HighLevelStateHandler getHighLevelStateHandler()
166 return highLevelStateHandler;
170 * Gets the current value of the given high-level state. <br>
171 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
173 * @see #setHighLevelState(String, Object)
174 * @see HighLevelStateHandler#getHighLevelState(String)
176 * @author Daniel Kirschten
178 public Object getHighLevelState(String stateID)
180 return highLevelStateHandler.getHighLevelState(stateID);
184 * Sets the given high-level state to the given value. <br>
185 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
187 * @see #getHighLevelState(String)
188 * @see HighLevelStateHandler#setHighLevelState(String, Object)
190 * @author Daniel Kirschten
192 public void setHighLevelState(String stateID, Object newState)
194 highLevelStateHandler.setHighLevelState(stateID, newState);
197 // "graphical" operations
200 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
202 * @author Daniel Kirschten
204 public void moveTo(double x, double y)
208 callComponentMovedListeners();
213 * Sets the size of this component and calls redrawListeners.
215 * @author Daniel Kirschten
217 protected void setSize(double width, double height)
219 bounds.width = width;
220 bounds.height = height;
221 callComponentResizedListener();
226 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
227 * {@link #getHeight}, because new objects are created.
229 * @author Daniel Kirschten
231 public final Rectangle getBounds()
233 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
237 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
239 * @author Daniel Kirschten
241 public double getPosX()
247 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
249 * @author Daniel Kirschten
251 public double getPosY()
257 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
259 * @author Daniel Kirschten
261 public double getWidth()
267 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
269 * @author Daniel Kirschten
271 public double getHeight()
273 return bounds.height;
277 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
280 * @author Daniel Kirschten
282 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
283 public boolean clicked(double x, double y)
289 * Render this component to the given gc, in absoulute coordinates.
291 * @author Daniel Kirschten
293 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
298 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
300 return JsonNull.INSTANCE;
306 * Calls redraw listeners.
308 * @author Daniel Kirschten
310 protected void requestRedraw()
312 callRedrawListeners();
316 public void addComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .add (listener);}
317 public void addComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.add (listener);}
318 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
319 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
320 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
322 public void removeComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
323 public void removeComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.remove(listener);}
324 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
325 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
326 public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);}
328 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
329 private void callComponentResizedListener( ) {componentResizedListeners.forEach(l -> l.accept(this));}
330 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
331 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}
332 private void callRedrawListeners ( ) {redrawListeners .forEach(l -> l.run( ));}