aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'src/jcgp/gui/handlers')
-rw-r--r--src/jcgp/gui/handlers/InputHandlers.java56
-rw-r--r--src/jcgp/gui/handlers/NodeHandlers.java164
-rw-r--r--src/jcgp/gui/handlers/OutputHandlers.java84
-rw-r--r--src/jcgp/gui/handlers/Target.java70
4 files changed, 374 insertions, 0 deletions
diff --git a/src/jcgp/gui/handlers/InputHandlers.java b/src/jcgp/gui/handlers/InputHandlers.java
new file mode 100644
index 0000000..cc677eb
--- /dev/null
+++ b/src/jcgp/gui/handlers/InputHandlers.java
@@ -0,0 +1,56 @@
+package jcgp.gui.handlers;
+
+import javafx.event.EventHandler;
+import javafx.scene.input.MouseEvent;
+import jcgp.gui.population.GUIGene.GUIGeneState;
+import jcgp.gui.population.GUIInput;
+
+/**
+ * Holds the handlers that define the behaviour of {@code GUIInput}.
+ * <br><br>
+ * The handlers are instantiated here statically and added to {@code GUIInput}
+ * instances using {@code InputHandlers.addHandlers(...)}. This guarantees that
+ * all inputs behave the same way without instantiating a new set of handlers for
+ * each input instance.
+ *
+ * @author Eduardo Pedroni
+ *
+ */
+public final class InputHandlers {
+
+ /**
+ * Private constructor to prevent instantiation.
+ */
+ private InputHandlers() {}
+
+ /**
+ * Inputs don't do much; set state to hover when mouse enters.
+ */
+ private static EventHandler<MouseEvent> mouseEnteredHandler = new EventHandler<MouseEvent>() {
+ @Override
+ public void handle(MouseEvent event) {
+ ((GUIInput) event.getSource()).setState(GUIGeneState.HOVER);
+ }
+ };
+
+ /**
+ * Inputs don't do much; set state to neutral when mouse exits.
+ */
+ private static EventHandler<MouseEvent> mouseExitedHandler = new EventHandler<MouseEvent>() {
+ @Override
+ public void handle(MouseEvent event) {
+ ((GUIInput) event.getSource()).setState(GUIGeneState.NEUTRAL);
+ }
+ };
+
+ /**
+ * Adds all handlers to the specified input.
+ *
+ * @param input the {@code GUIInput} to which the handlers will be added.
+ */
+ public static void addHandlers(GUIInput input) {
+ input.addEventHandler(MouseEvent.MOUSE_ENTERED, mouseEnteredHandler);
+ input.addEventHandler(MouseEvent.MOUSE_EXITED, mouseExitedHandler);
+ }
+
+}
diff --git a/src/jcgp/gui/handlers/NodeHandlers.java b/src/jcgp/gui/handlers/NodeHandlers.java
new file mode 100644
index 0000000..b413a62
--- /dev/null
+++ b/src/jcgp/gui/handlers/NodeHandlers.java
@@ -0,0 +1,164 @@
+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;
+
+/**
+ * Holds the handlers that define the behaviour of {@code GUINode}.
+ * <br><br>
+ * The handlers are instantiated here statically and added to {@code GUINode}
+ * instances using {@code NodeHandlers.addHandlers(...)}. This guarantees that
+ * all nodes behave the same way without instantiating a new set of handlers for
+ * each node instance.
+ *
+ * @author Eduardo Pedroni
+ *
+ */
+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}.
+ */
+ private static EventHandler<MouseEvent> mouseEnteredHandler = new EventHandler<MouseEvent>() {
+ @Override
+ 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);
+ }
+ }
+ };
+
+ /**
+ * Set the node and its immediate connections to {@code GUIGeneState.NEUTRAL} state.
+ */
+ private static EventHandler<MouseEvent> mouseExitedHandler = new EventHandler<MouseEvent>() {
+ @Override
+ public void handle(MouseEvent event) {
+ // acquire the source, we can safely cast it to GUINode
+ GUINode source = (GUINode) event.getSource();
+
+ 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<MouseEvent> socketDragDetected = new EventHandler<MouseEvent>() {
+ @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<MouseEvent> socketMousePressedHandler = new EventHandler<MouseEvent>() {
+ @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<MouseEvent> socketMouseDraggedHandler = new EventHandler<MouseEvent>() {
+ @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<MouseEvent> socketMouseReleasedHandler = new EventHandler<MouseEvent>() {
+ @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<MouseDragEvent> dragEnteredHandler = new EventHandler<MouseDragEvent>() {
+ @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<MouseDragEvent> dragExitedHandler = new EventHandler<MouseDragEvent>() {
+ @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.
+ *
+ * @param node the {@code GUINode} to which the handlers will be added.
+ */
+ 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);
+ }
+ }
+}
diff --git a/src/jcgp/gui/handlers/OutputHandlers.java b/src/jcgp/gui/handlers/OutputHandlers.java
new file mode 100644
index 0000000..b89d746
--- /dev/null
+++ b/src/jcgp/gui/handlers/OutputHandlers.java
@@ -0,0 +1,84 @@
+package jcgp.gui.handlers;
+
+import javafx.event.EventHandler;
+import javafx.scene.input.MouseEvent;
+import jcgp.backend.population.Gene;
+import jcgp.gui.population.GUIConnection;
+import jcgp.gui.population.GUIGene.GUIGeneState;
+import jcgp.gui.population.GUIOutput;
+
+/**
+ * Holds the handlers that define the behaviour of {@code GUIOutput}.
+ * <br><br>
+ * The handlers are instantiated here statically and added to {@code GUIOutput}
+ * instances using {@code OutputHandlers.addHandlers(...)}. This guarantees that
+ * all outputs behave the same way without instantiating a new set of handlers for
+ * each output instance.
+ *
+ * @author Eduardo Pedroni
+ *
+ */
+public final class OutputHandlers {
+
+ /**
+ * Private constructor to prevent instantiation.
+ */
+ private OutputHandlers() {}
+
+ /**
+ * Set the output to {@code GUIGeneState.HOVER} state, and recursively set its active genes
+ * to {@code GUIGeneState.ACTIVE_HOVER}.
+ */
+ private static EventHandler<MouseEvent> mouseEnteredHandler = new EventHandler<MouseEvent>() {
+ @Override
+ public void handle(MouseEvent event) {
+ // acquire the source, we can safely cast it to GUIOutput
+ GUIOutput source = (GUIOutput) event.getSource();
+
+ source.setState(GUIGeneState.HOVER);
+ ((GUIConnection) ((Gene) source.getOutput().getSource()).getGUIObject()).setStateRecursively(GUIGeneState.ACTIVE_HOVER);
+ }
+ };
+
+ /**
+ * Set the output and all of its active genes to {@code GUIGeneState.NEUTRAL} state.
+ */
+ private static EventHandler<MouseEvent> mouseExitedHandler = new EventHandler<MouseEvent>() {
+ @Override
+ public void handle(MouseEvent event) {
+ // acquire the source, we can safely cast it to GUIOutput
+ GUIOutput source = (GUIOutput) event.getSource();
+
+ source.setState(GUIGeneState.NEUTRAL);
+ ((GUIConnection) ((Gene) source.getOutput().getSource()).getGUIObject()).setStateRecursively(GUIGeneState.NEUTRAL);
+ }
+ };
+
+ /**
+ * If the output is locked, unlock it and all of its associated genes recursively.
+ * If it is unlocked, lock it and its active genes.
+ */
+ private static EventHandler<MouseEvent> mouseClickHandler = new EventHandler<MouseEvent>() {
+ @Override
+ public void handle(MouseEvent event) {
+ // acquire the source, we can safely cast it to GUIOutput
+ GUIOutput source = (GUIOutput) event.getSource();
+
+ boolean lock = !source.isLocked();
+ source.setLock(lock);
+ ((GUIConnection) ((Gene) source.getOutput().getSource()).getGUIObject()).setLockRecursively(lock);
+ }
+ };
+
+ /**
+ * Adds all handlers to the specified output.
+ *
+ * @param output the {@code GUIOutput} to which the handlers will be added.
+ */
+ public static void addHandlers(GUIOutput output) {
+ output.addEventHandler(MouseEvent.MOUSE_ENTERED, mouseEnteredHandler);
+ output.addEventHandler(MouseEvent.MOUSE_EXITED, mouseExitedHandler);
+ output.addEventHandler(MouseEvent.MOUSE_CLICKED, mouseClickHandler);
+ }
+
+}
diff --git a/src/jcgp/gui/handlers/Target.java b/src/jcgp/gui/handlers/Target.java
new file mode 100644
index 0000000..b050663
--- /dev/null
+++ b/src/jcgp/gui/handlers/Target.java
@@ -0,0 +1,70 @@
+package jcgp.gui.handlers;
+
+import javafx.scene.shape.Circle;
+import javafx.scene.shape.Line;
+import jcgp.gui.population.GUIConnection;
+import jcgp.gui.population.GUIMutable;
+
+/**
+ * @author Eduardo Pedroni
+ *
+ */
+public final class Target {
+
+ /**
+ * Private constructor to prevent instantiation.
+ */
+ private Target() {}
+
+ private static GUIConnection targetConnection;
+ private static GUIMutable sourceMutable;
+ private static int connectionIndex;
+ private static Line connectionLine;
+ private static Circle sourceSocket;
+ private static boolean prospecting = false;
+
+ public static void start(Circle newSocket) {
+ // store new socket
+ sourceSocket = newSocket;
+ // derive the rest of the information from it
+ connectionIndex = Integer.valueOf(newSocket.getId());
+ sourceMutable = (GUIMutable) newSocket.getParent();
+ connectionLine = sourceMutable.getLines()[connectionIndex];
+ }
+
+ public static GUIMutable getSourceMutable() {
+ return sourceMutable;
+ }
+
+ public static int getConnectionIndex() {
+ return connectionIndex;
+ }
+
+ public static Line getConnectionLine() {
+ return connectionLine;
+ }
+
+ public static Circle getSourceSocket() {
+ return sourceSocket;
+ }
+
+ public static GUIConnection getTarget() {
+ return targetConnection;
+ }
+
+ public static GUIConnection getCurrentConnection() {
+ return sourceMutable.getConnections()[connectionIndex];
+ }
+
+ public static void setProspecting(boolean value) {
+ prospecting = value;
+ }
+
+ public static boolean isProspecting() {
+ return prospecting;
+ }
+
+ public static void setTarget(GUIConnection newTarget) {
+ targetConnection = newTarget;
+ }
+}