aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/handlers/InputHandlers.java
blob: 1d18ef5d914daccdf8b9fb83818d06525f230b79 (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
package jcgp.gui.handlers;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import jcgp.gui.population.GUIGene.GUIGeneState;
import jcgp.gui.population.GUIInput;

/**
 * Holds the handlers that define the behaviour of {@code GUIInput}.
 * <br><br>
 * The handlers are instantiated here statically and added to {@code GUIInput}
 * instances using {@code InputHandlers.addHandlers(...)}. This guarantees that
 * all inputs behave the same way without instantiating a new set of handlers for
 * each input instance.
 * 
 * @author Eduardo Pedroni
 *
 */
public final class InputHandlers {

	/**
	 * Inputs don't do much; set state to hover when mouse enters.
	 */
	private static EventHandler<MouseEvent> mouseEnteredHandler = new EventHandler<MouseEvent>() {
		@Override
		public void handle(MouseEvent event) {
			((GUIInput) event.getSource()).setState(GUIGeneState.HOVER);
		}
	};
	
	/**
	 * Inputs don't do much; set state to neutral when mouse exits.
	 */
	private static EventHandler<MouseEvent> mouseExitedHandler = new EventHandler<MouseEvent>() {
		@Override
		public void handle(MouseEvent event) {
			((GUIInput) event.getSource()).setState(GUIGeneState.NEUTRAL);
		}
	};
	
	/**
	 * Adds all handlers to the specified input.
	 * 
	 * @param input the {@code GUIInput} to which the handlers will be added.
	 */
	public static void addHandlers(GUIInput input) {
		input.addEventHandler(MouseEvent.MOUSE_ENTERED, mouseEnteredHandler);
		input.addEventHandler(MouseEvent.MOUSE_EXITED, mouseExitedHandler);
	}
	
}