Source code: org/libsdl/event/SDLEventManager.java
1
2
3 package org.libsdl.event;
4
5
6 /**
7 * The <code>SDLEventManager</code> class encompasses the ability to
8 * receive events, either by polling or waiting. Most of the event related
9 * SDL functions can be found here, such as SDL_WaitEvent, SDL_PumpEvents,
10 * and SDL_PollEvent (to name a few).
11 * <p>
12 * ------ EXAMPLE ------
13 * <p><blockquote><pre>
14 * SDL sdl = SDL.getInstance();
15 * sdl.init(SDL.SDL_INIT_EVERYTHING);
16 * SDLEventManager eventManager = sdl.getEventManager();
17 * SDLEvent event = null;
18 * while ( (event = eventManager.waitEvent()) != null ) {
19 * if (event instanceOf SDLMouseButtonEvent) {
20 * // do something
21 * } else if (event instanceOf SDLKeyboardEvent) {
22 * // do something else
23 * } // etc...
24 * }
25 * </pre></blockquote><p>
26 *
27 * @author Eric Wittmann
28 * @version $revision$
29 * @see org.libsdl.SDL#getEventManager()
30 */
31 public class SDLEventManager
32 {
33 public static short SDL_APPMOUSEFOCUS = 0x01;
34 public static short SDL_APPINPUTFOCUS = 0x02;
35 public static short SDL_APPACTIVE = 0x04;
36
37 public static int SDL_QUERY = -1;
38 public static int SDL_IGNORE = 0;
39 public static int SDL_DISABLE = 0;
40 public static int SDL_ENABLE = 1;
41
42 /**
43 * Pumps the event queue. This method is typically used when filtering
44 * events, as it is implicitly called by <code>pollEvent()</code> and
45 * <code>waitEvent()</code>.
46 * <p>
47 * <b>Corresponds</b>:<blockquote><code>SDL_PumpEvents()</code></blockquote>
48 * <p>
49 */
50 public native void pumpEvents();
51 // public native int peepEvents(...);
52 /**
53 * Polls the event queue. If an event is ready to be returned, it will be.
54 * If no event is ready, this method will return null.
55 * <p>
56 * <b>Corresponds</b>:<blockquote><code>SDL_PollEvent()</code></blockquote>
57 * <p>
58 * @return SDLEvent - the event
59 */
60 public native SDLEvent pollEvent();
61 /**
62 * Blocks until an event is ready to be returned.
63 * <p>
64 * <b>Corresponds</b>:<blockquote><code>SDL_WaitEvent()</code></blockquote>
65 * <p>
66 * @return SDLEvent - the event
67 */
68 public native SDLEvent waitEvent();
69 /**
70 * Push an event onto the event queue.
71 * <p>
72 * <b>Corresponds</b>:<blockquote><code>SDL_PushEvent()</code></blockquote>
73 * <p>
74 * @param event SDLEvent - the event to put on the queue
75 * @return Boolean - true if successful
76 */
77 public native boolean pushEvent(SDLEvent event);
78 // public native void setEventFilter(SDLEventFilter filter);
79 // public native SDLEventFilter getEventFilter();
80 /**
81 * Set the event state. This method sets how certain kinds of
82 * events are handled.
83 * <p>
84 * <b>Corresponds</b>:<blockquote><code>SDL_EventState()</code></blockquote>
85 * <p>
86 * @param type Short - the type of event
87 * @param state Integer - the state to set
88 * @return Short - the new/current state for the given event type
89 */
90 public native short eventState(short type, int state);
91 // public native int getKeyState(...);
92 /**
93 * Returns the state of the modifier keys.
94 * <p>
95 * <b>Corresponds</b>:<blockquote><code>SDL_GetModState()</code></blockquote>
96 * <p>
97 * @return SDLMod - get the state of the modifier keys
98 */
99 public native SDLMod getModState();
100 /**
101 * Sets the state of the modifier keys.
102 * <p>
103 * <b>Corresponds</b>:<blockquote><code>SDL_SetModState()</code></blockquote>
104 * <p>
105 * @param modstate SDLMod - new state for the modifier keys
106 */
107 public native void setModState(SDLMod modstate);
108 /**
109 * Get the name of an SDL virtual keysym.
110 * <p>
111 * <b>Corresponds</b>:<blockquote><code>SDL_GetKeyName()</code></blockquote>
112 * <p>
113 * @param key Integer - the key
114 * @return String - the name of the key
115 */
116 public native String getKeyName(int key);
117 /**
118 * This method needs to be called to enable UNICODE translation.
119 * <p>
120 * <b>Corresponds</b>:<blockquote><code>SDL_EnableUNICODE()</code></blockquote>
121 * <p>
122 * @param enable Boolean - enable true/false
123 * @return Boolean - the previous translation state
124 */
125 public native boolean enableUNICODE(boolean enable);
126 /**
127 * This method needs to be called to enable key repeat.
128 * <p>
129 * <b>Corresponds</b>:<blockquote><code>SDL_EnableKeyRepeat()</code></blockquote>
130 * <p>
131 * @param delay Integer - key repeat delay
132 * @param interval Integer - key repeat interval
133 * @return Boolean - true if successful
134 */
135 public native boolean enableKeyRepeat(int delay, int interval);
136 /**
137 * Gets the current mouse state.
138 * <p>
139 * <b>Corresponds</b>:<blockquote><code>SDL_GetMouseState()</code></blockquote>
140 * <p>
141 * @return SDLMouseState - the current mouse state
142 */
143 public native SDLMouseState getMouseState();
144 /**
145 * Gets the current relative mouse state (relative to the last time
146 * the method was called).
147 * <p>
148 * <b>Corresponds</b>:<blockquote><code>SDL_GetRelativeMouseState()</code></blockquote>
149 * <p>
150 * @return SDLMouseState - the current mouse state
151 */
152 public native SDLMouseState getRelativeMouseState();
153 /**
154 * Gets the current app state. This method will return various pieces of
155 * information.
156 * <p>
157 * <b>Corresponds</b>:<blockquote><code>SDL_GetAppState()</code></blockquote>
158 * <p>
159 * @return Short - a bitwise combination of app state flags
160 */
161 public native short getAppState();
162 /**
163 * Enable/disable joystick event polling.
164 * <p>
165 * <b>Corresponds</b>:<blockquote><code>SDL_JoystickEventState()</code></blockquote>
166 * <p>
167 * @param state Integer - query, enable, or ignore (see above flags)
168 * @return Integer - if state was query, the current state. Otherwise the
169 * new state.
170 */
171 public native int joystickEventState(int state);
172
173 /**
174 * Move the mouse pointer to a specific location.
175 * <p>
176 * <b>Corresponds</b>:<blockquote><code>SDL_WarpMouse()</code></blockquote>
177 * <p>
178 * @param x Integer - new mouse x position
179 * @param y Integer - new mouse y position
180 */
181 public native void warpMouse(int x, int y);
182
183 /**
184 * Change the current cursor.
185 * <p>
186 * <b>Corresponds</b>:<blockquote><code>SDL_SetCursor()</code></blockquote>
187 * <p>
188 * @param cursor SDLCursor - the new cursor
189 */
190 public native void setCursor(SDLCursor cursor);
191 /**
192 * Get the current cursor.
193 * <p>
194 * <b>Corresponds</b>:<blockquote><code>SDL_GetCursor()</code></blockquote>
195 * <p>
196 * @return SDLCursor - the current cursor
197 */
198 public native SDLCursor getCursor();
199 /**
200 * Toggle whether or not to show the cursor.
201 * <p>
202 * <b>Corresponds</b>:<blockquote><code>SDL_ShowCursor()</code></blockquote>
203 * <p>
204 * @param toggle Integer - either enable or disable
205 * @return Integer - the current state of the cursor
206 */
207 public native int showCursor(int toggle);
208
209 /**
210 * Convenience method to break down the output of <code>getAppState()</code>.
211 * @return Boolean - true if the app has the mouse focus
212 */
213 public boolean appHasMouseFocus() {
214 short flags = getAppState();
215 return (flags & SDL_APPMOUSEFOCUS) > 0;
216 }
217
218 /**
219 * Convenience method to break down the output of <code>getAppState()</code>.
220 * @return Boolean - true if the app has the input focus
221 */
222 public boolean appHasInputFocus() {
223 short flags = getAppState();
224 return (flags & SDL_APPINPUTFOCUS) > 0;
225 }
226
227 /**
228 * Convenience method to break down the output of <code>getAppState()</code>.
229 * @return Boolean - true if the app is active
230 */
231 public boolean appIsActive() {
232 short flags = getAppState();
233 return (flags & SDL_APPACTIVE) > 0;
234 }
235 }