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