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

import javafx.event.EventHandler;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import jcgp.backend.population.Connection;
import jcgp.backend.population.Input;
import jcgp.backend.population.Output;
import jcgp.gui.GUI;

public class GUIInput extends GUIGene {

	private Input input;

	public GUIInput(ChromosomePane parentRef, final Input input) {
		super();
		
		this.parent = parentRef;
		this.input = input;
		
		relocate(NODE_RADIUS,
				(input.getIndex() * (2 * NODE_RADIUS + SPACING)) + NODE_RADIUS);
		
		text.setText("I: " + input.getIndex());
		

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

		getChildren().addAll(mainCircle, text, outputSocket);
		
		/*
		 * 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
				((GUIGene) event.getGestureSource()).setConnectionLine((GUIGene) event.getSource());
				
				if (event.getGestureSource() instanceof GUINode) {
					Connection source = ((GUINode) event.getGestureSource()).getChangingConnection();
					if (input == source) {
						setState(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);
					}
				}
				setState(GUIGeneState.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
				parent.setTarget(false);
				if (event.isPrimaryButtonDown()) {
					if (getState() == GUIGeneState.NO_CHANGE_TARGET) {
						setState(GUIGeneState.INDIRECT_HOVER);
					} else {
						setState(GUIGeneState.NEUTRAL);
						((GUIGene) event.getGestureSource()).setConnections(GUIGeneState.INDIRECT_HOVER);
					}
				}
			}

		});

		addEventFilter(MouseDragEvent.MOUSE_DRAG_RELEASED, new EventHandler<MouseDragEvent>() {
			@Override
			public void handle(MouseDragEvent event) {
				GUIGene source = ((GUIGene) event.getGestureSource());
				// set states to reflect the new situation
				if (source.isLocked()) {
					source.setState(GUIGeneState.HOVER);
					source.setConnections(GUIGeneState.HOVER);
				} else {
					source.setState(GUIGeneState.NEUTRAL);
					source.setConnections(GUIGeneState.NEUTRAL);
				}
				
				// the user released the drag gesture on this node, react appropriately
				if (source.isLocked()) {
					// remove locks from the old connection, add the to the new
					// note that the old connection may still have locks after this
					parent.getGuiGene(source.getChangingConnection()).removeLocks(source.getLocks());
					source.setChangingConnection(input);
					addLocks(source.getLocks());
				} else {
					source.setChangingConnection(input);
				}

				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_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) {
					setState(GUIGeneState.NEUTRAL);
					setConnections(GUIGeneState.NEUTRAL);
				}
			}
		});
	}
	
	@Override
	public void setState(GUIGeneState newState) {
		super.setState(newState);
		
		switch (newState) {
		case ACTIVE_HOVER:
			if (locked > 0) {
				setState(GUIGeneState.LOCKED_HOVER);
			} else {
				mainCircle.setFill(Paint.valueOf(GUI.SOFT_HIGHLIGHT_COLOUR));
			}
			break;
		case FORBIDDEN_TARGET:
			mainCircle.setFill(Paint.valueOf(GUI.BAD_SELECTION_COLOUR));
			break;
		case LOCKED_HOVER:
			mainCircle.setFill(Paint.valueOf(GUI.SOFT_HIGHLIGHT_COLOUR));
			break;
		case HOVER:
			mainCircle.setFill(Paint.valueOf(GUI.MEDIUM_HIGHLIGHT_COLOUR));
			break;
		case INDIRECT_HOVER:
			mainCircle.setFill(Paint.valueOf(GUI.SOFT_HIGHLIGHT_COLOUR));
			break;
		case NEUTRAL:
			if (locked > 0) {
				setState(GUIGeneState.HOVER);
			} else {
				mainCircle.setFill(Paint.valueOf(GUI.NEUTRAL_COLOUR));
			}
			break;
		case NO_CHANGE_TARGET:
			parent.setTarget(true);
			mainCircle.setFill(Paint.valueOf(GUI.NEUTRAL_SELECTION_COLOUR));
			break;
		case SOURCE:
			mainCircle.setFill(Paint.valueOf(GUI.MEDIUM_HIGHLIGHT_COLOUR));
			break;
		case TARGET:
			parent.setTarget(true);
			mainCircle.setFill(Paint.valueOf(GUI.GOOD_SELECTION_COLOUR));
			break;
		default:
			break;
		
		}
		
	}

	@Override
	public Input getGene() {
		return input;
	}

	/**
	 * Set all connections to a given state.
	 * 
	 * @param newState the state to set connections to.
	 */
	@Override
	public void setConnections(GUIGeneState newState) {
		// nothing
	}

	@Override
	public void resetState() {
		setState(GUIGeneState.NEUTRAL);
	}

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

	@Override
	public void setChangingConnection(Connection newConnection) {
		// do nothing
	}

	@Override
	public Connection getChangingConnection() {
		return null;
	}

	@Override
	public void addLocks(int value) {
		locked += value;
		setState(locked > 0 ? GUIGeneState.HOVER : GUIGeneState.ACTIVE_HOVER);
	}

	@Override
	public void updateLines() {
		// nothing
	}

	@Override
	public void removeLocks(int value) {
		locked -= value;
		setState(locked > 0 ? GUIGeneState.HOVER : GUIGeneState.NEUTRAL);
	}

	@Override
	public void setConnectionLine(GUIGene gene) {
		// nothing
	}	
}