1 package era.mi.gui.model.components;
3 import java.util.ArrayList;
4 import java.util.Collections;
6 import java.util.function.Consumer;
8 import era.mi.gui.model.ViewModel;
9 import era.mi.gui.model.wires.Pin;
10 import net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13 public abstract class GUIComponent
15 protected final ViewModel model;
16 private final Rectangle bounds;
17 private final List<Pin> pins;
18 protected final List<Pin> pinsUnmodifiable;
20 private final List<Consumer<GUIComponent>> componentChangedListeners;
21 private final List<Consumer<GUIComponent>> componentMovedListeners;
22 private final List<Consumer<Pin>> pinAddedListeners;
23 private final List<Consumer<Pin>> pinRemovedListeners;
25 public GUIComponent(ViewModel model)
28 this.bounds = new Rectangle(0, 0, 0, 0);
29 this.pins = new ArrayList<>();
30 this.pinsUnmodifiable = Collections.unmodifiableList(pins);
32 this.componentChangedListeners = new ArrayList<>();
33 this.componentMovedListeners = new ArrayList<>();
34 this.pinAddedListeners = new ArrayList<>();
35 this.pinRemovedListeners = new ArrayList<>();
37 model.componentCreated(this);
40 public void moveTo(double x, double y)
44 callComponentMovedListeners();
48 * Returns the bounds of this component. Used for calculating which component is clicked.
50 public Rectangle getBounds()
52 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
56 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
59 public boolean clicked(double x, double y)
65 * Returns a list of pins of this component.
67 public List<Pin> getPins()
69 return pinsUnmodifiable;
73 public void addComponentChangedListener (Consumer<GUIComponent> listener) {componentChangedListeners.add (listener);}
74 public void addComponentMovedListener (Consumer<GUIComponent> listener) {componentMovedListeners .add (listener);}
75 public void addPinAddedListener (Consumer<Pin > listener) {pinAddedListeners .add (listener);}
76 public void addPinRemovedListener (Consumer<Pin > listener) {pinRemovedListeners .add (listener);}
78 public void removeComponentChangedListener(Consumer<GUIComponent> listener) {componentChangedListeners.remove(listener);}
79 public void removeComponentMovedListener (Consumer<GUIComponent> listener) {componentMovedListeners .remove(listener);}
80 public void removePinAddedListener (Consumer<Pin > listener) {pinAddedListeners .remove(listener);}
81 public void removePinRemovedListener (Consumer<Pin > listener) {pinRemovedListeners .remove(listener);}
83 private void callComponentChangedListeners( ) {componentChangedListeners.forEach(l -> l.accept(this));}
84 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
85 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
86 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}
90 * Render this component to the given gc.
92 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
94 protected void setSize(double width, double height)
97 bounds.height = height;
98 callComponentChangedListeners();
101 protected void addPin(Pin pin)
104 callPinAddedListeners(pin);
107 protected void removePin(Pin pin)
110 callPinRemovedListeners(pin);