-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputHandler.java
More file actions
226 lines (204 loc) · 6.55 KB
/
InputHandler.java
File metadata and controls
226 lines (204 loc) · 6.55 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**************************************************************************************
* bias_tree
* Copyright (c) 2014-2017 National University of Colombia, https://github.com/remixlab
* @author Jean Pierre Charalambos, http://otrolado.info/
*
* All rights reserved. Library that eases the creation of interactive
* scenes, released under the terms of the GNU Public License v3.0
* which is available at http://www.gnu.org/licenses/gpl.html
**************************************************************************************/
package remixlab.bias;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* The InputHandler object is the high level package handler which holds a collection of
* {@link #agents()}, and an event dispatcher queue of
* {@link EventGrabberTuple}s ({@link #eventTupleQueue()}). Such tuple
* represents a message passing to application objects, allowing an object to be
* instructed to perform a particular user-defined action from a given
* {@link BogusEvent}. For an introduction to BIAS please refer to
* <a href="http://nakednous.github.io/projects/bias">this</a>.
* <p>
* At runtime, the input handler should continuously run the two loops defined in
* {@link #handle()}. Therefore, simply attach a call to {@link #handle()} at the end of
* your main event (drawing) loop for that to take effect (like it's done in
* <b>dandelion</b> by the <b>AbstractScene.postDraw()</b> method).
*/
public class InputHandler {
// D E V I C E S & E V E N T S
protected List<Agent> agents;
protected LinkedList<EventGrabberTuple> eventTupleQueue;
public InputHandler() {
// agents
agents = new ArrayList<Agent>();
// events
eventTupleQueue = new LinkedList<EventGrabberTuple>();
}
/**
* Main handler method. Call it at the end of your main event (drawing) loop (like it's
* done in <b>dandelion</b> by the <b>AbstractScene.postDraw()</b> method)
* <p>
* The handle comprises the following two loops:
* <p>
* 1. {@link EventGrabberTuple} producer loop which for each
* registered agent calls: a.
* {@link Agent#updateTrackedGrabber(BogusEvent)}; and, b.
* {@link Agent#handle(BogusEvent)}. Note that the bogus event are
* obtained from the agents callback
* {@link Agent#updateTrackedGrabberFeed()} and
* {@link Agent#handleFeed()} methods, respectively. The bogus event
* may also be obtained from {@link Agent#handleFeed()} which may
* replace both of the previous feeds when they are null.<br>
* 2. User-defined action consumer loop: which for each
* {@link EventGrabberTuple} calls
* {@link EventGrabberTuple#perform()}.<br>
*
* @see Agent#feed()
* @see Agent#updateTrackedGrabberFeed()
* @see Agent#handleFeed()
*/
public void handle() {
// 1. Agents
for (Agent agent : agents()) {
agent.updateTrackedGrabber(
agent.updateTrackedGrabberFeed() != null ? agent.updateTrackedGrabberFeed() : agent.feed());
agent.handle(agent.handleFeed() != null ? agent.handleFeed() : agent.feed());
}
// 2. Low level events
while (!eventTupleQueue.isEmpty())
eventTupleQueue.remove().perform();
}
/**
* Calls {@link Agent#addGrabber(Grabber)} on registered
* {@link #agents()}.
*/
public void addGrabber(Grabber grabber) {
for (Agent agent : agents())
agent.addGrabber(grabber);
}
/**
* Calls {@link Agent#removeGrabber(Grabber)} on registered
* {@link #agents()}.
*/
public void removeGrabber(Grabber grabber) {
for (Agent agent : agents())
agent.removeGrabber(grabber);
}
/**
* Calls {@link Agent#removeGrabbers()} on registered
* {@link #agents()}.
*/
public void removeGrabbers() {
for (Agent agent : agents())
agent.removeGrabbers();
}
/**
* Calls {@link Agent#setDefaultGrabber(Grabber)} on registered
* {@link #agents()}.
*/
public void setDefaultGrabber(Grabber grabber) {
for (Agent agent : agents())
agent.setDefaultGrabber(grabber);
}
/**
* Calls {@link Agent#shiftDefaultGrabber(Grabber, Grabber)} on
* registered {@link #agents()}.
*/
public void shiftDefaultGrabber(Grabber g1, Grabber g2) {
for (Agent agent : agents())
agent.shiftDefaultGrabber(g1, g2);
}
/**
* Returns {@code true} if {@link Agent#isInputGrabber(Grabber)} is
* {@code true} for at least one agent in {@link #agents()}.
*/
public boolean isInputGrabber(Grabber g) {
for (Agent agent : agents())
if (agent.isInputGrabber(g))
return true;
return false;
}
/**
* Returns {@code true} if {@link Agent#hasGrabber(Grabber)} is
* {@code true} for at least one agent in {@link #agents()}.
*/
public boolean hasGrabber(Grabber g) {
for (Agent agent : agents())
if (agent.hasGrabber(g))
return true;
return false;
}
/**
* Calls {@link Agent#resetTrackedGrabber()} on registered
* {@link #agents()}.
*/
public void resetTrackedGrabber() {
for (Agent agent : agents())
agent.resetTrackedGrabber();
}
/**
* Returns a list of the registered agents.
*/
public List<Agent> agents() {
return agents;
}
/**
* Registers the given agent.
*/
public boolean registerAgent(Agent agent) {
if (agents().contains(agent))
return false;
else
return agents().add(agent);
}
/**
* Returns true if the given agent is registered.
*/
public boolean isAgentRegistered(Agent agent) {
return agents().contains(agent);
}
/**
* Unregisters the given agent.
*/
public boolean unregisterAgent(Agent agent) {
return agents().remove(agent);
}
/**
* Unregisters all agents from the handler.
*/
public void unregisterAgents() {
agents.clear();
}
/**
* Returns the event tuple queue. Rarely needed.
*/
public LinkedList<EventGrabberTuple> eventTupleQueue() {
return eventTupleQueue;
}
/**
* Enqueues the eventTuple for later execution which happens at the end of
* {@link #handle()}. Returns {@code true} if succeeded and {@code false} otherwise.
*
* @see #handle()
*/
public boolean enqueueEventTuple(EventGrabberTuple eventTuple) {
if (!eventTupleQueue.contains(eventTuple))
return eventTupleQueue.add(eventTuple);
return false;
}
/**
* Removes the given event from the event queue. No action is executed.
*
* @param event to be removed.
*/
public void removeEventTuple(BogusEvent event) {
eventTupleQueue.remove(event);
}
/**
* Clears the event queue. Nothing is executed.
*/
public void removeEventTuples() {
eventTupleQueue.clear();
}
}