1 package net.mograsim.logic.model.editor.handles;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Optional;
7 import net.haspamelodica.swt.helper.gcs.GeneralGC;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
10 import net.mograsim.logic.model.editor.Editor.ComponentInfo;
11 import net.mograsim.logic.model.editor.states.EditorState;
13 public abstract class Handle
15 private final Rectangle bounds;
16 private final Collection<Runnable> redrawListeners, destroyListeners;
17 private final int priority;
19 public Handle(int priority)
21 this.priority = priority;
22 redrawListeners = new ArrayList<>();
23 destroyListeners = new ArrayList<>();
24 bounds = new Rectangle(0, 0, 0, 0);
25 callRedrawListeners();
28 final public void render(GeneralGC gc, Rectangle visibleRegion)
30 if (bounds.intersects(visibleRegion))
34 protected abstract void render(GeneralGC gc);
36 protected void setSize(double width, double height)
39 bounds.height = height;
40 callRedrawListeners();
43 protected void moveTo(double x, double y)
47 callRedrawListeners();
50 public Rectangle getBounds()
52 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
55 public void addRedrawListener(Runnable listener)
57 redrawListeners.add(listener);
60 public void removeRedrawListener(Runnable listener)
62 redrawListeners.remove(listener);
65 protected void callRedrawListeners()
67 redrawListeners.forEach(l -> l.run());
70 public double getPosX()
75 public double getPosY()
82 destroyListeners.forEach(l -> l.run());
85 public void addDestroyListener(Runnable listener)
87 redrawListeners.add(listener);
90 public void removeDestroyListener(Runnable listener)
92 redrawListeners.remove(listener);
95 public boolean contains(double x, double y)
97 return bounds.contains(x, y);
100 public boolean contains(Point p)
102 return contains(p.x, p.y);
106 * Register a mouse click
108 * @param x Coordinate of the click in the world, not the display context
109 * @param y Coordinate of the click in the world, not the display context
110 * @return true if the click was consumed, false otherwise
112 public boolean click(double x, double y, int stateMask, EditorState state)
115 return state.clickedHandle(new HandleClickInfo(this, stateMask));
119 // These methods are intended to be overridden
121 @SuppressWarnings("unused") public void reqMove(double x, double y) {/**/}
122 public void reqDelete() {/**/}
123 @SuppressWarnings({ "unused", "static-method" }) public Optional<ComponentInfo> reqCopy(Point refPoint) {return Optional.empty();}
124 public void onSelect() {/**/}
125 public void onDeselect() {/**/}
128 public final int getPriority()
133 public abstract HandleType getType();
135 public static enum HandleType
137 COMPONENT, STATIC_PIN, INTERFACE_PIN, WIRE_POINT, WIRE, CORNER;
140 public static class HandleClickInfo
142 public final int stateMask;
143 public final Handle clicked;
145 HandleClickInfo(Handle clicked, int stateMask)
147 this.clicked = clicked;
148 this.stateMask = stateMask;