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;
17 import net.mograsim.logic.ui.serializing.snippets.HighLevelStateHandler;
20 * The base class for all GUI components.<br>
21 * A <code>GUIComponent</code> has a position and size. The size can only be modified by subclasses.<br>
23 * @author Daniel Kirschten
25 public abstract class GUIComponent
28 * The model this component is a part of.
30 protected final ViewModelModifiable model;
31 private final Rectangle bounds;
33 * The list of all pins of this component by name.
35 private final Map<String, Pin> pinsByName;
37 * An unmodifiable view of {@link #pinsByName}.
39 protected final Map<String, Pin> pinsUnmodifiable;
41 private final List<Consumer<? super GUIComponent>> componentMovedListeners;
42 private final List<Consumer<? super GUIComponent>> componentResizedListeners;
43 private final List<Consumer<? super Pin>> pinAddedListeners;
44 private final List<Consumer<? super Pin>> pinRemovedListeners;
45 private final List<Runnable> redrawListeners;
47 private final Runnable redrawListenerForSubcomponents;
48 // creation and destruction
50 public GUIComponent(ViewModelModifiable model)
53 this.bounds = new Rectangle(0, 0, 0, 0);
54 this.pinsByName = new HashMap<>();
55 this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName);
57 this.componentMovedListeners = new ArrayList<>();
58 this.componentResizedListeners = new ArrayList<>();
59 this.pinAddedListeners = new ArrayList<>();
60 this.pinRemovedListeners = new ArrayList<>();
61 this.redrawListeners = new ArrayList<>();
63 redrawListenerForSubcomponents = this::requestRedraw;
65 model.componentCreated(this);
69 * Destroys this component. This method implicitly calls {@link ViewModelModifiable#componentDestroyed(GUIComponent)
70 * componentDestroyed()} for the model this component is a part of.
72 * @author Daniel Kirschten
76 pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
77 model.componentDestroyed(this);
83 * Adds the given pin to this component and calls pinAddedListeners and redrawListeners.
85 * @throws IllegalArgumentException if the pin doesn't belong to this component
86 * @throws IllegalArgumentException if there already is a pin with the given name
88 * @author Daniel Kirschten
90 protected void addPin(Pin pin)
92 if (pin.component != this)
93 throw new IllegalArgumentException("Can't add a pin not belonging to this component!");
94 if (pinsByName.containsKey(pin.name))
95 throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
96 pinsByName.put(pin.name, pin);
97 callPinAddedListeners(pin);
98 pin.addRedrawListener(redrawListenerForSubcomponents);
103 * Removes the given pin from this component and calls pinAddedListeners and redrawListeners.
105 * @throws NullPointerException if there was no pin with this name
107 * @author Daniel Kirschten
109 protected void removePin(String name)
111 Pin pin = pinsByName.remove(name);
112 callPinRemovedListeners(pin);
113 pin.removeRedrawListener(redrawListenerForSubcomponents);
118 * Returns a collection of pins of this component.
120 * @author Daniel Kirschten
122 public Map<String, Pin> getPins()
124 return pinsUnmodifiable;
128 * Returns the pin with the given name of this component.
130 * @throws IllegalArgumentException if there is no pin with the given name
132 * @author Daniel Kirschten
134 public Pin getPin(String name)
136 Pin pin = pinsByName.get(name);
138 throw new IllegalArgumentException("No pin with the name " + name);
145 * Sets the given high-level state to the given value. <br>
146 * See {@link HighLevelStateHandler#setHighLevelState(String, Object)} for an explanation of high-level state IDs.
148 * @see #getHighLevelState(String)
149 * @see HighLevelStateHandler#setHighLevelState(String, Object)
151 * @author Daniel Kirschten
153 @SuppressWarnings({ "static-method", "unused" }) // this method is intended to be overridden
154 public void setHighLevelState(String stateID, Object newState)
156 throw new IllegalArgumentException("No high level state with ID " + stateID);
160 * Gets the current value of the given high-level state. <br>
161 * See {@link HighLevelStateHandler#setHighLevelState(String, Object)} for an explanation of high-level state IDs.
163 * @see #setHighLevelState(String, Object)
164 * @see HighLevelStateHandler#getHighLevelState(String)
166 * @author Daniel Kirschten
168 @SuppressWarnings("static-method") // this method is intended to be overridden
169 public Object getHighLevelState(String stateID)
171 throw new IllegalArgumentException("No high level state with ID " + stateID);
174 // "graphical" operations
177 * Sets the position of this component and calls componentMovedListeners and redrawListeners.
179 * @author Daniel Kirschten
181 public void moveTo(double x, double y)
185 callComponentMovedListeners();
190 * Sets the size of this component and calls redrawListeners.
192 * @author Daniel Kirschten
194 protected void setSize(double width, double height)
196 bounds.width = width;
197 bounds.height = height;
198 callComponentResizedListener();
203 * Returns the bounds of this component. Is a bit slower than {@link #getPosX()}, {@link #getPosY()}, {@link #getWidth},
204 * {@link #getHeight}, because new objects are created.
206 * @author Daniel Kirschten
208 public final Rectangle getBounds()
210 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
214 * Returns the x coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
216 * @author Daniel Kirschten
218 public double getPosX()
224 * Returns the y coordinate of the position of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
226 * @author Daniel Kirschten
228 public double getPosY()
234 * Returns the (graphical) width of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
236 * @author Daniel Kirschten
238 public double getWidth()
244 * Returns the height of this component. Is a bit faster than {@link #getBounds()} because no objects are created.
246 * @author Daniel Kirschten
248 public double getHeight()
250 return bounds.height;
254 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
257 * @author Daniel Kirschten
259 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
260 public boolean clicked(double x, double y)
266 * Render this component to the given gc, in absoulute coordinates.
268 * @author Daniel Kirschten
270 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
274 @SuppressWarnings("static-method") // this method is intended to be overridden
275 public JsonElement getParams()
277 return JsonNull.INSTANCE;
283 * Calls redraw listeners.
285 * @author Daniel Kirschten
287 protected void requestRedraw()
289 callRedrawListeners();
293 public void addComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .add (listener);}
294 public void addComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.add (listener);}
295 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
296 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
297 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
299 public void removeComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
300 public void removeComponentResizedListener (Consumer<? super GUIComponent> listener) {componentResizedListeners.remove(listener);}
301 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
302 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
303 public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);}
305 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
306 private void callComponentResizedListener( ) {componentResizedListeners.forEach(l -> l.accept(this));}
307 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
308 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}
309 private void callRedrawListeners ( ) {redrawListeners .forEach(l -> l.run( ));}