aboutsummaryrefslogtreecommitdiffstats
path: root/src/ru/olamedia/input/MouseJail.java
blob: 9168cc8ccb03975b080fce39202554e77e8f20e6 (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
package ru.olamedia.input;

import java.util.ArrayList;
import java.util.List;

import com.jogamp.newt.event.MouseAdapter;
import com.jogamp.newt.event.MouseEvent;

import ru.olamedia.game.GameFrame;

public class MouseJail extends MouseAdapter {
	public static MouseJail instance = new MouseJail();

	public int x;
	public int y;

	public MouseJail() {
	}

	private static boolean isActive = false;
	private static boolean isEnabled = true; // disabled if GUI is opened

	public static void disable() {
		isEnabled = false;
	}

	public static void enable() {
		isEnabled = true;
	}

	/**
	 * @return the isActive
	 */
	public static boolean isActive() {
		return isActive;
	}

	/**
	 * @param isActive
	 *            the isActive to set
	 */
	public static void setActive(boolean isActive) {
		if (isEnabled) {
			System.out.println("Mouse jail " + (isActive ? "active" : "not active"));
			MouseJail.isActive = isActive;
			GameFrame.confinePointer(isActive);
			GameFrame.setPointerVisible(!isActive);
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// if (isEnabled) {
		if (e.isAltDown()) {
			setActive(false);
		} else {
			setActive(true);
		}
		for (ru.olamedia.input.MouseListener l : listeners) {
			l.onMouseClick(e);
		}
		// }
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		System.out.println("Entered");

	}

	@Override
	public void mouseExited(MouseEvent e) {
		System.out.println("Exited");
		isActive = false;
		if (isActive) {
			// moveToCenter();
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {

	}

	@Override
	public void mouseDragged(MouseEvent e) {
		onMove(e);
	}

	private float sensitivity = 2f;

	private void onMove(MouseEvent e) {
		x = e.getX();
		y = e.getY();
		if (isActive) {
			final float dx = (e.getX() - GameFrame.getGLWidth() / 2) * sensitivity / 10;
			final float dy = (e.getY() - GameFrame.getGLHeight() / 2) * sensitivity / 10;
			// System.out.println("Mouse moved " + " dx:" + dx + " dy:" + dy
			// + " x:" + e.getX() + " y:" + e.getY());
			for (ru.olamedia.input.MouseListener l : listeners) {
				l.onMouseMove(dx, dy);
			}
			GameFrame.getWindow().warpPointer(GameFrame.getGLWidth() / 2, GameFrame.getGLHeight() / 2);
		}
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		onMove(e);
	}

	private static List<ru.olamedia.input.MouseListener> listeners = new ArrayList<ru.olamedia.input.MouseListener>();

	public static void attach(ru.olamedia.input.MouseListener l) {
		listeners.add(l);
	}

}