-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInput.java
140 lines (115 loc) · 2.63 KB
/
Input.java
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
// Copyright (c) 2012 Joe Davisson. All Rights Reserved.
import java.awt.event.*;
public class Input implements KeyListener, MouseListener, MouseMotionListener
{
// access through methods
private boolean keyList[];
// access directly
public int mousex = 0;
public int mousey = 0;
public boolean button1;
public boolean button2;
public boolean button3;
public boolean dragged;
public boolean keydown;
public int lastkey = 0;
private boolean initialized = false;
public Input()
{
// only initialize everything once
if(initialized)
return;
keyList = new boolean[65536];
int i;
for(i = 0; i < 65536; i++)
keyList[i] = false;
mousex = 0;
mousey = 0;
button1 = false;
button2 = false;
button3 = false;
dragged = false;
keydown = false;
lastkey = 0;
// initialization complete
initialized = true;
}
public boolean getKey(int num)
{
if(num >= 0 && num < 65536)
return keyList[num];
return false;
}
public void setKey(int num, boolean status)
{
if(num >= 0 && num < 65536)
keyList[num] = status;
}
// keyListener
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key >= 0 && key < 65536)
keyList[key] = true;
keydown = true;
lastkey = key;
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if(key >= 0 && key < 65536)
keyList[key] = false;
}
public void keyTyped(KeyEvent e)
{
}
// mouseListener
public void mousePressed(MouseEvent e)
{
if(e.getButton() == MouseEvent.BUTTON1)
button1 = true;
if(e.getButton() == MouseEvent.BUTTON2)
button2 = true;
if(e.getButton() == MouseEvent.BUTTON3)
button3 = true;
}
public void mouseReleased(MouseEvent e)
{
if(e.getButton() == MouseEvent.BUTTON1)
button1 = false;
if(e.getButton() == MouseEvent.BUTTON2)
button2 = false;
if(e.getButton() == MouseEvent.BUTTON3)
button3 = false;
}
public void mouseEntered(MouseEvent e)
{
mouseMoved(e);
}
public void mouseExited(MouseEvent e)
{
mouseMoved(e);
}
public void mouseClicked(MouseEvent e)
{
}
// mouseMotionListener
public void mouseMoved(MouseEvent e)
{
mousex = (e.getX() - Screen.xpos) / Screen.scale;
mousey = (e.getY() - Screen.ypos) / Screen.scale;
if(mousex < 0)
mousex = 0;
if(mousey < 0)
mousey = 0;
if(mousex > Screen.w - 1)
mousex = Screen.w - 1;
if(mousey > Screen.h - 1)
mousey = Screen.h - 1;
}
public void mouseDragged(MouseEvent e)
{
dragged = true;
mouseMoved(e);
}
}