LogicUICanvas now uses the new listener system; changes in clicked()
[Mograsim.git] / LogicUI / src / era / mi / gui / model / components / GUIComponent.java
1 package era.mi.gui.model.components;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.function.Consumer;
7
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;
12
13 public abstract class GUIComponent
14 {
15         protected final ViewModel model;
16         private final Rectangle bounds;
17         private final List<Pin> pins;
18         protected final List<Pin> pinsUnmodifiable;
19
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;
24
25         public GUIComponent(ViewModel model)
26         {
27                 this.model = model;
28                 this.bounds = new Rectangle(0, 0, 0, 0);
29                 this.pins = new ArrayList<>();
30                 this.pinsUnmodifiable = Collections.unmodifiableList(pins);
31
32                 this.componentChangedListeners = new ArrayList<>();
33                 this.componentMovedListeners = new ArrayList<>();
34                 this.pinAddedListeners = new ArrayList<>();
35                 this.pinRemovedListeners = new ArrayList<>();
36
37                 model.componentCreated(this);
38         }
39
40         public void moveTo(double x, double y)
41         {
42                 bounds.x = x;
43                 bounds.y = y;
44                 callComponentMovedListeners();
45         }
46
47         /**
48          * Returns the bounds of this component. Used for calculating which component is clicked.
49          */
50         public Rectangle getBounds()
51         {
52                 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
53         }
54
55         /**
56          * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
57          * click.
58          */
59         public boolean clicked(double x, double y)
60         {
61                 return false;
62         }
63
64         /**
65          * Returns a list of pins of this component.
66          */
67         public List<Pin> getPins()
68         {
69                 return pinsUnmodifiable;
70         }
71
72         // @formatter:off
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);}
77
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);}
82
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   ));}
87         // @formatter:on
88
89         /**
90          * Render this component to the given gc.
91          */
92         public abstract void render(GeneralGC gc, Rectangle visibleRegion);
93
94         protected void setSize(double width, double height)
95         {
96                 bounds.width = width;
97                 bounds.height = height;
98                 callComponentChangedListeners();
99         }
100
101         protected void addPin(Pin pin)
102         {
103                 pins.add(pin);
104                 callPinAddedListeners(pin);
105         }
106
107         protected void removePin(Pin pin)
108         {
109                 pins.remove(pin);
110                 callPinRemovedListeners(pin);
111         }
112 }