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