1 package net.mograsim.logic.ui.model.components;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
9 import java.util.TreeMap;
10 import java.util.function.Consumer;
11 import java.util.function.Supplier;
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;
18 public abstract class GUIComponent
20 protected final ViewModelModifiable model;
21 private final Rectangle bounds;
22 private final Map<String, Pin> pinsByName;
23 protected final Collection<Pin> pinsUnmodifiable;
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;
30 private final Runnable redrawListenerForSubcomponents;
31 // Defines how the GUIComponent is referenced in SubmodelComponentParams
32 protected Supplier<String> identifierDelegate = () -> "class:".concat(getClass().getCanonicalName());
34 public GUIComponent(ViewModelModifiable model)
37 this.bounds = new Rectangle(0, 0, 0, 0);
38 this.pinsByName = new HashMap<>();
39 this.pinsUnmodifiable = Collections.unmodifiableCollection(pinsByName.values());
41 this.componentMovedListeners = new ArrayList<>();
42 this.pinAddedListeners = new ArrayList<>();
43 this.pinRemovedListeners = new ArrayList<>();
44 this.redrawListeners = new ArrayList<>();
46 redrawListenerForSubcomponents = this::callRedrawListeners;
48 model.componentCreated(this);
53 pinsByName.values().forEach(p -> pinRemovedListeners.forEach(l -> l.accept(p)));
54 model.componentDestroyed(this);
57 public void moveTo(double x, double y)
61 callComponentMovedListeners();
65 * Returns the bounds of this component. Used for calculating which component is clicked.
67 public Rectangle getBounds()
69 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
73 * Called when this component is clicked. Absolute coordinates of the click are given. Returns true if this component consumed this
76 @SuppressWarnings({ "static-method", "unused" }) // this method is inteded to be overridden
77 public boolean clicked(double x, double y)
83 * Returns a collection of pins of this component.
85 public Collection<Pin> getPins()
87 return pinsUnmodifiable;
90 public Pin getPin(String name)
92 Pin pin = pinsByName.get(name);
94 throw new IllegalArgumentException("No pin with the name " + name);
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);}
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);}
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( ));}
116 * Render this component to the given gc.
118 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
120 protected void requestRedraw()
122 callRedrawListeners();
125 protected void setSize(double width, double height)
127 bounds.width = width;
128 bounds.height = height;
129 callRedrawListeners();
132 protected void addPin(Pin pin)
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();
142 protected void removePin(String name)
144 Pin pin = pinsByName.remove(name);
145 callPinRemovedListeners(pin);
146 pin.removeRedrawListener(redrawListenerForSubcomponents);
147 callRedrawListeners();
151 * @return an identifier used to reference this GUIComponent inside of {@link SubmodelComponentParams}
153 public String getIdentifier()
155 return identifierDelegate.get();
158 @SuppressWarnings("static-method")
159 public Map<String, Object> getInstantiationParameters()
161 return new TreeMap<>();