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.wires.Pin;
14 public abstract class GUIComponent
16 protected final ViewModelModifiable model;
17 private final Rectangle bounds;
18 private final List<Pin> pins;
19 protected final List<Pin> pinsUnmodifiable;
21 private final List<Consumer<? super GUIComponent>> componentMovedListeners;
22 private final List<Consumer<? super Pin>> pinAddedListeners;
23 private final List<Consumer<? super Pin>> pinRemovedListeners;
24 private final List<Runnable> redrawListeners;
26 private final Runnable redrawListenerForSubcomponents;
27 // Defines how the GUIComponent is referenced in SubmodelComponentParams
28 protected Supplier<String> identifierDelegate = () -> "class:".concat(getClass().getCanonicalName());
30 public GUIComponent(ViewModelModifiable model)
33 this.bounds = new Rectangle(0, 0, 0, 0);
34 this.pins = new ArrayList<>();
35 this.pinsUnmodifiable = Collections.unmodifiableList(pins);
37 this.componentMovedListeners = new ArrayList<>();
38 this.pinAddedListeners = new ArrayList<>();
39 this.pinRemovedListeners = new ArrayList<>();
40 this.redrawListeners = new ArrayList<>();
42 redrawListenerForSubcomponents = this::callRedrawListeners;
44 model.componentCreated(this);
49 pins.forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
50 model.componentDestroyed(this);
53 public void moveTo(double x, double y)
57 callComponentMovedListeners();
61 * Returns the bounds of this component. Used for calculating which component is clicked.
63 public Rectangle getBounds()
65 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
69 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
72 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
73 public boolean clicked(double x, double y)
79 * Returns a list of pins of this component.
81 public List<Pin> getPins()
83 return pinsUnmodifiable;
87 public void addComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners.add (listener);}
88 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
89 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
90 public void addRedrawListener (Runnable listener) {redrawListeners .add (listener);}
92 public void removeComponentMovedListener(Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
93 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
94 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
95 public void removeRedrawListener (Runnable listener) {redrawListeners .remove(listener);}
97 private void callComponentMovedListeners( ) {componentMovedListeners.forEach(l -> l.accept(this));}
98 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
99 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}
100 private void callRedrawListeners ( ) {redrawListeners .forEach(l -> l.run( ));}
104 * Render this component to the given gc.
106 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
108 protected void requestRedraw()
110 callRedrawListeners();
113 protected void setSize(double width, double height)
115 bounds.width = width;
116 bounds.height = height;
117 callRedrawListeners();
120 protected void addPin(Pin pin)
123 callPinAddedListeners(pin);
124 pin.addRedrawListener(redrawListenerForSubcomponents);
125 callRedrawListeners();
128 protected void removePin(Pin pin)
131 callPinRemovedListeners(pin);
132 pin.removeRedrawListener(redrawListenerForSubcomponents);
133 callRedrawListeners();
137 * @return an identifier used to reference this GUIComponent inside of {@link SubmodelComponentParams}
139 public String getIdentifier()
141 return identifierDelegate.get();