1 package net.mograsim.logic.ui.model.components;
3 import java.util.ArrayList;
4 import java.util.Collections;
6 import java.util.function.Consumer;
7 import java.util.function.Supplier;
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
11 import net.mograsim.logic.ui.model.ViewModelModifiable;
12 import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams;
13 import net.mograsim.logic.ui.model.wires.Pin;
15 public abstract class GUIComponent
17 protected final ViewModelModifiable model;
18 private final Rectangle bounds;
19 private final List<Pin> pins;
20 protected final List<Pin> pinsUnmodifiable;
22 private final List<Consumer<? super GUIComponent>> componentMovedListeners;
23 private final List<Consumer<? super Pin>> pinAddedListeners;
24 private final List<Consumer<? super Pin>> pinRemovedListeners;
25 private final List<Runnable> redrawListeners;
27 private final Runnable redrawListenerForSubcomponents;
28 // Defines how the GUIComponent is referenced in SubmodelComponentParams
29 protected Supplier<String> identifierDelegate = () -> "class:".concat(getClass().getCanonicalName());
31 public GUIComponent(ViewModelModifiable model)
34 this.bounds = new Rectangle(0, 0, 0, 0);
35 this.pins = new ArrayList<>();
36 this.pinsUnmodifiable = Collections.unmodifiableList(pins);
38 this.componentMovedListeners = new ArrayList<>();
39 this.pinAddedListeners = new ArrayList<>();
40 this.pinRemovedListeners = new ArrayList<>();
41 this.redrawListeners = new ArrayList<>();
43 redrawListenerForSubcomponents = this::callRedrawListeners;
45 model.componentCreated(this);
50 pins.forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
51 model.componentDestroyed(this);
54 public void moveTo(double x, double y)
58 callComponentMovedListeners();
62 * Returns the bounds of this component. Used for calculating which component is clicked.
64 public Rectangle getBounds()
66 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
70 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
73 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
74 public boolean clicked(double x, double y)
80 * Returns a list of pins of this component.
82 public List<Pin> getPins()
84 return pinsUnmodifiable;
88 public void addComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners.add (listener);}
89 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
90 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
91 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
93 public void removeComponentMovedListener(Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
94 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
95 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
96 public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);}
98 private void callComponentMovedListeners( ) {componentMovedListeners.forEach(l -> l.accept(this));}
99 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
100 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}
101 private void callRedrawListeners ( ) {redrawListeners .forEach(l -> l.run( ));}
105 * Render this component to the given gc.
107 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
109 protected void requestRedraw()
111 callRedrawListeners();
114 protected void setSize(double width, double height)
116 bounds.width = width;
117 bounds.height = height;
118 callRedrawListeners();
121 protected void addPin(Pin pin)
124 callPinAddedListeners(pin);
125 pin.addRedrawListener(redrawListenerForSubcomponents);
126 callRedrawListeners();
129 protected void removePin(Pin pin)
132 callPinRemovedListeners(pin);
133 pin.removeRedrawListener(redrawListenerForSubcomponents);
134 callRedrawListeners();
138 * @return an identifier used to reference this GUIComponent inside of {@link SubmodelComponentParams}
140 public String getIdentifier()
142 return identifierDelegate.get();