hover, drag cleanup
This commit is contained in:
parent
ba182a102a
commit
3d8342d8a4
@ -36,11 +36,6 @@ public class Window extends JFrame {
|
|||||||
setVisible(true);
|
setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
final Window window = new Window();
|
|
||||||
window.setCircuit(Demos.potiAndVoltmeter());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCircuit(final Circuit circuit) {
|
public void setCircuit(final Circuit circuit) {
|
||||||
circuitPanel.setCircuit(circuit);
|
circuitPanel.setCircuit(circuit);
|
||||||
}
|
}
|
||||||
@ -56,4 +51,9 @@ public class Window extends JFrame {
|
|||||||
setLocation(screenBounds.x, screenBounds.y);
|
setLocation(screenBounds.x, screenBounds.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final Window window = new Window();
|
||||||
|
window.setCircuit(Demos.potiAndVoltmeter());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,49 +32,7 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
|||||||
circuitPanel.addMouseMotionListener(this);
|
circuitPanel.addMouseMotionListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/* HOVER ---------------------------------------------------------------------------------------- */
|
||||||
public void mouseClicked(final MouseEvent event) {
|
|
||||||
findHovers(event);
|
|
||||||
if (mouseClickedAction(event)) {
|
|
||||||
terminateMouseAction();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean mouseClickedAction(final MouseEvent event) {
|
|
||||||
if (wire != null) {
|
|
||||||
switch (event.getButton()) {
|
|
||||||
case BUTTON3:
|
|
||||||
circuitPanel.getCircuit().disconnect(wire);
|
|
||||||
circuitPanel.getCircuit().evaluate();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (part != null) {
|
|
||||||
switch (event.getButton()) {
|
|
||||||
case BUTTON1:
|
|
||||||
part.action();
|
|
||||||
circuitPanel.getCircuit().evaluate();
|
|
||||||
return true;
|
|
||||||
case BUTTON2:
|
|
||||||
part.rotate();
|
|
||||||
return true;
|
|
||||||
case BUTTON3:
|
|
||||||
circuitPanel.getCircuit().removePart(part);
|
|
||||||
circuitPanel.getCircuit().evaluate();
|
|
||||||
part = null;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void terminateMouseAction() {
|
|
||||||
part = null;
|
|
||||||
node = null;
|
|
||||||
wire = null;
|
|
||||||
dragging = null;
|
|
||||||
circuitPanel.repaint();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseMoved(@NonNull final MouseEvent event) {
|
public void mouseMoved(@NonNull final MouseEvent event) {
|
||||||
@ -82,6 +40,71 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
|||||||
circuitPanel.repaint();
|
circuitPanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void findHovers(@NonNull final MouseEvent event) {
|
||||||
|
final Point position = event.getPoint();
|
||||||
|
part = circuitPanel.getCircuit().findPartByPosition(position).orElse(null);
|
||||||
|
node = part != null ? part.findNodeByPosition(position).orElse(null) : null;
|
||||||
|
if (node != null) {
|
||||||
|
part = null;
|
||||||
|
wire = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
wire = circuitPanel.getCircuit().findWireByPosition(position).orElse(null);
|
||||||
|
if (wire != null) {
|
||||||
|
part = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void drawHovers(@NonNull final Graphics2D g) {
|
||||||
|
drawPartHover(g);
|
||||||
|
drawNodeHover(g);
|
||||||
|
drawWireHover(g);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawPartHover(final Graphics2D g) {
|
||||||
|
if (part == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
g.setColor(NODE_HOVER_BORDER_COLOR);
|
||||||
|
g.setStroke(HOVER_STROKE);
|
||||||
|
g.drawRect(part.getPosition().x, part.getPosition().y, RASTER, RASTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawNodeHover(final Graphics2D g) {
|
||||||
|
if (node == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Point absolute = node.getPosition();
|
||||||
|
|
||||||
|
g.setColor(node.getColor());
|
||||||
|
g.fillArc(absolute.x - NODE_RADIUS_HOVER, absolute.y - NODE_RADIUS_HOVER, 2 * NODE_RADIUS_HOVER, 2 * NODE_RADIUS_HOVER, 0, 360);
|
||||||
|
|
||||||
|
g.setColor(NODE_HOVER_BORDER_COLOR);
|
||||||
|
g.setStroke(HOVER_STROKE);
|
||||||
|
g.drawArc(absolute.x - NODE_RADIUS_HOVER, absolute.y - NODE_RADIUS_HOVER, 2 * NODE_RADIUS_HOVER, 2 * NODE_RADIUS_HOVER, 0, 360);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawWireHover(final Graphics2D g) {
|
||||||
|
if (wire == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Point aa = wire.getA().getPosition();
|
||||||
|
final Point bb = wire.getB().getPosition();
|
||||||
|
|
||||||
|
g.setColor(WIRE_HOVER_COLOR_BACK);
|
||||||
|
g.setStroke(WIRE_HOVER_STROKE_BACK);
|
||||||
|
g.drawLine(aa.x, aa.y, bb.x, bb.y);
|
||||||
|
|
||||||
|
g.setColor(wire.getA().getColor());
|
||||||
|
g.setStroke(WIRE_HOVER_STROKE);
|
||||||
|
g.drawLine(aa.x, aa.y, bb.x, bb.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* DRAG ----------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseDragged(@NonNull final MouseEvent event) {
|
public void mouseDragged(@NonNull final MouseEvent event) {
|
||||||
if (dragging == null) {
|
if (dragging == null) {
|
||||||
@ -122,56 +145,75 @@ class CircuitPanelMouseAdapter extends MouseAdapter {
|
|||||||
terminateMouseAction();
|
terminateMouseAction();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void findHovers(@NonNull final MouseEvent event) {
|
|
||||||
final Point position = event.getPoint();
|
|
||||||
part = circuitPanel.getCircuit().findPartByPosition(position).orElse(null);
|
|
||||||
node = part != null ? part.findNodeByPosition(position).orElse(null) : null;
|
|
||||||
wire = circuitPanel.getCircuit().findWireByPosition(position).orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void drawHovers(@NonNull final Graphics2D g) {
|
|
||||||
if (part != null) {
|
|
||||||
g.setColor(NODE_HOVER_BORDER_COLOR);
|
|
||||||
g.setStroke(HOVER_STROKE);
|
|
||||||
g.drawRect(part.getPosition().x, part.getPosition().y, RASTER, RASTER);
|
|
||||||
}
|
|
||||||
if (node != null) {
|
|
||||||
final Point absolute = node.getPosition();
|
|
||||||
|
|
||||||
g.setColor(node.getColor());
|
|
||||||
g.fillArc(absolute.x - NODE_RADIUS_HOVER, absolute.y - NODE_RADIUS_HOVER, 2 * NODE_RADIUS_HOVER, 2 * NODE_RADIUS_HOVER, 0, 360);
|
|
||||||
|
|
||||||
g.setColor(NODE_HOVER_BORDER_COLOR);
|
|
||||||
g.setStroke(HOVER_STROKE);
|
|
||||||
g.drawArc(absolute.x - NODE_RADIUS_HOVER, absolute.y - NODE_RADIUS_HOVER, 2 * NODE_RADIUS_HOVER, 2 * NODE_RADIUS_HOVER, 0, 360);
|
|
||||||
}
|
|
||||||
if (wire != null) {
|
|
||||||
final Point aa = wire.getA().getPosition();
|
|
||||||
final Point bb = wire.getB().getPosition();
|
|
||||||
|
|
||||||
g.setColor(WIRE_HOVER_COLOR_BACK);
|
|
||||||
g.setStroke(WIRE_HOVER_STROKE_BACK);
|
|
||||||
g.drawLine(aa.x, aa.y, bb.x, bb.y);
|
|
||||||
|
|
||||||
g.setColor(wire.getA().getColor());
|
|
||||||
g.setStroke(WIRE_HOVER_STROKE);
|
|
||||||
g.drawLine(aa.x, aa.y, bb.x, bb.y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void drawDrag(@NonNull final Graphics2D g) {
|
public void drawDrag(@NonNull final Graphics2D g) {
|
||||||
if (dragging != null) {
|
drawPartDrag(g);
|
||||||
if (part != null) {
|
drawNodeDrag(g);
|
||||||
g.setColor(PART_HOVER_COLOR);
|
}
|
||||||
g.fillRect(dragging.x - P50, dragging.y - P50, RASTER, RASTER);
|
|
||||||
|
private void drawNodeDrag(final Graphics2D g) {
|
||||||
|
if (dragging == null || node == null) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (node != null) {
|
|
||||||
g.setColor(node.getColor());
|
g.setColor(node.getColor());
|
||||||
g.setStroke(WIRE_STROKE);
|
g.setStroke(WIRE_STROKE);
|
||||||
final Point absolute = node.getPosition();
|
final Point absolute = node.getPosition();
|
||||||
g.drawLine(absolute.x, absolute.y, dragging.x, dragging.y);
|
g.drawLine(absolute.x, absolute.y, dragging.x, dragging.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void drawPartDrag(final Graphics2D g) {
|
||||||
|
if (dragging == null || part == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
g.setColor(PART_HOVER_COLOR);
|
||||||
|
g.fillRect(dragging.x - P50, dragging.y - P50, RASTER, RASTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* CLICK ---------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(final MouseEvent event) {
|
||||||
|
findHovers(event);
|
||||||
|
if (mouseClickedAction(event)) {
|
||||||
|
terminateMouseAction();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean mouseClickedAction(final MouseEvent event) {
|
||||||
|
if (wire != null) {
|
||||||
|
switch (event.getButton()) {
|
||||||
|
case BUTTON3:
|
||||||
|
circuitPanel.getCircuit().disconnect(wire);
|
||||||
|
circuitPanel.getCircuit().evaluate();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (part != null) {
|
||||||
|
switch (event.getButton()) {
|
||||||
|
case BUTTON1:
|
||||||
|
part.action();
|
||||||
|
circuitPanel.getCircuit().evaluate();
|
||||||
|
return true;
|
||||||
|
case BUTTON2:
|
||||||
|
part.rotate();
|
||||||
|
return true;
|
||||||
|
case BUTTON3:
|
||||||
|
circuitPanel.getCircuit().removePart(part);
|
||||||
|
circuitPanel.getCircuit().evaluate();
|
||||||
|
part = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* COMMON --------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
private void terminateMouseAction() {
|
||||||
|
part = null;
|
||||||
|
node = null;
|
||||||
|
wire = null;
|
||||||
|
dragging = null;
|
||||||
|
circuitPanel.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user