diff options
Diffstat (limited to 'src/jcgp/gui/population/ChromosomePane.java')
-rw-r--r-- | src/jcgp/gui/population/ChromosomePane.java | 127 |
1 files changed, 42 insertions, 85 deletions
diff --git a/src/jcgp/gui/population/ChromosomePane.java b/src/jcgp/gui/population/ChromosomePane.java index 3546011..f09d452 100644 --- a/src/jcgp/gui/population/ChromosomePane.java +++ b/src/jcgp/gui/population/ChromosomePane.java @@ -1,10 +1,7 @@ package jcgp.gui.population; -import java.util.ArrayList; - import javafx.scene.control.ScrollPane; import javafx.scene.layout.Pane; -import javafx.scene.shape.Line; import jcgp.backend.population.Chromosome; import jcgp.backend.population.Connection; import jcgp.backend.population.Input; @@ -12,6 +9,7 @@ import jcgp.backend.population.Node; import jcgp.backend.resources.Resources; import jcgp.gui.GUI; import jcgp.gui.constants.Constants; +import jcgp.gui.handlers.GUIHandlers; /** * This extension of {@code ScrollPane} contains a series of @@ -30,27 +28,18 @@ public class ChromosomePane extends ScrollPane { private Pane content; - private ArrayList<Line> connectionLines; - private ArrayList<GUIOutput> relock = new ArrayList<GUIOutput>(); - private int rows, columns; - private Object[] testInputs; - private boolean target = false; - private PopulationPane parent; public ChromosomePane(Chromosome chromosome, GUI gui, PopulationPane parent) { super(); final Resources resources = gui.getExperiment().getResources(); - this.parent = parent; rows = resources.rows(); columns = resources.columns(); - connectionLines = new ArrayList<Line>(); - content = new Pane(); content.setId("content pane for genes"); @@ -59,47 +48,35 @@ public class ChromosomePane extends ScrollPane { guiInputs = new GUIInput[resources.inputs()]; for (int i = 0; i < guiInputs.length; i++) { // make the GUI elements - guiInputs[i] = new GUIInput(this, chromosome.getInput(i)); - content.getChildren().addAll(guiInputs[i]); + guiInputs[i] = new GUIInput(chromosome.getInput(i)); + guiInputs[i].relocate(Constants.NODE_RADIUS, + (chromosome.getInput(i).getIndex() * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS); + GUIHandlers.addHandlers(guiInputs[i]); } + content.getChildren().addAll(guiInputs); // nodes guiNodes = new GUINode[rows][columns]; - double angle, xPos, yPos; for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { - // make the connection lines - Line lines[] = new Line[resources.arity()]; - for (int l = 0; l < lines.length; l++) { - angle = ((((double) (l + 1)) / ((double) (lines.length + 1))) * Constants.THETA) - (Constants.THETA / 2); - xPos = (-Math.cos(angle) * Constants.NODE_RADIUS) + (((c + 1) * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS); - yPos = (Math.sin(angle) * Constants.NODE_RADIUS) + ((r * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS); - - lines[l] = new Line(xPos, yPos, xPos, yPos); - lines[l].setMouseTransparent(true); - lines[l].setVisible(false); - connectionLines.add(lines[l]); - } // make the GUI elements - guiNodes[r][c] = new GUINode(this, chromosome.getNode(r, c), lines, gui); + guiNodes[r][c] = new GUINode(chromosome.getNode(r, c)); + guiNodes[r][c].relocate(((chromosome.getNode(r, c).getColumn() + 1) * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS, + (chromosome.getNode(r, c).getRow() * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS); + GUIHandlers.addHandlers(guiNodes[r][c]); } content.getChildren().addAll(guiNodes[r]); } // outputs guiOutputs = new GUIOutput[resources.outputs()]; for (int i = 0; i < guiOutputs.length; i++) { - xPos = ((resources.columns() + 1) * (2 * Constants.NODE_RADIUS + Constants.SPACING)); - yPos = (chromosome.getOutput(i).getIndex() * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS; - // make the line - Line line = new Line(xPos, yPos, xPos, yPos); - line.setMouseTransparent(true); - line.setVisible(false); - connectionLines.add(line); // make the GUI elements - guiOutputs[i] = new GUIOutput(this, chromosome.getOutput(i), line, gui); - content.getChildren().addAll(guiOutputs[i]); + guiOutputs[i] = new GUIOutput(chromosome.getOutput(i)); + guiOutputs[i].relocate(((resources.columns() + 1) * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS, + (chromosome.getOutput(i).getIndex() * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS); + GUIHandlers.addHandlers(guiOutputs[i]); } + content.getChildren().addAll(guiOutputs); - content.getChildren().addAll(connectionLines); setPrefWidth(620); setContent(content); } @@ -127,61 +104,41 @@ public class ChromosomePane extends ScrollPane { for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { guiNodes[r][c].setNode(chr.getNode(r, c)); - guiNodes[r][c].updateLines(); - guiNodes[r][c].updateText(); } } for (int i = 0; i < guiOutputs.length; i++) { guiOutputs[i].setOutput(chr.getOutput(i)); - guiOutputs[i].updateLines(); - } - if (isEvaluating()) { - setInputs(testInputs); - } - } - - public void unlockOutputs() { - relock.clear(); - for (int i = 0; i < guiOutputs.length; i++) { - if (guiOutputs[i].isLocked()) { - guiOutputs[i].unlock(); - relock.add(guiOutputs[i]); - } - } - } - - public void relockOutputs() { - for (int i = 0; i < relock.size(); i++) { - relock.get(i).lock(); } } - public void setInputs(Object[] values) { - testInputs = values; - for (int i = 0; i < guiInputs.length; i++) { - guiInputs[i].setValue(values[i]); - } - updateValues(); - } - - public void updateValues() { - for (int i = 0; i < guiInputs.length; i++) { - guiInputs[i].updateText(); - } - for (int c = 0; c < columns; c++) { - for (int r = 0; r < rows; r++) { - guiNodes[r][c].updateText(); + public static boolean isAllowed(GUIGene source, GUIGene target) { + if (source instanceof GUINode) { + // if the source is a node, all inputs and some nodes are valid + if (target instanceof GUIInput) { + return true; + } else if (target instanceof GUINode) { + // target and source are nodes, let's look at levels back + Node t = ((GUINode) target).getNode(), s = ((GUINode) source).getNode(); + if (s.getColumn() - t.getColumn() > 0 && s.getColumn() - t.getColumn() <= 1 /* TODO this should be levels back */) { + return true; + } + return false; + } else if (target instanceof GUIOutput) { + return false; + } else { + throw new ClassCastException("Target was neither GUINode nor GUIInput nor GUIOutput."); + } + } else if (source instanceof GUIOutput) { + // if the source is an output, any node or input is valid + if (target instanceof GUINode || target instanceof GUIInput) { + return true; + } else if (target instanceof GUIOutput) { + return false; + } else { + throw new ClassCastException("Target was neither GUINode nor GUIInput nor GUIOutput."); } } - for (int o = 0; o < guiOutputs.length; o++) { - guiOutputs[o].updateText(); - } - } - - /** - * @return the evaluating attribute. - */ - public boolean isEvaluating() { - return parent.isEvaluating(); + // if the source was neither node nor output, something bad is happening + throw new ClassCastException("Source was neither GUINode nor GUIOutput."); } } |