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

import java.awt.AWTException;
import java.awt.Point;
import java.awt.Robot;
import java.lang.reflect.InvocationTargetException;
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 MouseJail() {
	}

	private static boolean isActive = false;

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

	/**
	 * @param isActive
	 *            the isActive to set
	 */
	public static void setActive(boolean isActive) {
		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 (e.isAltDown()) {
			setActive(false);
		} else {
			setActive(true);
		}
		for (ru.olamedia.input.MouseListener l : listeners) {
			l.onMouseClick();
		}
	}

	@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) {
		if (isActive) {
			int cx = GameFrame.getWidth() / 2;
			int cy = GameFrame.getHeight() / 2;
			float dx = e.getX() - cx;
			float dy = e.getY() - cy;
			dx *= sensitivity / 10;
			dy *= 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(cx, cy);
		}
	}

	@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);
	}

}