aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/population/GUINode.java
blob: 6b6bafc89b9263eb106c36a10c699dd58ba1f964 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package jcgp.gui.population;

import javafx.scene.control.Label;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import jcgp.backend.population.Input;
import jcgp.backend.population.Node;
import jcgp.gui.GUI;
import jcgp.gui.constants.Constants;

public abstract class GUINode extends GUIConnection implements GUIMutable {

	private Node node;
	private Line[] connectionLines;
	
	public GUINode(Node node, Line[] connectionLines) {
		super();
		// store references
		this.node = node;
		this.connectionLines = connectionLines;
		
		Label connectionNumber = new Label();
		connectionNumber.setStyle("-fx-background-color:rgb(255, 255, 255); -fx-border-color:rgba(0, 0, 0, 0.5);");
		connectionNumber.setVisible(false);

		Circle output = new Circle(Constants.NODE_RADIUS, 0, Constants.SOCKET_RADIUS, Paint.valueOf("white"));
		output.setStroke(Paint.valueOf("black"));
		
		Circle[] sockets = new Circle[GUI.resources.arity()];
		double angle, xPos, yPos;
		for (int l = 0; l < sockets.length; l++) {
			angle = (((l + 1) / ((double) (GUI.resources.arity() + 1))) * Constants.THETA) - (Constants.THETA / 2);
			xPos = -Math.cos(angle) * Constants.NODE_RADIUS;
			yPos = Math.sin(angle) * Constants.NODE_RADIUS;

			sockets[l] = new Circle(xPos, yPos, Constants.SOCKET_RADIUS, Paint.valueOf("white"));
			sockets[l].setId(String.valueOf(l));
			sockets[l].setStroke(Paint.valueOf("black"));
		}
		
		getChildren().addAll(sockets);
		getChildren().addAll(output, connectionNumber);
		
		for (int l = 0; l < connectionLines.length; l++) {
			if (node.getConnection(l) instanceof Node) {
				int row = ((Node) node.getConnection(l)).getRow(), 
						column = ((Node) node.getConnection(l)).getColumn();
				connectionLines[l].setEndX(((column + 1) * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + 2 * Constants.NODE_RADIUS);
				connectionLines[l].setEndY((row * (2 * Constants.NODE_RADIUS + Constants.SPACING)) + Constants.NODE_RADIUS);
			} else if (node.getConnection(l) instanceof Input) {
				int inputIndex = ((Input) node.getConnection(l)).getIndex();
				connectionLines[l].setEndX(2 * Constants.NODE_RADIUS);
				connectionLines[l].setEndY(inputIndex * (2 * Constants.NODE_RADIUS + Constants.SPACING) + Constants.NODE_RADIUS);
			}
		}
		
	}
	
	public Node getNode() {
		return node;
	}

	void setNode(Node node2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEnter() {
		setState(GUIGeneState.HOVER);
		for (int i = 0; i < GUI.resources.arity(); i++) {
			getGUIConnection(node.getConnection(i)).setState(GUIGeneState.EXTENDED_HOVER);
		}
	}

	@Override
	public void mouseExit() {
		setState(GUIGeneState.NEUTRAL);
		for (int i = 0; i < GUI.resources.arity(); i++) {
			getGUIConnection(node.getConnection(i)).setState(GUIGeneState.NEUTRAL);
		}
	}

	@Override
	public void activeHover(boolean value) {
		setState(value ? GUIGeneState.EXTENDED_HOVER : GUIGeneState.NEUTRAL);
		for (int i = 0; i < GUI.resources.arity(); i++) {
			getGUIConnection(node.getConnection(i)).activeHover(value);
		}
	}
	
}