Started restructuring LogicUI
[Mograsim.git] / LogicUI / src / era / mi / gui / model / components / GUIComponent.java
1 package era.mi.gui.model.components;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import era.mi.gui.model.ViewModel;
8 import era.mi.gui.model.wires.Pin;
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
11
12 public abstract class GUIComponent
13 {
14         protected final ViewModel model;
15         private final Rectangle bounds;
16         private final List<Pin> pins;
17         protected final List<Pin> pinsUnmodifiable;
18
19         public GUIComponent(ViewModel model)
20         {
21                 this.model = model;
22                 this.bounds = new Rectangle(0, 0, 0, 0);
23                 this.pins = new ArrayList<>();
24                 this.pinsUnmodifiable = Collections.unmodifiableList(pins);
25         }
26
27         public void moveTo(double x, double y)
28         {
29                 bounds.x = x;
30                 bounds.y = y;
31         }
32
33         /**
34          * Returns the bounds of this component. Used for calculating which component is clicked.
35          */
36         public Rectangle getBounds()
37         {
38                 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
39         }
40
41         // TODO
42         /**
43          * Called when this component is clicked. Relative coordinates of the click are given. Returns true if this component has to be redrawn.
44          */
45         public boolean clicked(double x, double y)
46         {
47                 return false;
48         }
49
50         /**
51          * Returns a list of pins of this component.
52          */
53         public List<Pin> getPins()
54         {
55                 return pinsUnmodifiable;
56         }
57
58         /**
59          * Render this component to the given gc.
60          */
61         public abstract void render(GeneralGC gc, Rectangle visibleRegion);
62
63         protected void setSize(double width, double height)
64         {
65                 bounds.width = width;
66                 bounds.height = height;
67         }
68
69         protected void addPin(Pin pin)
70         {// TODO notify pins they are "created"
71                 pins.add(pin);
72         }
73
74         protected void removePin(Pin pin)
75         {// TODO notify pins they are "destroyed"
76                 pins.remove(pin);
77         }
78 }