140 likes | 222 Vues
User Interactivity. Chapter 3. AWT Event Model. AWT has own thread, “listening” to OS Listener: an object that receives events from another object Different types of listeners for different types of events. You didn’t hear about last night’s big event?
E N D
User Interactivity Chapter 3
AWT Event Model • AWT has own thread, “listening” to OS • Listener: an object that receives events from another object • Different types of listeners for different types of events. You didn’t hear about last night’s big event? You must not be on the right notification list!
How Listeners Work • User presses a key • OS sends the key event to the Java runtime thread • Java posts the event to AWT’s event queue • AWT dispatch thread posts the event to all objects that are KeyListeners • The KeyListener receives the event.
Listeners are Interfaces • public class MyGame extends GameCore implements KeyListener{ • Interfaces vs Superclasses : • Use interfaces for behaviors, superclasses for ISA relationships • Interfaces typically give an object a behavior secondary to its main behavior
Key Listeners • Used for firing, moving, not really for String input • Create an object that implements KeyListener: • keyPressed() • keyReleased() • keyTyped() (key held down) • Register the listener with the window you want to listen to Window window = screen.getFullScreenWindow(); window.addKeyListener(keyListener);
Extras • Focus Traversal Keys – keys that already have meaning in a window interface (tab goes from one field to the next) window.setFocusTraversalKeysEnabled(false); • KeyCodes – how you know which key was pressed. int keycode = e.getKeyCode(); if(keycode == KeyEvent.VK_ENTER) • Consume the event so other Listeners don’t see it: e.consume(); • Beware small differences between OS’s
Mouse Input • Three Types of Mouse Events: • Button clicks (MouseListener) • Mouse motion (MouseMotionListener) • Regular movement • Drag motion • Wheel scrolls (MouseWheelListener)
Changing the Mouse • Sometimes you don’t want mouse motion to stop at the end of the screen. (First-person game) • Constantly reposition mouse in middle of screen. • Calculate distance moved from center each time to update display.
Changing the Mouse • Your mouse icon can also look like: • Hand • Crosshairs • Hourglass • I (text based) • Make it go away altogether
Threads • Threads – executions of code. • Multithreading – many threads running at once • Multiple threads accessing the same data can cause problems • Hit key “g” Hit key “->” - Find s’ position (p1) - Move s to p2 - Resize s - Redraw s at p2 - Redraw s at p1
Synchronization • Draws need to happen one at a time. • Mouse movements need to happen one at a time. • “Lock” the code so that only one thread can execute the code at a time. publicsynchronizedvoid mouseMoved(MouseEvent e) {
GameAction (420 engine) • Model a user interaction • Tell if a key or mouse button is pressed, and how many times • Each GameAction will map to some animation in the game: • Ex: Hitting “->” makes the screen scroll to the right.
Input Manager (420 engine) • One class that will: • Handle all key & mouse events • Save the events • Map the events to game actions • Let the user configure the keyboard • It implements all listener types • Contains a keyCode to GameAction mapping
Pausing • One GameAction can map to “pause” • What does it mean to pause a game? • Continue drawing • Stop updating • Stop taking input from user • In update of GameCore extension: if (!isPaused()){ checkGameInput(); player.update(elapsedTime);} • After coming back from being paused, clear the GameActions that happened. inputManager.resetAllGameActions();