Implemented SubmodelComponent.clicked(); removed obsolete TODOs
[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                 Pin pin = pinsByName.get(name);
93                 if (pin == null)
94                         throw new IllegalArgumentException("No pin with the name " + name);
95                 return pin;
96         }
97
98         // @formatter:off
99         public void addComponentMovedListener   (Consumer<? super GUIComponent> listener) {componentMovedListeners.add   (listener);}
100         public void addPinAddedListener         (Consumer<? super Pin         > listener) {pinAddedListeners      .add   (listener);}
101         public void addPinRemovedListener       (Consumer<? super Pin         > listener) {pinRemovedListeners    .add   (listener);}
102         public void addRedrawListener           (Runnable                       listener) {redrawListeners        .add   (listener);}
103
104         public void removeComponentMovedListener(Consumer<? super GUIComponent> listener) {componentMovedListeners .remove(listener);}
105         public void removePinAddedListener      (Consumer<? super Pin         > listener) {pinAddedListeners       .remove(listener);}
106         public void removePinRemovedListener    (Consumer<? super Pin         > listener) {pinRemovedListeners     .remove(listener);}
107         public void removeRedrawListener        (Runnable                       listener) {redrawListeners         .remove(listener);}
108
109         private void callComponentMovedListeners(     ) {componentMovedListeners.forEach(l -> l.accept(this));}
110         private void callPinAddedListeners      (Pin p) {pinAddedListeners      .forEach(l -> l.accept(p   ));}
111         private void callPinRemovedListeners    (Pin p) {pinRemovedListeners    .forEach(l -> l.accept(p   ));}
112         private void callRedrawListeners        (     ) {redrawListeners        .forEach(l -> l.run(       ));}
113         // @formatter:on
114
115         /**
116          * Render this component to the given gc.
117          */
118         public abstract void render(GeneralGC gc, Rectangle visibleRegion);
119
120         protected void requestRedraw()
121         {
122                 callRedrawListeners();
123         }
124
125         protected void setSize(double width, double height)
126         {
127                 bounds.width = width;
128                 bounds.height = height;
129                 callRedrawListeners();
130         }
131
132         protected void addPin(Pin pin)
133         {
134                 if (pinsByName.containsKey(pin.name))
135                         throw new IllegalArgumentException("Duplicate pin name: " + pin.name);
136                 pinsByName.put(pin.name, pin);
137                 callPinAddedListeners(pin);
138                 pin.addRedrawListener(redrawListenerForSubcomponents);
139                 callRedrawListeners();
140         }
141
142         protected void removePin(String name)
143         {
144                 Pin pin = pinsByName.remove(name);
145                 callPinRemovedListeners(pin);
146                 pin.removeRedrawListener(redrawListenerForSubcomponents);
147                 callRedrawListeners();
148         }
149
150         /**
151          * @return an identifier used to reference this GUIComponent inside of {@link SubmodelComponentParams}
152          */
153         public String getIdentifier()
154         {
155                 return identifierDelegate.get();
156         }
157
158         @SuppressWarnings("static-method")
159         public Map<String, Object> getInstantiationParameters()
160         {
161                 return new TreeMap<>();
162         }
163 }