InputEvent.java
2.99 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
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
package com.mumfrey.liteloader.util;
import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.Event;
/**
* A handle to an input event, this handle will be used to call back the handler for the specified
* component's events. This class represents a singly-linked list of delegates with each delegate's
* next field pointing to the next delegate in the chain
*
* @author Adam Mummery-Smith
*/
public class InputEvent
{
/**
* Controller this event is delegating events for
*/
private final Controller controller;
/**
* Component this event is delegating events for
*/
private final Component component;
/**
* Event handler
*/
private final InputHandler handler;
/**
* Next event handler in the chain
*/
private InputEvent next;
/**
* @param controller
* @param component
* @param handler
*/
InputEvent(Controller controller, Component component, InputHandler handler)
{
this.controller = controller;
this.component = component;
this.handler = handler;
}
/**
* Link this delegate to the specified delegate and return the start of the delegate chain
*
* @param chain delegate to link to (will be null if the chain is empty)
*/
InputEvent link(InputEvent chain)
{
if (chain == null) return this; // Chain is empty, return self
return chain.append(this); // Append self to the start of the chain
}
/**
* Append specified delegate to the end of the delegate chain
*
* @param delegate
*/
private InputEvent append(InputEvent delegate)
{
InputEvent tail = this; // Start here
while (tail.next != null) // Find the end of the chain
tail = tail.next;
tail.next = delegate; // Append the new delegate
return this; // Return the start of the delegate chain (eg. this node)
}
/**
* @param event
*/
void onEvent(Event event)
{
if (this.component.isAnalog())
{
this.onAxisEvent(event.getValue(), event.getNanos());
}
else if (this.component.getIdentifier() == Component.Identifier.Axis.POV)
{
this.onPovEvent(event.getValue(), event.getNanos());
}
else
{
this.onButtonEvent(event.getValue() == 1.0F);
}
}
/**
* @param value
* @param nanos
*/
private void onAxisEvent(float value, long nanos)
{
this.handler.onAxisEvent(this, value, nanos);
if (this.next != null) this.next.onAxisEvent(value, nanos);
}
/**
* @param value
* @param nanos
*/
private void onPovEvent(float value, long nanos)
{
this.handler.onPovEvent(this, value, nanos);
if (this.next != null) this.next.onPovEvent(value, nanos);
}
/**
* @param pressed
*/
private void onButtonEvent(boolean pressed)
{
this.handler.onButtonEvent(this, pressed);
if (this.next != null) this.next.onButtonEvent(pressed);
}
public Controller getController()
{
return this.controller;
}
public Component getComponent()
{
return this.component;
}
}