aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/population/GUINode.java
blob: 4350e52173bb4d94447161dae90bbdddfe55232e (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package jcgp.gui.population;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.VPos;
import javafx.scene.control.Label;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import jcgp.CGP.Resources;
import jcgp.GUI;
import jcgp.function.Function;
import jcgp.population.Connection;
import jcgp.population.Input;
import jcgp.population.Node;
import jcgp.population.Output;

public class GUINode extends GUIGene {
	
	private Circle[] sockets;
	private Circle output;

	private Line[] lines;

	private Label connectionNumber;

	private Node node;
	
	private int connectionIndex;
	
	public GUINode(ChromosomePane parentRef, final Node node, Resources resources, Line[] connectionLines) {

		// store references
		this.parent = parentRef;
		this.node = node;
		this.lines = connectionLines;
		
		// move the GUIGene to the right position
		relocate(((node.getColumn() + 1) * (2 * NODE_RADIUS + SPACING)) + NODE_RADIUS,
				(node.getRow() * (2 * NODE_RADIUS + SPACING)) + NODE_RADIUS);

		// set the line ends correctly
		updateLines();
		
		connectionNumber = new Label();
		connectionNumber.setStyle("-fx-background-color:rgb(255, 255, 255); -fx-border-color:rgba(0, 0, 0, 0.5); ");
		connectionNumber.setVisible(false);

		output = new Circle(NODE_RADIUS, 0, SOCKET_RADIUS, Paint.valueOf("white"));
		output.setStroke(Paint.valueOf("black"));

		mainCircle = new Circle(NODE_RADIUS, Paint.valueOf("white"));
		mainCircle.setStroke(Paint.valueOf("black"));

		text = new Text(node.getFunction().getName());
		this.node.functionProperty().addListener(new ChangeListener<Function>() {
			@Override
			public void changed(ObservableValue<? extends Function> observable,
					Function oldValue, Function newValue) {
				text.setText(newValue.getName());				
			}
		});
		text.setFont(Font.font("Arial", 12));
		text.setTextOrigin(VPos.CENTER);
		text.setTextAlignment(TextAlignment.CENTER);
		text.setWrappingWidth(NODE_RADIUS * 2);
		text.setX(-NODE_RADIUS);
		text.setVisible(true);

		sockets = new Circle[(int) resources.get("arity")];
		double angle, xPos, yPos;
		for (int l = 0; l < sockets.length; l++) {
			angle = ((((double) (l + 1)) / ((double) ((int) resources.get("arity") + 1))) * THETA) - (THETA / 2);
			xPos = -Math.cos(angle) * NODE_RADIUS;
			yPos = Math.sin(angle) * NODE_RADIUS;

			sockets[l] = new Circle(xPos, yPos, GUIGene.SOCKET_RADIUS, Paint.valueOf("white"));
			sockets[l].setId(String.valueOf(l));
			sockets[l].setStroke(Paint.valueOf("black"));
			
			final Circle s = sockets[l];
			final int index = l;
			
			/*
			 * Mouse event handlers on sockets
			 * 
			 */
			s.addEventFilter(MouseDragEvent.DRAG_DETECTED, new EventHandler<MouseEvent>() {
				@Override
				public void handle(MouseEvent event) {
					// the mouse has been dragged out of the socket, this means a full drag is in progress
					startFullDrag();
				}
			});
			
			s.addEventFilter(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
				@Override
				public void handle(MouseEvent event) {
					// user is hovering over connection socket
					connectionNumber.setText("C: " + s.getId());
					connectionNumber.relocate(s.getCenterX() + 5, s.getCenterY() - 10);
					connectionNumber.setVisible(true);
				}
			});
			
			s.addEventFilter(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
				@Override
				public void handle(MouseEvent event) {
					// user exits the connection socket
					connectionNumber.setVisible(false);
				}
			});
			
			s.addEventFilter(MouseDragEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
				@Override
				public void handle(MouseEvent event) {
					// mouse was pressed on the socket
					stateProperty.set(GUIGeneState.SOURCE);
					connectionIndex = index;
 				}
			});
			
			s.addEventFilter(MouseDragEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
				@Override
				public void handle(MouseEvent event) {
					lines[connectionIndex].setEndX(event.getX() + ((Circle) event.getSource()).getParent().getLayoutX());
					lines[connectionIndex].setEndY(event.getY() + ((Circle) event.getSource()).getParent().getLayoutY());
				}
			});	
			
			s.addEventFilter(MouseDragEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() {
				@Override
				public void handle(MouseEvent event) {
					if (event.isDragDetect()) {
						// mouse was released before dragging out of the socket
						updateLine(index);
						stateProperty.set(GUIGeneState.HOVER);
					}
 				}
			});
		}
		
		/*
		 * Mouse event handlers on whole gene
		 */
		addEventFilter(MouseDragEvent.MOUSE_DRAG_ENTERED, new EventHandler<MouseDragEvent>() {
			@Override
			public void handle(MouseDragEvent event) {
				// the drag has entered this node, react appropriately
				// this happens even if we are the source of the drag
				if (isAllowed((GUIGene) event.getGestureSource(), (GUIGene) event.getSource())) {
					if (event.getGestureSource() instanceof GUINode) {
						Node source = ((GUINode) event.getGestureSource()).getGene();
						for (int i = 0; i < lines.length; i++) {
							if (node == source.getConnection(i)) {
								stateProperty.set(GUIGeneState.NO_CHANGE_TARGET);
								return;
							}
						}
					} else if (event.getGestureSource() instanceof GUIOutput) {
						Output source = ((GUIOutput) event.getGestureSource()).getGene();
						if (((GUIGene) event.getSource()).getGene() == source.getSource()) {
							((GUIGene) event.getSource()).setState(GUIGeneState.NO_CHANGE_TARGET);
						}
					}
					stateProperty.set(GUIGeneState.TARGET);
				} else {
					stateProperty.set(GUIGeneState.FORBIDDEN_TARGET);
				}
			}
		});

		addEventFilter(MouseDragEvent.MOUSE_DRAG_OVER, new EventHandler<MouseDragEvent>() {
			@Override
			public void handle(MouseDragEvent event) {
				// the user is dragging over this node, react appropriately
				// this happens even if we are the source of the drag
				
			}
		});
		
		addEventFilter(MouseDragEvent.MOUSE_DRAG_EXITED, new EventHandler<MouseDragEvent>() {
			@Override
			public void handle(MouseDragEvent event) {
				// the drag has exited this node, react appropriately
				// this happens even if we are the source of the drag
				if (event.isPrimaryButtonDown()) {
					if (event.getGestureSource() == event.getSource()) {
						stateProperty.set(GUIGeneState.SOURCE);
					} else {
						if (stateProperty.get() == GUIGeneState.NO_CHANGE_TARGET) {
							stateProperty.set(GUIGeneState.INDIRECT_HOVER);
						} else {
							stateProperty.set(GUIGeneState.NEUTRAL);
						}
					}
				}
			}
		});
		
		addEventFilter(MouseDragEvent.MOUSE_DRAG_RELEASED, new EventHandler<MouseDragEvent>() {
			@Override
			public void handle(MouseDragEvent event) {
				// set states to reflect the new situation
				((GUIGene) event.getGestureSource()).setState(GUIGeneState.NEUTRAL);
				((GUIGene) event.getGestureSource()).setConnections(GUIGeneState.NEUTRAL);
				stateProperty.set(GUIGeneState.HOVER);
				// the user released the drag gesture on this node, react appropriately
				if (isAllowed((GUIGene) event.getGestureSource(), (GUIGene) event.getSource())) {
					if (event.getGestureSource() instanceof GUINode) {
						((GUINode) event.getGestureSource()).setChangingConnection(node);
					} else if (event.getGestureSource() instanceof GUIOutput) {
						((GUIOutput) event.getGestureSource()).getGene().setConnection(0, node);
					}
				}
				if (event.getGestureSource() instanceof GUINode) {
					((GUINode) event.getGestureSource()).updateLines();
				} else if (event.getGestureSource() instanceof GUIOutput) {
					((GUIOutput) event.getGestureSource()).updateLine();
				}
				
			}
		});
		
		
		addEventFilter(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				// cursor has entered this node without dragging, or it is dragging and this is the source
				if (stateProperty.get() == GUIGeneState.NEUTRAL) {
					stateProperty.set(GUIGeneState.HOVER);
				}
			}
		});
		
		addEventFilter(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				// cursor has left this node without dragging, or it is dragging and this is the source
				if (stateProperty.get() == GUIGeneState.HOVER) {
					stateProperty.set(GUIGeneState.NEUTRAL);
					setConnections(GUIGeneState.NEUTRAL);
				}
			}
		});

		getChildren().addAll(mainCircle, text);
		getChildren().addAll(sockets);
		getChildren().addAll(output, connectionNumber);
		
		stateProperty.addListener(new ChangeListener<GUIGeneState>() {
			@Override
			public void changed(ObservableValue<? extends GUIGeneState> observable,	GUIGeneState oldValue, GUIGeneState newValue) {
				
				switch (newValue) {
				case ACTIVE_HOVER:
					mainCircle.setFill(Paint.valueOf(GUI.SOFT_HIGHLIGHT_COLOUR));
					showLines(true);
					setConnections(GUIGeneState.ACTIVE_HOVER);
					break;
				case FORBIDDEN_TARGET:
					mainCircle.setFill(Paint.valueOf(GUI.BAD_SELECTION_COLOUR));
					break;
				case HOVER:
					mainCircle.setFill(Paint.valueOf(GUI.HARD_HIGHLIGHT_COLOUR));
					showLines(true);
					setConnections(GUIGeneState.INDIRECT_HOVER);
					break;
				case INDIRECT_HOVER:
					mainCircle.setFill(Paint.valueOf(GUI.SOFT_HIGHLIGHT_COLOUR));
					break;
				case NEUTRAL:
					mainCircle.setFill(Paint.valueOf(GUI.NEUTRAL_COLOUR));
					showLines(false);
					if (oldValue == GUIGeneState.ACTIVE_HOVER) {
						setConnections(GUIGeneState.NEUTRAL);
					}
					break;
				case NO_CHANGE_TARGET:
					mainCircle.setFill(Paint.valueOf(GUI.NEUTRAL_SELECTION_COLOUR));
					break;
				case SOURCE:
					mainCircle.setFill(Paint.valueOf(GUI.HARD_HIGHLIGHT_COLOUR));
					break;
				case TARGET:
					mainCircle.setFill(Paint.valueOf(GUI.GOOD_SELECTION_COLOUR));
					break;
				default:
					break;
				
				}
			}
		});
		
		for (int c = 0; c < lines.length; c++) {
			final int i = c;
			node.connections().get(c).addListener(new ChangeListener<Connection>() {
				@Override
				public void changed(ObservableValue<? extends Connection> observable,
						Connection oldValue, Connection newValue) {
					updateLine(i);
				}
			});
		}
		
	}
	
	
	private 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).getGene(), s = ((GUINode) source).getGene();
				if (s.getColumn() - t.getColumn() > 0 && s.getColumn() - t.getColumn() <= (int) GUI.resources.get("levelsBack")) {
					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.");
			}
		}
		// if the source was neither node nor output, something bad is happening
		throw new ClassCastException("Source was neither GUINode nor GUIOutput.");
	}


	/**
	 * Place the end of the specified line on the output of the associated connection. 
	 * 
	 * @param index the line to be updated.
	 */
	public void updateLine(int index) {
		if (node.getConnection(index) instanceof Node) {
			int row = ((Node) node.getConnection(index)).getRow(), 
					column = ((Node) node.getConnection(index)).getColumn();
			lines[index].setEndX(((column + 1) * (2 * NODE_RADIUS + SPACING)) + 2 * NODE_RADIUS);
			lines[index].setEndY((row * (2 * NODE_RADIUS + SPACING)) + NODE_RADIUS);
		} else if (node.getConnection(index) instanceof Input) {
			int inputIndex = ((Input) node.getConnection(index)).getIndex();
			lines[index].setEndX(2 * NODE_RADIUS);
			lines[index].setEndY(inputIndex * (2 * NODE_RADIUS + SPACING) + NODE_RADIUS);
		}
	}

	/**
	 * Updates the end of all lines to match the associated connections. 
	 */
	public void updateLines() {
		for (int c = 0; c < lines.length; c++) {
			updateLine(c);
		}
	}
	
	/**
	 * Toggle visibility of all connection lines.
	 * 
	 * @param value whether to show the lines or not.
	 */
	private void showLines(boolean value) {
		for (int i = 0; i < lines.length; i++) {
			lines[i].setVisible(value);
		}
	}

	@Override
	public Node getGene() {
		return node;
	}


	@Override
	public void setConnections(GUIGeneState newState) {
		for (int i = 0; i < lines.length; i++) {
			if (node.getConnection(i) instanceof Node) {
				parent.getGuiNode(((Node) node.getConnection(i)).getRow(), ((Node) node.getConnection(i)).getColumn()).setState(newState);
			} else if (node.getConnection(i) instanceof Input) {
				parent.getGuiInput(((Input) node.getConnection(i)).getIndex()).setState(newState);
			}
		}
	}
	
	/**
	 * This method sets the connection currently being changed to the 
	 * specified value.
	 */
	public void setChangingConnection(Connection newConnection) {
		node.setConnection(connectionIndex, newConnection);
	}


	@Override
	public void resetState() {
		stateProperty.set(GUIGeneState.NEUTRAL);
		setConnections(GUIGeneState.NEUTRAL);
	}
	
}