e266afbf6eee065506e4ef3290e4f6cc5eb60b77
[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 destroy()
41         {
42                 pins.forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
43                 model.componentDestroyed(this);
44         }
45
46         public void moveTo(double x, double y)
47         {
48                 bounds.x = x;
49                 bounds.y = y;
50                 callComponentMovedListeners();
51         }
52
53         /**
54          * Returns the bounds of this component. Used for calculating which component is clicked.
55          */
56         public Rectangle getBounds()
57         {
58                 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
59         }
60
61         /**
62          * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
63          * click.
64          */
65         public boolean clicked(double x, double y)
66         {
67                 return false;
68         }
69
70         /**
71          * Returns a list of pins of this component.
72          */
73         public List<Pin> getPins()
74         {
75                 return pinsUnmodifiable;
76         }
77
78         // @formatter:off
79         public void addComponentChangedListener   (Consumer<GUIComponent> listener) {componentChangedListeners.add   (listener);}
80         public void addComponentMovedListener     (Consumer<GUIComponent> listener) {componentMovedListeners  .add   (listener);}
81         public void addPinAddedListener           (Consumer<Pin         > listener) {pinAddedListeners        .add   (listener);}
82         public void addPinRemovedListener         (Consumer<Pin         > listener) {pinRemovedListeners      .add   (listener);}
83
84         public void removeComponentChangedListener(Consumer<GUIComponent> listener) {componentChangedListeners.remove(listener);}
85         public void removeComponentMovedListener  (Consumer<GUIComponent> listener) {componentMovedListeners  .remove(listener);}
86         public void removePinAddedListener        (Consumer<Pin         > listener) {pinAddedListeners        .remove(listener);}
87         public void removePinRemovedListener      (Consumer<Pin         > listener) {pinRemovedListeners      .remove(listener);}
88
89         private void callComponentChangedListeners(     ) {componentChangedListeners.forEach(l -> l.accept(this));}
90         private void callComponentMovedListeners  (     ) {componentMovedListeners  .forEach(l -> l.accept(this));}
91         private void callPinAddedListeners        (Pin p) {pinAddedListeners        .forEach(l -> l.accept(p   ));}
92         private void callPinRemovedListeners      (Pin p) {pinRemovedListeners      .forEach(l -> l.accept(p   ));}
93         // @formatter:on
94
95         /**
96          * Render this component to the given gc.
97          */
98         public abstract void render(GeneralGC gc, Rectangle visibleRegion);
99
100         protected void setSize(double width, double height)
101         {
102                 bounds.width = width;
103                 bounds.height = height;
104                 callComponentChangedListeners();
105         }
106
107         protected void addPin(Pin pin)
108         {
109                 pins.add(pin);
110                 callPinAddedListeners(pin);
111         }
112
113         protected void removePin(Pin pin)
114         {
115                 pins.remove(pin);
116                 callPinRemovedListeners(pin);
117         }
118 }