Merge remote-tracking branch 'origin/development' into development
[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 import java.util.function.Supplier;
8
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
11 import net.mograsim.logic.ui.model.ViewModelModifiable;
12 import net.mograsim.logic.ui.model.components.params.SubmodelComponentParams;
13 import net.mograsim.logic.ui.model.wires.Pin;
14
15 public abstract class GUIComponent
16 {
17         protected final ViewModelModifiable model;
18         private final Rectangle bounds;
19         private final List<Pin> pins;
20         protected final List<Pin> pinsUnmodifiable;
21
22         private final List<Consumer<? super GUIComponent>> componentMovedListeners;
23         private final List<Consumer<? super Pin>> pinAddedListeners;
24         private final List<Consumer<? super Pin>> pinRemovedListeners;
25         private final List<Runnable> redrawListeners;
26
27         private final Runnable redrawListenerForSubcomponents;
28         // Defines how the GUIComponent is referenced in SubmodelComponentParams
29         protected Supplier<String> identifierDelegate = () -> "class:".concat(getClass().getCanonicalName());
30
31         public GUIComponent(ViewModelModifiable model)
32         {
33                 this.model = model;
34                 this.bounds = new Rectangle(0, 0, 0, 0);
35                 this.pins = new ArrayList<>();
36                 this.pinsUnmodifiable = Collections.unmodifiableList(pins);
37
38                 this.componentMovedListeners = new ArrayList<>();
39                 this.pinAddedListeners = new ArrayList<>();
40                 this.pinRemovedListeners = new ArrayList<>();
41                 this.redrawListeners = new ArrayList<>();
42
43                 redrawListenerForSubcomponents = this::callRedrawListeners;
44
45                 model.componentCreated(this);
46         }
47
48         public void destroy()
49         {
50                 pins.forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
51                 model.componentDestroyed(this);
52         }
53
54         public void moveTo(double x, double y)
55         {
56                 bounds.x = x;
57                 bounds.y = y;
58                 callComponentMovedListeners();
59         }
60
61         /**
62          * Returns the bounds of this component. Used for calculating which component is clicked.
63          */
64         public Rectangle getBounds()
65         {
66                 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
67         }
68
69         /**
70          * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
71          * click.
72          */
73         @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
74         public boolean clicked(double x, double y)
75         {
76                 return false;
77         }
78
79         /**
80          * Returns a list of pins of this component.
81          */
82         public List<Pin> getPins()
83         {
84                 return pinsUnmodifiable;
85         }
86
87         // @formatter:off
88         public void addComponentMovedListener   (Consumer<? super GUIComponent> listener) {componentMovedListeners.add   (listener);}
89         public void addPinAddedListener         (Consumer<? super Pin         > listener) {pinAddedListeners      .add   (listener);}
90         public void addPinRemovedListener       (Consumer<? super Pin         > listener) {pinRemovedListeners    .add   (listener);}
91         public void addRedrawListener           (Runnable                       listener) {redrawListeners        .add   (listener);}
92
93         public void removeComponentMovedListener(Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
94         public void removePinAddedListener      (Consumer<? super Pin         > listener) {pinAddedListeners       .remove(listener);}
95         public void removePinRemovedListener    (Consumer<? super Pin         > listener) {pinRemovedListeners     .remove(listener);}
96         public void removeRedrawListener        (Runnable                       listener) {redrawListeners         .remove(listener);}
97
98         private void callComponentMovedListeners(     ) {componentMovedListeners.forEach(l -> l.accept(this));}
99         private void callPinAddedListeners      (Pin p) {pinAddedListeners      .forEach(l -> l.accept(p   ));}
100         private void callPinRemovedListeners    (Pin p) {pinRemovedListeners    .forEach(l -> l.accept(p   ));}
101         private void callRedrawListeners        (     ) {redrawListeners        .forEach(l -> l.run(       ));}
102         // @formatter:on
103
104         /**
105          * Render this component to the given gc.
106          */
107         public abstract void render(GeneralGC gc, Rectangle visibleRegion);
108
109         protected void requestRedraw()
110         {
111                 callRedrawListeners();
112         }
113
114         protected void setSize(double width, double height)
115         {
116                 bounds.width = width;
117                 bounds.height = height;
118                 callRedrawListeners();
119         }
120
121         protected void addPin(Pin pin)
122         {
123                 pins.add(pin);
124                 callPinAddedListeners(pin);
125                 pin.addRedrawListener(redrawListenerForSubcomponents);
126                 callRedrawListeners();
127         }
128
129         protected void removePin(Pin pin)
130         {
131                 pins.remove(pin);
132                 callPinRemovedListeners(pin);
133                 pin.removeRedrawListener(redrawListenerForSubcomponents);
134                 callRedrawListeners();
135         }
136
137         /**
138          * @return an identifier used to reference this GUIComponent inside of {@link SubmodelComponentParams}
139          */
140         public String getIdentifier()
141         {
142                 return identifierDelegate.get();
143         }
144 }