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

import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import jcgp.backend.function.FunctionSet;
import jcgp.gui.constants.Constants;

/**
 * A menu class, exposes all of the allowed functions
 * when called by a node, so that the node function can be changed.
 * 
 * 
 * @author Eduardo Pedroni
 *
 */
public class FunctionSelector extends VBox {
	
	private GUINode target;
		
	public FunctionSelector(FunctionSet functionSet) {
		setFillWidth(true);
		setVisible(false);
		setStyle("-fx-border-color: #A0A0A0; -fx-border-width: 1 1 0 1");
		
		remakeFunctions(functionSet);
		
		addEventFilter(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				dismiss();
			}
		});
	}
	
	public void remakeFunctions(final FunctionSet fs) {
		getChildren().clear();
		
		for (int i = 0; i < fs.getAllowedFunctionCount(); i++) {
			final int index = i;
			Label l = new Label(fs.getAllowedFunction(i).toString());
			l.setMaxWidth(Double.MAX_VALUE);
			l.setStyle("-fx-background-color: #FFFFFF; -fx-border-color: #A0A0A0; -fx-border-width: 0 0 1 0; -fx-padding: 2");
			
			l.addEventFilter(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
				@Override
				public void handle(MouseEvent event) {
					((Label) event.getSource()).setStyle("-fx-background-color: " + Constants.SOFT_HIGHLIGHT_COLOUR + "; -fx-border-color: #B0B0B0; -fx-border-width: 0 0 1 0; -fx-padding: 2");
				}
			});
			l.addEventFilter(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
				@Override
				public void handle(MouseEvent event) {
					((Label) event.getSource()).setStyle("-fx-background-color: #FFFFFF; -fx-border-color: #A0A0A0; -fx-border-width: 0 0 1 0; -fx-padding: 2");
				}
			});
			l.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
				@Override
				public void handle(MouseEvent event) {
					target.setFunction(fs.getAllowedFunction(index));
					dismiss();
				}
			});
			
			getChildren().add(l);
		}
	}
	
	public void relocateAndShow(MouseEvent event, GUINode node) {
		relocate(event.getSceneX() - 5, event.getSceneY() - 5);
		target = node;
		setVisible(true);
	}
	
	private void dismiss() {
		setVisible(false);
	}
	
}