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