1 package net.mograsim.logic.ui.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.ui.model.ViewModelModifiable;
16 import net.mograsim.logic.ui.model.wires.Pin;
19 * The base class for all GUI components.<br>
20 * A <code>GUIComponent</code> has a position and size. The size can only be modified by subclasses.<br>
22 * @author Daniel Kirschten
24 public abstract class GUIComponent
27 * The model this component is a part of.
29 protected final ViewModelModifiable model;
30 private final Rectangle bounds;
32 * The list of all pins of this component by name.
34 private final Map<String, Pin> pinsByName;
36 * An unmodifiable view of {@link #pinsByName}.
38 protected final Map<String, Pin> pinsUnmodifiable;
40 private final List<Consumer<? super GUIComponent>> componentMovedListeners;
41 private final List<Consumer<? super Pin>> pinAddedListeners;
42 private final List<Consumer<? super Pin>> pinRemovedListeners;
43 private final List<Runnable> redrawListeners;
45 private final Runnable redrawListenerForSubcomponents;
47 // creation and destruction
49 public GUIComponent(ViewModelModifiable model)
52 this.bounds = new Rectangle(0, 0, 0, 0);
53 this.pinsByName = new HashMap<>();
54 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
56 this.componentMovedListeners = new ArrayList<>();
57 this.pinAddedListeners = new ArrayList<>();
58 this.pinRemovedListeners = new ArrayList<>();
59 this.redrawListeners = new ArrayList<>();
61 redrawListenerForSubcomponents = this::requestRedraw;
63 model.componentCreated(this);
67 * Destroys this component. This method implicitly calls {@link ViewModelModifiable#componentDestroyed(GUIComponent)
68 * componentDestroyed()} for the model this component is a part of.
70 * @author Daniel Kirschten
74 pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
75 model.componentDestroyed(this);
81 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
83 * @throws IllegalArgumentException if the pin doesn't belong to this component
84 * @throws IllegalArgumentException if there already is a pin with the given name
86 * @author Daniel Kirschten
88 protected void addPin(Pin pin)
90 if (pin.component != this)
91 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
92 if (pinsByName.containsKey(pin.name))
93 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
94 pinsByName.put(pin.name, pin);
95 callPinAddedListeners(pin);
96 pin.addRedrawListener(redrawListenerForSubcomponents);
101 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
103 * @throws NullPointerException if there was no pin with this name
105 * @author Daniel Kirschten
107 protected void removePin(String name)
109 Pin pin = pinsByName.remove(name);
110 callPinRemovedListeners(pin);
111 pin.removeRedrawListener(redrawListenerForSubcomponents);
116 * Returns a collection of pins of this component.
118 * @author Daniel Kirschten
120 public Map<String, Pin> getPins()
122 return pinsUnmodifiable;
126 * Returns the pin with the given name of this component.
128 * @throws IllegalArgumentException if there is no pin with the given name
130 * @author Daniel Kirschten
132 public Pin getPin(String name)
134 Pin pin = pinsByName.get(name);
136 throw new IllegalArgumentException("No pin with the name " + name);
143 * Sets the given high-level state to the given value. <br>
144 * A high level state ID consists of parts separated by dots ('.').<br>
145 * The last part (the part after the last dot) is called "atomic high level state ID". The parts before that part are called
146 * "subcomponent ID"s.<br>
147 * If there is no dot in a high level state ID, the whole high level state ID is called atomic.<br>
148 * Note that subcomponent IDs don't have to correspond to actual subcomponents. For example, a RAM component may supply subcomponent IDs
149 * "c0000", "c0001" ... "cFFFF" without actually having a subcomponent for each cell. It also is allowed for an atomic high level state
150 * ID to be delegated to a subcomponent.
152 * @author Daniel Kirschten
154 @SuppressWarnings({ "static-method", "unused" }) // this method is intended to be overridden
155 public void setHighLevelState(String stateID, Object newState)
157 throw new IllegalArgumentException("No high level state with ID " + stateID);
161 * Gets the current value of the given high-level state. <br>
162 * See {@link #setHighLevelState(String, Object)} for an explanation of high-level state IDs.
164 * @author Daniel Kirschten
166 @SuppressWarnings("static-method") // this method is intended to be overridden
167 public Object getHighLevelState(String stateID)
169 throw new IllegalArgumentException("No high level state with ID " + stateID);
172 // "graphical" operations
175 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
177 * @author Daniel Kirschten
179 public void moveTo(double x, double y)
183 callComponentMovedListeners();
188 * Sets the size of this component and calls redrawListeners.
190 * @author Daniel Kirschten
192 protected void setSize(double width, double height)
194 bounds.width = width;
195 bounds.height = height;
200 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
201 * {@link #getHeight}, because new objects are created.
203 * @author Daniel Kirschten
205 public final Rectangle getBounds()
207 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
211 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
213 * @author Daniel Kirschten
215 public double getPosX()
221 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
223 * @author Daniel Kirschten
225 public double getPosY()
231 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
233 * @author Daniel Kirschten
235 public double getWidth()
241 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
243 * @author Daniel Kirschten
245 public double getHeight()
247 return bounds.height;
251 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
254 * @author Daniel Kirschten
256 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
257 public boolean clicked(double x, double y)
263 * Render this component to the given gc, in absoulute coordinates.
265 * @author Daniel Kirschten
267 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
271 @SuppressWarnings("static-method") // this method is intended to be overridden
272 public JsonElement getParams()
274 return JsonNull.INSTANCE;
280 * Calls redraw listeners.
282 * @author Daniel Kirschten
284 protected void requestRedraw()
286 callRedrawListeners();
290 public void addComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners.add (listener);}
291 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
292 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
293 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
295 public void removeComponentMovedListener(Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
296 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
297 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
298 public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);}
300 private void callComponentMovedListeners( ) {componentMovedListeners.forEach(l -> l.accept(this));}
301 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
302 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}
303 private void callRedrawListeners ( ) {redrawListeners .forEach(l -> l.run( ));}