1 package net.mograsim.logic.ui.model.components;
3 import java.util.ArrayList;
4 import java.util.Collections;
6 import java.util.function.Consumer;
8 import net.mograsim.logic.ui.model.ViewModel;
9 import net.mograsim.logic.ui.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<? super GUIComponent>> componentLookChangedListeners;
21 private final List<Consumer<? super GUIComponent>> componentMovedListeners;
22 private final List<Consumer<? super Pin>> pinAddedListeners;
23 private final List<Consumer<? super 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.componentLookChangedListeners = new ArrayList<>();
33 this.componentMovedListeners = new ArrayList<>();
34 this.pinAddedListeners = new ArrayList<>();
35 this.pinRemovedListeners = new ArrayList<>();
37 model.componentCreated(this);
42 pins.forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
43 model.componentDestroyed(this);
46 public void moveTo(double x, double y)
50 callComponentMovedListeners();
54 * Returns the bounds of this component. Used for calculating which component is clicked.
56 public Rectangle getBounds()
58 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
62 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
65 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
66 public boolean clicked(double x, double y)
72 * Returns a list of pins of this component.
74 public List<Pin> getPins()
76 return pinsUnmodifiable;
80 public void addComponentLookChangedListener (Consumer<? super GUIComponent> listener) {componentLookChangedListeners.add (listener);}
81 public void addComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .add (listener);}
82 public void addPinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .add (listener);}
83 public void addPinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .add (listener);}
85 public void removeComponentLookChangedListener(Consumer<? super GUIComponent> listener) {componentLookChangedListeners.remove(listener);}
86 public void removeComponentMovedListener (Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
87 public void removePinAddedListener (Consumer<? super Pin > listener) {pinAddedListeners .remove(listener);}
88 public void removePinRemovedListener (Consumer<? super Pin > listener) {pinRemovedListeners .remove(listener);}
90 protected void callComponentLookChangedListeners( ) {componentLookChangedListeners.forEach(l -> l.accept(this));}
91 private void callComponentMovedListeners ( ) {componentMovedListeners .forEach(l -> l.accept(this));}
92 private void callPinAddedListeners (Pin p) {pinAddedListeners .forEach(l -> l.accept(p ));}
93 private void callPinRemovedListeners (Pin p) {pinRemovedListeners .forEach(l -> l.accept(p ));}
97 * Render this component to the given gc.
99 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
101 protected void setSize(double width, double height)
103 bounds.width = width;
104 bounds.height = height;
105 callComponentLookChangedListeners();
108 protected void addPin(Pin pin)
111 callPinAddedListeners(pin);
114 protected void removePin(Pin pin)
117 callPinRemovedListeners(pin);