From db2bc6e935ff1632d78ea8a03606b396944ef21e Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 24 Nov 2014 16:29:06 +0000 Subject: Added partial support for connection manipulation, now using a static storage class for targetting. --- src/jcgp/gui/handlers/NodeHandlers.java | 107 ++++++++++++++++++++++++++++++-- 1 file changed, 103 insertions(+), 4 deletions(-) (limited to 'src/jcgp/gui/handlers/NodeHandlers.java') diff --git a/src/jcgp/gui/handlers/NodeHandlers.java b/src/jcgp/gui/handlers/NodeHandlers.java index 10a334a..b413a62 100644 --- a/src/jcgp/gui/handlers/NodeHandlers.java +++ b/src/jcgp/gui/handlers/NodeHandlers.java @@ -1,9 +1,15 @@ package jcgp.gui.handlers; import javafx.event.EventHandler; +import javafx.scene.input.MouseDragEvent; import javafx.scene.input.MouseEvent; +import javafx.scene.shape.Circle; +import javafx.scene.shape.Line; import jcgp.backend.population.Gene; import jcgp.gui.GUI; +import jcgp.gui.constants.Position; +import jcgp.gui.population.ChromosomePane; +import jcgp.gui.population.GUIConnection; import jcgp.gui.population.GUIGene; import jcgp.gui.population.GUIGene.GUIGeneState; import jcgp.gui.population.GUINode; @@ -21,6 +27,11 @@ import jcgp.gui.population.GUINode; */ public final class NodeHandlers { + /** + * Private constructor to prevent instantiation. + */ + private NodeHandlers() {} + /** * Set the node to {@code GUIGeneState.HOVER} state, and set its immediate connections to {@code GUIGeneState.EXTENDED_HOVER}. */ @@ -29,7 +40,7 @@ public final class NodeHandlers { public void handle(MouseEvent event) { // acquire the source, we can safely cast it to GUINode GUINode source = (GUINode) event.getSource(); - + source.setState(GUIGeneState.HOVER); for (int i = 0; i < GUI.resources.arity(); i++) { ((GUIGene) ((Gene) source.getNode().getConnection(i)).getGUIObject()).setState(GUIGeneState.EXTENDED_HOVER); @@ -46,13 +57,89 @@ public final class NodeHandlers { // acquire the source, we can safely cast it to GUINode GUINode source = (GUINode) event.getSource(); - source.setState(GUIGeneState.NEUTRAL); - for (int i = 0; i < GUI.resources.arity(); i++) { - ((GUIGene) ((Gene) source.getNode().getConnection(i)).getGUIObject()).setState(GUIGeneState.NEUTRAL); + if (Target.getSourceMutable() != source) { + source.setState(GUIGeneState.NEUTRAL); + for (int i = 0; i < GUI.resources.arity(); i++) { + ((GUIGene) ((Gene) source.getNode().getConnection(i)).getGUIObject()).setState(GUIGeneState.NEUTRAL); + } + } + } + }; + + private static EventHandler socketDragDetected = new EventHandler() { + @Override + public void handle(MouseEvent event) { + // it's safe to assume that the source is the socket + ((GUINode) ((Circle) event.getSource()).getParent()).startFullDrag(); + } + }; + + private static EventHandler socketMousePressedHandler = new EventHandler() { + @Override + public void handle(MouseEvent event) { + // it's safe to assume that the source is the socket + Target.start((Circle) event.getSource()); + } + }; + + private static EventHandler socketMouseDraggedHandler = new EventHandler() { + @Override + public void handle(MouseEvent event) { + // this can only happen after a press, so we know Target is up-to-date + if (!Target.isProspecting()) { + GUINode node = (GUINode) Target.getSourceMutable(); + Line line = Target.getConnectionLine(); + line.setEndX(event.getX() + node.getLayoutX()); + line.setEndY(event.getY() + node.getLayoutY()); + } + + } + }; + + private static EventHandler socketMouseReleasedHandler = new EventHandler() { + @Override + public void handle(MouseEvent event) { + + GUINode node = (GUINode) ((Circle) event.getSource()).getParent(); + int connectionId = Integer.valueOf(((Circle) event.getSource()).getId()); + + Position.connect(node.getLines()[connectionId], (GUIGene) ((Gene) node.getNode().getConnection(connectionId)).getGUIObject()); + } + }; + + private static EventHandler dragEnteredHandler = new EventHandler() { + @Override + public void handle(MouseDragEvent event) { + // acquire the source, we can safely cast it to GUINode + GUINode source = (GUINode) event.getSource(); + if (Target.getCurrentConnection() == source) { + source.setState(GUIGeneState.NEUTRAL_TARGET); + // we are now prospecting + Target.setProspecting(true); + Position.connect(Target.getConnectionLine(), source); + } else if (ChromosomePane.isAllowed(Target.getSourceMutable(), (GUIConnection) source)) { + source.setState(GUIGeneState.GOOD_TARGET); + // we are now prospecting + Target.setProspecting(true); + Position.connect(Target.getConnectionLine(), source); + } else { + source.setState(GUIGeneState.BAD_TARGET); } } }; + private static EventHandler dragExitedHandler = new EventHandler() { + @Override + public void handle(MouseDragEvent event) { + // acquire the source, we can safely cast it to GUINode + GUINode source = (GUINode) event.getSource(); + source.setState(GUIGeneState.NEUTRAL); + + // no longer prospecting + Target.setProspecting(false); + } + }; + /** * Adds all handlers to the specified node. * @@ -61,5 +148,17 @@ public final class NodeHandlers { public static void addHandlers(GUINode node) { node.addEventHandler(MouseEvent.MOUSE_ENTERED, mouseEnteredHandler); node.addEventHandler(MouseEvent.MOUSE_EXITED, mouseExitedHandler); + + node.addEventHandler(MouseDragEvent.MOUSE_DRAG_ENTERED, dragEnteredHandler); + node.addEventHandler(MouseDragEvent.MOUSE_DRAG_EXITED, dragExitedHandler); + + Circle[] sockets = node.getSockets(); + for (int s = 0; s < sockets.length; s++) { + + sockets[s].addEventFilter(MouseEvent.DRAG_DETECTED, socketDragDetected); + sockets[s].addEventHandler(MouseEvent.MOUSE_PRESSED, socketMousePressedHandler); + sockets[s].addEventHandler(MouseEvent.MOUSE_DRAGGED, socketMouseDraggedHandler); + sockets[s].addEventHandler(MouseEvent.MOUSE_RELEASED, socketMouseReleasedHandler); + } } } -- cgit v1.2.3