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.snippets.HighLevelStateHandler;
20 * The base class for all GUI components.<br>
21 * A <code>GUIComponent</code> has a reference to the ViewModel it belongs to.<br>
22 * A <code>GUIComponent</code> has a name. This name is unique in the model the <code>GUIComponent</code> belongs to.<br>
23 * A <code>GUIComponent</code> has a position and size. The size can only be modified by subclasses.
25 * @author Daniel Kirschten
27 public abstract class GUIComponent
30 * The model this component is a part of.
32 protected final ViewModelModifiable model;
34 * The name of this component. Is unique for all components in its model.
36 public final String name;
37 private final Rectangle bounds;
39 * The list of all pins of this component by name.
41 private final Map<String, Pin> pinsByName;
43 * An unmodifiable view of {@link #pinsByName}.
45 protected final Map<String, Pin> pinsUnmodifiable;
47 private final List<Consumer<? super GUIComponent>> componentMovedListeners;
48 private final List<Consumer<? super GUIComponent>> componentResizedListeners;
49 private final List<Consumer<? super Pin>> pinAddedListeners;
50 private final List<Consumer<? super Pin>> pinRemovedListeners;
51 private final List<Runnable> redrawListeners;
53 private final Runnable redrawListenerForSubcomponents;
55 private HighLevelStateHandler highLevelStateHandler;
57 // creation and destruction
59 public GUIComponent(ViewModelModifiable model, String name)
62 this.name = name == null ? model.getDefaultComponentName(this) : name;
63 this.bounds = new Rectangle(0, 0, 0, 0);
64 this.pinsByName = new HashMap<>();
65 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
67 this.componentMovedListeners = new ArrayList<>();
68 this.componentResizedListeners = new ArrayList<>();
69 this.pinAddedListeners = new ArrayList<>();
70 this.pinRemovedListeners = new ArrayList<>();
71 this.redrawListeners = new ArrayList<>();
73 redrawListenerForSubcomponents = this::requestRedraw;
75 model.componentCreated(this);
79 * Destroys this component. This method implicitly calls {@link ViewModelModifiable#componentDestroyed(GUIComponent)
80 * componentDestroyed()} for the model this component is a part of.
82 * @author Daniel Kirschten
86 pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
87 model.componentDestroyed(this);
93 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
95 * @throws IllegalArgumentException if the pin doesn't belong to this component
96 * @throws IllegalArgumentException if there already is a pin with the given name
98 * @author Daniel Kirschten
100 protected void addPin(Pin pin)
102 if (pin.component != this)
103 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
104 if (pinsByName.containsKey(pin.name))
105 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
106 pinsByName.put(pin.name, pin);
107 callPinAddedListeners(pin);
108 pin.addRedrawListener(redrawListenerForSubcomponents);
113 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
115 * @throws NullPointerException if there was no pin with this name
117 * @author Daniel Kirschten
119 protected void removePin(String name)
121 Pin pin = pinsByName.remove(name);
122 callPinRemovedListeners(pin);
123 pin.removeRedrawListener(redrawListenerForSubcomponents);
128 * Returns a collection of pins of this component.
130 * @author Daniel Kirschten
132 public Map<String, Pin> getPins()
134 return pinsUnmodifiable;
138 * Returns the pin with the given name of this component.
140 * @throws IllegalArgumentException if there is no pin with the given name
142 * @author Daniel Kirschten
144 public Pin getPin(String name)
146 Pin pin = pinsByName.get(name);
148 throw new IllegalArgumentException("No pin with the name " + name);
155 * @author Daniel Kirschten
157 protected void setHighLevelStateHandler(HighLevelStateHandler highLevelStateHandler)
159 this.highLevelStateHandler = highLevelStateHandler;
163 * Gets the current value of the given high-level state. <br>
164 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
166 * @see #setHighLevelState(String, Object)
167 * @see HighLevelStateHandler#getHighLevelState(String)
169 * @author Daniel Kirschten
171 public Object getHighLevelState(String stateID)
173 return highLevelStateHandler.getHighLevelState(stateID);
177 * Sets the given high-level state to the given value. <br>
178 * See {@link HighLevelStateHandler} for an explanation of high-level state IDs.
180 * @see #getHighLevelState(String)
181 * @see HighLevelStateHandler#setHighLevelState(String, Object)
183 * @author Daniel Kirschten
185 public void setHighLevelState(String stateID, Object newState)
187 highLevelStateHandler.setHighLevelState(stateID, newState);
190 // "graphical" operations
193 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
195 * @author Daniel Kirschten
197 public void moveTo(double x, double y)
201 callComponentMovedListeners();
206 * Sets the size of this component and calls redrawListeners.
208 * @author Daniel Kirschten
210 protected void setSize(double width, double height)
212 bounds.width = width;
213 bounds.height = height;
214 callComponentResizedListener();
219 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
220 * {@link #getHeight}, because new objects are created.
222 * @author Daniel Kirschten
224 public final Rectangle getBounds()
226 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
230 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
232 * @author Daniel Kirschten
234 public double getPosX()
240 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
242 * @author Daniel Kirschten
244 public double getPosY()
250 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
252 * @author Daniel Kirschten
254 public double getWidth()
260 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
262 * @author Daniel Kirschten
264 public double getHeight()
266 return bounds.height;
270 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
273 * @author Daniel Kirschten
275 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
276 public boolean clicked(double x, double y)
282 * Render this component to the given gc, in absoulute coordinates.
284 * @author Daniel Kirschten
286 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
290 @SuppressWarnings("static-method") // this method is intended to be overridden
291 public JsonElement getParamsForSerializing()
293 return JsonNull.INSTANCE;
299 * Calls redraw listeners.
301 * @author Daniel Kirschten
303 protected void requestRedraw()
305 callRedrawListeners();
309 public void addComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .add (listener);}
310 public void addComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.add (listener);}
311 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
312 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
313 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
315 public void removeComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
316 public void removeComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.remove(listener);}
317 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
318 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
319 public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);}
321 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
322 private void callComponentResizedListener( ) {componentResizedListeners.forEach(l -> l.accept(this));}
323 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
324 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}
325 private void callRedrawListeners ( ) {redrawListeners .forEach(l -> l.run( ));}