aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/population/GUIOutput.java
blob: 4a12385f258e9b1ce7a131849048e210cc5dcc72 (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
package jcgp.gui.population;

import javafx.event.EventHandler;
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 jcgp.backend.population.Connection;
import jcgp.backend.population.Input;
import jcgp.backend.population.Node;
import jcgp.backend.population.Output;
import jcgp.gui.GUI;

public class GUIOutput extends GUIGene {

	private Line sourceLine;

	private Output output;

	public GUIOutput(ChromosomePane parentRef, final Output output, Line line) {
		super();
		
		this.parent = parentRef;
		this.output = output;
		this.sourceLine = line;

		relocate(((GUI.resources.getInt("columns") + 1) * (2 * NODE_RADIUS + SPACING)) + NODE_RADIUS,
				(output.getIndex() * (2 * NODE_RADIUS + SPACING)) + NODE_RADIUS);

		// set the line ends correctly
		updateLines();

		text.setText("O: " + output.getIndex());

		Circle socket = new Circle(-NODE_RADIUS, 0, SOCKET_RADIUS, Paint.valueOf("white"));
		socket.setId(String.valueOf(0));
		socket.setStroke(Paint.valueOf("black"));

		final Label connectionLabel = new Label("S");
		connectionLabel.setStyle("-fx-background-color:rgb(255, 255, 255); -fx-border-color:rgba(0, 0, 0, 0.5); ");
		connectionLabel.relocate(socket.getCenterX() + 5, socket.getCenterY() - 10);
		connectionLabel.setVisible(false);

		/*
		 * Mouse event handlers on sockets
		 * 
		 */
		socket.addEventFilter(MouseEvent.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();
			}
		});

		socket.addEventFilter(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				// user is hovering over connection socket
				connectionLabel.setVisible(true);
			}
		});

		socket.addEventFilter(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				// user exits the connection socket
				connectionLabel.setVisible(false);
			}
		});

		socket.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				// mouse was pressed on the socket
				setState(GUIGeneState.SOURCE);
			}
		});

		socket.addEventFilter(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				if (!parent.isTarget()) {
					sourceLine.setEndX(event.getX() + ((Circle) event.getSource()).getParent().getLayoutX());
					sourceLine.setEndY(event.getY() + ((Circle) event.getSource()).getParent().getLayoutY());
				}
				
			}
		});	

		socket.addEventFilter(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				if (event.isStillSincePress()) {
					// mouse was released before dragging out of the socket
					updateLines();
					setState(GUIGeneState.HOVER);
				} else if (getState() == GUIGeneState.SOURCE) {
					// no connection has been made, fallback
					resetState();
					updateLines();
				}
				
			}
		});


		/*
		 * 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
				setState(GUIGeneState.FORBIDDEN_TARGET);
			}
		});

		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()) {
						setState(GUIGeneState.SOURCE);
					} else {
						setState(isLocked() ? GUIGeneState.HOVER : GUIGeneState.NEUTRAL);
					}
				}
			}
		});

		addEventFilter(MouseDragEvent.MOUSE_DRAG_RELEASED, new EventHandler<MouseDragEvent>() {
			@Override
			public void handle(MouseDragEvent event) {
				// making a connection to an output is illegal
				// set states to reflect the new situation
				GUIGene source = ((GUIGene) event.getGestureSource());
				
				if (source.isLocked()) {
					source.setState(GUIGeneState.HOVER);
					source.setConnections(GUIGeneState.HOVER);
				} else {
					source.setState(GUIGeneState.NEUTRAL);
					source.setConnections(GUIGeneState.NEUTRAL);
				}
				
				source.updateLines();
				setState(GUIGeneState.HOVER);
			}
		});


		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 (getState() == GUIGeneState.NEUTRAL) {
					setState(GUIGeneState.HOVER);
				}
			}
		});

		addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				setLocked(!isLocked());
			}
		});
		
		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 (getState() == GUIGeneState.HOVER && !isLocked()) {
					setState(GUIGeneState.NEUTRAL);
					setConnections(GUIGeneState.NEUTRAL);
				}
			}
		});


		getChildren().addAll(mainCircle, text, socket, connectionLabel);
		
	}

	@Override
	public void setState(GUIGeneState newState) {
		super.setState(newState);
		
		switch (newState) {
		case ACTIVE_HOVER:
			break;
		case FORBIDDEN_TARGET:
			mainCircle.setFill(Paint.valueOf(GUI.BAD_SELECTION_COLOUR));
			break;
		case HOVER:
			mainCircle.setFill(Paint.valueOf(GUI.MEDIUM_HIGHLIGHT_COLOUR));
			sourceLine.setVisible(true);
			if (!isLocked()) {
				setConnections(GUIGeneState.ACTIVE_HOVER);
			}
			break;
		case INDIRECT_HOVER:
			mainCircle.setFill(Paint.valueOf(GUI.SOFT_HIGHLIGHT_COLOUR));
			break;
		case NEUTRAL:
			mainCircle.setFill(Paint.valueOf(GUI.NEUTRAL_COLOUR));
			sourceLine.setVisible(false);
			break;
		case NO_CHANGE_TARGET:
			mainCircle.setFill(Paint.valueOf(GUI.NEUTRAL_SELECTION_COLOUR));
			break;
		case SOURCE:
			mainCircle.setFill(Paint.valueOf(GUI.HARD_HIGHLIGHT_COLOUR));
			setConnections(GUIGeneState.NEUTRAL);
			setConnections(GUIGeneState.INDIRECT_HOVER);
			break;
		case TARGET:
			mainCircle.setFill(Paint.valueOf(GUI.GOOD_SELECTION_COLOUR));
			break;
		default:
			break;
		}
	}
	
	@Override
	public void updateLines() {
		if (output.getSource() instanceof Node) {
			int row = ((Node) output.getSource()).getRow(), 
					column = ((Node) output.getSource()).getColumn();
			sourceLine.setEndX(((column + 1) * (2 * NODE_RADIUS + SPACING)) + 2 * NODE_RADIUS);
			sourceLine.setEndY((row * (2 * NODE_RADIUS + SPACING)) + NODE_RADIUS);
		} else if (output.getSource() instanceof Input) {
			int inputIndex = ((Input) output.getSource()).getIndex();
			sourceLine.setEndX(2 * NODE_RADIUS);
			sourceLine.setEndY(inputIndex * (2 * NODE_RADIUS + SPACING) + NODE_RADIUS);
		}
	}

	@Override
	public Output getGene() {
		return output;
	}

	@Override
	public void setConnections(GUIGeneState newState) {
		parent.getGuiGene(output.getSource()).setState(newState);
	}

	@Override
	public void resetState() {
		if (locked > 0) {
			setState(GUIGeneState.HOVER);
			setConnections(GUIGeneState.HOVER);
		} else {
			setState(GUIGeneState.NEUTRAL);
			setConnections(GUIGeneState.NEUTRAL);
		}
	}

	@Override
	void setLocked(boolean value) {
		locked += value ? 1 : -1;
		setConnections(value ? GUIGeneState.HOVER : GUIGeneState.ACTIVE_HOVER);

		parent.getGuiGene(output.getSource()).setLocked(value);

	}

	@Override
	public void setChangingConnection(Connection newConnection) {
		output.setConnection(0, newConnection);	
	}

	@Override
	public Connection getChangingConnection() {
		return output.getSource();
	}

	@Override
	public void addLocks(int value) {
		locked += value;
	}

	@Override
	public void removeLocks(int value) {
		locked -= value;
	}

	@Override
	public void setConnectionLine(GUIGene gene) {
		sourceLine.setEndX(gene.getLayoutX() + NODE_RADIUS);
		sourceLine.setEndY(gene.getLayoutY());	
	}
	
	public void unlock() {
		if (isLocked()) {
			setLocked(false);
			setState(GUIGeneState.NEUTRAL);
			setConnections(GUIGeneState.NEUTRAL);
		}
	}

	public void lock() {
		if (!isLocked()) {
			setState(GUIGeneState.HOVER);
			setLocked(true);
		}
	}

}