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;
20 redrawListeners = new ArrayList<>();
21 destroyListeners = new ArrayList<>();
22 bounds = new Rectangle(0, 0, 0, 0);
23 callRedrawListeners();
26 final public void render(GeneralGC gc, Rectangle visibleRegion)
28 if (bounds.intersects(visibleRegion))
32 protected abstract void render(GeneralGC gc);
34 protected void setSize(double width, double height)
37 bounds.height = height;
38 callRedrawListeners();
41 protected void moveTo(double x, double y)
45 callRedrawListeners();
48 public Rectangle getBounds()
50 return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
53 public void addRedrawListener(Runnable listener)
55 redrawListeners.add(listener);
58 public void removeRedrawListener(Runnable listener)
60 redrawListeners.remove(listener);
63 protected void callRedrawListeners()
65 redrawListeners.forEach(l -> l.run());
68 public double getPosX()
73 public double getPosY()
80 destroyListeners.forEach(l -> l.run());
83 public void addDestroyListener(Runnable listener)
85 redrawListeners.add(listener);
88 public void removeDestroyListener(Runnable listener)
90 redrawListeners.remove(listener);
93 public boolean contains(double x, double y)
95 return bounds.contains(x, y);
98 public boolean contains(Point p)
100 return contains(p.x, p.y);
104 * Register a mouse click
105 * @param x Coordinate of the click in the world, not the display context
106 * @param y Coordinate of the click in the world, not the display context
107 * @return true if the click was consumed, false otherwise
109 public boolean click(double x, double y, int stateMask, EditorState state)
112 return state.clickedHandle(new HandleClickInfo(this, stateMask));
117 public void reqMove(double x, double y) {}
118 public void reqDelete() {}
119 public Optional<ComponentInfo> reqCopy(Point refPoint) { return Optional.empty(); }
120 public void onSelect() {}
121 public void onDeselect() {}
124 public abstract HandleType getType();
126 public static enum HandleType
128 COMPONENT, STATIC_PIN, INTERFACE_PIN, WIRE_POINT, WIRE, CORNER;
131 public static class HandleClickInfo
133 public final int stateMask;
134 public final Handle clicked;
136 HandleClickInfo(Handle clicked, int stateMask)
138 this.clicked = clicked;
139 this.stateMask = stateMask;