forked from aidiary/java-rpg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionKey.java
More file actions
60 lines (51 loc) · 1.45 KB
/
Copy pathActionKey.java
File metadata and controls
60 lines (51 loc) · 1.45 KB
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
public class ActionKey {
// constants for key mode
// isPressed() method returns true while key is pressed
public static final int NORMAL = 0;
// isPressed() method returns true
// only when a key is pressed for the first time
public static final int DETECT_INITIAL_PRESS_ONLY = 1;
// constants key state
private static final int STATE_RELEASED = 0;
private static final int STATE_PRESSED = 1;
private static final int STATE_WAITING_FOR_RELEASE = 2;
// current key's mode
private int mode;
// the number of the time that the key was pressed
private int amount;
// current key's state
private int state;
public ActionKey() {
this(NORMAL);
}
public ActionKey(int mode) {
this.mode = mode;
reset();
}
// reset key's state
public void reset() {
state = STATE_RELEASED;
amount = 0;
}
public void press() {
if (state != STATE_WAITING_FOR_RELEASE) {
amount++;
state = STATE_PRESSED;
}
}
public void release() {
state = STATE_RELEASED;
}
public boolean isPressed() {
if (amount != 0) {
if (state == STATE_RELEASED) {
amount = 0;
} else if (mode == DETECT_INITIAL_PRESS_ONLY) {
state = STATE_WAITING_FOR_RELEASE;
amount = 0;
}
return true;
}
return false;
}
}