Renamed project folders to match the respective project name
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / GUIComponent.java
1 package net.mograsim.logic.ui.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 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;
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<? 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;
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.componentLookChangedListeners = 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         @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
66         public boolean clicked(double x, double y)
67         {
68                 return false;
69         }
70
71         /**
72          * Returns a list of pins of this component.
73          */
74         public List<Pin> getPins()
75         {
76                 return pinsUnmodifiable;
77         }
78
79         // @formatter:off
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);}
84                                                   
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);}
89
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   ));}
94         // @form  atter:on
95
96         /**
97          * Render this component to the given gc.
98          */
99         public abstract void render(GeneralGC gc, Rectangle visibleRegion);
100
101         protected void setSize(double width, double height)
102         {
103                 bounds.width = width;
104                 bounds.height = height;
105                 callComponentLookChangedListeners();
106         }
107
108         protected void addPin(Pin pin)
109         {
110                 pins.add(pin);
111                 callPinAddedListeners(pin);
112         }
113
114         protected void removePin(Pin pin)
115         {
116                 pins.remove(pin);
117                 callPinRemovedListeners(pin);
118         }
119 }