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 return pinsByName.get(name);
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);}
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);}
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( ));}
113 * Render this component to the given gc.
115 public abstract void render(GeneralGC gc, Rectangle visibleRegion);
117 protected void requestRedraw()
119 callRedrawListeners();
122 protected void setSize(double width, double height)
124 bounds.width = width;
125 bounds.height = height;
126 callRedrawListeners();
129 protected void addPin(Pin pin)
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();
139 protected void removePin(String name)
141 Pin pin = pinsByName.remove(name);
142 callPinRemovedListeners(pin);
143 pin.removeRedrawListener(redrawListenerForSubcomponents);
144 callRedrawListeners();
148 * @return an identifier used to reference this GUIComponent inside of {@link SubmodelComponentParams}
150 public String getIdentifier()
152 return identifierDelegate.get();
155 @SuppressWarnings("static-method")
156 public Map<String, Object> getInstantiationParameters()
158 return new TreeMap<>();